Threads.cpp revision b9d73333cce3f9da3a7a0b33589f6bbe0f992a92
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>
26ad8510a339ffab330c2c46e5c247dd1cf9e15c22Glenn Kasten#include <linux/futex.h>
2781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <sys/stat.h>
28ad8510a339ffab330c2c46e5c247dd1cf9e15c22Glenn Kasten#include <sys/syscall.h>
2981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <cutils/properties.h>
301ab85ec401801ef9a9184650d0f5a1639b45eeb9Glenn Kasten#include <media/AudioParameter.h>
31cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung#include <media/AudioResamplerPublic.h>
3281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <utils/Log.h>
33371eb9756c32109ea572b91216b19bb623f6d3fdAlex Ray#include <utils/Trace.h>
3481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <private/media/AudioTrackShared.h>
3681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <hardware/audio.h>
3781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <audio_effects/effect_ns.h>
3881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <audio_effects/effect_aec.h>
3981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <audio_utils/primitives.h>
4098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung#include <audio_utils/format.h>
41c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten#include <audio_utils/minifloat.h>
4281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// NBAIO implementations
446dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#include <media/nbaio/AudioStreamInSource.h>
4581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/AudioStreamOutSink.h>
4681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/MonoPipe.h>
4781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/MonoPipeReader.h>
4881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/Pipe.h>
4981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/PipeReader.h>
5081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/SourceAudioBufferProvider.h>
5181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <powermanager/PowerManager.h>
5381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <common_time/cc_helper.h>
5581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <common_time/local_clock.h>
5681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "AudioFlinger.h"
5881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "AudioMixer.h"
59d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung#include "BufferProviders.h"
6081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "FastMixer.h"
616dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#include "FastCapture.h"
6281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "ServiceUtilities.h"
6381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "SchedulingPolicyService.h"
6481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef ADD_BATTERY_DATA
6681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/IMediaPlayerService.h>
6781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/IMediaDeathNotifier.h>
6881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
6981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
7081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef DEBUG_CPU_USAGE
7181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <cpustats/CentralTendencyStatistics.h>
7281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <cpustats/ThreadCpuUsage.h>
7381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
7481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
7581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
7681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
7781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Note: the following macro is used for extremely verbose logging message.  In
7881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
7981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// 0; but one side effect of this is to turn all LOGV's as well.  Some messages
8081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// are so verbose that we want to suppress them even when we have ALOG_ASSERT
8181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// turned on.  Do not uncomment the #def below unless you really know what you
8281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// are doing and want to see all of the extremely verbose messages.
8381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent//#define VERY_VERY_VERBOSE_LOGGING
8481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef VERY_VERY_VERBOSE_LOGGING
8581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#define ALOGVV ALOGV
8681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#else
8781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#define ALOGVV(a...) do { } while(0)
8881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
8981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
906770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung// TODO: Move these macro/inlines to a header file.
9149d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten#define max(a, b) ((a) > (b) ? (a) : (b))
926770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hungtemplate <typename T>
936770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hungstatic inline T min(const T& a, const T& b)
946770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung{
956770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    return a < b ? a : b;
966770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung}
9749d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten
98d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung#ifndef ARRAY_SIZE
99d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
100d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung#endif
101d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung
10281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentnamespace android {
10381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
10481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// retry counts for buffer fill timeout
10581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// 50 * ~20msecs = 1 second
10681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int8_t kMaxTrackRetries = 50;
10781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int8_t kMaxTrackStartupRetries = 50;
10881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// allow less retry attempts on direct output thread.
10981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// direct outputs can be a scarce resource in audio hardware and should
11081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// be released as quickly as possible.
11181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int8_t kMaxTrackRetriesDirect = 2;
11281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// don't warn about blocked writes or record buffer overflows more often than this
11481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const nsecs_t kWarningThrottleNs = seconds(5);
11581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// RecordThread loop sleep time upon application overrun or audio HAL read error
11781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int kRecordThreadSleepUs = 5000;
11881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1191035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// maximum time to wait in sendConfigEvent_l() for a status to be received
1201035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentstatic const nsecs_t kConfigEventTimeoutNs = seconds(2);
12181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
12281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// minimum sleep time for the mixer thread loop when tracks are active but in underrun
12381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const uint32_t kMinThreadSleepTimeUs = 5000;
12481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// maximum divider applied to the active sleep time in the mixer thread loop
12581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const uint32_t kMaxThreadSleepTimeShift = 2;
12681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
12709a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung// minimum normal sink buffer size, expressed in milliseconds rather than frames
12809a5007b17acb49d25cfa386a2e2534d942e8854Andy Hungstatic const uint32_t kMinNormalSinkBufferSizeMs = 20;
12909a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung// maximum normal sink buffer size
13009a5007b17acb49d25cfa386a2e2534d942e8854Andy Hungstatic const uint32_t kMaxNormalSinkBufferSizeMs = 24;
13181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
132972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent// Offloaded output thread standby delay: allows track transition without going to standby
133972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurentstatic const nsecs_t kOffloadStandbyDelayNs = seconds(1);
134972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent
13581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Whether to use fast mixer
13681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const enum {
13781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    FastMixer_Never,    // never initialize or use: for debugging only
13881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    FastMixer_Always,   // always initialize and use, even if not needed: for debugging only
13981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        // normal mixer multiplier is 1
14081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    FastMixer_Static,   // initialize if needed, then use all the time if initialized,
14181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        // multiplier is calculated based on min & max normal mixer buffer size
14281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    FastMixer_Dynamic,  // initialize if needed, then use dynamically depending on track load,
14381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        // multiplier is calculated based on min & max normal mixer buffer size
14481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME for FastMixer_Dynamic:
14581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    //  Supporting this option will require fixing HALs that can't handle large writes.
14681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    //  For example, one HAL implementation returns an error from a large write,
14781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    //  and another HAL implementation corrupts memory, possibly in the sample rate converter.
14881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    //  We could either fix the HAL implementations, or provide a wrapper that breaks
14981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    //  up large writes into smaller ones, and the wrapper would need to deal with scheduler.
15081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} kUseFastMixer = FastMixer_Static;
15181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1526dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten// Whether to use fast capture
1536dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kastenstatic const enum {
1546dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    FastCapture_Never,  // never initialize or use: for debugging only
1556dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    FastCapture_Always, // always initialize and use, even if not needed: for debugging only
1566dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    FastCapture_Static, // initialize if needed, then use all the time if initialized
1576dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten} kUseFastCapture = FastCapture_Static;
1586dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
15981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Priorities for requestPriority
16081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int kPriorityAudioApp = 2;
16181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int kPriorityFastMixer = 3;
1626dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kastenstatic const int kPriorityFastCapture = 3;
16381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
16481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// IAudioFlinger::createTrack() reports back to client the total size of shared memory area
16581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// for the track.  The client then sub-divides this into smaller buffers for its use.
166b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten// Currently the client uses N-buffering by default, but doesn't tell us about the value of N.
167b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten// So for now we just assume that client is double-buffered for fast tracks.
168b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten// FIXME It would be better for client to tell AudioFlinger the value of N,
169b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten// so AudioFlinger could allocate the right amount of memory.
17081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// See the client's minBufCount and mNotificationFramesAct calculations for details.
1710349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten
1720349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten// This is the default value, if not specified by property.
173b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kastenstatic const int kFastTrackMultiplier = 2;
17481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1750349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten// The minimum and maximum allowed values
1760349009fd19f89f8414c428f6b71b369f7546085Glenn Kastenstatic const int kFastTrackMultiplierMin = 1;
1770349009fd19f89f8414c428f6b71b369f7546085Glenn Kastenstatic const int kFastTrackMultiplierMax = 2;
1780349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten
1790349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten// The actual value to use, which can be specified per-device via property af.fast_track_multiplier.
1800349009fd19f89f8414c428f6b71b369f7546085Glenn Kastenstatic int sFastTrackMultiplier = kFastTrackMultiplier;
1810349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten
182b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten// See Thread::readOnlyHeap().
183b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten// Initially this heap is used to allocate client buffers for "fast" AudioRecord.
184b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten// Eventually it will be the single buffer that FastCapture writes into via HAL read(),
185b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten// and that all "fast" AudioRecord clients read from.  In either case, the size can be small.
1869f81de3452dfb2385bd57dc05456a045174a1ab1Glenn Kastenstatic const size_t kRecordThreadReadOnlyHeapSize = 0x2000;
187b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten
18881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
18981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1900349009fd19f89f8414c428f6b71b369f7546085Glenn Kastenstatic pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT;
1910349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten
1920349009fd19f89f8414c428f6b71b369f7546085Glenn Kastenstatic void sFastTrackMultiplierInit()
1930349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten{
1940349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten    char value[PROPERTY_VALUE_MAX];
1950349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten    if (property_get("af.fast_track_multiplier", value, NULL) > 0) {
1960349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten        char *endptr;
1970349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten        unsigned long ul = strtoul(value, &endptr, 0);
1980349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten        if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) {
1990349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten            sFastTrackMultiplier = (int) ul;
2000349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten        }
2010349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten    }
2020349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten}
2030349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten
2040349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten// ----------------------------------------------------------------------------
2050349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten
20681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef ADD_BATTERY_DATA
20781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// To collect the amplifier usage
20881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic void addBatteryData(uint32_t params) {
20981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService();
21081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (service == NULL) {
21181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // it already logged
21281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return;
21381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
21481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
21581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    service->addBatteryData(params);
21681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
21781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
21881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
21981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
22181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent//      CPU Stats
22281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
22381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass CpuStats {
22581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
22681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    CpuStats();
22781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void sample(const String8 &title);
22881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef DEBUG_CPU_USAGE
22981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
23081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ThreadCpuUsage mCpuUsage;           // instantaneous thread CPU usage in wall clock ns
23181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns
23281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
23381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles
23481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
23581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int mCpuNum;                        // thread's current CPU number
23681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int mCpukHz;                        // frequency of thread's current CPU in kHz
23781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
23881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
23981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
24081784c37c61b09289654b979567a42bf73cd2b12Eric LaurentCpuStats::CpuStats()
24181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef DEBUG_CPU_USAGE
24281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    : mCpuNum(-1), mCpukHz(-1)
24381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
24481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
24581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
24681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2470f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid CpuStats::sample(const String8 &title
2480f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten#ifndef DEBUG_CPU_USAGE
2490f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten                __unused
2500f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten#endif
2510f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten        ) {
25281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef DEBUG_CPU_USAGE
25381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // get current thread's delta CPU time in wall clock ns
25481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    double wcNs;
25581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool valid = mCpuUsage.sampleAndEnable(wcNs);
25681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
25781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // record sample for wall clock statistics
25881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (valid) {
25981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mWcStats.sample(wcNs);
26081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
26181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
26281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // get the current CPU number
26381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int cpuNum = sched_getcpu();
26481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
26581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // get the current CPU frequency in kHz
26681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int cpukHz = mCpuUsage.getCpukHz(cpuNum);
26781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
26881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // check if either CPU number or frequency changed
26981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (cpuNum != mCpuNum || cpukHz != mCpukHz) {
27081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mCpuNum = cpuNum;
27181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mCpukHz = cpukHz;
27281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // ignore sample for purposes of cycles
27381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        valid = false;
27481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
27581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
27681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // if no change in CPU number or frequency, then record sample for cycle statistics
27781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (valid && mCpukHz > 0) {
27881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        double cycles = wcNs * cpukHz * 0.000001;
27981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mHzStats.sample(cycles);
28081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
28181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
28281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    unsigned n = mWcStats.n();
28381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mCpuUsage.elapsed() is expensive, so don't call it every loop
28481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if ((n & 127) == 1) {
28581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        long long elapsed = mCpuUsage.elapsed();
28681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) {
28781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            double perLoop = elapsed / (double) n;
28881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            double perLoop100 = perLoop * 0.01;
28981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            double perLoop1k = perLoop * 0.001;
29081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            double mean = mWcStats.mean();
29181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            double stddev = mWcStats.stddev();
29281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            double minimum = mWcStats.minimum();
29381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            double maximum = mWcStats.maximum();
29481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            double meanCycles = mHzStats.mean();
29581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            double stddevCycles = mHzStats.stddev();
29681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            double minCycles = mHzStats.minimum();
29781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            double maxCycles = mHzStats.maximum();
29881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mCpuUsage.resetElapsed();
29981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mWcStats.reset();
30081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHzStats.reset();
30181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGD("CPU usage for %s over past %.1f secs\n"
30281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                "  (%u mixer loops at %.1f mean ms per loop):\n"
30381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                "  us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n"
30481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                "  %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n"
30581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                "  MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f",
30681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    title.string(),
30781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    elapsed * .000000001, n, perLoop * .000001,
30881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mean * .001,
30981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    stddev * .001,
31081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    minimum * .001,
31181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    maximum * .001,
31281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mean / perLoop100,
31381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    stddev / perLoop100,
31481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    minimum / perLoop100,
31581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    maximum / perLoop100,
31681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    meanCycles / perLoop1k,
31781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    stddevCycles / perLoop1k,
31881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    minCycles / perLoop1k,
31981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    maxCycles / perLoop1k);
32081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
32181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
32281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
32381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
32481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
32581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
32681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
32781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent//      ThreadBase
32881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
32981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
33097b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten// static
33197b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kastenconst char *AudioFlinger::ThreadBase::threadTypeToString(AudioFlinger::ThreadBase::type_t type)
33297b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten{
33397b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    switch (type) {
33497b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    case MIXER:
33597b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten        return "MIXER";
33697b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    case DIRECT:
33797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten        return "DIRECT";
33897b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    case DUPLICATING:
33997b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten        return "DUPLICATING";
34097b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    case RECORD:
34197b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten        return "RECORD";
34297b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    case OFFLOAD:
34397b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten        return "OFFLOAD";
34497b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    default:
34597b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten        return "unknown";
34697b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    }
34797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten}
34897b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten
3490f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn KastenString8 devicesToString(audio_devices_t devices)
3500f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten{
3510f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    static const struct mapping {
3520f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        audio_devices_t mDevices;
3530f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        const char *    mString;
3540f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    } mappingsOut[] = {
3550f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        AUDIO_DEVICE_OUT_EARPIECE,          "EARPIECE",
3560f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        AUDIO_DEVICE_OUT_SPEAKER,           "SPEAKER",
3570f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        AUDIO_DEVICE_OUT_WIRED_HEADSET,     "WIRED_HEADSET",
3580f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        AUDIO_DEVICE_OUT_WIRED_HEADPHONE,   "WIRED_HEADPHONE",
35984d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_OUT_BLUETOOTH_SCO,     "BLUETOOTH_SCO",
36084d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,     "BLUETOOTH_SCO_HEADSET",
36184d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT,      "BLUETOOTH_SCO_CARKIT",
36284d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_OUT_BLUETOOTH_A2DP,            "BLUETOOTH_A2DP",
36384d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, "BLUETOOTH_A2DP_HEADPHONES",
36484d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER,    "BLUETOOTH_A2DP_SPEAKER",
36584d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_OUT_AUX_DIGITAL,       "AUX_DIGITAL",
36684d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_OUT_HDMI,              "HDMI",
36784d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET, "ANLG_DOCK_HEADSET",
36884d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, "DGTL_DOCK_HEADSET",
36984d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_OUT_USB_ACCESSORY,     "USB_ACCESSORY",
37084d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_OUT_USB_DEVICE,        "USB_DEVICE",
3710f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        AUDIO_DEVICE_OUT_TELEPHONY_TX,      "TELEPHONY_TX",
37284d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_OUT_LINE,              "LINE",
37384d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_OUT_HDMI_ARC,          "HDMI_ARC",
37484d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_OUT_SPDIF,             "SPDIF",
37584d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_OUT_FM,                "FM",
37684d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_OUT_AUX_LINE,          "AUX_LINE",
37784d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_OUT_SPEAKER_SAFE,      "SPEAKER_SAFE",
378b9d73333cce3f9da3a7a0b33589f6bbe0f992a92Eric Laurent        AUDIO_DEVICE_OUT_IP,                "IP",
3790f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        AUDIO_DEVICE_NONE,                  "NONE",         // must be last
3800f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    }, mappingsIn[] = {
38184d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_IN_COMMUNICATION,      "COMMUNICATION",
38284d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_IN_AMBIENT,            "AMBIENT",
3830f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        AUDIO_DEVICE_IN_BUILTIN_MIC,        "BUILTIN_MIC",
38484d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET,  "BLUETOOTH_SCO_HEADSET",
3850f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        AUDIO_DEVICE_IN_WIRED_HEADSET,      "WIRED_HEADSET",
38684d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_IN_AUX_DIGITAL,        "AUX_DIGITAL",
3870f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        AUDIO_DEVICE_IN_VOICE_CALL,         "VOICE_CALL",
38884d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_IN_TELEPHONY_RX,       "TELEPHONY_RX",
38984d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_IN_BACK_MIC,           "BACK_MIC",
3900f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        AUDIO_DEVICE_IN_REMOTE_SUBMIX,      "REMOTE_SUBMIX",
39184d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET,  "ANLG_DOCK_HEADSET",
39284d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET,  "DGTL_DOCK_HEADSET",
39384d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_IN_USB_ACCESSORY,      "USB_ACCESSORY",
39484d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_IN_USB_DEVICE,         "USB_DEVICE",
39584d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_IN_FM_TUNER,           "FM_TUNER",
39684d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_IN_TV_TUNER,           "TV_TUNER",
39784d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_IN_LINE,               "LINE",
39884d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_IN_SPDIF,              "SPDIF",
39984d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_IN_BLUETOOTH_A2DP,     "BLUETOOTH_A2DP",
40084d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten        AUDIO_DEVICE_IN_LOOPBACK,           "LOOPBACK",
401b9d73333cce3f9da3a7a0b33589f6bbe0f992a92Eric Laurent        AUDIO_DEVICE_IN_IP,                 "IP",
4020f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        AUDIO_DEVICE_NONE,                  "NONE",         // must be last
4030f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    };
4040f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    String8 result;
4050f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    audio_devices_t allDevices = AUDIO_DEVICE_NONE;
4060f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    const mapping *entry;
4070f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    if (devices & AUDIO_DEVICE_BIT_IN) {
4080f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        devices &= ~AUDIO_DEVICE_BIT_IN;
4090f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        entry = mappingsIn;
4100f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    } else {
4110f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        entry = mappingsOut;
4120f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    }
4130f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    for ( ; entry->mDevices != AUDIO_DEVICE_NONE; entry++) {
4140f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        allDevices = (audio_devices_t) (allDevices | entry->mDevices);
4150f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        if (devices & entry->mDevices) {
4160f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten            if (!result.isEmpty()) {
4170f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten                result.append("|");
4180f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten            }
4190f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten            result.append(entry->mString);
4200f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        }
4210f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    }
4220f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    if (devices & ~allDevices) {
4230f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        if (!result.isEmpty()) {
4240f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten            result.append("|");
4250f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        }
4260f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        result.appendFormat("0x%X", devices & ~allDevices);
4270f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    }
4280f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    if (result.isEmpty()) {
4290f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        result.append(entry->mString);
4300f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    }
4310f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    return result;
4320f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten}
4330f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten
4340f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn KastenString8 inputFlagsToString(audio_input_flags_t flags)
4350f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten{
4360f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    static const struct mapping {
4370f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        audio_input_flags_t     mFlag;
4380f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        const char *            mString;
4390f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    } mappings[] = {
4400f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        AUDIO_INPUT_FLAG_FAST,              "FAST",
4410f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        AUDIO_INPUT_FLAG_HW_HOTWORD,        "HW_HOTWORD",
4420f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        AUDIO_INPUT_FLAG_NONE,              "NONE",         // must be last
4430f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    };
4440f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    String8 result;
4450f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    audio_input_flags_t allFlags = AUDIO_INPUT_FLAG_NONE;
4460f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    const mapping *entry;
4470f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    for (entry = mappings; entry->mFlag != AUDIO_INPUT_FLAG_NONE; entry++) {
4480f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        allFlags = (audio_input_flags_t) (allFlags | entry->mFlag);
4490f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        if (flags & entry->mFlag) {
4500f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten            if (!result.isEmpty()) {
4510f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten                result.append("|");
4520f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten            }
4530f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten            result.append(entry->mString);
4540f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        }
4550f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    }
4560f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    if (flags & ~allFlags) {
4570f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        if (!result.isEmpty()) {
4580f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten            result.append("|");
4590f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        }
4600f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        result.appendFormat("0x%X", flags & ~allFlags);
4610f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    }
4620f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    if (result.isEmpty()) {
4630f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten        result.append(entry->mString);
4640f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    }
4650f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    return result;
4660f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten}
4670f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten
4680f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn KastenString8 outputFlagsToString(audio_output_flags_t flags)
46997b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten{
47097b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    static const struct mapping {
47197b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten        audio_output_flags_t    mFlag;
47297b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten        const char *            mString;
47397b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    } mappings[] = {
47497b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten        AUDIO_OUTPUT_FLAG_DIRECT,           "DIRECT",
47597b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten        AUDIO_OUTPUT_FLAG_PRIMARY,          "PRIMARY",
47697b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten        AUDIO_OUTPUT_FLAG_FAST,             "FAST",
47797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten        AUDIO_OUTPUT_FLAG_DEEP_BUFFER,      "DEEP_BUFFER",
478dfb0e115d827887e2f56a8877fe41b256d24360eGlenn Kasten        AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD, "COMPRESS_OFFLOAD",
47997b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten        AUDIO_OUTPUT_FLAG_NON_BLOCKING,     "NON_BLOCKING",
48097b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten        AUDIO_OUTPUT_FLAG_HW_AV_SYNC,       "HW_AV_SYNC",
48197b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten        AUDIO_OUTPUT_FLAG_NONE,             "NONE",         // must be last
48297b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    };
48397b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    String8 result;
48497b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    audio_output_flags_t allFlags = AUDIO_OUTPUT_FLAG_NONE;
48597b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    const mapping *entry;
48697b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    for (entry = mappings; entry->mFlag != AUDIO_OUTPUT_FLAG_NONE; entry++) {
48797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten        allFlags = (audio_output_flags_t) (allFlags | entry->mFlag);
48897b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten        if (flags & entry->mFlag) {
48997b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten            if (!result.isEmpty()) {
49097b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten                result.append("|");
49197b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten            }
49297b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten            result.append(entry->mString);
49397b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten        }
49497b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    }
49597b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    if (flags & ~allFlags) {
49697b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten        if (!result.isEmpty()) {
49797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten            result.append("|");
49897b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten        }
49997b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten        result.appendFormat("0x%X", flags & ~allFlags);
50097b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    }
50197b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    if (result.isEmpty()) {
50297b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten        result.append(entry->mString);
50397b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    }
50497b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    return result;
50597b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten}
50697b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten
5070f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kastenconst char *sourceToString(audio_source_t source)
5080f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten{
5090f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    switch (source) {
5100f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    case AUDIO_SOURCE_DEFAULT:              return "default";
5110f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    case AUDIO_SOURCE_MIC:                  return "mic";
5120f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    case AUDIO_SOURCE_VOICE_UPLINK:         return "voice uplink";
5130f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    case AUDIO_SOURCE_VOICE_DOWNLINK:       return "voice downlink";
5140f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    case AUDIO_SOURCE_VOICE_CALL:           return "voice call";
5150f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    case AUDIO_SOURCE_CAMCORDER:            return "camcorder";
5160f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    case AUDIO_SOURCE_VOICE_RECOGNITION:    return "voice recognition";
5170f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    case AUDIO_SOURCE_VOICE_COMMUNICATION:  return "voice communication";
5180f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    case AUDIO_SOURCE_REMOTE_SUBMIX:        return "remote submix";
5190f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    case AUDIO_SOURCE_FM_TUNER:             return "FM tuner";
5200f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    case AUDIO_SOURCE_HOTWORD:              return "hotword";
5210f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    default:                                return "unknown";
5220f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten    }
5230f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten}
5240f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten
52581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
52672e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        audio_devices_t outDevice, audio_devices_t inDevice, type_t type, bool systemReady)
52781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    :   Thread(false /*canCallJava*/),
52881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mType(type),
5299b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten        mAudioFlinger(audioFlinger),
53070949c47fbae3f836d15f040551d7631be3ed7c2Glenn Kasten        // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize
531deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten        // are set by PlaybackThread::readOutputParameters_l() or
532deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten        // RecordThread::readInputParameters_l()
533fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        //FIXME: mStandby should be true here. Is this some kind of hack?
53481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mStandby(false), mOutDevice(outDevice), mInDevice(inDevice),
535e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent        mPrevInDevice(AUDIO_DEVICE_NONE), mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id),
53681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // mName will be set by concrete (non-virtual) subclass
53772e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        mDeathRecipient(new PMDeathRecipient(this)),
53872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        mSystemReady(systemReady)
53981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
540296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    memset(&mPatch, 0, sizeof(struct audio_patch));
54181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
54281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
54381784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::ThreadBase::~ThreadBase()
54481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
545c6ae3c8a261794fd4445e4e152d1ada074a3f92fGlenn Kasten    // mConfigEvents should be empty, but just in case it isn't, free the memory it owns
546c6ae3c8a261794fd4445e4e152d1ada074a3f92fGlenn Kasten    mConfigEvents.clear();
547c6ae3c8a261794fd4445e4e152d1ada074a3f92fGlenn Kasten
54881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // do not lock the mutex in destructor
54981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    releaseWakeLock_l();
55081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mPowerManager != 0) {
55106b46062d2f8bc82ca3061a23d197734ae51918bMarco Nelissen        sp<IBinder> binder = IInterface::asBinder(mPowerManager);
55281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        binder->unlinkToDeath(mDeathRecipient);
55381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
55481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
55581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
556cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kastenstatus_t AudioFlinger::ThreadBase::readyToRun()
557cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten{
558cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten    status_t status = initCheck();
559cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten    if (status == NO_ERROR) {
560cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten        ALOGI("AudioFlinger's thread %p ready to run", this);
561cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten    } else {
562cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten        ALOGE("No working audio driver found.");
563cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten    }
564cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten    return status;
565cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten}
566cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten
56781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::exit()
56881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
56981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("ThreadBase::exit");
57081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // do any cleanup required for exit to succeed
57181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    preExit();
57281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
57381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // This lock prevents the following race in thread (uniprocessor for illustration):
57481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        //  if (!exitPending()) {
57581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        //      // context switch from here to exit()
57681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        //      // exit() calls requestExit(), what exitPending() observes
57781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        //      // exit() calls signal(), which is dropped since no waiters
57881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        //      // context switch back from exit() to here
57981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        //      mWaitWorkCV.wait(...);
58081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        //      // now thread is hung
58181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        //  }
58281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AutoMutex lock(mLock);
58381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        requestExit();
58481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mWaitWorkCV.broadcast();
58581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
58681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // When Thread::requestExitAndWait is made virtual and this method is renamed to
58781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();"
58881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    requestExitAndWait();
58981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
59081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
59181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs)
59281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
59381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t status;
59481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
59581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string());
59681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
59781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5981035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    return sendSetParameterConfigEvent_l(keyValuePairs);
5991035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent}
6001035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
6011035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// sendConfigEvent_l() must be called with ThreadBase::mLock held
6021035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// Can temporarily release the lock if waiting for a reply from processConfigEvents_l().
6031035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentstatus_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event)
6041035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent{
6051035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    status_t status = NO_ERROR;
6061035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
60772e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    if (event->mRequiresSystemReady && !mSystemReady) {
60872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        event->mWaitStatus = false;
60972e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        mPendingConfigEvents.add(event);
61072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        return status;
61172e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    }
6121035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    mConfigEvents.add(event);
6131035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    ALOGV("sendConfigEvent_l() num events %d event %d", mConfigEvents.size(), event->mType);
61481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mWaitWorkCV.signal();
6151035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    mLock.unlock();
6161035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    {
6171035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        Mutex::Autolock _l(event->mLock);
6181035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        while (event->mWaitStatus) {
6191035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) {
6201035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                event->mStatus = TIMED_OUT;
6211035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                event->mWaitStatus = false;
6221035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            }
6231035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
6241035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        status = event->mStatus;
62581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
6261035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    mLock.lock();
62781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return status;
62881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
62981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
63073e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurentvoid AudioFlinger::ThreadBase::sendIoConfigEvent(audio_io_config_event event)
63181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
63281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
63373e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    sendIoConfigEvent_l(event);
63481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
63581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
63681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// sendIoConfigEvent_l() must be called with ThreadBase::mLock held
63773e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurentvoid AudioFlinger::ThreadBase::sendIoConfigEvent_l(audio_io_config_event event)
63881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
63973e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event);
6401035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    sendConfigEvent_l(configEvent);
64181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
64281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
64372e3f39146fce4686bd96f11057c051bea376dfbEric Laurentvoid AudioFlinger::ThreadBase::sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio)
64472e3f39146fce4686bd96f11057c051bea376dfbEric Laurent{
64572e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    Mutex::Autolock _l(mLock);
64672e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    sendPrioConfigEvent_l(pid, tid, prio);
64772e3f39146fce4686bd96f11057c051bea376dfbEric Laurent}
64872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent
64981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// sendPrioConfigEvent_l() must be called with ThreadBase::mLock held
65081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio)
65181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
6521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio);
6531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    sendConfigEvent_l(configEvent);
65481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
65581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6561035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held
6571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentstatus_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair)
65881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
6591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    sp<ConfigEvent> configEvent = (ConfigEvent *)new SetParameterConfigEvent(keyValuePair);
6601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    return sendConfigEvent_l(configEvent);
661f777331418a86cd9fd709af898ef24a69967aeb4Glenn Kasten}
662f777331418a86cd9fd709af898ef24a69967aeb4Glenn Kasten
6631c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::ThreadBase::sendCreateAudioPatchConfigEvent(
6641c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                                        const struct audio_patch *patch,
6651c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                                        audio_patch_handle_t *handle)
6661c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{
6671c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    Mutex::Autolock _l(mLock);
6681c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle);
6691c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    status_t status = sendConfigEvent_l(configEvent);
6701c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    if (status == NO_ERROR) {
6711c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        CreateAudioPatchConfigEventData *data =
6721c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                        (CreateAudioPatchConfigEventData *)configEvent->mData.get();
6731c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        *handle = data->mHandle;
6741c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    }
6751c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    return status;
6761c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent}
6771c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
6781c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::ThreadBase::sendReleaseAudioPatchConfigEvent(
6791c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                                                const audio_patch_handle_t handle)
6801c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{
6811c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    Mutex::Autolock _l(mLock);
6821c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle);
6831c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    return sendConfigEvent_l(configEvent);
6841c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent}
6851c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
6861c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
6872cfbf88b89854f30b295e8ae26a031edb8d712f8Glenn Kasten// post condition: mConfigEvents.isEmpty()
688021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurentvoid AudioFlinger::ThreadBase::processConfigEvents_l()
689f777331418a86cd9fd709af898ef24a69967aeb4Glenn Kasten{
6901035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    bool configChanged = false;
6911035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
69281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    while (!mConfigEvents.isEmpty()) {
6931035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        ALOGV("processConfigEvents_l() remaining events %d", mConfigEvents.size());
6941035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        sp<ConfigEvent> event = mConfigEvents[0];
69581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mConfigEvents.removeAt(0);
6961035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        switch (event->mType) {
6973468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten        case CFG_EVENT_PRIO: {
6981035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get();
6991035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            // FIXME Need to understand why this has to be done asynchronously
7001035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            int err = requestPriority(data->mPid, data->mTid, data->mPrio,
7013468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten                    true /*asynchronous*/);
7023468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten            if (err != 0) {
7033468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten                ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d",
7041035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                      data->mPrio, data->mPid, data->mTid, err);
7053468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten            }
7063468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten        } break;
7073468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten        case CFG_EVENT_IO: {
7081035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            IoConfigEventData *data = (IoConfigEventData *)event->mData.get();
70973e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent            ioConfigChanged(data->mEvent);
7101035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        } break;
7111035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        case CFG_EVENT_SET_PARAMETER: {
7121035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get();
7131035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) {
7141035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                configChanged = true;
715d5418eb594435c958d6c37fa9938161a0112adbdGlenn Kasten            }
7163468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten        } break;
7171c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        case CFG_EVENT_CREATE_AUDIO_PATCH: {
7181c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            CreateAudioPatchConfigEventData *data =
7191c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                            (CreateAudioPatchConfigEventData *)event->mData.get();
7201c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle);
7211c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        } break;
7221c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        case CFG_EVENT_RELEASE_AUDIO_PATCH: {
7231c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            ReleaseAudioPatchConfigEventData *data =
7241c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                            (ReleaseAudioPatchConfigEventData *)event->mData.get();
7251c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            event->mStatus = releaseAudioPatch_l(data->mHandle);
7261c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        } break;
7273468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten        default:
7281035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType);
7293468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten            break;
73081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
7311035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        {
7321035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            Mutex::Autolock _l(event->mLock);
7331035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            if (event->mWaitStatus) {
7341035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                event->mWaitStatus = false;
7351035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                event->mCond.signal();
7361035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            }
7371035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
7381035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this);
7391035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
7401035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
7411035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (configChanged) {
7421035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        cacheParameters_l();
74381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
74481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
74581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
746b220884bf3129253cc5bc8d030bc475411ea4911Marco NelissenString8 channelMaskToString(audio_channel_mask_t mask, bool output) {
747b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    String8 s;
748e1635ec096d1110c33a5aa46847af59c261fb7faGlenn Kasten    const audio_channel_representation_t representation =
749e1635ec096d1110c33a5aa46847af59c261fb7faGlenn Kasten            audio_channel_mask_get_representation(mask);
750f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung
751f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung    switch (representation) {
752f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung    case AUDIO_CHANNEL_REPRESENTATION_POSITION: {
753f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung        if (output) {
754f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, ");
755f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, ");
756f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, ");
757f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, ");
758f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, ");
759f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, ");
760f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, ");
761f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, ");
762f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, ");
763f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, ");
764f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, ");
765f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,");
766f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, ");
767f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, ");
768f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, ");
769f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, ");
770f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " );
771f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " );
772f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown,  ");
773f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung        } else {
774f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, ");
775f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, ");
776f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, ");
777f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, ");
778f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, ");
779f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, ");
780f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, ");
781f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, ");
782f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, ");
783f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, ");
784f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, ");
785f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, ");
786f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, ");
787f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, ");
788f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown,  ");
789f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung        }
790f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung        const int len = s.length();
791f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung        if (len > 2) {
792f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            char *str = s.lockBuffer(len); // needed?
793f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung            s.unlockBuffer(len - 2);       // remove trailing ", "
794f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung        }
795f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung        return s;
796b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    }
797f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung    case AUDIO_CHANNEL_REPRESENTATION_INDEX:
798f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung        s.appendFormat("index mask, bits:%#x", audio_channel_mask_get_bits(mask));
799f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung        return s;
800f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung    default:
801f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung        s.appendFormat("unknown mask, representation:%d  bits:%#x",
802f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung                representation, audio_channel_mask_get_bits(mask));
803f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung        return s;
804b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    }
805b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen}
806b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen
8070f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args __unused)
80881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
80981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const size_t SIZE = 256;
81081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    char buffer[SIZE];
81181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    String8 result;
81281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
81381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool locked = AudioFlinger::dumpTryLock(mLock);
81481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!locked) {
81597b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten        dprintf(fd, "thread %p may be deadlocked\n", this);
816b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    }
817b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen
8180b89bc0d285b8fd4798df1ff0ba9f93851a3bd48Glenn Kasten    dprintf(fd, "  Thread name: %s\n", mThreadName);
81987cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  I/O handle: %d\n", mId);
82087cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  TID: %d\n", getTid());
82187cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Standby: %s\n", mStandby ? "yes" : "no");
82297b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    dprintf(fd, "  Sample rate: %u Hz\n", mSampleRate);
82387cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  HAL frame count: %zu\n", mFrameCount);
82497b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    dprintf(fd, "  HAL format: 0x%x (%s)\n", mHALFormat, formatToString(mHALFormat));
82587cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  HAL buffer size: %u bytes\n", mBufferSize);
82697b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    dprintf(fd, "  Channel count: %u\n", mChannelCount);
82797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    dprintf(fd, "  Channel mask: 0x%08x (%s)\n", mChannelMask,
828b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            channelMaskToString(mChannelMask, mType != RECORD).string());
82997b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    dprintf(fd, "  Format: 0x%x (%s)\n", mFormat, formatToString(mFormat));
83097b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    dprintf(fd, "  Frame size: %zu bytes\n", mFrameSize);
83187cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Pending config events:");
832b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    size_t numConfig = mConfigEvents.size();
833b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    if (numConfig) {
834b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        for (size_t i = 0; i < numConfig; i++) {
835b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            mConfigEvents[i]->dump(buffer, SIZE);
83687cebadd48710e42474756fc3513df678de045ceElliott Hughes            dprintf(fd, "\n    %s", buffer);
837b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        }
83887cebadd48710e42474756fc3513df678de045ceElliott Hughes        dprintf(fd, "\n");
839b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    } else {
84087cebadd48710e42474756fc3513df678de045ceElliott Hughes        dprintf(fd, " none\n");
84181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
8420b89bc0d285b8fd4798df1ff0ba9f93851a3bd48Glenn Kasten    dprintf(fd, "  Output device: %#x (%s)\n", mOutDevice, devicesToString(mOutDevice).string());
8430b89bc0d285b8fd4798df1ff0ba9f93851a3bd48Glenn Kasten    dprintf(fd, "  Input device: %#x (%s)\n", mInDevice, devicesToString(mInDevice).string());
8440b89bc0d285b8fd4798df1ff0ba9f93851a3bd48Glenn Kasten    dprintf(fd, "  Audio source: %d (%s)\n", mAudioSource, sourceToString(mAudioSource));
84581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
84681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (locked) {
84781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mLock.unlock();
84881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
84981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
85081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
85181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args)
85281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
85381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const size_t SIZE = 256;
85481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    char buffer[SIZE];
85581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    String8 result;
85681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
857b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    size_t numEffectChains = mEffectChains.size();
8581d6fa7af1288b550faabe4ec2cf98684236723dbNarayan Kamath    snprintf(buffer, SIZE, "  %zu Effect Chains\n", numEffectChains);
85981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    write(fd, buffer, strlen(buffer));
86081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
861b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    for (size_t i = 0; i < numEffectChains; ++i) {
86281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<EffectChain> chain = mEffectChains[i];
86381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (chain != 0) {
86481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chain->dump(fd, args);
86581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
86681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
86781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
86881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
869e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissenvoid AudioFlinger::ThreadBase::acquireWakeLock(int uid)
87081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
87181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
872e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen    acquireWakeLock_l(uid);
87381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
87481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
875014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan KamathString16 AudioFlinger::ThreadBase::getWakeLockTag()
876014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath{
877014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath    switch (mType) {
878bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten    case MIXER:
879bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten        return String16("AudioMix");
880bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten    case DIRECT:
881bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten        return String16("AudioDirectOut");
882bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten    case DUPLICATING:
883bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten        return String16("AudioDup");
884bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten    case RECORD:
885bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten        return String16("AudioIn");
886bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten    case OFFLOAD:
887bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten        return String16("AudioOffload");
888bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten    default:
889bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten        ALOG_ASSERT(false);
890bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten        return String16("AudioUnknown");
891014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath    }
892014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath}
893014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath
894e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissenvoid AudioFlinger::ThreadBase::acquireWakeLock_l(int uid)
89581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
896462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    getPowerManager_l();
89781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mPowerManager != 0) {
89881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<IBinder> binder = new BBinder();
899e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen        status_t status;
900e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen        if (uid >= 0) {
901547789d25dc6bd6561553bcf6b384fb0d4fee834Eric Laurent            status = mPowerManager->acquireWakeLockWithUid(POWERMANAGER_PARTIAL_WAKE_LOCK,
902e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen                    binder,
903014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath                    getWakeLockTag(),
904e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen                    String16("media"),
9053abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten                    uid,
9063abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten                    true /* FIXME force oneway contrary to .aidl */);
907e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen        } else {
908547789d25dc6bd6561553bcf6b384fb0d4fee834Eric Laurent            status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK,
909e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen                    binder,
910014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath                    getWakeLockTag(),
9113abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten                    String16("media"),
9123abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten                    true /* FIXME force oneway contrary to .aidl */);
913e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen        }
91481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (status == NO_ERROR) {
91581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mWakeLockToken = binder;
91681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
917d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten        ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status);
91881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
91981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
92081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
92181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::releaseWakeLock()
92281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
92381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
92481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    releaseWakeLock_l();
92581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
92681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
92781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::releaseWakeLock_l()
92881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
92981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mWakeLockToken != 0) {
930d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten        ALOGV("releaseWakeLock_l() %s", mThreadName);
93181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (mPowerManager != 0) {
9323abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten            mPowerManager->releaseWakeLock(mWakeLockToken, 0,
9333abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten                    true /* FIXME force oneway contrary to .aidl */);
93481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
93581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mWakeLockToken.clear();
93681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
93781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
93881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
939462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissenvoid AudioFlinger::ThreadBase::updateWakeLockUids(const SortedVector<int> &uids) {
940462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    Mutex::Autolock _l(mLock);
941462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    updateWakeLockUids_l(uids);
942462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen}
943462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen
944462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissenvoid AudioFlinger::ThreadBase::getPowerManager_l() {
94572e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    if (mSystemReady && mPowerManager == 0) {
946462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        // use checkService() to avoid blocking if power service is not up yet
947462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        sp<IBinder> binder =
948462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen            defaultServiceManager()->checkService(String16("power"));
949462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        if (binder == 0) {
950d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten            ALOGW("Thread %s cannot connect to the power manager service", mThreadName);
951462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        } else {
952462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen            mPowerManager = interface_cast<IPowerManager>(binder);
953462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen            binder->linkToDeath(mDeathRecipient);
954462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        }
955462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    }
956462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen}
957462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen
958462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissenvoid AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<int> &uids) {
959462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    getPowerManager_l();
960462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    if (mWakeLockToken == NULL) {
961462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        ALOGE("no wake lock to update!");
962462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        return;
963462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    }
964462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    if (mPowerManager != 0) {
965462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        sp<IBinder> binder = new BBinder();
966462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        status_t status;
9673abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten        status = mPowerManager->updateWakeLockUids(mWakeLockToken, uids.size(), uids.array(),
9683abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten                    true /* FIXME force oneway contrary to .aidl */);
969d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten        ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status);
970462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    }
971462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen}
972462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen
97381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::clearPowerManager()
97481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
97581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
97681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    releaseWakeLock_l();
97781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mPowerManager.clear();
97881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
97981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
9800f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused)
98181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
98281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<ThreadBase> thread = mThread.promote();
98381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread != 0) {
98481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread->clearPowerManager();
98581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
98681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGW("power manager service died !!!");
98781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
98881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
98981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::setEffectSuspended(
99081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const effect_uuid_t *type, bool suspend, int sessionId)
99181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
99281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
99381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    setEffectSuspended_l(type, suspend, sessionId);
99481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
99581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
99681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::setEffectSuspended_l(
99781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const effect_uuid_t *type, bool suspend, int sessionId)
99881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
99981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> chain = getEffectChain_l(sessionId);
100081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (chain != 0) {
100181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (type != NULL) {
100281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chain->setEffectSuspended_l(type, suspend);
100381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
100481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chain->setEffectSuspendedAll_l(suspend);
100581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
100681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
100781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
100881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    updateSuspendedSessions_l(type, suspend, sessionId);
100981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
101081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
101181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain)
101281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
101381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId());
101481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (index < 0) {
101581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return;
101681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
101781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
101881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects =
101981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mSuspendedSessions.valueAt(index);
102081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
102181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < sessionEffects.size(); i++) {
102281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<SuspendedSessionDesc> desc = sessionEffects.valueAt(i);
102381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (int j = 0; j < desc->mRefCount; j++) {
102481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) {
102581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                chain->setEffectSuspendedAll_l(true);
102681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            } else {
102781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x",
102881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    desc->mType.timeLow);
102981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                chain->setEffectSuspended_l(&desc->mType, true);
103081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
103181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
103281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
103381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
103481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
103581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type,
103681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                         bool suspend,
103781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                         int sessionId)
103881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
103981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ssize_t index = mSuspendedSessions.indexOfKey(sessionId);
104081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
104181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects;
104281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
104381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (suspend) {
104481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (index >= 0) {
104581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sessionEffects = mSuspendedSessions.valueAt(index);
104681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
104781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mSuspendedSessions.add(sessionId, sessionEffects);
104881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
104981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
105081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (index < 0) {
105181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return;
105281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
105381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sessionEffects = mSuspendedSessions.valueAt(index);
105481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
105581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
105681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
105781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int key = EffectChain::kKeyForSuspendAll;
105881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (type != NULL) {
105981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        key = type->timeLow;
106081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
106181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    index = sessionEffects.indexOfKey(key);
106281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
106381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<SuspendedSessionDesc> desc;
106481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (suspend) {
106581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (index >= 0) {
106681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            desc = sessionEffects.valueAt(index);
106781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
106881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            desc = new SuspendedSessionDesc();
106981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (type != NULL) {
107081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                desc->mType = *type;
107181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
107281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sessionEffects.add(key, desc);
107381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key);
107481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
107581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        desc->mRefCount++;
107681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
107781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (index < 0) {
107881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return;
107981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
108081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        desc = sessionEffects.valueAt(index);
108181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (--desc->mRefCount == 0) {
108281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key);
108381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sessionEffects.removeItemsAt(index);
108481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (sessionEffects.isEmpty()) {
108581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGV("updateSuspendedSessions_l() restore removing session %d",
108681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                 sessionId);
108781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mSuspendedSessions.removeItem(sessionId);
108881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
108981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
109081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
109181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!sessionEffects.isEmpty()) {
109281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mSuspendedSessions.replaceValueFor(sessionId, sessionEffects);
109381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
109481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
109581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
109681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
109781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                            bool enabled,
109881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                            int sessionId)
109981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
110081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
110181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    checkSuspendOnEffectEnabled_l(effect, enabled, sessionId);
110281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
110381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
110481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect,
110581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                            bool enabled,
110681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                            int sessionId)
110781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
110881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mType != RECORD) {
110981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on
111081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // another session. This gives the priority to well behaved effect control panels
111181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // and applications not using global effects.
111281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect
111381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // global effects
111481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) {
111581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX);
111681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
111781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
111881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
111981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> chain = getEffectChain_l(sessionId);
112081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (chain != 0) {
112181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        chain->checkSuspendOnEffectEnabled(effect, enabled);
112281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
112381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
112481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
112581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held
112681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l(
112781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const sp<AudioFlinger::Client>& client,
112881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const sp<IEffectClient>& effectClient,
112981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int32_t priority,
113081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int sessionId,
113181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effect_descriptor_t *desc,
113281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int *enabled,
11339156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten        status_t *status)
113481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
113581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectModule> effect;
113681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectHandle> handle;
113781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t lStatus;
113881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> chain;
113981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool chainCreated = false;
114081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool effectCreated = false;
114181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool effectRegistered = false;
114281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
114381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    lStatus = initCheck();
114481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (lStatus != NO_ERROR) {
114581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("createEffect_l() Audio driver not initialized.");
114681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
114781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
114881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
114998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Reject any effect on Direct output threads for now, since the format of
115098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo).
115198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    if (mType == DIRECT) {
115298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        ALOGW("createEffect_l() Cannot add effect %s on Direct output type thread %s",
1153d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten                desc->name, mThreadName);
115498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        lStatus = BAD_VALUE;
115598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        goto Exit;
115698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    }
115798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
1158389cfdbb9a92a438a0d7710321c2964c7ad55ecaAndy Hung    // Reject any effect on mixer or duplicating multichannel sinks.
11599a59276fb465e492138e0576523b54079671e8f4Andy Hung    // TODO: fix both format and multichannel issues with effects.
1160389cfdbb9a92a438a0d7710321c2964c7ad55ecaAndy Hung    if ((mType == MIXER || mType == DUPLICATING) && mChannelCount != FCC_2) {
1161389cfdbb9a92a438a0d7710321c2964c7ad55ecaAndy Hung        ALOGW("createEffect_l() Cannot add effect %s for multichannel(%d) %s threads",
1162389cfdbb9a92a438a0d7710321c2964c7ad55ecaAndy Hung                desc->name, mChannelCount, mType == MIXER ? "MIXER" : "DUPLICATING");
11639a59276fb465e492138e0576523b54079671e8f4Andy Hung        lStatus = BAD_VALUE;
11649a59276fb465e492138e0576523b54079671e8f4Andy Hung        goto Exit;
11659a59276fb465e492138e0576523b54079671e8f4Andy Hung    }
11669a59276fb465e492138e0576523b54079671e8f4Andy Hung
11675baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    // Allow global effects only on offloaded and mixer threads
11685baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
11695baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        switch (mType) {
11705baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        case MIXER:
11715baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        case OFFLOAD:
11725baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            break;
11735baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        case DIRECT:
11745baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        case DUPLICATING:
11755baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        case RECORD:
11765baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        default:
1177d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten            ALOGW("createEffect_l() Cannot add global effect %s on thread %s",
1178d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten                    desc->name, mThreadName);
11795baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            lStatus = BAD_VALUE;
11805baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            goto Exit;
11815baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        }
118281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
11835baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent
118481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Only Pre processor effects are allowed on input threads and only on input threads
118581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) {
118681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d",
118781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                desc->name, desc->flags, mType);
118881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = BAD_VALUE;
118981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
119081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
119181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
119281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId);
119381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
119481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    { // scope for mLock
119581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
119681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
119781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // check for existing effect chain with the requested audio session
119881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        chain = getEffectChain_l(sessionId);
119981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (chain == 0) {
120081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // create a new chain for this session
120181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("createEffect_l() new effect chain for session %d", sessionId);
120281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chain = new EffectChain(this, sessionId);
120381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            addEffectChain_l(chain);
120481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chain->setStrategy(getStrategyForSession_l(sessionId));
120581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chainCreated = true;
120681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
120781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect = chain->getEffectFromDesc_l(desc);
120881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
120981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
121081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get());
121181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
121281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (effect == 0) {
121381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            int id = mAudioFlinger->nextUniqueId();
121481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // Check CPU and memory usage
121581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id);
121681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (lStatus != NO_ERROR) {
121781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
121881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
121981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effectRegistered = true;
122081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // create a new effect module if none present in the chain
122181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect = new EffectModule(this, chain, desc, id, sessionId);
122281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = effect->status();
122381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (lStatus != NO_ERROR) {
122481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
122581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
12265baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            effect->setOffloaded(mType == OFFLOAD, mId);
12275baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent
122881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = chain->addEffect_l(effect);
122981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (lStatus != NO_ERROR) {
123081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
123181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
123281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effectCreated = true;
123381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
123481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect->setDevice(mOutDevice);
123581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect->setDevice(mInDevice);
123681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect->setMode(mAudioFlinger->getMode());
123781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect->setAudioSource(mAudioSource);
123881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
123981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // create effect handle and connect it to effect module
124081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        handle = new EffectHandle(effect, client, effectClient, priority);
1241e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten        lStatus = handle->initCheck();
1242e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten        if (lStatus == OK) {
1243e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten            lStatus = effect->addHandle(handle.get());
1244e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten        }
124581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (enabled != NULL) {
124681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            *enabled = (int)effect->isEnabled();
124781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
124881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
124981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
125081784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit:
125181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) {
125281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
125381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (effectCreated) {
125481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chain->removeEffect_l(effect);
125581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
125681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (effectRegistered) {
125781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioSystem::unregisterEffect(effect->id());
125881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
125981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (chainCreated) {
126081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            removeEffectChain_l(chain);
126181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
126281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        handle.clear();
126381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
126481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
12659156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
126681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return handle;
126781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
126881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
126981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(int sessionId, int effectId)
127081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
127181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
127281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return getEffect_l(sessionId, effectId);
127381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
127481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
127581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId)
127681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
127781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> chain = getEffectChain_l(sessionId);
127881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return chain != 0 ? chain->getEffectFromId_l(effectId) : 0;
127981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
128081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
128181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and
128281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// PlaybackThread::mLock held
128381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect)
128481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
128581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // check for existing effect chain with the requested audio session
128681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int sessionId = effect->sessionId();
128781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> chain = getEffectChain_l(sessionId);
128881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool chainCreated = false;
128981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
12905baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(),
12915baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent             "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %x",
12925baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                    this, effect->desc().name, effect->desc().flags);
12935baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent
129481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (chain == 0) {
129581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // create a new chain for this session
129681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("addEffect_l() new effect chain for session %d", sessionId);
129781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        chain = new EffectChain(this, sessionId);
129881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        addEffectChain_l(chain);
129981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        chain->setStrategy(getStrategyForSession_l(sessionId));
130081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        chainCreated = true;
130181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
130281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get());
130381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
130481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (chain->getEffectFromId_l(effect->id()) != 0) {
130581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("addEffect_l() %p effect %s already present in chain %p",
130681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                this, effect->desc().name, chain.get());
130781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
130881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
130981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
13105baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    effect->setOffloaded(mType == OFFLOAD, mId);
13115baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent
131281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t status = chain->addEffect_l(effect);
131381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (status != NO_ERROR) {
131481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (chainCreated) {
131581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            removeEffectChain_l(chain);
131681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
131781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return status;
131881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
131981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
132081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect->setDevice(mOutDevice);
132181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect->setDevice(mInDevice);
132281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect->setMode(mAudioFlinger->getMode());
132381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect->setAudioSource(mAudioSource);
132481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
132581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
132681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
132781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) {
132881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
132981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("removeEffect_l() %p effect %p", this, effect.get());
133081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect_descriptor_t desc = effect->desc();
133181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
133281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        detachAuxEffect_l(effect->id());
133381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
133481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
133581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> chain = effect->chain().promote();
133681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (chain != 0) {
133781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // remove effect chain if removing last effect
133881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (chain->removeEffect_l(effect) == 0) {
133981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            removeEffectChain_l(chain);
134081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
134181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
134281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get());
134381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
134481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
134581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
134681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::lockEffectChains_l(
134781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Vector< sp<AudioFlinger::EffectChain> >& effectChains)
134881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
134981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effectChains = mEffectChains;
135081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mEffectChains.size(); i++) {
135181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mEffectChains[i]->lock();
135281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
135381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
135481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
135581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::unlockEffectChains(
135681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const Vector< sp<AudioFlinger::EffectChain> >& effectChains)
135781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
135881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < effectChains.size(); i++) {
135981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effectChains[i]->unlock();
136081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
136181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
136281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
136381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId)
136481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
136581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
136681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return getEffectChain_l(sessionId);
136781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
136881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
136981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) const
137081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
137181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t size = mEffectChains.size();
137281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < size; i++) {
137381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (mEffectChains[i]->sessionId() == sessionId) {
137481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return mEffectChains[i];
137581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
137681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
137781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return 0;
137881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
137981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
138081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::setMode(audio_mode_t mode)
138181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
138281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
138381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t size = mEffectChains.size();
138481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < size; i++) {
138581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mEffectChains[i]->setMode_l(mode);
138681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
138781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
138881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
138983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::ThreadBase::getAudioPortConfig(struct audio_port_config *config)
139083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
139183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    config->type = AUDIO_PORT_TYPE_MIX;
139283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    config->ext.mix.handle = mId;
139383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    config->sample_rate = mSampleRate;
139483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    config->format = mFormat;
139583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    config->channel_mask = mChannelMask;
139683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK|
139783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                            AUDIO_PORT_CONFIG_FORMAT;
139883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
139983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
140072e3f39146fce4686bd96f11057c051bea376dfbEric Laurentvoid AudioFlinger::ThreadBase::systemReady()
140172e3f39146fce4686bd96f11057c051bea376dfbEric Laurent{
140272e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    Mutex::Autolock _l(mLock);
140372e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    if (mSystemReady) {
140472e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        return;
140572e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    }
140672e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    mSystemReady = true;
140772e3f39146fce4686bd96f11057c051bea376dfbEric Laurent
140872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    for (size_t i = 0; i < mPendingConfigEvents.size(); i++) {
140972e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        sendConfigEvent_l(mPendingConfigEvents.editItemAt(i));
141072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    }
141172e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    mPendingConfigEvents.clear();
141272e3f39146fce4686bd96f11057c051bea376dfbEric Laurent}
141372e3f39146fce4686bd96f11057c051bea376dfbEric Laurent
141483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
141581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
141681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent//      Playback
141781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
141881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
141981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger,
142081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                             AudioStreamOut* output,
142181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                             audio_io_handle_t id,
142281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                             audio_devices_t device,
142372e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                                             type_t type,
142472e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                                             bool systemReady)
142572e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    :   ThreadBase(audioFlinger, id, device, AUDIO_DEVICE_NONE, type, systemReady),
14262098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung        mNormalFrameCount(0), mSinkBuffer(NULL),
14276146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung        mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision),
142869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung        mMixerBuffer(NULL),
142969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung        mMixerBufferSize(0),
143069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung        mMixerBufferFormat(AUDIO_FORMAT_INVALID),
143169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung        mMixerBufferValid(false),
14326146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung        mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision),
143398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        mEffectBuffer(NULL),
143498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        mEffectBufferSize(0),
143598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        mEffectBufferFormat(AUDIO_FORMAT_INVALID),
143698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        mEffectBufferValid(false),
1437c1fac191069774c7bfcb062edbb821ea56e7dbc0Glenn Kasten        mSuspended(0), mBytesWritten(0),
1438462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        mActiveTracksGeneration(0),
143981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // mStreamTypes[] initialized in constructor body
144081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mOutput(output),
144181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false),
144281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mMixerStatus(MIXER_IDLE),
144381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mMixerStatusIgnoringFastTracks(MIXER_IDLE),
1444ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent        mStandbyDelayNs(AudioFlinger::mStandbyTimeInNsecs),
1445bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        mBytesRemaining(0),
1446bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        mCurrentWriteLength(0),
1447bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        mUseAsyncWrite(false),
14483b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mWriteAckSequence(0),
14493b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mDrainSequence(0),
1450ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent        mSignalPending(false),
145181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mScreenState(AudioFlinger::mScreenState),
145281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // index 0 is reserved for normal mixer's submix
1453bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten        mFastTrackAvailMask(((1 << FastMixerState::kMaxFastTracks) - 1) & ~1),
1454d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent        mHwSupportsPause(false), mHwPaused(false), mFlushPending(false),
1455bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten        // mLatchD, mLatchQ,
1456bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten        mLatchDValid(false), mLatchQValid(false)
145781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
1458d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten    snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id);
1459d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten    mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName);
146081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
146181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Assumes constructor is called by AudioFlinger with it's mLock held, but
146281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // it would be safer to explicitly pass initial masterVolume/masterMute as
146381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // parameter.
146481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    //
146581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // If the HAL we are using has support for master volume or master mute,
146681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // then do not attenuate or mute during mixing (just leave the volume at 1.0
146781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // and the mute set to false).
146881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mMasterVolume = audioFlinger->masterVolume_l();
146981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mMasterMute = audioFlinger->masterMute_l();
147081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mOutput && mOutput->audioHwDev) {
147181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (mOutput->audioHwDev->canSetMasterVolume()) {
147281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mMasterVolume = 1.0;
147381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
147481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
147581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (mOutput->audioHwDev->canSetMasterMute()) {
147681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mMasterMute = false;
147781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
147881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
147981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1480deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten    readOutputParameters_l();
148181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1482223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    // ++ operator does not compile
148366e4635cb09fadcaccf912f37c387396c428378aGlenn Kasten    for (audio_stream_type_t stream = AUDIO_STREAM_MIN; stream < AUDIO_STREAM_CNT;
148481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            stream = (audio_stream_type_t) (stream + 1)) {
148581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream);
148681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream);
148781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
148881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
148981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
149081784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread::~PlaybackThread()
149181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
14929e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    mAudioFlinger->unregisterWriter(mNBLogWriter);
1493010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    free(mSinkBuffer);
149469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    free(mMixerBuffer);
149598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    free(mEffectBuffer);
149681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
149781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
149881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args)
149981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
150081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    dumpInternals(fd, args);
150181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    dumpTracks(fd, args);
150281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    dumpEffectChains(fd, args);
150381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
150481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
15050f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args __unused)
150681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
150781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const size_t SIZE = 256;
150881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    char buffer[SIZE];
150981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    String8 result;
151081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1511b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    result.appendFormat("  Stream volumes in dB: ");
151281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (int i = 0; i < AUDIO_STREAM_CNT; ++i) {
151381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const stream_type_t *st = &mStreamTypes[i];
151481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (i > 0) {
151581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            result.appendFormat(", ");
151681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
151781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume));
151881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (st->mute) {
151981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            result.append("M");
152081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
152181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
152281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    result.append("\n");
152381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    write(fd, result.string(), result.length());
152481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    result.clear();
152581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1526b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    // These values are "raw"; they will wrap around.  See prepareTracks_l() for a better way.
1527b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    FastTrackUnderruns underruns = getFastTrackUnderruns(0);
152887cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Normal mixer raw underrun counters: partial=%u empty=%u\n",
1529b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty);
1530b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen
1531b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    size_t numtracks = mTracks.size();
1532b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    size_t numactive = mActiveTracks.size();
153387cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  %d Tracks", numtracks);
1534b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    size_t numactiveseen = 0;
1535b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    if (numtracks) {
153687cebadd48710e42474756fc3513df678de045ceElliott Hughes        dprintf(fd, " of which %d are active\n", numactive);
1537b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        Track::appendDumpHeader(result);
1538b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        for (size_t i = 0; i < numtracks; ++i) {
1539b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            sp<Track> track = mTracks[i];
1540b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            if (track != 0) {
1541b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                bool active = mActiveTracks.indexOf(track) >= 0;
1542b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                if (active) {
1543b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                    numactiveseen++;
1544b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                }
1545b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                track->dump(buffer, SIZE, active);
1546b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                result.append(buffer);
1547b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            }
154881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
1549b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    } else {
1550b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        result.append("\n");
155181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
1552b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    if (numactiveseen != numactive) {
1553b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        // some tracks in the active list were not in the tracks list
1554b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        snprintf(buffer, SIZE, "  The following tracks are in the active list but"
1555b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                " not in the track list\n");
1556b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        result.append(buffer);
1557b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        Track::appendDumpHeader(result);
1558b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        for (size_t i = 0; i < numactive; ++i) {
1559b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            sp<Track> track = mActiveTracks[i].promote();
1560b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            if (track != 0 && mTracks.indexOf(track) < 0) {
1561b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                track->dump(buffer, SIZE, true);
1562b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                result.append(buffer);
1563b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            }
156481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
156581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
1566b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen
156781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    write(fd, result.string(), result.size());
156881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
156981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
157081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args)
157181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
157297b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    dprintf(fd, "\nOutput thread %p type %d (%s):\n", this, type(), threadTypeToString(type()));
157344182c206f7c5584ef2cf504da6be98fab665dbfGlenn Kasten
157444182c206f7c5584ef2cf504da6be98fab665dbfGlenn Kasten    dumpBase(fd, args);
157544182c206f7c5584ef2cf504da6be98fab665dbfGlenn Kasten
157687cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Normal frame count: %zu\n", mNormalFrameCount);
157787cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime));
157887cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Total writes: %d\n", mNumWrites);
157987cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Delayed writes: %d\n", mNumDelayedWrites);
158087cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Blocked in write: %s\n", mInWrite ? "yes" : "no");
158187cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Suspend count: %d\n", mSuspended);
158287cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Sink buffer : %p\n", mSinkBuffer);
158387cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Mixer buffer: %p\n", mMixerBuffer);
158487cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Effect buffer: %p\n", mEffectBuffer);
158587cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Fast track availMask=%#x\n", mFastTrackAvailMask);
158697b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    AudioStreamOut *output = mOutput;
158797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    audio_output_flags_t flags = output != NULL ? output->flags : AUDIO_OUTPUT_FLAG_NONE;
158897b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    String8 flagsAsString = outputFlagsToString(flags);
158997b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    dprintf(fd, "  AudioStreamOut: %p flags %#x (%s)\n", output, flags, flagsAsString.string());
159081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
159181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
159281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Thread virtuals
159381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
159481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::onFirstRef()
159581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
1596d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten    run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO);
159781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
159881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
159981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ThreadBase virtuals
160081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::preExit()
160181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
160281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("  preExit()");
160381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME this is using hard-coded strings but in the future, this functionality will be
160481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    //       converted to use audio HAL extensions required to support tunneling
160581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mOutput->stream->common.set_parameters(&mOutput->stream->common, "exiting=1");
160681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
160781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
160881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held
160981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l(
161081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const sp<AudioFlinger::Client>& client,
161181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_stream_type_t streamType,
161281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t sampleRate,
161381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_format_t format,
161481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_channel_mask_t channelMask,
161574935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten        size_t *pFrameCount,
161681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const sp<IMemory>& sharedBuffer,
161781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int sessionId,
161881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        IAudioFlinger::track_flags_t *flags,
161981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        pid_t tid,
1620462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        int uid,
162181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status_t *status)
162281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
162374935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten    size_t frameCount = *pFrameCount;
162481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<Track> track;
162581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t lStatus;
162681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
162781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool isTimed = (*flags & IAudioFlinger::TRACK_TIMED) != 0;
162881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
162981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // client expresses a preference for FAST, but we get the final say
163081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (*flags & IAudioFlinger::TRACK_FAST) {
163181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent      if (
163281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // not timed
163381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (!isTimed) &&
163481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // either of these use cases:
163581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (
163681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              // use case 1: shared buffer with any frame count
163781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              (
163881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                (sharedBuffer != 0)
163981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              ) ||
16401dfe2f9c2d03fc8d0ed0cdfe0b9fb894bc0bcc11Glenn Kasten              // use case 2: frame count is default or at least as large as HAL
164181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              (
16421dfe2f9c2d03fc8d0ed0cdfe0b9fb894bc0bcc11Glenn Kasten                // we formerly checked for a callback handler (non-0 tid),
16431dfe2f9c2d03fc8d0ed0cdfe0b9fb894bc0bcc11Glenn Kasten                // but that is no longer required for TRANSFER_OBTAIN mode
164481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ((frameCount == 0) ||
1645b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten                (frameCount >= mFrameCount))
164681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              )
164781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ) &&
164881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // PCM data
164981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            audio_is_linear_pcm(format) &&
16501f439e1cf16a29347288ba9ddd06c0b6d086a145Andy Hung            // TODO: extract as a data library function that checks that a computationally
16511f439e1cf16a29347288ba9ddd06c0b6d086a145Andy Hung            // expensive downmixer is not required: isFastOutputChannelConversion()
16529a59276fb465e492138e0576523b54079671e8f4Andy Hung            (channelMask == mChannelMask ||
16531f439e1cf16a29347288ba9ddd06c0b6d086a145Andy Hung                    mChannelMask != AUDIO_CHANNEL_OUT_STEREO ||
16541f439e1cf16a29347288ba9ddd06c0b6d086a145Andy Hung                    (channelMask == AUDIO_CHANNEL_OUT_MONO
16551f439e1cf16a29347288ba9ddd06c0b6d086a145Andy Hung                            /* && mChannelMask == AUDIO_CHANNEL_OUT_STEREO */)) &&
165681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // hardware sample rate
165781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (sampleRate == mSampleRate) &&
165881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // normal mixer has an associated fast mixer
165981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            hasFastMixer() &&
166081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // there are sufficient fast track slots available
166181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (mFastTrackAvailMask != 0)
166281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // FIXME test that MixerThread for this fast track has a capable output HAL
166381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // FIXME add a permission test also?
166481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ) {
166581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // if frameCount not specified, then it defaults to fast mixer (HAL) frame count
166681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (frameCount == 0) {
16670349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten            // read the fast track multiplier property the first time it is needed
16680349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten            int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit);
16690349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten            if (ok != 0) {
16700349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten                ALOGE("%s pthread_once failed: %d", __func__, ok);
16710349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten            }
16720349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten            frameCount = mFrameCount * sFastTrackMultiplier;
167381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
167481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%d mFrameCount=%d",
167581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                frameCount, mFrameCount);
167681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent      } else {
167781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: isTimed=%d sharedBuffer=%p frameCount=%d "
16786146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung                "mFrameCount=%d format=%#x mFormat=%#x isLinear=%d channelMask=%#x "
16796146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung                "sampleRate=%u mSampleRate=%u "
168081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x",
16816146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung                isTimed, sharedBuffer.get(), frameCount, mFrameCount, format, mFormat,
168281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_is_linear_pcm(format),
168381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask);
168481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        *flags &= ~IAudioFlinger::TRACK_FAST;
16850e48d25606c82def035ad10a5b3923767a765cddAndy Hung      }
16860e48d25606c82def035ad10a5b3923767a765cddAndy Hung    }
16870e48d25606c82def035ad10a5b3923767a765cddAndy Hung    // For normal PCM streaming tracks, update minimum frame count.
16880e48d25606c82def035ad10a5b3923767a765cddAndy Hung    // For compatibility with AudioTrack calculation, buffer depth is forced
16890e48d25606c82def035ad10a5b3923767a765cddAndy Hung    // to be at least 2 x the normal mixer frame count and cover audio hardware latency.
16900e48d25606c82def035ad10a5b3923767a765cddAndy Hung    // This is probably too conservative, but legacy application code may depend on it.
16910e48d25606c82def035ad10a5b3923767a765cddAndy Hung    // If you change this calculation, also review the start threshold which is related.
16920e48d25606c82def035ad10a5b3923767a765cddAndy Hung    if (!(*flags & IAudioFlinger::TRACK_FAST)
16930e48d25606c82def035ad10a5b3923767a765cddAndy Hung            && audio_is_linear_pcm(format) && sharedBuffer == 0) {
16948edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung        // this must match AudioTrack.cpp calculateMinFrameCount().
16958edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung        // TODO: Move to a common library
169681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream);
169781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate);
169881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (minBufCount < 2) {
169981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            minBufCount = 2;
170081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
17018edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung        // For normal mixing tracks, if speed is > 1.0f (normal), AudioTrack
17028edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung        // or the client should compute and pass in a larger buffer request.
17030e48d25606c82def035ad10a5b3923767a765cddAndy Hung        size_t minFrameCount =
17048edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung                minBufCount * sourceFramesNeededWithTimestretch(
17058edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung                        sampleRate, mNormalFrameCount,
17068edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung                        mSampleRate, AUDIO_TIMESTRETCH_SPEED_NORMAL /*speed*/);
17070e48d25606c82def035ad10a5b3923767a765cddAndy Hung        if (frameCount < minFrameCount) { // including frameCount == 0
170881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            frameCount = minFrameCount;
170981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
171081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
171174935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten    *pFrameCount = frameCount;
171281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1713c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten    switch (mType) {
1714c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten
1715c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten    case DIRECT:
1716993fa0603707e94ce259e95e56838a85b5ccbdc5Glenn Kasten        if (audio_is_linear_pcm(format)) {
171781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) {
1718cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten                ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x "
1719cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten                        "for output %p with format %#x",
172081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        sampleRate, format, channelMask, mOutput, mFormat);
172181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = BAD_VALUE;
172281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
172381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
172481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
1725c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten        break;
1726c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten
1727c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten    case OFFLOAD:
1728bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) {
1729cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten            ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \""
1730cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten                    "for output %p with format %#x",
1731bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    sampleRate, format, channelMask, mOutput, mFormat);
1732bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            lStatus = BAD_VALUE;
1733bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            goto Exit;
1734bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
1735c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten        break;
1736c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten
1737c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten    default:
1738993fa0603707e94ce259e95e56838a85b5ccbdc5Glenn Kasten        if (!audio_is_linear_pcm(format)) {
1739cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten                ALOGE("createTrack_l() Bad parameter: format %#x \""
1740cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten                        "for output %p with format %#x",
1741bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        format, mOutput, mFormat);
1742bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                lStatus = BAD_VALUE;
1743bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                goto Exit;
1744bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
1745cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung        if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) {
174681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate);
174781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = BAD_VALUE;
174881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
174981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
1750c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten        break;
1751c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten
175281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
175381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
175481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    lStatus = initCheck();
175581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (lStatus != NO_ERROR) {
175615e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten        ALOGE("createTrack_l() audio driver not initialized");
175781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
175881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
175981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
176081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    { // scope for mLock
176181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
176281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
176381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // all tracks in same audio session must share the same routing strategy otherwise
176481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // conflicts will happen when tracks are moved from one output to another by audio policy
176581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // manager
176681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t strategy = AudioSystem::getStrategyForStream(streamType);
176781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t i = 0; i < mTracks.size(); ++i) {
176881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<Track> t = mTracks[i];
176983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            if (t != 0 && t->isExternalTrack()) {
177081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t actual = AudioSystem::getStrategyForStream(t->streamType());
177181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (sessionId == t->sessionId() && strategy != actual) {
177281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ALOGE("createTrack_l() mismatched strategy; expected %u but found %u",
177381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            strategy, actual);
177481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    lStatus = BAD_VALUE;
177581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    goto Exit;
177681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
177781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
177881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
177981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
178081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (!isTimed) {
178181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            track = new Track(this, client, streamType, sampleRate, format,
178283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                              channelMask, frameCount, NULL, sharedBuffer,
178383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                              sessionId, uid, *flags, TrackBase::TYPE_DEFAULT);
178481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
178581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            track = TimedTrack::create(this, client, streamType, sampleRate, format,
1786462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                    channelMask, frameCount, sharedBuffer, sessionId, uid);
178781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
1788030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten
1789030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten        // new Track always returns non-NULL,
1790030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten        // but TimedTrack::create() is a factory that could fail by returning NULL
1791030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten        lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY;
1792030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten        if (lStatus != NO_ERROR) {
17930cde076ddb283c84c3801a2df4cc3df99bd1577fGlenn Kasten            ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus);
179403e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George            // track must be cleared from the caller as the caller has the AF lock
179581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
179681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
179781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mTracks.add(track);
179881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
179981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<EffectChain> chain = getEffectChain_l(sessionId);
180081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (chain != 0) {
180181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer());
180281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            track->setMainBuffer(chain->inBuffer());
180381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType()));
180481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chain->incTrackCnt();
180581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
180681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
180781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) {
180881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            pid_t callingPid = IPCThreadState::self()->getCallingPid();
180981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful,
181081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // so ask activity manager to do this on our behalf
181181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp);
181281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
181381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
181481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
181581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    lStatus = NO_ERROR;
181681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
181781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit:
18189156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
181981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return track;
182081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
182181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
182281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const
182381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
182481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return latency;
182581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
182681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
182781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::latency() const
182881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
182981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
183081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return latency_l();
183181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
183281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::latency_l() const
183381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
183481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (initCheck() == NO_ERROR) {
183581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return correctLatency_l(mOutput->stream->get_latency(mOutput->stream));
183681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
183781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
183881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
183981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
184081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
184181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::setMasterVolume(float value)
184281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
184381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
184481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Don't apply master volume in SW if our HAL can do it for us.
184581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mOutput && mOutput->audioHwDev &&
184681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mOutput->audioHwDev->canSetMasterVolume()) {
184781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mMasterVolume = 1.0;
184881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
184981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mMasterVolume = value;
185081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
185181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
185281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
185381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::setMasterMute(bool muted)
185481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
185581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
185681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Don't apply master mute in SW if our HAL can do it for us.
185781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mOutput && mOutput->audioHwDev &&
185881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mOutput->audioHwDev->canSetMasterMute()) {
185981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mMasterMute = false;
186081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
186181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mMasterMute = muted;
186281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
186381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
186481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
186581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value)
186681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
186781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
186881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mStreamTypes[stream].volume = value;
1869ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent    broadcast_l();
187081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
187181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
187281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted)
187381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
187481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
187581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mStreamTypes[stream].mute = muted;
1876ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent    broadcast_l();
187781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
187881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
187981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentfloat AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const
188081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
188181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
188281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mStreamTypes[stream].volume;
188381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
188481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
188581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// addTrack_l() must be called with ThreadBase::mLock held
188681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track)
188781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
188881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t status = ALREADY_EXISTS;
188981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
189081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // set retry count for buffer fill
189181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    track->mRetryCount = kMaxTrackStartupRetries;
189281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mActiveTracks.indexOf(track) < 0) {
189381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // the track is newly added, make sure it fills up all its
189481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // buffers before playing. This is to ensure the client will
189581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // effectively get the latency it requested.
189683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        if (track->isExternalTrack()) {
1897bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            TrackBase::track_state state = track->mState;
1898bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            mLock.unlock();
1899e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent            status = AudioSystem::startOutput(mId, track->streamType(),
1900e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                              (audio_session_t)track->sessionId());
1901bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            mLock.lock();
1902bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            // abort track was stopped/paused while we released the lock
1903bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (state != track->mState) {
1904bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                if (status == NO_ERROR) {
1905bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    mLock.unlock();
1906e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                    AudioSystem::stopOutput(mId, track->streamType(),
1907e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                            (audio_session_t)track->sessionId());
1908bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    mLock.lock();
1909bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
1910bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                return INVALID_OPERATION;
1911bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
1912bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            // abort if start is rejected by audio policy manager
1913bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (status != NO_ERROR) {
1914bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                return PERMISSION_DENIED;
1915bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
1916bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent#ifdef ADD_BATTERY_DATA
1917bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            // to track the speaker usage
1918bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart);
1919bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent#endif
1920bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
1921bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
19229f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        track->mFillingUpStatus = track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING;
192381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        track->mResetDone = false;
192481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        track->mPresentationCompleteFrames = 0;
192581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mActiveTracks.add(track);
1926462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        mWakeLockUids.add(track->uid());
1927462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        mActiveTracksGeneration++;
1928fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        mLatestActiveTrack = track;
1929d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent        sp<EffectChain> chain = getEffectChain_l(track->sessionId());
1930d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent        if (chain != 0) {
1931d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent            ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(),
1932d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent                    track->sessionId());
1933d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent            chain->incActiveTrackCnt();
193481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
193581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
193681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status = NO_ERROR;
193781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
193881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
19394c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George    onAddNewTrack_l();
194081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return status;
194181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
194281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1943bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track)
194481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
1945bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    track->terminate();
194681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // active tracks are removed by threadLoop()
1947bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    bool trackActive = (mActiveTracks.indexOf(track) >= 0);
1948bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    track->mState = TrackBase::STOPPED;
1949bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if (!trackActive) {
195081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        removeTrack_l(track);
1951ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent    } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) {
1952bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        track->mState = TrackBase::STOPPING_1;
195381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
1954bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1955bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    return trackActive;
195681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
195781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
195881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track)
195981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
196081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE);
196181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mTracks.remove(track);
196281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    deleteTrackName_l(track->name());
196381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // redundant as track is about to be destroyed, for dumpsys only
196481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    track->mName = -1;
196581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (track->isFastTrack()) {
196681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int index = track->mFastIndex;
196781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOG_ASSERT(0 < index && index < (int)FastMixerState::kMaxFastTracks);
196881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index)));
196981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mFastTrackAvailMask |= 1 << index;
197081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // redundant as track is about to be destroyed, for dumpsys only
197181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        track->mFastIndex = -1;
197281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
197381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> chain = getEffectChain_l(track->sessionId());
197481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (chain != 0) {
197581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        chain->decTrackCnt();
197681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
197781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
197881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1979ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurentvoid AudioFlinger::PlaybackThread::broadcast_l()
1980bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
1981bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // Thread could be blocked waiting for async
1982bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // so signal it to handle state changes immediately
1983bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // If threadLoop is currently unlocked a signal of mWaitWorkCV will
1984bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // be lost so we also flag to prevent it blocking on mWaitWorkCV
1985bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    mSignalPending = true;
1986ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent    mWaitWorkCV.broadcast();
1987bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
1988bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
198981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentString8 AudioFlinger::PlaybackThread::getParameters(const String8& keys)
199081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
199181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
199281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (initCheck() != NO_ERROR) {
1993d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten        return String8();
199481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
199581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1996d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten    char *s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string());
1997d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten    const String8 out_s8(s);
199881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    free(s);
199981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return out_s8;
200081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
200181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
200273e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurentvoid AudioFlinger::PlaybackThread::ioConfigChanged(audio_io_config_event event) {
200373e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    sp<AudioIoDescriptor> desc = new AudioIoDescriptor();
200473e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    ALOGV("PlaybackThread::ioConfigChanged, thread %p, event %d", this, event);
200581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
200673e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    desc->mIoHandle = mId;
200781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
200881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    switch (event) {
200973e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    case AUDIO_OUTPUT_OPENED:
201073e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    case AUDIO_OUTPUT_CONFIG_CHANGED:
2011296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        desc->mPatch = mPatch;
201273e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        desc->mChannelMask = mChannelMask;
201373e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        desc->mSamplingRate = mSampleRate;
201473e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        desc->mFormat = mFormat;
201573e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        desc->mFrameCount = mNormalFrameCount; // FIXME see
201681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                             // AudioFlinger::frameCount(audio_io_handle_t)
201773e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        desc->mLatency = latency_l();
201881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        break;
201981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
202073e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    case AUDIO_OUTPUT_CLOSED:
202181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    default:
202281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        break;
202381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
202473e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    mAudioFlinger->ioConfigChanged(event, desc);
202581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
202681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2027bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::writeCallback()
2028bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
2029bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ALOG_ASSERT(mCallbackThread != 0);
20303b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    mCallbackThread->resetWriteBlocked();
2031bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
2032bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
2033bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::drainCallback()
2034bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
2035bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ALOG_ASSERT(mCallbackThread != 0);
20363b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    mCallbackThread->resetDraining();
2037bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
2038bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
20393b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence)
2040bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
2041bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    Mutex::Autolock _l(mLock);
20423b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // reject out of sequence requests
20433b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) {
20443b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mWriteAckSequence &= ~1;
2045bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        mWaitWorkCV.signal();
2046bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
2047bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
2048bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
20493b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence)
2050bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
2051bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    Mutex::Autolock _l(mLock);
20523b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // reject out of sequence requests
20533b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    if ((mDrainSequence & 1) && (sequence == mDrainSequence)) {
20543b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mDrainSequence &= ~1;
2055bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        mWaitWorkCV.signal();
2056bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
2057bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
2058bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
2059bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// static
2060bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentint AudioFlinger::PlaybackThread::asyncCallback(stream_callback_event_t event,
20610f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten                                                void *param __unused,
2062bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                                                void *cookie)
2063bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
2064bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    AudioFlinger::PlaybackThread *me = (AudioFlinger::PlaybackThread *)cookie;
2065bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ALOGV("asyncCallback() event %d", event);
2066bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    switch (event) {
2067bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    case STREAM_CBK_EVENT_WRITE_READY:
2068bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        me->writeCallback();
2069bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        break;
2070bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    case STREAM_CBK_EVENT_DRAIN_READY:
2071bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        me->drainCallback();
2072bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        break;
2073bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    default:
2074bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        ALOGW("asyncCallback() unknown event %d", event);
2075bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        break;
2076bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
2077bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    return 0;
2078bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
2079bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
2080deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kastenvoid AudioFlinger::PlaybackThread::readOutputParameters_l()
208181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
2082adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten    // unfortunately we have no way of recovering from errors here, hence the LOG_ALWAYS_FATAL
208381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common);
208481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common);
20857fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten    if (!audio_is_output_channel(mChannelMask)) {
2086adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten        LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask);
20877fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten    }
20889a59276fb465e492138e0576523b54079671e8f4Andy Hung    if ((mType == MIXER || mType == DUPLICATING)
20899a59276fb465e492138e0576523b54079671e8f4Andy Hung            && !isValidPcmSinkChannelMask(mChannelMask)) {
20909a59276fb465e492138e0576523b54079671e8f4Andy Hung        LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output",
20919a59276fb465e492138e0576523b54079671e8f4Andy Hung                mChannelMask);
20927fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten    }
2093e541269be94f3a1072932d51537905b120ef4733Andy Hung    mChannelCount = audio_channel_count_from_out_mask(mChannelMask);
2094463be250de73907965faa6a216c00312bf81e049Andy Hung    mHALFormat = mOutput->stream->common.get_format(&mOutput->stream->common);
2095463be250de73907965faa6a216c00312bf81e049Andy Hung    mFormat = mHALFormat;
20967fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten    if (!audio_is_valid_format(mFormat)) {
2097adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten        LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat);
20987fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten    }
20996146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung    if ((mType == MIXER || mType == DUPLICATING)
21006146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung            && !isValidPcmSinkFormat(mFormat)) {
21016146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung        LOG_FATAL("HAL format %#x not supported for mixed output",
21026146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung                mFormat);
21037fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten    }
2104062e67a26e0553dd142be622821f493df541f0c6Phil Burk    mFrameSize = mOutput->getFrameSize();
210570949c47fbae3f836d15f040551d7631be3ed7c2Glenn Kasten    mBufferSize = mOutput->stream->common.get_buffer_size(&mOutput->stream->common);
210670949c47fbae3f836d15f040551d7631be3ed7c2Glenn Kasten    mFrameCount = mBufferSize / mFrameSize;
210781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mFrameCount & 15) {
210881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("HAL output buffer size is %u frames but AudioMixer requires multiples of 16 frames",
210981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mFrameCount);
211081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
211181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2112bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if ((mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) &&
2113bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            (mOutput->stream->set_callback != NULL)) {
2114bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (mOutput->stream->set_callback(mOutput->stream,
2115bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                                      AudioFlinger::PlaybackThread::asyncCallback, this) == 0) {
2116bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            mUseAsyncWrite = true;
21174de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent            mCallbackThread = new AudioFlinger::AsyncCallbackThread(this);
2118bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
2119bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
2120bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
2121d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    mHwSupportsPause = false;
2122d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) {
2123d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent        if (mOutput->stream->pause != NULL) {
2124d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent            if (mOutput->stream->resume != NULL) {
2125d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent                mHwSupportsPause = true;
2126d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent            } else {
2127d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent                ALOGW("direct output implements pause but not resume");
2128d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent            }
2129d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent        } else if (mOutput->stream->resume != NULL) {
2130d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent            ALOGW("direct output implements resume but not pause");
2131d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent        }
2132d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    }
21336fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk    if (!mHwSupportsPause && mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) {
21346fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk        LOG_ALWAYS_FATAL("HW_AV_SYNC requested but HAL does not implement pause and resume");
21356fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk    }
2136d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent
2137fbfc3959f4aac839445edc7075532067fef497c2Andy Hung    if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) {
2138fbfc3959f4aac839445edc7075532067fef497c2Andy Hung        // For best precision, we use float instead of the associated output
2139fbfc3959f4aac839445edc7075532067fef497c2Andy Hung        // device format (typically PCM 16 bit).
2140fbfc3959f4aac839445edc7075532067fef497c2Andy Hung
2141fbfc3959f4aac839445edc7075532067fef497c2Andy Hung        mFormat = AUDIO_FORMAT_PCM_FLOAT;
2142fbfc3959f4aac839445edc7075532067fef497c2Andy Hung        mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat);
2143fbfc3959f4aac839445edc7075532067fef497c2Andy Hung        mBufferSize = mFrameSize * mFrameCount;
2144fbfc3959f4aac839445edc7075532067fef497c2Andy Hung
2145fbfc3959f4aac839445edc7075532067fef497c2Andy Hung        // TODO: We currently use the associated output device channel mask and sample rate.
2146fbfc3959f4aac839445edc7075532067fef497c2Andy Hung        // (1) Perhaps use the ORed channel mask of all downstream MixerThreads
2147fbfc3959f4aac839445edc7075532067fef497c2Andy Hung        // (if a valid mask) to avoid premature downmix.
2148fbfc3959f4aac839445edc7075532067fef497c2Andy Hung        // (2) Perhaps use the maximum sample rate of all downstream MixerThreads
2149fbfc3959f4aac839445edc7075532067fef497c2Andy Hung        // instead of the output device sample rate to avoid loss of high frequency information.
2150fbfc3959f4aac839445edc7075532067fef497c2Andy Hung        // This may need to be updated as MixerThread/OutputTracks are added and not here.
2151fbfc3959f4aac839445edc7075532067fef497c2Andy Hung    }
2152fbfc3959f4aac839445edc7075532067fef497c2Andy Hung
215309a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung    // Calculate size of normal sink buffer relative to the HAL output buffer size
215481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    double multiplier = 1.0;
215581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mType == MIXER && (kUseFastMixer == FastMixer_Static ||
215681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kUseFastMixer == FastMixer_Dynamic)) {
215709a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung        size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000;
215809a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung        size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000;
215981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer
216081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        minNormalFrameCount = (minNormalFrameCount + 15) & ~15;
216181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        maxNormalFrameCount = maxNormalFrameCount & ~15;
216281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (maxNormalFrameCount < minNormalFrameCount) {
216381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            maxNormalFrameCount = minNormalFrameCount;
216481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
216581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        multiplier = (double) minNormalFrameCount / (double) mFrameCount;
216681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (multiplier <= 1.0) {
216781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            multiplier = 1.0;
216881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else if (multiplier <= 2.0) {
216981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (2 * mFrameCount <= maxNormalFrameCount) {
217081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                multiplier = 2.0;
217181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            } else {
217281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                multiplier = (double) maxNormalFrameCount / (double) mFrameCount;
217381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
217481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
217581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // prefer an even multiplier, for compatibility with doubling of fast tracks due to HAL
217609a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung            // SRC (it would be unusual for the normal sink buffer size to not be a multiple of fast
217781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // track, but we sometimes have to do this to satisfy the maximum frame count
217881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // constraint)
217981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // FIXME this rounding up should not be done if no HAL SRC
218081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            uint32_t truncMult = (uint32_t) multiplier;
218181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if ((truncMult & 1)) {
218281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if ((truncMult + 1) * mFrameCount <= maxNormalFrameCount) {
218381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ++truncMult;
218481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
218581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
218681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            multiplier = (double) truncMult;
218781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
218881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
218981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mNormalFrameCount = multiplier * mFrameCount;
219081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // round up to nearest 16 frames to satisfy AudioMixer
2191ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent    if (mType == MIXER || mType == DUPLICATING) {
2192ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent        mNormalFrameCount = (mNormalFrameCount + 15) & ~15;
2193ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent    }
219409a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung    ALOGI("HAL output buffer size %u frames, normal sink buffer size %u frames", mFrameCount,
219581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mNormalFrameCount);
219681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
219708fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung    // Check if we want to throttle the processing to no more than 2x normal rate
219808fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung    mThreadThrottle = property_get_bool("af.thread.throttle", true /* default_value */);
219940eb1a1f8871909c272e72afaf7d5af84fea2412Andy Hung    mThreadThrottleTimeMs = 0;
220040eb1a1f8871909c272e72afaf7d5af84fea2412Andy Hung    mThreadThrottleEndMs = 0;
220108fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung    mHalfBufferMs = mNormalFrameCount * 1000 / (2 * mSampleRate);
220208fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung
2203010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    // mSinkBuffer is the sink buffer.  Size is always multiple-of-16 frames.
2204010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    // Originally this was int16_t[] array, need to remove legacy implications.
2205010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    free(mSinkBuffer);
2206010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    mSinkBuffer = NULL;
22075b10a2037a835e790994b9ebec3c2e55052f1f3bAndy Hung    // For sink buffer size, we use the frame size from the downstream sink to avoid problems
22085b10a2037a835e790994b9ebec3c2e55052f1f3bAndy Hung    // with non PCM formats for compressed music, e.g. AAC, and Offload threads.
22095b10a2037a835e790994b9ebec3c2e55052f1f3bAndy Hung    const size_t sinkBufferSize = mNormalFrameCount * mFrameSize;
2210010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize);
221181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
221269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // We resize the mMixerBuffer according to the requirements of the sink buffer which
221369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // drives the output.
221469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    free(mMixerBuffer);
221569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    mMixerBuffer = NULL;
221669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    if (mMixerBufferEnabled) {
221769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung        mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // also valid: AUDIO_FORMAT_PCM_16_BIT.
221869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung        mMixerBufferSize = mNormalFrameCount * mChannelCount
221969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                * audio_bytes_per_sample(mMixerBufferFormat);
222069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung        (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize);
222169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    }
222298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    free(mEffectBuffer);
222398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    mEffectBuffer = NULL;
222498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    if (mEffectBufferEnabled) {
222598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        mEffectBufferFormat = AUDIO_FORMAT_PCM_16_BIT; // Note: Effects support 16b only
222698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        mEffectBufferSize = mNormalFrameCount * mChannelCount
222798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung                * audio_bytes_per_sample(mEffectBufferFormat);
222898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize);
222998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    }
223069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
223181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // force reconfiguration of effect chains and engines to take new buffer size and audio
223281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // parameters into account
2233deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten    // Note that mLock is not held when readOutputParameters_l() is called from the constructor
223481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // but in this case nothing is done below as no audio sessions have effect yet so it doesn't
223581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // matter.
223681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains
223781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Vector< sp<EffectChain> > effectChains = mEffectChains;
223881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < effectChains.size(); i ++) {
223981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false);
224081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
224181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
224281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
224381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2244377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETITstatus_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames)
224581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
224681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (halFrames == NULL || dspFrames == NULL) {
224781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
224881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
224981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
225081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (initCheck() != NO_ERROR) {
225181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return INVALID_OPERATION;
225281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
225381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t framesWritten = mBytesWritten / mFrameSize;
225481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    *halFrames = framesWritten;
225581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
225681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (isSuspended()) {
225781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // return an estimation of rendered frames when the output is suspended
225881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        size_t latencyFrames = (latency_l() * mSampleRate) / 1000;
225981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        *dspFrames = framesWritten >= latencyFrames ? framesWritten - latencyFrames : 0;
226081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return NO_ERROR;
226181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
2262377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT        status_t status;
2263377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT        uint32_t frames;
2264062e67a26e0553dd142be622821f493df541f0c6Phil Burk        status = mOutput->getRenderPosition(&frames);
2265377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT        *dspFrames = (size_t)frames;
2266377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT        return status;
226781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
226881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
226981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
227081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) const
227181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
227281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
227381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t result = 0;
227481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (getEffectChain_l(sessionId) != 0) {
227581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        result = EFFECT_SESSION;
227681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
227781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
227881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mTracks.size(); ++i) {
227981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<Track> track = mTracks[i];
22805736c35b841de56ce394b4879389f669b61425e6Glenn Kasten        if (sessionId == track->sessionId() && !track->isInvalid()) {
228181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            result |= TRACK_SESSION;
228281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
228381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
228481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
228581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
228681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return result;
228781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
228881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
228981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId)
229081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
229181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that
229281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // it is moved to correct output by audio policy manager when A2DP is connected or disconnected
229381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
229481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
229581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
229681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mTracks.size(); i++) {
229781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<Track> track = mTracks[i];
22985736c35b841de56ce394b4879389f669b61425e6Glenn Kasten        if (sessionId == track->sessionId() && !track->isInvalid()) {
229981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return AudioSystem::getStrategyForStream(track->streamType());
230081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
230181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
230281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
230381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
230481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
230581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2306062e67a26e0553dd142be622821f493df541f0c6Phil BurkAudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const
230781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
230881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
230981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mOutput;
231081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
231181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2312062e67a26e0553dd142be622821f493df541f0c6Phil BurkAudioStreamOut* AudioFlinger::PlaybackThread::clearOutput()
231381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
231481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
231581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioStreamOut *output = mOutput;
231681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mOutput = NULL;
231781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME FastMixer might also have a raw ptr to mOutputSink;
231881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    //       must push a NULL and wait for ack
231981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mOutputSink.clear();
232081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mPipeSink.clear();
232181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mNormalSink.clear();
232281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return output;
232381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
232481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
232581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// this method must always be called either with ThreadBase mLock held or inside the thread loop
232681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_stream_t* AudioFlinger::PlaybackThread::stream() const
232781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
232881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mOutput == NULL) {
232981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return NULL;
233081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
233181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return &mOutput->stream->common;
233281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
233381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
233481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const
233581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
233681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000);
233781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
233881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
233981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event)
234081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
234181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!isValidSyncEvent(event)) {
234281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
234381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
234481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
234581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
234681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
234781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mTracks.size(); ++i) {
234881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<Track> track = mTracks[i];
234981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (event->triggerSession() == track->sessionId()) {
235081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (void) track->setSyncEvent(event);
235181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return NO_ERROR;
235281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
235381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
235481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
235581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NAME_NOT_FOUND;
235681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
235781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
235881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const
235981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
236081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE;
236181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
236281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
236381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::threadLoop_removeTracks(
236481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const Vector< sp<Track> >& tracksToRemove)
236581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
236681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t count = tracksToRemove.size();
236734fca34606b448e6b71c2942f63cb13a0aebd620Glenn Kasten    if (count > 0) {
236881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t i = 0 ; i < count ; i++) {
236981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            const sp<Track>& track = tracksToRemove.itemAt(i);
237083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            if (track->isExternalTrack()) {
2371e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                AudioSystem::stopOutput(mId, track->streamType(),
2372e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                        (audio_session_t)track->sessionId());
2373bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent#ifdef ADD_BATTERY_DATA
2374bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // to track the speaker usage
2375bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop);
2376bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent#endif
2377bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                if (track->isTerminated()) {
2378e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                    AudioSystem::releaseOutput(mId, track->streamType(),
2379e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                               (audio_session_t)track->sessionId());
2380bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
238181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
238281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
238381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
238481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
238581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
238681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::checkSilentMode_l()
238781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
238881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!mMasterMute) {
238981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        char value[PROPERTY_VALUE_MAX];
239081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (property_get("ro.audio.silent", value, "0") > 0) {
239181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            char *endptr;
239281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            unsigned long ul = strtoul(value, &endptr, 0);
239381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (*endptr == '\0' && ul != 0) {
239481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGD("Silence is golden");
239581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // The setprop command will not allow a property to be changed after
239681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // the first time it is set, so we don't have to worry about un-muting.
239781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                setMasterMute_l(true);
239881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
239981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
240081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
240181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
240281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
240381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// shared by MIXER and DIRECT, overridden by DUPLICATING
2404bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentssize_t AudioFlinger::PlaybackThread::threadLoop_write()
240581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
240681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME rewrite to reduce number of system calls
240781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mLastWriteTime = systemTime();
240881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mInWrite = true;
2409bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ssize_t bytesWritten;
2410010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    const size_t offset = mCurrentWriteLength - mBytesRemaining;
241181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
241281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // If an NBAIO sink is present, use it to write the normal mixer's submix
241381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mNormalSink != 0) {
24144c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten
2415010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung        const size_t count = mBytesRemaining / mFrameSize;
2416010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung
24172d590964aa58e137d17a43e095e6443dd0fe2e98Simon Wilson        ATRACE_BEGIN("write");
241881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // update the setpoint when AudioFlinger::mScreenState changes
241981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t screenState = AudioFlinger::mScreenState;
242081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (screenState != mScreenState) {
242181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mScreenState = screenState;
242281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            MonoPipe *pipe = (MonoPipe *)mPipeSink.get();
242381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (pipe != NULL) {
242481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                pipe->setAvgFrames((mScreenState & 1) ?
242581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2);
242681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
242781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
2428010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung        ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count);
24292d590964aa58e137d17a43e095e6443dd0fe2e98Simon Wilson        ATRACE_END();
243081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (framesWritten > 0) {
2431010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung            bytesWritten = framesWritten * mFrameSize;
243281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
243381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            bytesWritten = framesWritten;
243481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
2435efaa7ab620b2ee936efa4fb74dc4c670ed757a46Glenn Kasten        mLatchDValid = false;
2436767094dd98b01baf21de2ad09c27b3c98776cf73Glenn Kasten        status_t status = mNormalSink->getTimestamp(mLatchD.mTimestamp);
2437bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten        if (status == NO_ERROR) {
2438bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten            size_t totalFramesWritten = mNormalSink->framesWritten();
2439bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten            if (totalFramesWritten >= mLatchD.mTimestamp.mPosition) {
2440bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten                mLatchD.mUnpresentedFrames = totalFramesWritten - mLatchD.mTimestamp.mPosition;
24414c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten                // mLatchD.mFramesReleased is set immediately before D is clocked into Q
2442bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten                mLatchDValid = true;
2443bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten            }
2444bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten        }
244581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // otherwise use the HAL / AudioStreamOut directly
244681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
2447bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        // Direct output and offload threads
2448010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung
2449bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (mUseAsyncWrite) {
24503b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request");
24513b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            mWriteAckSequence += 2;
24523b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            mWriteAckSequence |= 1;
2453bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            ALOG_ASSERT(mCallbackThread != 0);
24543b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            mCallbackThread->setWriteBlocked(mWriteAckSequence);
2455bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
2456767094dd98b01baf21de2ad09c27b3c98776cf73Glenn Kasten        // FIXME We should have an implementation of timestamps for direct output threads.
2457767094dd98b01baf21de2ad09c27b3c98776cf73Glenn Kasten        // They are used e.g for multichannel PCM playback over HDMI.
2458062e67a26e0553dd142be622821f493df541f0c6Phil Burk        bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining);
2459bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (mUseAsyncWrite &&
2460bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) {
2461bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            // do not wait for async callback in case of error of full write
24623b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            mWriteAckSequence &= ~1;
2463bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            ALOG_ASSERT(mCallbackThread != 0);
24643b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            mCallbackThread->setWriteBlocked(mWriteAckSequence);
2465bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
246681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
246781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
246881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mNumWrites++;
246981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mInWrite = false;
2470fd4779740ec3e9e865d5514464df26d015354388Eric Laurent    mStandby = false;
2471bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    return bytesWritten;
2472bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
2473bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
2474bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::threadLoop_drain()
2475bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
2476bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if (mOutput->stream->drain) {
2477bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full");
2478bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (mUseAsyncWrite) {
24793b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request");
24803b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            mDrainSequence |= 1;
2481bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            ALOG_ASSERT(mCallbackThread != 0);
24823b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            mCallbackThread->setDraining(mDrainSequence);
2483bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
2484bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        mOutput->stream->drain(mOutput->stream,
2485bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            (mMixerStatus == MIXER_DRAIN_TRACK) ? AUDIO_DRAIN_EARLY_NOTIFY
2486bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                                                : AUDIO_DRAIN_ALL);
2487bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
2488bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
2489bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
2490bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::threadLoop_exit()
2491bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
2492275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent    {
2493275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent        Mutex::Autolock _l(mLock);
2494275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent        for (size_t i = 0; i < mTracks.size(); i++) {
2495275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent            sp<Track> track = mTracks[i];
2496275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent            track->invalidate();
2497275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent        }
2498275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent    }
249981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
250081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
250181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent/*
250281784c37c61b09289654b979567a42bf73cd2b12Eric LaurentThe derived values that are cached:
250325c2dac12114699e90deb1c579cadebce7b91a97Andy Hung - mSinkBufferSize from frame count * frame size
2504ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent - mActiveSleepTimeUs from activeSleepTimeUs()
2505ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent - mIdleSleepTimeUs from idleSleepTimeUs()
2506ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent - mStandbyDelayNs from mActiveSleepTimeUs (DIRECT only)
250781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - maxPeriod from frame count and sample rate (MIXER only)
250881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
250981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentThe parameters that affect these derived values are:
251081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - frame count
251181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - frame size
251281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - sample rate
251381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - device type: A2DP or not
251481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - device latency
251581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - format: PCM or not
251681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - active sleep time
251781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - idle sleep time
251881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent*/
251981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
252081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::cacheParameters_l()
252181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
252225c2dac12114699e90deb1c579cadebce7b91a97Andy Hung    mSinkBufferSize = mNormalFrameCount * mFrameSize;
2523ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    mActiveSleepTimeUs = activeSleepTimeUs();
2524ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    mIdleSleepTimeUs = idleSleepTimeUs();
252581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
252681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
252781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType)
252881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
25297c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten    ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d",
253081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            this,  streamType, mTracks.size());
253181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
253281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
253381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t size = mTracks.size();
253481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < size; i++) {
253581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<Track> t = mTracks[i];
253681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (t->streamType() == streamType) {
25375736c35b841de56ce394b4879389f669b61425e6Glenn Kasten            t->invalidate();
253881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
253981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
254081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
254181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
254281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain)
254381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
254481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int session = chain->sessionId();
2545010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    int16_t* buffer = reinterpret_cast<int16_t*>(mEffectBufferEnabled
2546010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung            ? mEffectBuffer : mSinkBuffer);
254781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool ownsBuffer = false;
254881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
254981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session);
255081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (session > 0) {
255181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Only one effect chain can be present in direct output thread and it uses
25522098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung        // the sink buffer as input
255381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (mType != DIRECT) {
255481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            size_t numSamples = mNormalFrameCount * mChannelCount;
255581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            buffer = new int16_t[numSamples];
255681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            memset(buffer, 0, numSamples * sizeof(int16_t));
255781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session);
255881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ownsBuffer = true;
255981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
256081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
256181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Attach all tracks with same session ID to this chain.
256281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t i = 0; i < mTracks.size(); ++i) {
256381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<Track> track = mTracks[i];
256481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (session == track->sessionId()) {
256581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(),
256681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        buffer);
256781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                track->setMainBuffer(buffer);
256881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                chain->incTrackCnt();
256981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
257081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
257181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
257281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // indicate all active tracks in the chain
257381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) {
257481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<Track> track = mActiveTracks[i].promote();
257581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (track == 0) {
257681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                continue;
257781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
257881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (session == track->sessionId()) {
257981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session);
258081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                chain->incActiveTrackCnt();
258181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
258281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
258381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
2584aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    chain->setThread(this);
258581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    chain->setInBuffer(buffer, ownsBuffer);
2586010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    chain->setOutBuffer(reinterpret_cast<int16_t*>(mEffectBufferEnabled
2587010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung            ? mEffectBuffer : mSinkBuffer));
258881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect
258981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // chains list in order to be processed last as it contains output stage effects
259081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before
259181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // session AUDIO_SESSION_OUTPUT_STAGE to be processed
259281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // after track specific effects and before output stage
259381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and
259481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX
259581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Effect chain for other sessions are inserted at beginning of effect
259681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // chains list to be processed before output mix effects. Relative order between other
259781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // sessions is not important
259881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t size = mEffectChains.size();
259981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t i = 0;
260081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (i = 0; i < size; i++) {
260181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (mEffectChains[i]->sessionId() < session) {
260281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
260381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
260481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
260581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mEffectChains.insertAt(chain, i);
260681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    checkSuspendOnAddEffectChain_l(chain);
260781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
260881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
260981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
261081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
261181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsize_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain)
261281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
261381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int session = chain->sessionId();
261481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
261581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session);
261681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
261781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mEffectChains.size(); i++) {
261881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (chain == mEffectChains[i]) {
261981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mEffectChains.removeAt(i);
262081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // detach all active tracks from the chain
262181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) {
262281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<Track> track = mActiveTracks[i].promote();
262381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (track == 0) {
262481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    continue;
262581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
262681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (session == track->sessionId()) {
262781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d",
262881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            chain.get(), session);
262981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    chain->decActiveTrackCnt();
263081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
263181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
263281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
263381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // detach all tracks with same session ID from this chain
263481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            for (size_t i = 0; i < mTracks.size(); ++i) {
263581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<Track> track = mTracks[i];
263681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (session == track->sessionId()) {
2637010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                    track->setMainBuffer(reinterpret_cast<int16_t*>(mSinkBuffer));
263881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    chain->decTrackCnt();
263981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
264081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
264181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
264281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
264381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
264481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mEffectChains.size();
264581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
264681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
264781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::attachAuxEffect(
264881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId)
264981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
265081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
265181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return attachAuxEffect_l(track, EffectId);
265281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
265381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
265481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::attachAuxEffect_l(
265581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId)
265681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
265781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t status = NO_ERROR;
265881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
265981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (EffectId == 0) {
266081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        track->setAuxBuffer(0, NULL);
266181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
266281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX
266381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId);
266481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (effect != 0) {
266581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
266681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer());
266781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            } else {
266881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                status = INVALID_OPERATION;
266981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
267081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
267181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status = BAD_VALUE;
267281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
267381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
267481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return status;
267581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
267681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
267781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId)
267881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
267981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mTracks.size(); ++i) {
268081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<Track> track = mTracks[i];
268181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (track->auxEffectId() == effectId) {
268281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            attachAuxEffect_l(track, 0);
268381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
268481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
268581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
268681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
268781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::PlaybackThread::threadLoop()
268881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
268981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Vector< sp<Track> > tracksToRemove;
269081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2691ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    mStandbyTimeNs = systemTime();
269281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
269381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // MIXER
269481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    nsecs_t lastWarning = 0;
269581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
269681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // DUPLICATING
269781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME could this be made local to while loop?
269881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    writeFrames = 0;
269981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2700462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    int lastGeneration = 0;
2701462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen
270281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    cacheParameters_l();
2703ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    mSleepTimeUs = mIdleSleepTimeUs;
270481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
270581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mType == MIXER) {
270681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sleepTimeShift = 0;
270781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
270881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
270981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    CpuStats cpuStats;
271081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid()));
271181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
271281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    acquireWakeLock();
271381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
27149e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    // mNBLogWriter->log can only be called while thread mutex mLock is held.
27159e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    // So if you need to log when mutex is unlocked, set logString to a non-NULL string,
27169e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    // and then that string will be logged at the next convenient opportunity.
27179e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    const char *logString = NULL;
27189e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
2719664539d25180ab8f77e0521533ea2821cf28985fEric Laurent    checkSilentMode_l();
2720664539d25180ab8f77e0521533ea2821cf28985fEric Laurent
272181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    while (!exitPending())
272281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
272381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        cpuStats.sample(myName);
272481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
272581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Vector< sp<EffectChain> > effectChains;
272681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
272781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        { // scope for mLock
272881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
272981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            Mutex::Autolock _l(mLock);
273081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2731021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            processConfigEvents_l();
27321035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
27339e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            if (logString != NULL) {
27349e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                mNBLogWriter->logTimestamp();
27359e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                mNBLogWriter->log(logString);
27369e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                logString = NULL;
27379e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            }
27389e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
27394c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten            // Gather the framesReleased counters for all active tracks,
27404c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten            // and latch them atomically with the timestamp.
27414c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten            // FIXME We're using raw pointers as indices. A unique track ID would be a better index.
27424c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten            mLatchD.mFramesReleased.clear();
27434c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten            size_t size = mActiveTracks.size();
27444c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten            for (size_t i = 0; i < size; i++) {
27454c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten                sp<Track> t = mActiveTracks[i].promote();
27464c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten                if (t != 0) {
27474c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten                    mLatchD.mFramesReleased.add(t.get(),
27484c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten                            t->mAudioTrackServerProxy->framesReleased());
27494c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten                }
27504c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten            }
2751bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten            if (mLatchDValid) {
2752bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten                mLatchQ = mLatchD;
2753bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten                mLatchDValid = false;
2754bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten                mLatchQValid = true;
2755bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten            }
2756bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten
275781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            saveOutputTracks();
2758bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (mSignalPending) {
2759bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // A signal was raised while we were unlocked
2760bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                mSignalPending = false;
2761bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            } else if (waitingAsyncCallback_l()) {
2762bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                if (exitPending()) {
2763bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    break;
2764bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
2765078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen                bool released = false;
2766078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen                // The following works around a bug in the offload driver. Ideally we would release
2767078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen                // the wake lock every time, but that causes the last offload buffer(s) to be
2768078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen                // dropped while the device is on battery, so we need to hold a wake lock during
2769078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen                // the drain phase.
2770078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen                if (mBytesRemaining && !(mDrainSequence & 1)) {
2771078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen                    releaseWakeLock_l();
2772078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen                    released = true;
2773078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen                }
2774462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                mWakeLockUids.clear();
2775462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                mActiveTracksGeneration++;
2776bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                ALOGV("wait async completion");
2777bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                mWaitWorkCV.wait(mLock);
2778bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                ALOGV("async completion/wake");
2779078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen                if (released) {
2780078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen                    acquireWakeLock_l();
2781078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen                }
2782ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent                mStandbyTimeNs = systemTime() + mStandbyDelayNs;
2783ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent                mSleepTimeUs = 0;
2784ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent
2785ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent                continue;
2786ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent            }
2787ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent            if ((!mActiveTracks.size() && systemTime() > mStandbyTimeNs) ||
2788bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                                   isSuspended()) {
2789bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // put audio hardware into standby after short delay
2790bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                if (shouldStandby_l()) {
279181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
279281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    threadLoop_standby();
279381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
279481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mStandby = true;
279581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
279681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
279781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (!mActiveTracks.size() && mConfigEvents.isEmpty()) {
279881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // we're about to wait, flush the binder command buffer
279981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    IPCThreadState::self()->flushCommands();
280081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
280181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    clearOutputTracks();
280281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
280381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    if (exitPending()) {
280481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        break;
280581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    }
280681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
280781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    releaseWakeLock_l();
2808462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                    mWakeLockUids.clear();
2809462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                    mActiveTracksGeneration++;
281081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // wait until we have something to do...
281181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ALOGV("%s going to sleep", myName.string());
281281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mWaitWorkCV.wait(mLock);
281381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ALOGV("%s waking up", myName.string());
281481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    acquireWakeLock_l();
281581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
281681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mMixerStatus = MIXER_IDLE;
281781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mMixerStatusIgnoringFastTracks = MIXER_IDLE;
281881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mBytesWritten = 0;
2819bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    mBytesRemaining = 0;
282081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    checkSilentMode_l();
282181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2822ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent                    mStandbyTimeNs = systemTime() + mStandbyDelayNs;
2823ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent                    mSleepTimeUs = mIdleSleepTimeUs;
282481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    if (mType == MIXER) {
282581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        sleepTimeShift = 0;
282681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    }
282781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
282881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    continue;
282981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
283081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
283181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // mMixerStatusIgnoringFastTracks is also updated internally
283281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mMixerStatus = prepareTracks_l(&tracksToRemove);
283381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2834462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen            // compare with previously applied list
2835462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen            if (lastGeneration != mActiveTracksGeneration) {
2836462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                // update wakelock
2837462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                updateWakeLockUids_l(mWakeLockUids);
2838462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                lastGeneration = mActiveTracksGeneration;
2839462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen            }
2840462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen
284181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // prevent any changes in effect chain list and in each effect chain
284281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // during mixing and effect process as the audio buffers could be deleted
284381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // or modified if an effect is created or deleted
284481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lockEffectChains_l(effectChains);
2845462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        } // mLock scope ends
284681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2847bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (mBytesRemaining == 0) {
2848bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            mCurrentWriteLength = 0;
2849bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (mMixerStatus == MIXER_TRACKS_READY) {
2850bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // threadLoop_mix() sets mCurrentWriteLength
2851bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                threadLoop_mix();
2852bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            } else if ((mMixerStatus != MIXER_DRAIN_TRACK)
2853bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        && (mMixerStatus != MIXER_DRAIN_ALL)) {
2854ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent                // threadLoop_sleepTime sets mSleepTimeUs to 0 if data
2855bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // must be written to HAL
2856bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                threadLoop_sleepTime();
2857ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent                if (mSleepTimeUs == 0) {
285825c2dac12114699e90deb1c579cadebce7b91a97Andy Hung                    mCurrentWriteLength = mSinkBufferSize;
2859bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
2860bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
286198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            // Either threadLoop_mix() or threadLoop_sleepTime() should have set
2862ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent            // mMixerBuffer with data if mMixerBufferValid is true and mSleepTimeUs == 0.
286398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid)
286498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            // or mSinkBuffer (if there are no effects).
286598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            //
286698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            // This is done pre-effects computation; if effects change to
286798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            // support higher precision, this needs to move.
286898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            //
286998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            // mMixerBufferValid is only set true by MixerThread::prepareTracks_l().
2870ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent            // TODO use mSleepTimeUs == 0 as an additional condition.
287198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            if (mMixerBufferValid) {
287298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung                void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer;
287398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung                audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat;
287498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
287598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung                memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat,
287698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung                        mNormalFrameCount * mChannelCount);
287798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            }
287898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
2879bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            mBytesRemaining = mCurrentWriteLength;
2880bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (isSuspended()) {
2881ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent                mSleepTimeUs = suspendSleepTimeUs();
2882bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // simulate write to HAL when suspended
288325c2dac12114699e90deb1c579cadebce7b91a97Andy Hung                mBytesWritten += mSinkBufferSize;
2884bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                mBytesRemaining = 0;
2885bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
288681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2887bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            // only process effects if we're going to write
2888ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent            if (mSleepTimeUs == 0 && mType != OFFLOAD) {
2889bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                for (size_t i = 0; i < effectChains.size(); i ++) {
2890bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    effectChains[i]->process_l();
2891bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
289281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
289381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
289459fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent        // Process effect chains for offloaded thread even if no audio
289559fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent        // was read from audio track: process only updates effect state
289659fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent        // and thus does have to be synchronized with audio writes but may have
289759fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent        // to be called while waiting for async write callback
289859fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent        if (mType == OFFLOAD) {
289959fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent            for (size_t i = 0; i < effectChains.size(); i ++) {
290059fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent                effectChains[i]->process_l();
290159fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent            }
290259fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent        }
290381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
290498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        // Only if the Effects buffer is enabled and there is data in the
290598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        // Effects buffer (buffer valid), we need to
290698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        // copy into the sink buffer.
2907ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent        // TODO use mSleepTimeUs == 0 as an additional condition.
290898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        if (mEffectBufferValid) {
290998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            //ALOGV("writing effect buffer to sink buffer format %#x", mFormat);
291098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat,
291198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung                    mNormalFrameCount * mChannelCount);
291298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        }
291398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
291481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // enable changes in effect chain
291581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        unlockEffectChains(effectChains);
291681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2917bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (!waitingAsyncCallback()) {
2918ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent            // mSleepTimeUs == 0 means we must write to audio hardware
2919ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent            if (mSleepTimeUs == 0) {
292008fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                ssize_t ret = 0;
2921bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                if (mBytesRemaining) {
292208fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                    ret = threadLoop_write();
2923bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    if (ret < 0) {
2924bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        mBytesRemaining = 0;
2925bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    } else {
2926bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        mBytesWritten += ret;
2927bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        mBytesRemaining -= ret;
2928bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    }
2929bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                } else if ((mMixerStatus == MIXER_DRAIN_TRACK) ||
2930bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        (mMixerStatus == MIXER_DRAIN_ALL)) {
2931bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    threadLoop_drain();
2932bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
293308fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                if (mType == MIXER && !mStandby) {
29344944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                    // write blocked detection
29354944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                    nsecs_t now = systemTime();
29364944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                    nsecs_t delta = now - mLastWriteTime;
293708fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                    if (delta > maxPeriod) {
29384944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                        mNumDelayedWrites++;
29394944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                        if ((now - lastWarning) > kWarningThrottleNs) {
29404944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                            ATRACE_NAME("underrun");
29414944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                            ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p",
29424944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                                    ns2ms(delta), mNumDelayedWrites, this);
29434944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                            lastWarning = now;
29444944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                        }
2945bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    }
294608fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung
294708fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                    if (mThreadThrottle
294808fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                            && mMixerStatus == MIXER_TRACKS_READY // we are mixing (active tracks)
294908fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                            && ret > 0) {                         // we wrote something
295008fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                        // Limit MixerThread data processing to no more than twice the
295108fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                        // expected processing rate.
295208fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                        //
295308fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                        // This helps prevent underruns with NuPlayer and other applications
295408fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                        // which may set up buffers that are close to the minimum size, or use
295508fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                        // deep buffers, and rely on a double-buffering sleep strategy to fill.
295608fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                        //
295708fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                        // The throttle smooths out sudden large data drains from the device,
295808fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                        // e.g. when it comes out of standby, which often causes problems with
295908fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                        // (1) mixer threads without a fast mixer (which has its own warm-up)
296008fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                        // (2) minimum buffer sized tracks (even if the track is full,
296108fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                        //     the app won't fill fast enough to handle the sudden draw).
296208fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung
296308fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                        const int32_t deltaMs = delta / 1000000;
296408fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                        const int32_t throttleMs = mHalfBufferMs - deltaMs;
296508fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                        if ((signed)mHalfBufferMs >= throttleMs && throttleMs > 0) {
296608fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                            usleep(throttleMs * 1000);
296740eb1a1f8871909c272e72afaf7d5af84fea2412Andy Hung                            // notify of throttle start on verbose log
296840eb1a1f8871909c272e72afaf7d5af84fea2412Andy Hung                            ALOGV_IF(mThreadThrottleEndMs == mThreadThrottleTimeMs,
296940eb1a1f8871909c272e72afaf7d5af84fea2412Andy Hung                                    "mixer(%p) throttle begin:"
297040eb1a1f8871909c272e72afaf7d5af84fea2412Andy Hung                                    " ret(%zd) deltaMs(%d) requires sleep %d ms",
297108fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                                    this, ret, deltaMs, throttleMs);
297240eb1a1f8871909c272e72afaf7d5af84fea2412Andy Hung                            mThreadThrottleTimeMs += throttleMs;
297340eb1a1f8871909c272e72afaf7d5af84fea2412Andy Hung                        } else {
297440eb1a1f8871909c272e72afaf7d5af84fea2412Andy Hung                            uint32_t diff = mThreadThrottleTimeMs - mThreadThrottleEndMs;
297540eb1a1f8871909c272e72afaf7d5af84fea2412Andy Hung                            if (diff > 0) {
297640eb1a1f8871909c272e72afaf7d5af84fea2412Andy Hung                                // notify of throttle end on debug log
297740eb1a1f8871909c272e72afaf7d5af84fea2412Andy Hung                                ALOGD("mixer(%p) throttle end: throttle time(%u)", this, diff);
297840eb1a1f8871909c272e72afaf7d5af84fea2412Andy Hung                                mThreadThrottleEndMs = mThreadThrottleTimeMs;
297940eb1a1f8871909c272e72afaf7d5af84fea2412Andy Hung                            }
298008fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                        }
298108fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                    }
298281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
298381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2984bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            } else {
2985e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten                ATRACE_BEGIN("sleep");
2986ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent                usleep(mSleepTimeUs);
2987e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten                ATRACE_END();
2988bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
298981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
299081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
299181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Finally let go of removed track(s), without the lock held
299281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // since we can't guarantee the destructors won't acquire that
299381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // same lock.  This will also mutate and push a new fast mixer state.
299481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        threadLoop_removeTracks(tracksToRemove);
299581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        tracksToRemove.clear();
299681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
299781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // FIXME I don't understand the need for this here;
299881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        //       it was in the original code but maybe the
299981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        //       assignment in saveOutputTracks() makes this unnecessary?
300081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        clearOutputTracks();
300181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
300281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Effect chains will be actually deleted here if they were removed from
300381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // mEffectChains list during mixing or effects processing
300481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effectChains.clear();
300581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
300681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // FIXME Note that the above .clear() is no longer necessary since effectChains
300781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // is now local to this block, but will keep it for now (at least until merge done).
300881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
300981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3010bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    threadLoop_exit();
3011bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
3012cf817a2330936947df94c11859f48771f5596a59Eric Laurent    if (!mStandby) {
3013cf817a2330936947df94c11859f48771f5596a59Eric Laurent        threadLoop_standby();
3014cf817a2330936947df94c11859f48771f5596a59Eric Laurent        mStandby = true;
301581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
301681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
301781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    releaseWakeLock();
3018462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    mWakeLockUids.clear();
3019462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    mActiveTracksGeneration++;
302081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
302181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("Thread %p type %d exiting", this, mType);
302281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return false;
302381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
302481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3025bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// removeTracks_l() must be called with ThreadBase::mLock held
3026bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove)
3027bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
3028bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t count = tracksToRemove.size();
302934fca34606b448e6b71c2942f63cb13a0aebd620Glenn Kasten    if (count > 0) {
3030bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        for (size_t i=0 ; i<count ; i++) {
3031bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            const sp<Track>& track = tracksToRemove.itemAt(i);
3032bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            mActiveTracks.remove(track);
3033462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen            mWakeLockUids.remove(track->uid());
3034462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen            mActiveTracksGeneration++;
3035bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            ALOGV("removeTracks_l removing track on session %d", track->sessionId());
3036bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            sp<EffectChain> chain = getEffectChain_l(track->sessionId());
3037bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (chain != 0) {
3038bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                ALOGV("stopping track on chain %p for session Id: %d", chain.get(),
3039bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        track->sessionId());
3040bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                chain->decActiveTrackCnt();
3041bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
3042bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (track->isTerminated()) {
3043bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                removeTrack_l(track);
3044bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
3045bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
3046bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
3047bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
3048bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
304981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3050accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurentstatus_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp)
3051accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent{
3052accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent    if (mNormalSink != 0) {
3053accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent        return mNormalSink->getTimestamp(timestamp);
3054accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent    }
30559a1c8892f6835325db6931529dc74bf98cf0aee8Andy Hung    if ((mType == OFFLOAD || mType == DIRECT)
30569a1c8892f6835325db6931529dc74bf98cf0aee8Andy Hung            && mOutput != NULL && mOutput->stream->get_presentation_position) {
3057accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent        uint64_t position64;
3058062e67a26e0553dd142be622821f493df541f0c6Phil Burk        int ret = mOutput->getPresentationPosition(&position64, &timestamp.mTime);
3059accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent        if (ret == 0) {
3060accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent            timestamp.mPosition = (uint32_t)position64;
3061accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent            return NO_ERROR;
3062accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent        }
3063accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent    }
3064accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent    return INVALID_OPERATION;
3065accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent}
30661c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
3067054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurentstatus_t AudioFlinger::MixerThread::createAudioPatch_l(const struct audio_patch *patch,
3068054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent                                                          audio_patch_handle_t *handle)
3069054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent{
3070054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    // if !&IDLE, holds the FastMixer state to restore after new parameters processed
3071054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE;
3072054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    if (mFastMixer != 0) {
3073054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        FastMixerStateQueue *sq = mFastMixer->sq();
3074054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        FastMixerState *state = sq->begin();
3075054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        if (!(state->mCommand & FastMixerState::IDLE)) {
3076054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent            previousCommand = state->mCommand;
3077054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent            state->mCommand = FastMixerState::HOT_IDLE;
3078054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent            sq->end();
3079054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent            sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED);
3080054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        } else {
3081054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent            sq->end(false /*didModify*/);
3082054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        }
3083054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    }
3084054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    status_t status = PlaybackThread::createAudioPatch_l(patch, handle);
3085054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
3086054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    if (!(previousCommand & FastMixerState::IDLE)) {
3087054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        ALOG_ASSERT(mFastMixer != 0);
3088054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        FastMixerStateQueue *sq = mFastMixer->sq();
3089054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        FastMixerState *state = sq->begin();
3090054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE);
3091054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        state->mCommand = previousCommand;
3092054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        sq->end();
3093054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
3094054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    }
3095054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
3096054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    return status;
3097054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent}
3098054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
30991c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch,
31001c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                                          audio_patch_handle_t *handle)
31011c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{
31021c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    status_t status = NO_ERROR;
3103054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
3104054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    // store new device and send to effects
3105054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    audio_devices_t type = AUDIO_DEVICE_NONE;
3106054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    for (unsigned int i = 0; i < patch->num_sinks; i++) {
3107054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        type |= patch->sinks[i].ext.device.type;
3108054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    }
3109054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
3110054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent#ifdef ADD_BATTERY_DATA
3111054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    // when changing the audio output device, call addBatteryData to notify
3112054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    // the change
3113054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    if (mOutDevice != type) {
3114054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        uint32_t params = 0;
3115054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        // check whether speaker is on
3116054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        if (type & AUDIO_DEVICE_OUT_SPEAKER) {
3117054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent            params |= IMediaPlayerService::kBatteryDataSpeakerOn;
31181c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
3119054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
3120054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        audio_devices_t deviceWithoutSpeaker
3121054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent            = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER;
3122054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        // check if any other device (except speaker) is on
3123054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        if (type & deviceWithoutSpeaker) {
3124054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent            params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn;
3125054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        }
3126054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
3127054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        if (params != 0) {
3128054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent            addBatteryData(params);
31291c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
3130054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    }
3131054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent#endif
31321c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
3133054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    for (size_t i = 0; i < mEffectChains.size(); i++) {
3134054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        mEffectChains[i]->setDevice_l(type);
3135054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    }
3136e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent    bool configChanged = mOutDevice != type;
3137054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    mOutDevice = type;
3138296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    mPatch = *patch;
3139054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
3140054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) {
31411c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice();
31421c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        status = hwDevice->create_audio_patch(hwDevice,
31431c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                               patch->num_sources,
31441c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                               patch->sources,
31451c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                               patch->num_sinks,
31461c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                               patch->sinks,
31471c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                               handle);
31481c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    } else {
3149054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        char *address;
3150054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        if (strcmp(patch->sinks[0].ext.device.address, "") != 0) {
3151054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent            //FIXME: we only support address on first sink with HAL version < 3.0
3152054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent            address = audio_device_address_to_parameter(
3153054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent                                                        patch->sinks[0].ext.device.type,
3154054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent                                                        patch->sinks[0].ext.device.address);
3155054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        } else {
3156054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent            address = (char *)calloc(1, 1);
3157054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        }
3158054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        AudioParameter param = AudioParameter(String8(address));
3159054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        free(address);
3160054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), (int)type);
3161054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
3162054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent                param.toString().string());
3163054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        *handle = AUDIO_PATCH_HANDLE_NONE;
3164054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    }
3165e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent    if (configChanged) {
3166e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent        sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED);
3167e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent    }
3168054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    return status;
3169054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent}
3170054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
3171054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurentstatus_t AudioFlinger::MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle)
3172054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent{
3173054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    // if !&IDLE, holds the FastMixer state to restore after new parameters processed
3174054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE;
3175054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    if (mFastMixer != 0) {
3176054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        FastMixerStateQueue *sq = mFastMixer->sq();
3177054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        FastMixerState *state = sq->begin();
3178054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        if (!(state->mCommand & FastMixerState::IDLE)) {
3179054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent            previousCommand = state->mCommand;
3180054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent            state->mCommand = FastMixerState::HOT_IDLE;
3181054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent            sq->end();
3182054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent            sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED);
3183054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        } else {
3184054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent            sq->end(false /*didModify*/);
3185054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        }
31861c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    }
3187054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
3188054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    status_t status = PlaybackThread::releaseAudioPatch_l(handle);
3189054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
3190054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    if (!(previousCommand & FastMixerState::IDLE)) {
3191054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        ALOG_ASSERT(mFastMixer != 0);
3192054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        FastMixerStateQueue *sq = mFastMixer->sq();
3193054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        FastMixerState *state = sq->begin();
3194054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE);
3195054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        state->mCommand = previousCommand;
3196054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        sq->end();
3197054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
3198054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    }
3199054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
32001c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    return status;
32011c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent}
32021c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
32031c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle)
32041c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{
32051c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    status_t status = NO_ERROR;
3206054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
3207054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    mOutDevice = AUDIO_DEVICE_NONE;
3208054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
32091c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) {
32101c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice();
32111c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        status = hwDevice->release_audio_patch(hwDevice, handle);
32121c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    } else {
3213054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        AudioParameter param;
3214054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0);
3215054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
3216054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent                param.toString().string());
32171c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    }
32181c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    return status;
32191c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent}
32201c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
322183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track)
322283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
322383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    Mutex::Autolock _l(mLock);
322483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    mTracks.add(track);
322583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
322683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
322783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track)
322883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
322983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    Mutex::Autolock _l(mLock);
323083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    destroyTrack_l(track);
323183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
323283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
323383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::PlaybackThread::getAudioPortConfig(struct audio_port_config *config)
323483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
323583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    ThreadBase::getAudioPortConfig(config);
323683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    config->role = AUDIO_PORT_ROLE_SOURCE;
323783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    config->ext.mix.hw_module = mOutput->audioHwDev->handle();
323883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT;
323983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
324083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
324181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
324281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
324381784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
324472e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        audio_io_handle_t id, audio_devices_t device, bool systemReady, type_t type)
324572e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    :   PlaybackThread(audioFlinger, output, id, device, type, systemReady),
324681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // mAudioMixer below
324781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // mFastMixer below
324881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mFastMixerFutex(0)
324981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // mOutputSink below
325081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // mPipeSink below
325181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // mNormalSink below
325281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
325381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type);
3254f6ed423af92a56ef54bba23eba883b1f21448b54Glenn Kasten    ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%d, mFrameSize=%u, "
325581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            "mFrameCount=%d, mNormalFrameCount=%d",
325681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount,
325781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mNormalFrameCount);
325881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate);
325981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3260fbfc3959f4aac839445edc7075532067fef497c2Andy Hung    if (type == DUPLICATING) {
3261fbfc3959f4aac839445edc7075532067fef497c2Andy Hung        // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks
3262fbfc3959f4aac839445edc7075532067fef497c2Andy Hung        // (downstream MixerThreads) in DuplicatingThread::threadLoop_write().
3263fbfc3959f4aac839445edc7075532067fef497c2Andy Hung        // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink.
3264fbfc3959f4aac839445edc7075532067fef497c2Andy Hung        return;
3265fbfc3959f4aac839445edc7075532067fef497c2Andy Hung    }
326681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // create an NBAIO sink for the HAL output stream, and negotiate
326781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mOutputSink = new AudioStreamOutSink(output->stream);
326881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t numCounterOffers = 0;
3269f69f9869514730aebe5724c461768507084dfff7Glenn Kasten    const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)};
327081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ssize_t index = mOutputSink->negotiate(offers, 1, NULL, numCounterOffers);
327181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOG_ASSERT(index == 0);
327281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
327381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // initialize fast mixer depending on configuration
327481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool initFastMixer;
327581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    switch (kUseFastMixer) {
327681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    case FastMixer_Never:
327781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        initFastMixer = false;
327881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        break;
327981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    case FastMixer_Always:
328081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        initFastMixer = true;
328181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        break;
328281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    case FastMixer_Static:
328381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    case FastMixer_Dynamic:
328481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        initFastMixer = mFrameCount < mNormalFrameCount;
328581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        break;
328681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
328781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (initFastMixer) {
32881258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung        audio_format_t fastMixerFormat;
32891258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung        if (mMixerBufferEnabled && mEffectBufferEnabled) {
32901258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung            fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT;
32911258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung        } else {
32921258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung            fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT;
32931258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung        }
32941258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung        if (mFormat != fastMixerFormat) {
32951258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung            // change our Sink format to accept our intermediate precision
32961258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung            mFormat = fastMixerFormat;
32971258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung            free(mSinkBuffer);
32981258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung            mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat);
32991258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung            const size_t sinkBufferSize = mNormalFrameCount * mFrameSize;
33001258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung            (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize);
33011258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung        }
330281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
330381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // create a MonoPipe to connect our submix to FastMixer
330481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        NBAIO_Format format = mOutputSink->format();
3305ba0b34c18da93681c0813ecdab19b0e215b6d261Glenn Kasten        NBAIO_Format origformat = format;
33061258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung        // adjust format to match that of the Fast Mixer
330797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten        ALOGV("format changed from %d to %d", format.mFormat, fastMixerFormat);
33081258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung        format.mFormat = fastMixerFormat;
33091258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung        format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount;
33101258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung
331181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // This pipe depth compensates for scheduling latency of the normal mixer thread.
331281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // When it wakes up after a maximum latency, it runs a few cycles quickly before
331381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // finally blocking.  Note the pipe implementation rounds up the request to a power of 2.
331481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/);
331581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const NBAIO_Format offers[1] = {format};
331681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        size_t numCounterOffers = 0;
331781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ssize_t index = monoPipe->negotiate(offers, 1, NULL, numCounterOffers);
331881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOG_ASSERT(index == 0);
331981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        monoPipe->setAvgFrames((mScreenState & 1) ?
332081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2);
332181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mPipeSink = monoPipe;
332281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
332346909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
3324da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        if (mTeeSinkOutputEnabled) {
3325da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            // create a Pipe to archive a copy of FastMixer's output for dumpsys
3326ba0b34c18da93681c0813ecdab19b0e215b6d261Glenn Kasten            Pipe *teeSink = new Pipe(mTeeSinkOutputFrames, origformat);
3327ba0b34c18da93681c0813ecdab19b0e215b6d261Glenn Kasten            const NBAIO_Format offers2[1] = {origformat};
3328da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            numCounterOffers = 0;
3329ba0b34c18da93681c0813ecdab19b0e215b6d261Glenn Kasten            index = teeSink->negotiate(offers2, 1, NULL, numCounterOffers);
3330da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            ALOG_ASSERT(index == 0);
3331da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            mTeeSink = teeSink;
3332da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            PipeReader *teeSource = new PipeReader(*teeSink);
3333da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            numCounterOffers = 0;
3334ba0b34c18da93681c0813ecdab19b0e215b6d261Glenn Kasten            index = teeSource->negotiate(offers2, 1, NULL, numCounterOffers);
3335da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            ALOG_ASSERT(index == 0);
3336da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            mTeeSource = teeSource;
3337da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        }
333846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
333981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
334081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // create fast mixer and configure it initially with just one fast track for our submix
334181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mFastMixer = new FastMixer();
334281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FastMixerStateQueue *sq = mFastMixer->sq();
334381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef STATE_QUEUE_DUMP
334481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sq->setObserverDump(&mStateQueueObserverDump);
334581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sq->setMutatorDump(&mStateQueueMutatorDump);
334681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
334781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FastMixerState *state = sq->begin();
334881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FastTrack *fastTrack = &state->mFastTracks[0];
334981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // wrap the source side of the MonoPipe to make it an AudioBufferProvider
335081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe));
335181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        fastTrack->mVolumeProvider = NULL;
3352e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung        fastTrack->mChannelMask = mChannelMask; // mPipeSink channel mask for audio to FastMixer
3353e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung        fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer
335481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        fastTrack->mGeneration++;
335581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mFastTracksGen++;
335681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mTrackMask = 1;
335781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // fast mixer will use the HAL output sink
335881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mOutputSink = mOutputSink.get();
335981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mOutputSinkGen++;
336081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mFrameCount = mFrameCount;
336181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mCommand = FastMixerState::COLD_IDLE;
336281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // already done in constructor initialization list
336381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        //mFastMixerFutex = 0;
336481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mColdFutexAddr = &mFastMixerFutex;
336581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mColdGen++;
336681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mDumpState = &mFastMixerDumpState;
336746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
336881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mTeeSink = mTeeSink.get();
336946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
33709e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer");
33719e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        state->mNBLogWriter = mFastMixerNBLogWriter.get();
337281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sq->end();
337381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
337481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
337581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // start the fast mixer
337681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO);
337781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        pid_t tid = mFastMixer->getTid();
337872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer);
337981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
338081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG
338181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // create and start the watchdog
338281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mAudioWatchdog = new AudioWatchdog();
338381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mAudioWatchdog->setDump(&mAudioWatchdogDump);
338481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO);
338581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        tid = mAudioWatchdog->getTid();
338672e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer);
338781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
338881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
338981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
339081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
339181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    switch (kUseFastMixer) {
339281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    case FastMixer_Never:
339381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    case FastMixer_Dynamic:
339481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mNormalSink = mOutputSink;
339581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        break;
339681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    case FastMixer_Always:
339781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mNormalSink = mPipeSink;
339881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        break;
339981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    case FastMixer_Static:
340081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mNormalSink = initFastMixer ? mPipeSink : mOutputSink;
340181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        break;
340281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
340381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
340481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
340581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::MixerThread::~MixerThread()
340681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
34074d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten    if (mFastMixer != 0) {
340881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FastMixerStateQueue *sq = mFastMixer->sq();
340981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FastMixerState *state = sq->begin();
341081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (state->mCommand == FastMixerState::COLD_IDLE) {
341181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            int32_t old = android_atomic_inc(&mFastMixerFutex);
341281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (old == -1) {
3413ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes                (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1);
341481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
341581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
341681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mCommand = FastMixerState::EXIT;
341781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sq->end();
341881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
341981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mFastMixer->join();
342081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Though the fast mixer thread has exited, it's state queue is still valid.
342181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // We'll use that extract the final state which contains one remaining fast track
342281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // corresponding to our sub-mix.
342381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state = sq->begin();
342481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOG_ASSERT(state->mTrackMask == 1);
342581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FastTrack *fastTrack = &state->mFastTracks[0];
342681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOG_ASSERT(fastTrack->mBufferProvider != NULL);
342781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        delete fastTrack->mBufferProvider;
342881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sq->end(false /*didModify*/);
34294d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten        mFastMixer.clear();
343081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG
343181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (mAudioWatchdog != 0) {
343281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mAudioWatchdog->requestExit();
343381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mAudioWatchdog->requestExitAndWait();
343481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mAudioWatchdog.clear();
343581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
343681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
343781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
34389e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter);
343981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    delete mAudioMixer;
344081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
344181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
344281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
344381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const
344481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
34454d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten    if (mFastMixer != 0) {
344681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        MonoPipe *pipe = (MonoPipe *)mPipeSink.get();
344781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        latency += (pipe->getAvgFrames() * 1000) / mSampleRate;
344881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
344981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return latency;
345081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
345181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
345281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
345381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove)
345481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
345581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread::threadLoop_removeTracks(tracksToRemove);
345681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
345781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3458bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentssize_t AudioFlinger::MixerThread::threadLoop_write()
345981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
346081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME we should only do one push per cycle; confirm this is true
346181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Start the fast mixer if it's not already running
34624d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten    if (mFastMixer != 0) {
346381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FastMixerStateQueue *sq = mFastMixer->sq();
346481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FastMixerState *state = sq->begin();
346581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (state->mCommand != FastMixerState::MIX_WRITE &&
346681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) {
346781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (state->mCommand == FastMixerState::COLD_IDLE) {
346881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                int32_t old = android_atomic_inc(&mFastMixerFutex);
346981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (old == -1) {
3470ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes                    (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1);
347181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
347281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG
347381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (mAudioWatchdog != 0) {
347481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mAudioWatchdog->resume();
347581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
347681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
347781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
347881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            state->mCommand = FastMixerState::MIX_WRITE;
3479d797a9d5daad3051f9ac1d348abc2f434ea3ddcfGlenn Kasten#ifdef FAST_THREAD_STATISTICS
34804182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten            mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ?
3481fbdb2aceab7317aa44bc8f301a93eb49e17b2bceGlenn Kasten                FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN);
3482d797a9d5daad3051f9ac1d348abc2f434ea3ddcfGlenn Kasten#endif
348381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sq->end();
348481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
348581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (kUseFastMixer == FastMixer_Dynamic) {
348681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mNormalSink = mPipeSink;
348781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
348881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
348981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sq->end(false /*didModify*/);
349081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
349181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
3492bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    return PlaybackThread::threadLoop_write();
349381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
349481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
349581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::threadLoop_standby()
349681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
349781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Idle the fast mixer if it's currently running
34984d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten    if (mFastMixer != 0) {
349981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FastMixerStateQueue *sq = mFastMixer->sq();
350081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FastMixerState *state = sq->begin();
350181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (!(state->mCommand & FastMixerState::IDLE)) {
350281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            state->mCommand = FastMixerState::COLD_IDLE;
350381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            state->mColdFutexAddr = &mFastMixerFutex;
350481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            state->mColdGen++;
350581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mFastMixerFutex = 0;
350681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sq->end();
350781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now
350881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED);
350981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (kUseFastMixer == FastMixer_Dynamic) {
351081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mNormalSink = mOutputSink;
351181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
351281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG
351381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (mAudioWatchdog != 0) {
351481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mAudioWatchdog->pause();
351581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
351681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
351781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
351881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sq->end(false /*didModify*/);
351981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
352081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
352181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread::threadLoop_standby();
352281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
352381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3524bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::PlaybackThread::waitingAsyncCallback_l()
3525bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
3526bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    return false;
3527bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
3528bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
3529bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::PlaybackThread::shouldStandby_l()
3530bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
3531bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    return !mStandby;
3532bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
3533bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
3534bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::PlaybackThread::waitingAsyncCallback()
3535bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
3536bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    Mutex::Autolock _l(mLock);
3537bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    return waitingAsyncCallback_l();
3538bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
3539bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
354081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// shared by MIXER and DIRECT, overridden by DUPLICATING
354181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::threadLoop_standby()
354281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
354381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended);
3544062e67a26e0553dd142be622821f493df541f0c6Phil Burk    mOutput->standby();
3545bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if (mUseAsyncWrite != 0) {
35463b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        // discard any pending drain or write ack by incrementing sequence
35473b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mWriteAckSequence = (mWriteAckSequence + 2) & ~1;
35483b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mDrainSequence = (mDrainSequence + 2) & ~1;
3549bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        ALOG_ASSERT(mCallbackThread != 0);
35503b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mCallbackThread->setWriteBlocked(mWriteAckSequence);
35513b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mCallbackThread->setDraining(mDrainSequence);
3552bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
3553d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    mHwPaused = false;
355481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
355581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
35564c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew Georgevoid AudioFlinger::PlaybackThread::onAddNewTrack_l()
35574c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George{
35584c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George    ALOGV("signal playback thread");
35594c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George    broadcast_l();
35604c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George}
35614c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George
356281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::threadLoop_mix()
356381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
356481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // obtain the presentation timestamp of the next output buffer
356581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int64_t pts;
356681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t status = INVALID_OPERATION;
356781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
356881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mNormalSink != 0) {
356981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status = mNormalSink->getNextWriteTimestamp(&pts);
357081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
357181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status = mOutputSink->getNextWriteTimestamp(&pts);
357281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
357381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
357481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (status != NO_ERROR) {
357581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        pts = AudioBufferProvider::kInvalidPTS;
357681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
357781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
357881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mix buffers...
357981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioMixer->process(pts);
358025c2dac12114699e90deb1c579cadebce7b91a97Andy Hung    mCurrentWriteLength = mSinkBufferSize;
358181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // increase sleep time progressively when application underrun condition clears.
358281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Only increase sleep time if the mixer is ready for two consecutive times to avoid
358381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // that a steady state of alternating ready/not ready conditions keeps the sleep time
358481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // such that we would underrun the audio HAL.
3585ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    if ((mSleepTimeUs == 0) && (sleepTimeShift > 0)) {
358681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sleepTimeShift--;
358781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
3588ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    mSleepTimeUs = 0;
3589ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    mStandbyTimeNs = systemTime() + mStandbyDelayNs;
359081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    //TODO: delay standby when effects have a tail
35914c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten
359281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
359381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
359481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::threadLoop_sleepTime()
359581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
359681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // If no tracks are ready, sleep once for the duration of an output
359781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // buffer size, then write 0s to the output
3598ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    if (mSleepTimeUs == 0) {
359981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (mMixerStatus == MIXER_TRACKS_ENABLED) {
3600ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent            mSleepTimeUs = mActiveSleepTimeUs >> sleepTimeShift;
3601ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent            if (mSleepTimeUs < kMinThreadSleepTimeUs) {
3602ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent                mSleepTimeUs = kMinThreadSleepTimeUs;
360381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
360481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // reduce sleep time in case of consecutive application underruns to avoid
360581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer
360681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // duration we would end up writing less data than needed by the audio HAL if
360781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // the condition persists.
360881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (sleepTimeShift < kMaxThreadSleepTimeShift) {
360981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sleepTimeShift++;
361081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
361181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
3612ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent            mSleepTimeUs = mIdleSleepTimeUs;
361381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
361481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) {
361598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared
361698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        // before effects processing or output.
361798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        if (mMixerBufferValid) {
361898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            memset(mMixerBuffer, 0, mMixerBufferSize);
361998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        } else {
362098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            memset(mSinkBuffer, 0, mSinkBufferSize);
362198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        }
3622ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent        mSleepTimeUs = 0;
362381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED),
362481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                "anticipated start");
362581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
362681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // TODO add standby time extension fct of effect tail
362781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
362881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
362981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// prepareTracks_l() must be called with ThreadBase::mLock held
363081784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l(
363181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Vector< sp<Track> > *tracksToRemove)
363281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
363381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
363481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mixer_state mixerStatus = MIXER_IDLE;
363581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // find out which tracks need to be processed
363681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t count = mActiveTracks.size();
363781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t mixedTracks = 0;
363881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t tracksWithEffect = 0;
363981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // counts only _active_ fast tracks
364081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t fastTracks = 0;
364181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset
364281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
364381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    float masterVolume = mMasterVolume;
364481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool masterMute = mMasterMute;
364581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
364681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (masterMute) {
364781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        masterVolume = 0;
364881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
364981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Delegate master volume control to effect in output mix effect chain if needed
365081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
365181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (chain != 0) {
365281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t v = (uint32_t)(masterVolume * (1 << 24));
365381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        chain->setVolume_l(&v, &v);
365481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        masterVolume = (float)((v + (1 << 23)) >> 24);
365581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        chain.clear();
365681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
365781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
365881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // prepare a new state to push
365981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    FastMixerStateQueue *sq = NULL;
366081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    FastMixerState *state = NULL;
366181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool didModify = false;
366281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED;
36634d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten    if (mFastMixer != 0) {
366481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sq = mFastMixer->sq();
366581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state = sq->begin();
366681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
366781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
366869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    mMixerBufferValid = false;  // mMixerBuffer has no valid data until appropriate tracks found.
366998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found.
367069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
367181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i=0 ; i<count ; i++) {
36729fdcb0a9497ca290bcf364b10868587b6bde3a34Glenn Kasten        const sp<Track> t = mActiveTracks[i].promote();
367381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (t == 0) {
367481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            continue;
367581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
367681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
367781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // this const just means the local variable doesn't change
367881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Track* const track = t.get();
367981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
368081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // process fast tracks
368181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (track->isFastTrack()) {
368281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
368381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // It's theoretically possible (though unlikely) for a fast track to be created
368481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // and then removed within the same normal mix cycle.  This is not a problem, as
368581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // the track never becomes active so it's fast mixer slot is never touched.
368681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // The converse, of removing an (active) track and then creating a new track
368781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // at the identical fast mixer slot within the same normal mix cycle,
368881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // is impossible because the slot isn't marked available until the end of each cycle.
368981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            int j = track->mFastIndex;
369081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOG_ASSERT(0 < j && j < (int)FastMixerState::kMaxFastTracks);
369181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j)));
369281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            FastTrack *fastTrack = &state->mFastTracks[j];
369381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
369481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // Determine whether the track is currently in underrun condition,
369581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // and whether it had a recent underrun.
369681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j];
369781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            FastTrackUnderruns underruns = ftDump->mUnderruns;
369881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            uint32_t recentFull = (underruns.mBitFields.mFull -
369981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK;
370081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            uint32_t recentPartial = (underruns.mBitFields.mPartial -
370181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK;
370281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            uint32_t recentEmpty = (underruns.mBitFields.mEmpty -
370381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK;
370481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            uint32_t recentUnderruns = recentPartial + recentEmpty;
370581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            track->mObservedUnderruns = underruns;
370681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // don't count underruns that occur while stopping or pausing
370781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // or stopped which can occur when flush() is called while active
370882aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten            if (!(track->isStopping() || track->isPausing() || track->isStopped()) &&
370982aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten                    recentUnderruns > 0) {
371082aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten                // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun
371182aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten                track->mAudioTrackServerProxy->tallyUnderrunFrames(recentUnderruns * mFrameCount);
371281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
371381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
371481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // This is similar to the state machine for normal tracks,
371581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // with a few modifications for fast tracks.
371681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            bool isActive = true;
371781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            switch (track->mState) {
371881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            case TrackBase::STOPPING_1:
371981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // track stays active in STOPPING_1 state until first underrun
3720bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                if (recentUnderruns > 0 || track->isTerminated()) {
372181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    track->mState = TrackBase::STOPPING_2;
372281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
372381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                break;
372481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            case TrackBase::PAUSING:
372581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // ramp down is not yet implemented
372681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                track->setPaused();
372781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                break;
372881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            case TrackBase::RESUMING:
372981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // ramp up is not yet implemented
373081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                track->mState = TrackBase::ACTIVE;
373181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                break;
373281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            case TrackBase::ACTIVE:
373381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (recentFull > 0 || recentPartial > 0) {
373481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // track has provided at least some frames recently: reset retry count
373581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    track->mRetryCount = kMaxTrackRetries;
373681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
373781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (recentUnderruns == 0) {
373881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // no recent underruns: stay active
373981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    break;
374081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
374181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // there has recently been an underrun of some kind
374281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (track->sharedBuffer() == 0) {
374381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // were any of the recent underruns "empty" (no frames available)?
374481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    if (recentEmpty == 0) {
374581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        // no, then ignore the partial underruns as they are allowed indefinitely
374681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        break;
374781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    }
374881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // there has recently been an "empty" underrun: decrement the retry counter
374981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    if (--(track->mRetryCount) > 0) {
375081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        break;
375181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    }
375281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // indicate to client process that the track was disabled because of underrun;
375381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // it will then automatically call start() when data is available
375496f60d8f04432a1ed503b3e24d5736d28c63c9a2Glenn Kasten                    android_atomic_or(CBLK_DISABLED, &track->mCblk->mFlags);
375581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // remove from active list, but state remains ACTIVE [confusing but true]
375681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    isActive = false;
375781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    break;
375881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
375981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // fall through
376081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            case TrackBase::STOPPING_2:
376181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            case TrackBase::PAUSED:
376281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            case TrackBase::STOPPED:
376381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            case TrackBase::FLUSHED:   // flush() while active
376481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Check for presentation complete if track is inactive
376581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // We have consumed all the buffers of this track.
376681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // This would be incomplete if we auto-paused on underrun
376781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                {
376881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    size_t audioHALFrames =
376981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000;
377081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    size_t framesWritten = mBytesWritten / mFrameSize;
377181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) {
377281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        // track stays in active list until presentation is complete
377381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        break;
377481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    }
377581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
377681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (track->isStopping_2()) {
377781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    track->mState = TrackBase::STOPPED;
377881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
377981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (track->isStopped()) {
378081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // Can't reset directly, as fast mixer is still polling this track
378181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    //   track->reset();
378281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // So instead mark this track as needing to be reset after push with ack
378381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    resetMask |= 1 << i;
378481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
378581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                isActive = false;
378681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                break;
378781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            case TrackBase::IDLE:
378881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            default:
3789adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten                LOG_ALWAYS_FATAL("unexpected track state %d", track->mState);
379081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
379181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
379281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (isActive) {
379381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // was it previously inactive?
379481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (!(state->mTrackMask & (1 << j))) {
379581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ExtendedAudioBufferProvider *eabp = track;
379681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    VolumeProvider *vp = track;
379781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    fastTrack->mBufferProvider = eabp;
379881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    fastTrack->mVolumeProvider = vp;
379981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    fastTrack->mChannelMask = track->mChannelMask;
3800e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung                    fastTrack->mFormat = track->mFormat;
380181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    fastTrack->mGeneration++;
380281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    state->mTrackMask |= 1 << j;
380381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    didModify = true;
380481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // no acknowledgement required for newly active tracks
380581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
380681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // cache the combined master volume and stream type volume for fast mixer; this
380781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // lacks any synchronization or barrier so VolumeProvider may read a stale value
3808e4756fe3a387615acb63c6a05788c8db9b5786cbGlenn Kasten                track->mCachedVolume = masterVolume * mStreamTypes[track->streamType()].volume;
380981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ++fastTracks;
381081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            } else {
381181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // was it previously active?
381281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (state->mTrackMask & (1 << j)) {
381381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    fastTrack->mBufferProvider = NULL;
381481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    fastTrack->mGeneration++;
381581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    state->mTrackMask &= ~(1 << j);
381681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    didModify = true;
381781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // If any fast tracks were removed, we must wait for acknowledgement
381881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // because we're about to decrement the last sp<> on those tracks.
381981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    block = FastMixerStateQueue::BLOCK_UNTIL_ACKED;
382081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                } else {
3821adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten                    LOG_ALWAYS_FATAL("fast track %d should have been active", j);
382281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
382381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                tracksToRemove->add(track);
382481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Avoids a misleading display in dumpsys
382581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL;
382681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
382781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            continue;
382881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
382981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
383081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        {   // local variable scope to avoid goto warning
383181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
383281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_track_cblk_t* cblk = track->cblk();
383381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
383481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // The first time a track is added we wait
383581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // for all its buffers to be filled before processing it
383681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int name = track->name();
383781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // make sure that we have enough frames to mix one full buffer.
383881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // enforce this condition only once to enable draining the buffer in case the client
383981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // app does not call stop() and relies on underrun to stop:
384081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed
384181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // during last round
38429f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        size_t desiredFrames;
38438edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung        const uint32_t sampleRate = track->mAudioTrackServerProxy->getSampleRate();
38445a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia        AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate();
38458edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung
38468edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung        desiredFrames = sourceFramesNeededWithTimestretch(
38475a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia                sampleRate, mNormalFrameCount, mSampleRate, playbackRate.mSpeed);
38488edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung        // TODO: ONLY USED FOR LEGACY RESAMPLERS, remove when they are removed.
38498edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung        // add frames already consumed but not yet released by the resampler
38508edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung        // because mAudioTrackServerProxy->framesReady() will include these frames
38518edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung        desiredFrames += mAudioMixer->getUnreleasedFrames(track->name());
38528edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung
385381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t minFrames = 1;
385481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() &&
385581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) {
38569f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            minFrames = desiredFrames;
38579f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }
385813e4c960ea3db03a43e084fbd85d52aa77f7b871Eric Laurent
385913e4c960ea3db03a43e084fbd85d52aa77f7b871Eric Laurent        size_t framesReady = track->framesReady();
3860e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten        if (ATRACE_ENABLED()) {
3861e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten            // I wish we had formatted trace names
3862e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten            char traceName[16];
3863e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten            strcpy(traceName, "nRdy");
3864e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten            int name = track->name();
3865e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten            if (AudioMixer::TRACK0 <= name &&
3866e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten                    name < (int) (AudioMixer::TRACK0 + AudioMixer::MAX_NUM_TRACKS)) {
3867e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten                name -= AudioMixer::TRACK0;
3868e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten                traceName[4] = (name / 10) + '0';
3869e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten                traceName[5] = (name % 10) + '0';
3870e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten            } else {
3871e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten                traceName[4] = '?';
3872e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten                traceName[5] = '?';
3873e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten            }
3874e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten            traceName[6] = '\0';
3875e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten            ATRACE_INT(traceName, framesReady);
3876e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten        }
38779f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if ((framesReady >= minFrames) && track->isReady() &&
387881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                !track->isPaused() && !track->isTerminated())
387981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        {
3880f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten            ALOGVV("track %d s=%08x [OK] on thread %p", name, cblk->mServer, this);
388181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
388281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mixedTracks++;
388381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
388469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            // track->mainBuffer() != mSinkBuffer or mMixerBuffer means
388569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            // there is an effect chain connected to the track
388681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chain.clear();
388769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            if (track->mainBuffer() != mSinkBuffer &&
388869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                    track->mainBuffer() != mMixerBuffer) {
388998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung                if (mEffectBufferEnabled) {
389098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung                    mEffectBufferValid = true; // Later can set directly.
389198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung                }
389281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                chain = getEffectChain_l(track->sessionId());
389381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Delegate volume control to effect in track effect chain if needed
389481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (chain != 0) {
389581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    tracksWithEffect++;
389681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                } else {
389781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on "
389881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            "session %d",
389981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            name, track->sessionId());
390081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
390181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
390281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
390381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
390481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            int param = AudioMixer::VOLUME;
390581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (track->mFillingUpStatus == Track::FS_FILLED) {
390681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // no ramp for the first volume setting
390781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                track->mFillingUpStatus = Track::FS_ACTIVE;
390881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (track->mState == TrackBase::RESUMING) {
390981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    track->mState = TrackBase::ACTIVE;
391081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    param = AudioMixer::RAMP_VOLUME;
391181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
391281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL);
3913f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten            // FIXME should not make a decision based on mServer
3914f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten            } else if (cblk->mServer != 0) {
391581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // If the track is stopped before the first frame was mixed,
391681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // do not apply ramp
391781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                param = AudioMixer::RAMP_VOLUME;
391881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
391981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
392081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // compute volume for this track
39216be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung            uint32_t vl, vr;       // in U8.24 integer format
39226be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung            float vlf, vrf, vaf;   // in [0.0, 1.0] float format
3923e4756fe3a387615acb63c6a05788c8db9b5786cbGlenn Kasten            if (track->isPausing() || mStreamTypes[track->streamType()].mute) {
39246be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                vl = vr = 0;
39256be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                vlf = vrf = vaf = 0.;
392681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (track->isPausing()) {
392781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    track->setPaused();
392881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
392981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            } else {
393081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
393181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // read original volumes with volume control
393281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                float typeVolume = mStreamTypes[track->streamType()].volume;
393381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                float v = masterVolume * typeVolume;
39349f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy;
3935c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten                gain_minifloat_packed_t vlr = proxy->getVolumeLR();
39366be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                vlf = float_from_gain(gain_minifloat_unpack_left(vlr));
39376be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                vrf = float_from_gain(gain_minifloat_unpack_right(vlr));
393881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // track volumes come from shared memory, so can't be trusted and must be clamped
3939c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten                if (vlf > GAIN_FLOAT_UNITY) {
3940c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten                    ALOGV("Track left volume out of range: %.3g", vlf);
3941c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten                    vlf = GAIN_FLOAT_UNITY;
394281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
3943c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten                if (vrf > GAIN_FLOAT_UNITY) {
3944c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten                    ALOGV("Track right volume out of range: %.3g", vrf);
3945c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten                    vrf = GAIN_FLOAT_UNITY;
394681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
394781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // now apply the master volume and stream type volume
39486be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                vlf *= v;
39496be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                vrf *= v;
395081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // assuming master volume and stream type volume each go up to 1.0,
39516be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                // then derive vl and vr as U8.24 versions for the effect chain
39526be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT;
39536be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                vl = (uint32_t) (scaleto8_24 * vlf);
39546be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                vr = (uint32_t) (scaleto8_24 * vrf);
39556be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                // vl and vr are now in U8.24 format
3956e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten                uint16_t sendLevel = proxy->getSendLevel_U4_12();
395781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // send level comes from shared memory and so may be corrupt
395881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (sendLevel > MAX_GAIN_INT) {
395981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ALOGV("Track send level out of range: %04X", sendLevel);
396081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    sendLevel = MAX_GAIN_INT;
396181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
39626be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                // vaf is represented as [0.0, 1.0] float by rescaling sendLevel
39636be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                vaf = v * sendLevel * (1. / MAX_GAIN_INT);
396481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
3965bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
396681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // Delegate volume control to effect in track effect chain if needed
396781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (chain != 0 && chain->setVolume_l(&vl, &vr)) {
396881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Do not ramp volume if volume is controlled by effect
396981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                param = AudioMixer::VOLUME;
3970b6be7f22a82ee3bad8bcc709d21e72fc4727da09Bryant Liu                // Update remaining floating point volume levels
3971b6be7f22a82ee3bad8bcc709d21e72fc4727da09Bryant Liu                vlf = (float)vl / (1 << 24);
3972b6be7f22a82ee3bad8bcc709d21e72fc4727da09Bryant Liu                vrf = (float)vr / (1 << 24);
397381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                track->mHasVolumeController = true;
397481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            } else {
397581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // force no volume ramp when volume controller was just disabled or removed
397681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // from effect chain to avoid volume spike
397781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (track->mHasVolumeController) {
397881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    param = AudioMixer::VOLUME;
397981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
398081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                track->mHasVolumeController = false;
398181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
398281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
398381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // XXX: these things DON'T need to be done each time
398481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mAudioMixer->setBufferProvider(name, track);
398581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mAudioMixer->enable(name);
398681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
39876be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung            mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, &vlf);
39886be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung            mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, &vrf);
39896be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung            mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, &vaf);
399081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mAudioMixer->setParameter(
399181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                name,
399281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioMixer::TRACK,
399381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioMixer::FORMAT, (void *)track->format());
399481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mAudioMixer->setParameter(
399581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                name,
399681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioMixer::TRACK,
3997377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT                AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask());
39989a59276fb465e492138e0576523b54079671e8f4Andy Hung            mAudioMixer->setParameter(
39999a59276fb465e492138e0576523b54079671e8f4Andy Hung                name,
40009a59276fb465e492138e0576523b54079671e8f4Andy Hung                AudioMixer::TRACK,
40019a59276fb465e492138e0576523b54079671e8f4Andy Hung                AudioMixer::MIXER_CHANNEL_MASK, (void *)(uintptr_t)mChannelMask);
4002e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten            // limit track sample rate to 2 x output sample rate, which changes at re-configuration
4003cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung            uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX;
40049f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            uint32_t reqSampleRate = track->mAudioTrackServerProxy->getSampleRate();
4005e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten            if (reqSampleRate == 0) {
4006e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten                reqSampleRate = mSampleRate;
4007e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten            } else if (reqSampleRate > maxSampleRate) {
4008e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten                reqSampleRate = maxSampleRate;
4009e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten            }
401081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mAudioMixer->setParameter(
401181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                name,
401281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioMixer::RESAMPLE,
401381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioMixer::SAMPLE_RATE,
4014377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT                (void *)(uintptr_t)reqSampleRate);
40158edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung
40165a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia            AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate();
40178edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung            mAudioMixer->setParameter(
40188edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung                name,
40198edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung                AudioMixer::TIMESTRETCH,
40208edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung                AudioMixer::PLAYBACK_RATE,
40215a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia                &playbackRate);
40228edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung
402369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            /*
402469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung             * Select the appropriate output buffer for the track.
402569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung             *
402698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung             * Tracks with effects go into their own effects chain buffer
402798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung             * and from there into either mEffectBuffer or mSinkBuffer.
402869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung             *
402969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung             * Other tracks can use mMixerBuffer for higher precision
403069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung             * channel accumulation.  If this buffer is enabled
403169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung             * (mMixerBufferEnabled true), then selected tracks will accumulate
403269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung             * into it.
403369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung             *
403469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung             */
403569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            if (mMixerBufferEnabled
403669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                    && (track->mainBuffer() == mSinkBuffer
403769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                            || track->mainBuffer() == mMixerBuffer)) {
403869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                mAudioMixer->setParameter(
403969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                        name,
404069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                        AudioMixer::TRACK,
4041788207057ed4b8df4719ed8089f376ef52de9ca1Andy Hung                        AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat);
404269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                mAudioMixer->setParameter(
404369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                        name,
404469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                        AudioMixer::TRACK,
404569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                        AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer);
404669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                // TODO: override track->mainBuffer()?
404769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                mMixerBufferValid = true;
404869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            } else {
404969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                mAudioMixer->setParameter(
405069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                        name,
405169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                        AudioMixer::TRACK,
4052788207057ed4b8df4719ed8089f376ef52de9ca1Andy Hung                        AudioMixer::MIXER_FORMAT, (void *)AUDIO_FORMAT_PCM_16_BIT);
405369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                mAudioMixer->setParameter(
405469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                        name,
405569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                        AudioMixer::TRACK,
405669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                        AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer());
405769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            }
405881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mAudioMixer->setParameter(
405981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                name,
406081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioMixer::TRACK,
406181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioMixer::AUX_BUFFER, (void *)track->auxBuffer());
406281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
406381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // reset retry count
406481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            track->mRetryCount = kMaxTrackRetries;
406581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
406681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // If one track is ready, set the mixer ready if:
406781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            //  - the mixer was not ready during previous round OR
406881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            //  - no other track is not ready
406981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY ||
407081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mixerStatus != MIXER_TRACKS_ENABLED) {
407181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mixerStatus = MIXER_TRACKS_READY;
407281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
407381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
40749f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) {
407508fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                ALOGV("track(%p) underrun,  framesReady(%zu) < framesDesired(%zd)",
407608fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung                        track, framesReady, desiredFrames);
407782aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten                track->mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames);
40789f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            }
407981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // clear effect chain input buffer if an active track underruns to avoid sending
408081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // previous audio buffer again to effects
408181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chain = getEffectChain_l(track->sessionId());
408281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (chain != 0) {
408381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                chain->clearInputBuffer();
408481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
408581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4086f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten            ALOGVV("track %d s=%08x [NOT READY] on thread %p", name, cblk->mServer, this);
408781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if ((track->sharedBuffer() != 0) || track->isTerminated() ||
408881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    track->isStopped() || track->isPaused()) {
408981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // We have consumed all the buffers of this track.
409081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Remove it from the list of active tracks.
409181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // TODO: use actual buffer filling status instead of latency when available from
409281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // audio HAL
409381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                size_t audioHALFrames = (latency_l() * mSampleRate) / 1000;
409481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                size_t framesWritten = mBytesWritten / mFrameSize;
409581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) {
409681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    if (track->isStopped()) {
409781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        track->reset();
409881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    }
409981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    tracksToRemove->add(track);
410081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
410181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            } else {
410281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // No buffers for this track. Give it a few chances to
410381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // fill a buffer, then remove it from active list.
410481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (--(track->mRetryCount) <= 0) {
4105c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten                    ALOGI("BUFFER TIMEOUT: remove(%d) from active list on thread %p", name, this);
410681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    tracksToRemove->add(track);
410781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // indicate to client process that the track was disabled because of underrun;
410881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // it will then automatically call start() when data is available
410996f60d8f04432a1ed503b3e24d5736d28c63c9a2Glenn Kasten                    android_atomic_or(CBLK_DISABLED, &cblk->mFlags);
411081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // If one track is not ready, mark the mixer also not ready if:
411181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                //  - the mixer was ready during previous round OR
411281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                //  - no other track is ready
411381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY ||
411481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                mixerStatus != MIXER_TRACKS_READY) {
411581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mixerStatus = MIXER_TRACKS_ENABLED;
411681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
411781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
411881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mAudioMixer->disable(name);
411981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
412081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
412181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }   // local variable scope to avoid goto warning
412281784c37c61b09289654b979567a42bf73cd2b12Eric Laurenttrack_is_ready: ;
412381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
412481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
412581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
412681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Push the new FastMixer state if necessary
412781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool pauseAudioWatchdog = false;
412881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (didModify) {
412981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mFastTracksGen++;
413081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle
413181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (kUseFastMixer == FastMixer_Dynamic &&
413281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) {
413381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            state->mCommand = FastMixerState::COLD_IDLE;
413481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            state->mColdFutexAddr = &mFastMixerFutex;
413581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            state->mColdGen++;
413681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mFastMixerFutex = 0;
413781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (kUseFastMixer == FastMixer_Dynamic) {
413881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mNormalSink = mOutputSink;
413981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
414081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // If we go into cold idle, need to wait for acknowledgement
414181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // so that fast mixer stops doing I/O.
414281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            block = FastMixerStateQueue::BLOCK_UNTIL_ACKED;
414381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            pauseAudioWatchdog = true;
414481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
414581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
414681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (sq != NULL) {
414781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sq->end(didModify);
414881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sq->push(block);
414981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
415081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG
415181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (pauseAudioWatchdog && mAudioWatchdog != 0) {
415281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mAudioWatchdog->pause();
415381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
415481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
415581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
415681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Now perform the deferred reset on fast tracks that have stopped
415781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    while (resetMask != 0) {
415881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        size_t i = __builtin_ctz(resetMask);
415981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOG_ASSERT(i < count);
416081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        resetMask &= ~(1 << i);
416181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<Track> t = mActiveTracks[i].promote();
416281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (t == 0) {
416381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            continue;
416481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
416581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Track* track = t.get();
416681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOG_ASSERT(track->isFastTrack() && track->isStopped());
416781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        track->reset();
416881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
416981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
417081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // remove all the tracks that need to be...
4171bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    removeTracks_l(*tracksToRemove);
417281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
417397d547da43c9c41711d1ed1e3f4fa87c2ee3cb9aEric Laurent    if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0) {
417497d547da43c9c41711d1ed1e3f4fa87c2ee3cb9aEric Laurent        mEffectBufferValid = true;
4175ac302143551a8b964f026385a524dda9ff8ea5baMarco Nelissen    }
4176ac302143551a8b964f026385a524dda9ff8ea5baMarco Nelissen
4177ac302143551a8b964f026385a524dda9ff8ea5baMarco Nelissen    if (mEffectBufferValid) {
417857088b5c8e76855b99b3e6b3e410de5b6382670eMarco Nelissen        // as long as there are effects we should clear the effects buffer, to avoid
417957088b5c8e76855b99b3e6b3e410de5b6382670eMarco Nelissen        // passing a non-clean buffer to the effect chain
418057088b5c8e76855b99b3e6b3e410de5b6382670eMarco Nelissen        memset(mEffectBuffer, 0, mEffectBufferSize);
418197d547da43c9c41711d1ed1e3f4fa87c2ee3cb9aEric Laurent    }
418269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // sink or mix buffer must be cleared if all tracks are connected to an
418369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // effect chain as in this case the mixer will not write to the sink or mix buffer
418469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // and track effects will accumulate into it
4185bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) ||
4186bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            (mixedTracks == 0 && fastTracks > 0))) {
418781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // FIXME as a performance optimization, should remember previous zero status
418869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung        if (mMixerBufferValid) {
418969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            memset(mMixerBuffer, 0, mMixerBufferSize);
419069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            // TODO: In testing, mSinkBuffer below need not be cleared because
419169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer
419269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            // after mixing.
419369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            //
419469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            // To enforce this guarantee:
419569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) ||
419669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            // (mixedTracks == 0 && fastTracks > 0))
419769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            // must imply MIXER_TRACKS_READY.
419869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            // Later, we may clear buffers regardless, and skip much of this logic.
419969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung        }
420098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        // FIXME as a performance optimization, should remember previous zero status
42015567aaf4818007cd8e77329683a91c0f5d7a8837Andy Hung        memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize);
420281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
420381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
420481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // if any fast tracks, then status is ready
420581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mMixerStatusIgnoringFastTracks = mixerStatus;
420681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (fastTracks > 0) {
420781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mixerStatus = MIXER_TRACKS_READY;
420881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
420981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mixerStatus;
421081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
421181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
421281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// getTrackName_l() must be called with ThreadBase::mLock held
4213e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hungint AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask,
4214e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung        audio_format_t format, int sessionId)
421581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
4216e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung    return mAudioMixer->getTrackName(channelMask, format, sessionId);
421781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
421881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
421981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// deleteTrackName_l() must be called with ThreadBase::mLock held
422081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::deleteTrackName_l(int name)
422181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
422281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("remove track (%d) and delete from mixer", name);
422381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioMixer->deleteTrackName(name);
422481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
422581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
42261035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// checkForNewParameter_l() must be called with ThreadBase::mLock held
42271035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentbool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair,
42281035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                       status_t& status)
422981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
423081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool reconfig = false;
423181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
42321035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    status = NO_ERROR;
423381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
42341035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // if !&IDLE, holds the FastMixer state to restore after new parameters processed
42351035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE;
42364d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten    if (mFastMixer != 0) {
42371035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        FastMixerStateQueue *sq = mFastMixer->sq();
42381035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        FastMixerState *state = sq->begin();
42391035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (!(state->mCommand & FastMixerState::IDLE)) {
42401035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            previousCommand = state->mCommand;
42411035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            state->mCommand = FastMixerState::HOT_IDLE;
42421035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            sq->end();
42431035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED);
42441035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        } else {
42451035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            sq->end(false /*didModify*/);
424681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
42471035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
424881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
42491035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    AudioParameter param = AudioParameter(keyValuePair);
42501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    int value;
42511035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
42521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        reconfig = true;
42531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
42541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
42559a59276fb465e492138e0576523b54079671e8f4Andy Hung        if (!isValidPcmSinkFormat((audio_format_t) value)) {
42561035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            status = BAD_VALUE;
42571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        } else {
42581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            // no need to save value, since it's constant
425981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            reconfig = true;
426081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
42611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
42621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
42639a59276fb465e492138e0576523b54079671e8f4Andy Hung        if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) {
42641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            status = BAD_VALUE;
42651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        } else {
42661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            // no need to save value, since it's constant
42671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            reconfig = true;
426881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
42691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
42701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
42711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // do not accept frame count changes if tracks are open as the track buffer
42721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // size depends on frame count and correct behavior would not be guaranteed
42731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // if frame count is changed after track creation
42741035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (!mTracks.isEmpty()) {
42751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            status = INVALID_OPERATION;
42761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        } else {
42771035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            reconfig = true;
427881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
42791035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
42801035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
428181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef ADD_BATTERY_DATA
42821035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // when changing the audio output device, call addBatteryData to notify
42831035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // the change
42841035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (mOutDevice != value) {
42851035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            uint32_t params = 0;
42861035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            // check whether speaker is on
42871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            if (value & AUDIO_DEVICE_OUT_SPEAKER) {
42881035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                params |= IMediaPlayerService::kBatteryDataSpeakerOn;
42891035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            }
429081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
42911035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            audio_devices_t deviceWithoutSpeaker
42921035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER;
42931035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            // check if any other device (except speaker) is on
4294054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent            if (value & deviceWithoutSpeaker) {
42951035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn;
42961035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            }
429781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
42981035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            if (params != 0) {
42991035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                addBatteryData(params);
430081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
43011035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
430281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
430381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
43041035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // forward device change to effects that have requested to be
43051035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // aware of attached audio device.
43061035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (value != AUDIO_DEVICE_NONE) {
43071035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mOutDevice = value;
43081035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            for (size_t i = 0; i < mEffectChains.size(); i++) {
43091035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                mEffectChains[i]->setDevice_l(mOutDevice);
431081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
431181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
43121035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
431381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
43141035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (status == NO_ERROR) {
43151035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
43161035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                keyValuePair.string());
43171035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (!mStandby && status == INVALID_OPERATION) {
4318062e67a26e0553dd142be622821f493df541f0c6Phil Burk            mOutput->standby();
43191035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mStandby = true;
43201035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mBytesWritten = 0;
432181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
43221035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                   keyValuePair.string());
43231035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
43241035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (status == NO_ERROR && reconfig) {
43251035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            readOutputParameters_l();
43261035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            delete mAudioMixer;
43271035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate);
43281035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            for (size_t i = 0; i < mTracks.size() ; i++) {
4329e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung                int name = getTrackName_l(mTracks[i]->mChannelMask,
4330e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung                        mTracks[i]->mFormat, mTracks[i]->mSessionId);
43311035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                if (name < 0) {
43321035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                    break;
433381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
43341035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                mTracks[i]->mName = name;
433581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
433673e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent            sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED);
433781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
433881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
433981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
434081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!(previousCommand & FastMixerState::IDLE)) {
43414d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten        ALOG_ASSERT(mFastMixer != 0);
434281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FastMixerStateQueue *sq = mFastMixer->sq();
434381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FastMixerState *state = sq->begin();
434481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE);
434581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mCommand = previousCommand;
434681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sq->end();
434781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
434881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
434981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
435081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return reconfig;
435181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
435281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
435381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
435481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args)
435581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
435681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const size_t SIZE = 256;
435781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    char buffer[SIZE];
435881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    String8 result;
435981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
436081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread::dumpInternals(fd, args);
436140eb1a1f8871909c272e72afaf7d5af84fea2412Andy Hung    dprintf(fd, "  Thread throttle time (msecs): %u\n", mThreadThrottleTimeMs);
436287cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  AudioMixer tracks: 0x%08x\n", mAudioMixer->trackNames());
436381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
436481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Make a non-atomic copy of fast mixer dump state so it won't change underneath us
43654182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    const FastMixerDumpState copy(mFastMixerDumpState);
436681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    copy.dump(fd);
436781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
436881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef STATE_QUEUE_DUMP
436981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Similar for state queue
437081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    StateQueueObserverDump observerCopy = mStateQueueObserverDump;
437181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    observerCopy.dump(fd);
437281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump;
437381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mutatorCopy.dump(fd);
437481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
437581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
437646909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
437781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Write the tee output to a .wav file
437881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    dumpTee(fd, mTeeSource, mId);
437946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
438081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
438181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG
438281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mAudioWatchdog != 0) {
438381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Make a non-atomic copy of audio watchdog dump so it won't change underneath us
438481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioWatchdogDump wdCopy = mAudioWatchdogDump;
438581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        wdCopy.dump(fd);
438681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
438781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
438881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
438981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
439081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const
439181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
439281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2;
439381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
439481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
439581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const
439681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
439781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000);
439881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
439981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
440081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::cacheParameters_l()
440181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
440281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread::cacheParameters_l();
440381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
440481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME: Relaxed timing because of a certain device that can't meet latency
440581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Should be reduced to 2x after the vendor fixes the driver issue
440681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // increase threshold again due to low power audio mode. The way this warning
440781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // threshold is calculated and its usefulness should be reconsidered anyway.
440881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15;
440981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
441081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
441181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
441281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
441381784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger,
441472e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device, bool systemReady)
441572e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    :   PlaybackThread(audioFlinger, output, id, device, DIRECT, systemReady)
441681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // mLeftVolFloat, mRightVolFloat
441781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
441881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
441981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4420bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger,
4421bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        AudioStreamOut* output, audio_io_handle_t id, uint32_t device,
442272e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        ThreadBase::type_t type, bool systemReady)
442372e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    :   PlaybackThread(audioFlinger, output, id, device, type, systemReady)
4424bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        // mLeftVolFloat, mRightVolFloat
4425bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
4426bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
4427bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
442881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::DirectOutputThread::~DirectOutputThread()
442981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
443081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
443181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4432bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack)
4433bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
4434bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    audio_track_cblk_t* cblk = track->cblk();
4435bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    float left, right;
4436bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4437bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if (mMasterMute || mStreamTypes[track->streamType()].mute) {
4438bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        left = right = 0;
4439bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    } else {
4440bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        float typeVolume = mStreamTypes[track->streamType()].volume;
4441bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        float v = mMasterVolume * typeVolume;
4442bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy;
4443c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten        gain_minifloat_packed_t vlr = proxy->getVolumeLR();
4444c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten        left = float_from_gain(gain_minifloat_unpack_left(vlr));
4445c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten        if (left > GAIN_FLOAT_UNITY) {
4446c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten            left = GAIN_FLOAT_UNITY;
4447c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten        }
4448c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten        left *= v;
4449c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten        right = float_from_gain(gain_minifloat_unpack_right(vlr));
4450c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten        if (right > GAIN_FLOAT_UNITY) {
4451c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten            right = GAIN_FLOAT_UNITY;
4452c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten        }
4453c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten        right *= v;
4454bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
4455bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4456bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if (lastTrack) {
4457bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (left != mLeftVolFloat || right != mRightVolFloat) {
4458bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            mLeftVolFloat = left;
4459bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            mRightVolFloat = right;
4460bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4461bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            // Convert volumes from float to 8.24
4462bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            uint32_t vl = (uint32_t)(left * (1 << 24));
4463bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            uint32_t vr = (uint32_t)(right * (1 << 24));
4464bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4465bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            // Delegate volume control to effect in track effect chain if needed
4466bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            // only one effect chain can be present on DirectOutputThread, so if
4467bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            // there is one, the track is connected to it
4468bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (!mEffectChains.isEmpty()) {
4469bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                mEffectChains[0]->setVolume_l(&vl, &vr);
4470bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                left = (float)vl / (1 << 24);
4471bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                right = (float)vr / (1 << 24);
4472bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
4473bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (mOutput->stream->set_volume) {
4474bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                mOutput->stream->set_volume(mOutput->stream, left, right);
4475bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
4476bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
4477bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
4478bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
4479bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
448043b4dcc660e6da96285e4672ae371070ab845401Phil Burkvoid AudioFlinger::DirectOutputThread::onAddNewTrack_l()
448143b4dcc660e6da96285e4672ae371070ab845401Phil Burk{
448243b4dcc660e6da96285e4672ae371070ab845401Phil Burk    sp<Track> previousTrack = mPreviousTrack.promote();
448343b4dcc660e6da96285e4672ae371070ab845401Phil Burk    sp<Track> latestTrack = mLatestActiveTrack.promote();
448443b4dcc660e6da96285e4672ae371070ab845401Phil Burk
448543b4dcc660e6da96285e4672ae371070ab845401Phil Burk    if (previousTrack != 0 && latestTrack != 0 &&
448643b4dcc660e6da96285e4672ae371070ab845401Phil Burk        (previousTrack->sessionId() != latestTrack->sessionId())) {
448743b4dcc660e6da96285e4672ae371070ab845401Phil Burk        mFlushPending = true;
448843b4dcc660e6da96285e4672ae371070ab845401Phil Burk    }
448943b4dcc660e6da96285e4672ae371070ab845401Phil Burk    PlaybackThread::onAddNewTrack_l();
449043b4dcc660e6da96285e4672ae371070ab845401Phil Burk}
4491bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
449281784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l(
449381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Vector< sp<Track> > *tracksToRemove
449481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent)
449581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
4496d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent    size_t count = mActiveTracks.size();
449781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mixer_state mixerStatus = MIXER_IDLE;
4498d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    bool doHwPause = false;
4499d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    bool doHwResume = false;
450081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
450181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // find out which tracks need to be processed
4502d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent    for (size_t i = 0; i < count; i++) {
4503d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent        sp<Track> t = mActiveTracks[i].promote();
450481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // The track died recently
450581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (t == 0) {
4506d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent            continue;
450781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
450881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
450943b4dcc660e6da96285e4672ae371070ab845401Phil Burk        if (t->isInvalid()) {
451043b4dcc660e6da96285e4672ae371070ab845401Phil Burk            ALOGW("An invalidated track shouldn't be in active list");
451143b4dcc660e6da96285e4672ae371070ab845401Phil Burk            tracksToRemove->add(t);
451243b4dcc660e6da96285e4672ae371070ab845401Phil Burk            continue;
451343b4dcc660e6da96285e4672ae371070ab845401Phil Burk        }
451443b4dcc660e6da96285e4672ae371070ab845401Phil Burk
451581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Track* const track = t.get();
451681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_track_cblk_t* cblk = track->cblk();
4517fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        // Only consider last track started for volume and mixer state control.
4518fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        // In theory an older track could underrun and restart after the new one starts
4519fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        // but as we only care about the transition phase between two tracks on a
4520fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        // direct output, it is not a problem to ignore the underrun case.
4521fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        sp<Track> l = mLatestActiveTrack.promote();
4522fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        bool last = l.get() == track;
452381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
45246fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk        if (track->isPausing()) {
4525d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent            track->setPaused();
45266fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk            if (mHwSupportsPause && last && !mHwPaused) {
4527d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent                doHwPause = true;
4528d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent                mHwPaused = true;
4529d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent            }
4530d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent            tracksToRemove->add(track);
4531d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent        } else if (track->isFlushPending()) {
4532d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent            track->flushAck();
4533d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent            if (last) {
453443b4dcc660e6da96285e4672ae371070ab845401Phil Burk                mFlushPending = true;
4535d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent            }
45366fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk        } else if (track->isResumePending()) {
4537d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent            track->resumeAck();
45386fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk            if (last && mHwPaused) {
45396fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk                doHwResume = true;
45406fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk                mHwPaused = false;
4541d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent            }
4542d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent        }
4543d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent
454481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // The first time a track is added we wait
454599adee3c3d9cde6819741a38163954808fea270aPhil Burk        // for all its buffers to be filled before processing it.
454699adee3c3d9cde6819741a38163954808fea270aPhil Burk        // Allow draining the buffer in case the client
454799adee3c3d9cde6819741a38163954808fea270aPhil Burk        // app does not call stop() and relies on underrun to stop:
454899adee3c3d9cde6819741a38163954808fea270aPhil Burk        // hence the test on (track->mRetryCount > 1).
454999adee3c3d9cde6819741a38163954808fea270aPhil Burk        // If retryCount<=1 then track is about to underrun and be removed.
455081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t minFrames;
455199adee3c3d9cde6819741a38163954808fea270aPhil Burk        if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing()
455299adee3c3d9cde6819741a38163954808fea270aPhil Burk            && (track->mRetryCount > 1)) {
455381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            minFrames = mNormalFrameCount;
455481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
455581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            minFrames = 1;
455681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
4557bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4558ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent        if ((track->framesReady() >= minFrames) && track->isReady() && !track->isPaused() &&
4559ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                !track->isStopping_2() && !track->isStopped())
456081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        {
4561f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten            ALOGVV("track %d s=%08x [OK]", track->name(), cblk->mServer);
456281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
456381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (track->mFillingUpStatus == Track::FS_FILLED) {
456481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                track->mFillingUpStatus = Track::FS_ACTIVE;
45651abbdb4429479975718421c4fef3f79ce7c820e3Eric Laurent                // make sure processVolume_l() will apply new volume even if 0
45661abbdb4429479975718421c4fef3f79ce7c820e3Eric Laurent                mLeftVolFloat = mRightVolFloat = -1.0;
4567d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent                if (!mHwSupportsPause) {
4568d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent                    track->resumeAck();
456981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
457081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
457181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
457281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // compute volume for this track
4573bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            processVolume_l(track, last);
4574bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (last) {
457543b4dcc660e6da96285e4672ae371070ab845401Phil Burk                sp<Track> previousTrack = mPreviousTrack.promote();
457643b4dcc660e6da96285e4672ae371070ab845401Phil Burk                if (previousTrack != 0) {
457743b4dcc660e6da96285e4672ae371070ab845401Phil Burk                    if (track != previousTrack.get()) {
457843b4dcc660e6da96285e4672ae371070ab845401Phil Burk                        // Flush any data still being written from last track
457943b4dcc660e6da96285e4672ae371070ab845401Phil Burk                        mBytesRemaining = 0;
458043b4dcc660e6da96285e4672ae371070ab845401Phil Burk                        // flush data already sent if changing audio session as audio
458143b4dcc660e6da96285e4672ae371070ab845401Phil Burk                        // comes from a different source. Also invalidate previous track to force a
458243b4dcc660e6da96285e4672ae371070ab845401Phil Burk                        // seek when resuming.
458343b4dcc660e6da96285e4672ae371070ab845401Phil Burk                        if (previousTrack->sessionId() != track->sessionId()) {
458443b4dcc660e6da96285e4672ae371070ab845401Phil Burk                            previousTrack->invalidate();
458543b4dcc660e6da96285e4672ae371070ab845401Phil Burk                        }
458643b4dcc660e6da96285e4672ae371070ab845401Phil Burk                    }
458743b4dcc660e6da96285e4672ae371070ab845401Phil Burk                }
458843b4dcc660e6da96285e4672ae371070ab845401Phil Burk                mPreviousTrack = track;
458943b4dcc660e6da96285e4672ae371070ab845401Phil Burk
4590d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent                // reset retry count
4591d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent                track->mRetryCount = kMaxTrackRetriesDirect;
4592d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent                mActiveTrack = t;
4593d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent                mixerStatus = MIXER_TRACKS_READY;
45945cff403679fc44c8293de81aed31c459c6129243Eric Laurent                if (mHwPaused) {
45950f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent                    doHwResume = true;
45960f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent                    mHwPaused = false;
45970f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent                }
4598d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent            }
459981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
4600d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent            // clear effect chain input buffer if the last active track started underruns
4601d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent            // to avoid sending previous audio buffer again to effects
4602fd4779740ec3e9e865d5514464df26d015354388Eric Laurent            if (!mEffectChains.isEmpty() && last) {
460381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mEffectChains[0]->clearInputBuffer();
460481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
4605ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent            if (track->isStopping_1()) {
4606ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                track->mState = TrackBase::STOPPING_2;
4607b369cafd67beb63dd0278dba543f519956208a7fEric Laurent                if (last && mHwPaused) {
4608b369cafd67beb63dd0278dba543f519956208a7fEric Laurent                     doHwResume = true;
4609b369cafd67beb63dd0278dba543f519956208a7fEric Laurent                     mHwPaused = false;
4610b369cafd67beb63dd0278dba543f519956208a7fEric Laurent                 }
4611ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent            }
4612ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent            if ((track->sharedBuffer() != 0) || track->isStopped() ||
4613ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                    track->isStopping_2() || track->isPaused()) {
461481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // We have consumed all the buffers of this track.
461581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Remove it from the list of active tracks.
4616ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                size_t audioHALFrames;
4617ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                if (audio_is_linear_pcm(mFormat)) {
4618ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                    audioHALFrames = (latency_l() * mSampleRate) / 1000;
4619ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                } else {
4620ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                    audioHALFrames = 0;
4621ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                }
4622ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent
462381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                size_t framesWritten = mBytesWritten / mFrameSize;
4624fd4779740ec3e9e865d5514464df26d015354388Eric Laurent                if (mStandby || !last ||
4625fd4779740ec3e9e865d5514464df26d015354388Eric Laurent                        track->presentationComplete(framesWritten, audioHALFrames)) {
4626ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                    if (track->isStopping_2()) {
4627ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                        track->mState = TrackBase::STOPPED;
4628ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                    }
462981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    if (track->isStopped()) {
463081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        track->reset();
463181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    }
4632d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent                    tracksToRemove->add(track);
463381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
463481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            } else {
463581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // No buffers for this track. Give it a few chances to
463681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // fill a buffer, then remove it from active list.
4637d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent                // Only consider last track started for mixer state control
463881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (--(track->mRetryCount) <= 0) {
463981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name());
4640d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent                    tracksToRemove->add(track);
4641a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent                    // indicate to client process that the track was disabled because of underrun;
4642a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent                    // it will then automatically call start() when data is available
4643a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent                    android_atomic_or(CBLK_DISABLED, &cblk->mFlags);
4644bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                } else if (last) {
464581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mixerStatus = MIXER_TRACKS_ENABLED;
46465cff403679fc44c8293de81aed31c459c6129243Eric Laurent                    if (mHwSupportsPause && !mHwPaused && !mStandby) {
46470f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent                        doHwPause = true;
46480f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent                        mHwPaused = true;
46490f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent                    }
465081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
465181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
465281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
465381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
465481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4655d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    // if an active track did not command a flush, check for pending flush on stopped tracks
465643b4dcc660e6da96285e4672ae371070ab845401Phil Burk    if (!mFlushPending) {
4657d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent        for (size_t i = 0; i < mTracks.size(); i++) {
4658d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent            if (mTracks[i]->isFlushPending()) {
4659d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent                mTracks[i]->flushAck();
466043b4dcc660e6da96285e4672ae371070ab845401Phil Burk                mFlushPending = true;
4661d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent            }
4662d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent        }
4663d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    }
4664d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent
4665d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    // make sure the pause/flush/resume sequence is executed in the right order.
4666d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    // If a flush is pending and a track is active but the HW is not paused, force a HW pause
4667d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    // before flush and then resume HW. This can happen in case of pause/flush/resume
4668d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    // if resume is received before pause is executed.
4669d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    if (mHwSupportsPause && !mStandby &&
467043b4dcc660e6da96285e4672ae371070ab845401Phil Burk            (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) {
4671d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent        mOutput->stream->pause(mOutput->stream);
4672d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    }
467343b4dcc660e6da96285e4672ae371070ab845401Phil Burk    if (mFlushPending) {
4674d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent        flushHw_l();
4675d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    }
4676d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    if (mHwSupportsPause && !mStandby && doHwResume) {
4677d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent        mOutput->stream->resume(mOutput->stream);
4678d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    }
467981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // remove all the tracks that need to be...
4680bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    removeTracks_l(*tracksToRemove);
468181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
468281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mixerStatus;
468381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
468481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
468581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DirectOutputThread::threadLoop_mix()
468681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
468781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t frameCount = mFrameCount;
46882098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung    int8_t *curBuf = (int8_t *)mSinkBuffer;
468981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // output audio to hardware
469081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    while (frameCount) {
469134542acfa25c6413c87a94b6f7cc315a0c496277Glenn Kasten        AudioBufferProvider::Buffer buffer;
469281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        buffer.frameCount = frameCount;
4693062e67a26e0553dd142be622821f493df541f0c6Phil Burk        status_t status = mActiveTrack->getNextBuffer(&buffer);
4694062e67a26e0553dd142be622821f493df541f0c6Phil Burk        if (status != NO_ERROR || buffer.raw == NULL) {
469581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            memset(curBuf, 0, frameCount * mFrameSize);
469681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
469781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
469881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize);
469981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        frameCount -= buffer.frameCount;
470081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        curBuf += buffer.frameCount * mFrameSize;
470181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mActiveTrack->releaseBuffer(&buffer);
470281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
47032098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung    mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer;
4704ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    mSleepTimeUs = 0;
4705ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    mStandbyTimeNs = systemTime() + mStandbyDelayNs;
470681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mActiveTrack.clear();
470781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
470881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
470981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DirectOutputThread::threadLoop_sleepTime()
471081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
4711d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    // do not write to HAL when paused
47120f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent    if (mHwPaused || (usesHwAvSync() && mStandby)) {
4713ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent        mSleepTimeUs = mIdleSleepTimeUs;
4714d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent        return;
4715d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    }
4716ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    if (mSleepTimeUs == 0) {
471781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (mMixerStatus == MIXER_TRACKS_ENABLED) {
4718ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent            mSleepTimeUs = mActiveSleepTimeUs;
471981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
4720ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent            mSleepTimeUs = mIdleSleepTimeUs;
472181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
472281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) {
47232098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung        memset(mSinkBuffer, 0, mFrameCount * mFrameSize);
4724ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent        mSleepTimeUs = 0;
472581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
472681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
472781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4728d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurentvoid AudioFlinger::DirectOutputThread::threadLoop_exit()
4729d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent{
4730d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    {
4731d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent        Mutex::Autolock _l(mLock);
4732d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent        for (size_t i = 0; i < mTracks.size(); i++) {
4733d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent            if (mTracks[i]->isFlushPending()) {
4734d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent                mTracks[i]->flushAck();
473543b4dcc660e6da96285e4672ae371070ab845401Phil Burk                mFlushPending = true;
4736d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent            }
4737d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent        }
473843b4dcc660e6da96285e4672ae371070ab845401Phil Burk        if (mFlushPending) {
4739d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent            flushHw_l();
4740d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent        }
4741d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    }
4742d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    PlaybackThread::threadLoop_exit();
4743d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent}
4744d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent
4745d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent// must be called with thread mutex locked
4746d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurentbool AudioFlinger::DirectOutputThread::shouldStandby_l()
4747d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent{
4748d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    bool trackPaused = false;
4749b369cafd67beb63dd0278dba543f519956208a7fEric Laurent    bool trackStopped = false;
4750d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent
4751d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack
4752d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    // after a timeout and we will enter standby then.
4753d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    if (mTracks.size() > 0) {
4754d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent        trackPaused = mTracks[mTracks.size() - 1]->isPaused();
4755b369cafd67beb63dd0278dba543f519956208a7fEric Laurent        trackStopped = mTracks[mTracks.size() - 1]->isStopped() ||
4756b369cafd67beb63dd0278dba543f519956208a7fEric Laurent                           mTracks[mTracks.size() - 1]->mState == TrackBase::IDLE;
4757d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    }
4758d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent
47595cff403679fc44c8293de81aed31c459c6129243Eric Laurent    return !mStandby && !(trackPaused || (mHwPaused && !trackStopped));
4760d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent}
4761d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent
476281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// getTrackName_l() must be called with ThreadBase::mLock held
47630f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenint AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask __unused,
4764e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung        audio_format_t format __unused, int sessionId __unused)
476581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
476681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return 0;
476781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
476881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
476981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// deleteTrackName_l() must be called with ThreadBase::mLock held
47700f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::DirectOutputThread::deleteTrackName_l(int name __unused)
477181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
477281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
477381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
47741035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// checkForNewParameter_l() must be called with ThreadBase::mLock held
47751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentbool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair,
47761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                              status_t& status)
477781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
477881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool reconfig = false;
477981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
47801035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    status = NO_ERROR;
47811035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
47821035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    AudioParameter param = AudioParameter(keyValuePair);
47831035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    int value;
47841035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
47851035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // forward device change to effects that have requested to be
47861035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // aware of attached audio device.
47871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (value != AUDIO_DEVICE_NONE) {
47881035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mOutDevice = value;
47891035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            for (size_t i = 0; i < mEffectChains.size(); i++) {
47901035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                mEffectChains[i]->setDevice_l(mOutDevice);
4791c125f38cd0ae35409a01b98a99e483550daa1313Glenn Kasten            }
4792c125f38cd0ae35409a01b98a99e483550daa1313Glenn Kasten        }
47931035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
47941035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
47951035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // do not accept frame count changes if tracks are open as the track buffer
47961035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // size depends on frame count and correct behavior would not be garantied
47971035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // if frame count is changed after track creation
47981035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (!mTracks.isEmpty()) {
47991035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            status = INVALID_OPERATION;
48001035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        } else {
48011035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            reconfig = true;
480281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
48031035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
48041035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (status == NO_ERROR) {
48051035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
48061035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                keyValuePair.string());
48071035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (!mStandby && status == INVALID_OPERATION) {
4808062e67a26e0553dd142be622821f493df541f0c6Phil Burk            mOutput->standby();
48091035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mStandby = true;
48101035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mBytesWritten = 0;
481181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
48121035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                   keyValuePair.string());
48131035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
48141035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (status == NO_ERROR && reconfig) {
48151035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            readOutputParameters_l();
481673e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent            sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED);
481781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
481881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
48191035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
482081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return reconfig;
482181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
482281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
482381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const
482481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
482581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t time;
482681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (audio_is_linear_pcm(mFormat)) {
482781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        time = PlaybackThread::activeSleepTimeUs();
482881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
482981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        time = 10000;
483081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
483181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return time;
483281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
483381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
483481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const
483581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
483681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t time;
483781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (audio_is_linear_pcm(mFormat)) {
483881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2;
483981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
484081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        time = 10000;
484181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
484281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return time;
484381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
484481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
484581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const
484681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
484781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t time;
484881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (audio_is_linear_pcm(mFormat)) {
484981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000);
485081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
485181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        time = 10000;
485281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
485381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return time;
485481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
485581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
485681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DirectOutputThread::cacheParameters_l()
485781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
485881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread::cacheParameters_l();
485981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
486081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // use shorter standby delay as on normal output to release
486181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // hardware resources as soon as possible
4862b369cafd67beb63dd0278dba543f519956208a7fEric Laurent    // no delay on outputs with HW A/V sync
4863b369cafd67beb63dd0278dba543f519956208a7fEric Laurent    if (usesHwAvSync()) {
4864ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent        mStandbyDelayNs = 0;
48655cff403679fc44c8293de81aed31c459c6129243Eric Laurent    } else if ((mType == OFFLOAD) && !audio_is_linear_pcm(mFormat)) {
4866ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent        mStandbyDelayNs = kOffloadStandbyDelayNs;
48675cff403679fc44c8293de81aed31c459c6129243Eric Laurent    } else {
4868ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent        mStandbyDelayNs = microseconds(mActiveSleepTimeUs*2);
4869972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent    }
487081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
487181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4872e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurentvoid AudioFlinger::DirectOutputThread::flushHw_l()
4873e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent{
4874062e67a26e0553dd142be622821f493df541f0c6Phil Burk    mOutput->flush();
4875d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    mHwPaused = false;
487643b4dcc660e6da96285e4672ae371070ab845401Phil Burk    mFlushPending = false;
4877e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent}
4878e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent
487981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
488081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4881bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::AsyncCallbackThread::AsyncCallbackThread(
48824de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent        const wp<AudioFlinger::PlaybackThread>& playbackThread)
4883bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    :   Thread(false /*canCallJava*/),
48844de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent        mPlaybackThread(playbackThread),
48853b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mWriteAckSequence(0),
48863b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mDrainSequence(0)
4887bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
4888bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
4889bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4890bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::AsyncCallbackThread::~AsyncCallbackThread()
4891bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
4892bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
4893bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4894bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::AsyncCallbackThread::onFirstRef()
4895bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
4896bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO);
4897bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
4898bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4899bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::AsyncCallbackThread::threadLoop()
4900bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
4901bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    while (!exitPending()) {
49023b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        uint32_t writeAckSequence;
49033b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        uint32_t drainSequence;
4904bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4905bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        {
4906bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            Mutex::Autolock _l(mLock);
490724a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George            while (!((mWriteAckSequence & 1) ||
490824a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George                     (mDrainSequence & 1) ||
490924a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George                     exitPending())) {
491024a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George                mWaitWorkCV.wait(mLock);
491124a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George            }
491224a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George
4913bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (exitPending()) {
4914bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                break;
4915bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
49163b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d",
49173b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent                  mWriteAckSequence, mDrainSequence);
49183b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            writeAckSequence = mWriteAckSequence;
49193b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            mWriteAckSequence &= ~1;
49203b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            drainSequence = mDrainSequence;
49213b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            mDrainSequence &= ~1;
4922bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
4923bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        {
49244de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent            sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote();
49254de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent            if (playbackThread != 0) {
49263b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent                if (writeAckSequence & 1) {
49274de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent                    playbackThread->resetWriteBlocked(writeAckSequence >> 1);
4928bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
49293b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent                if (drainSequence & 1) {
49304de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent                    playbackThread->resetDraining(drainSequence >> 1);
4931bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
4932bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
4933bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
4934bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
4935bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    return false;
4936bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
4937bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4938bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::AsyncCallbackThread::exit()
4939bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
4940bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ALOGV("AsyncCallbackThread::exit");
4941bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    Mutex::Autolock _l(mLock);
4942bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    requestExit();
4943bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    mWaitWorkCV.broadcast();
4944bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
4945bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
49463b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence)
4947bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
4948bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    Mutex::Autolock _l(mLock);
49493b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // bit 0 is cleared
49503b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    mWriteAckSequence = sequence << 1;
49513b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent}
49523b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent
49533b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::AsyncCallbackThread::resetWriteBlocked()
49543b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent{
49553b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    Mutex::Autolock _l(mLock);
49563b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // ignore unexpected callbacks
49573b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    if (mWriteAckSequence & 2) {
49583b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mWriteAckSequence |= 1;
4959bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        mWaitWorkCV.signal();
4960bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
4961bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
4962bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
49633b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence)
49643b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent{
49653b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    Mutex::Autolock _l(mLock);
49663b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // bit 0 is cleared
49673b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    mDrainSequence = sequence << 1;
49683b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent}
49693b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent
49703b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::AsyncCallbackThread::resetDraining()
4971bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
4972bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    Mutex::Autolock _l(mLock);
49733b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // ignore unexpected callbacks
49743b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    if (mDrainSequence & 2) {
49753b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mDrainSequence |= 1;
4976bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        mWaitWorkCV.signal();
4977bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
4978bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
4979bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4980bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4981bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// ----------------------------------------------------------------------------
4982bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger,
498372e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        AudioStreamOut* output, audio_io_handle_t id, uint32_t device, bool systemReady)
498472e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    :   DirectOutputThread(audioFlinger, output, id, device, OFFLOAD, systemReady),
4985d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent        mPausedBytesRemaining(0)
4986bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
4987fd4779740ec3e9e865d5514464df26d015354388Eric Laurent    //FIXME: mStandby should be set to true by ThreadBase constructor
4988fd4779740ec3e9e865d5514464df26d015354388Eric Laurent    mStandby = true;
4989bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
4990bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4991bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::OffloadThread::threadLoop_exit()
4992bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
4993bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if (mFlushPending || mHwPaused) {
4994bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        // If a flush is pending or track was paused, just discard buffered data
4995bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        flushHw_l();
4996bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    } else {
4997bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        mMixerStatus = MIXER_DRAIN_ALL;
4998bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        threadLoop_drain();
4999bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
500056604aa3a56dc8e15532597a0a74b3c7b165e006Uday Gupta    if (mUseAsyncWrite) {
500156604aa3a56dc8e15532597a0a74b3c7b165e006Uday Gupta        ALOG_ASSERT(mCallbackThread != 0);
500256604aa3a56dc8e15532597a0a74b3c7b165e006Uday Gupta        mCallbackThread->exit();
500356604aa3a56dc8e15532597a0a74b3c7b165e006Uday Gupta    }
5004bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    PlaybackThread::threadLoop_exit();
5005bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
5006bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
5007bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l(
5008bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    Vector< sp<Track> > *tracksToRemove
5009bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent)
5010bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
5011bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t count = mActiveTracks.size();
5012bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
5013bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    mixer_state mixerStatus = MIXER_IDLE;
5014972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent    bool doHwPause = false;
5015972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent    bool doHwResume = false;
5016972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent
5017ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent    ALOGV("OffloadThread::prepareTracks_l active tracks %d", count);
5018ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent
5019bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // find out which tracks need to be processed
5020bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    for (size_t i = 0; i < count; i++) {
5021bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        sp<Track> t = mActiveTracks[i].promote();
5022bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        // The track died recently
5023bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (t == 0) {
5024bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            continue;
5025bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
5026bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        Track* const track = t.get();
5027bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        audio_track_cblk_t* cblk = track->cblk();
5028fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        // Only consider last track started for volume and mixer state control.
5029fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        // In theory an older track could underrun and restart after the new one starts
5030fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        // but as we only care about the transition phase between two tracks on a
5031fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        // direct output, it is not a problem to ignore the underrun case.
5032fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        sp<Track> l = mLatestActiveTrack.promote();
5033fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        bool last = l.get() == track;
5034fd4779740ec3e9e865d5514464df26d015354388Eric Laurent
50357844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George        if (track->isInvalid()) {
50367844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George            ALOGW("An invalidated track shouldn't be in active list");
50377844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George            tracksToRemove->add(track);
50387844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George            continue;
50397844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George        }
50407844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George
50417844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George        if (track->mState == TrackBase::IDLE) {
50427844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George            ALOGW("An idle track shouldn't be in active list");
50437844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George            continue;
50447844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George        }
50457844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George
5046bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (track->isPausing()) {
5047bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            track->setPaused();
5048bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (last) {
50495cff403679fc44c8293de81aed31c459c6129243Eric Laurent                if (mHwSupportsPause && !mHwPaused) {
5050972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent                    doHwPause = true;
5051bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    mHwPaused = true;
5052bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
5053bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // If we were part way through writing the mixbuffer to
5054bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // the HAL we must save this until we resume
5055bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // BUG - this will be wrong if a different track is made active,
5056bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // in that case we want to discard the pending data in the
5057bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // mixbuffer and tell the client to present it again when the
5058bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // track is resumed
5059bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                mPausedWriteLength = mCurrentWriteLength;
5060bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                mPausedBytesRemaining = mBytesRemaining;
5061bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                mBytesRemaining = 0;    // stop writing
5062bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
5063bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            tracksToRemove->add(track);
50647844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George        } else if (track->isFlushPending()) {
50657844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George            track->flushAck();
50667844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George            if (last) {
50677844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George                mFlushPending = true;
50687844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George            }
50692d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George        } else if (track->isResumePending()){
50702d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George            track->resumeAck();
50712d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George            if (last) {
50722d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                if (mPausedBytesRemaining) {
50732d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                    // Need to continue write that was interrupted
50742d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                    mCurrentWriteLength = mPausedWriteLength;
50752d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                    mBytesRemaining = mPausedBytesRemaining;
50762d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                    mPausedBytesRemaining = 0;
50772d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                }
50782d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                if (mHwPaused) {
50792d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                    doHwResume = true;
50802d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                    mHwPaused = false;
50812d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                    // threadLoop_mix() will handle the case that we need to
50822d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                    // resume an interrupted write
50832d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                }
50842d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                // enable write to audio HAL
5085ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent                mSleepTimeUs = 0;
50862d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George
50872d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                // Do not handle new data in this iteration even if track->framesReady()
50882d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                mixerStatus = MIXER_TRACKS_ENABLED;
50892d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George            }
50902d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George        }  else if (track->framesReady() && track->isReady() &&
50913b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent                !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) {
5092f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten            ALOGVV("OffloadThread: track %d s=%08x [OK]", track->name(), cblk->mServer);
5093bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (track->mFillingUpStatus == Track::FS_FILLED) {
5094bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                track->mFillingUpStatus = Track::FS_ACTIVE;
50951abbdb4429479975718421c4fef3f79ce7c820e3Eric Laurent                // make sure processVolume_l() will apply new volume even if 0
50961abbdb4429479975718421c4fef3f79ce7c820e3Eric Laurent                mLeftVolFloat = mRightVolFloat = -1.0;
5097bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
5098bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
5099bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (last) {
5100d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent                sp<Track> previousTrack = mPreviousTrack.promote();
5101d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent                if (previousTrack != 0) {
5102d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent                    if (track != previousTrack.get()) {
51039da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                        // Flush any data still being written from last track
51049da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                        mBytesRemaining = 0;
51059da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                        if (mPausedBytesRemaining) {
51069da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                            // Last track was paused so we also need to flush saved
51079da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                            // mixbuffer state and invalidate track so that it will
51089da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                            // re-submit that unwritten data when it is next resumed
51099da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                            mPausedBytesRemaining = 0;
51109da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                            // Invalidate is a bit drastic - would be more efficient
51119da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                            // to have a flag to tell client that some of the
51129da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                            // previously written data was lost
5113d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent                            previousTrack->invalidate();
51149da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                        }
51159da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                        // flush data already sent to the DSP if changing audio session as audio
51169da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                        // comes from a different source. Also invalidate previous track to force a
51179da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                        // seek when resuming.
5118d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent                        if (previousTrack->sessionId() != track->sessionId()) {
5119d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent                            previousTrack->invalidate();
51209da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                        }
51219da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                    }
51229da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                }
51239da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                mPreviousTrack = track;
5124bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // reset retry count
5125bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                track->mRetryCount = kMaxTrackRetriesOffload;
5126bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                mActiveTrack = t;
5127bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                mixerStatus = MIXER_TRACKS_READY;
5128bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
5129bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        } else {
5130f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten            ALOGVV("OffloadThread: track %d s=%08x [NOT READY]", track->name(), cblk->mServer);
5131bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (track->isStopping_1()) {
5132bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // Hardware buffer can hold a large amount of audio so we must
5133bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // wait for all current track's data to drain before we say
5134bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // that the track is stopped.
5135bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                if (mBytesRemaining == 0) {
5136bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    // Only start draining when all data in mixbuffer
5137bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    // has been written
5138bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2");
5139bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    track->mState = TrackBase::STOPPING_2; // so presentation completes after drain
51406a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent                    // do not drain if no data was ever sent to HAL (mStandby == true)
51416a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent                    if (last && !mStandby) {
51421b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent                        // do not modify drain sequence if we are already draining. This happens
51431b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent                        // when resuming from pause after drain.
51441b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent                        if ((mDrainSequence & 1) == 0) {
5145ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent                            mSleepTimeUs = 0;
5146ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent                            mStandbyTimeNs = systemTime() + mStandbyDelayNs;
51471b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent                            mixerStatus = MIXER_DRAIN_TRACK;
51481b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent                            mDrainSequence += 2;
51491b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent                        }
5150bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        if (mHwPaused) {
5151bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                            // It is possible to move from PAUSED to STOPPING_1 without
5152bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                            // a resume so we must ensure hardware is running
51531b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent                            doHwResume = true;
5154bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                            mHwPaused = false;
5155bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        }
5156bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    }
5157bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
5158bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            } else if (track->isStopping_2()) {
51596a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent                // Drain has completed or we are in standby, signal presentation complete
51606a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent                if (!(mDrainSequence & 1) || !last || mStandby) {
5161bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    track->mState = TrackBase::STOPPED;
5162bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    size_t audioHALFrames =
5163bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                            (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000;
5164bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    size_t framesWritten =
5165062e67a26e0553dd142be622821f493df541f0c6Phil Burk                            mBytesWritten / mOutput->getFrameSize();
5166bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    track->presentationComplete(framesWritten, audioHALFrames);
5167bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    track->reset();
5168bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    tracksToRemove->add(track);
5169bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
5170bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            } else {
5171bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // No buffers for this track. Give it a few chances to
5172bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // fill a buffer, then remove it from active list.
5173bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                if (--(track->mRetryCount) <= 0) {
5174bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    ALOGV("OffloadThread: BUFFER TIMEOUT: remove(%d) from active list",
5175bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                          track->name());
5176bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    tracksToRemove->add(track);
5177a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent                    // indicate to client process that the track was disabled because of underrun;
5178a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent                    // it will then automatically call start() when data is available
5179a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent                    android_atomic_or(CBLK_DISABLED, &cblk->mFlags);
5180bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                } else if (last){
5181bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    mixerStatus = MIXER_TRACKS_ENABLED;
5182bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
5183bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
5184bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
5185bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        // compute volume for this track
5186bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        processVolume_l(track, last);
5187bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
51886bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent
5189ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent    // make sure the pause/flush/resume sequence is executed in the right order.
5190ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent    // If a flush is pending and a track is active but the HW is not paused, force a HW pause
5191ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent    // before flush and then resume HW. This can happen in case of pause/flush/resume
5192ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent    // if resume is received before pause is executed.
5193fd4779740ec3e9e865d5514464df26d015354388Eric Laurent    if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) {
5194972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent        mOutput->stream->pause(mOutput->stream);
5195972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent    }
51966bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent    if (mFlushPending) {
51976bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent        flushHw_l();
51986bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent    }
5199fd4779740ec3e9e865d5514464df26d015354388Eric Laurent    if (!mStandby && doHwResume) {
5200972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent        mOutput->stream->resume(mOutput->stream);
5201972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent    }
52026bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent
5203bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // remove all the tracks that need to be...
5204bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    removeTracks_l(*tracksToRemove);
5205bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
5206bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    return mixerStatus;
5207bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
5208bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
5209bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// must be called with thread mutex locked
5210bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::OffloadThread::waitingAsyncCallback_l()
5211bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
52123b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d",
52133b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent          mWriteAckSequence, mDrainSequence);
52143b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) {
5215bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        return true;
5216bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
5217bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    return false;
5218bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
5219bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
5220bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::OffloadThread::waitingAsyncCallback()
5221bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
5222bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    Mutex::Autolock _l(mLock);
5223bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    return waitingAsyncCallback_l();
5224bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
5225bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
5226bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::OffloadThread::flushHw_l()
5227bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
5228e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent    DirectOutputThread::flushHw_l();
5229bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // Flush anything still waiting in the mixbuffer
5230bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    mCurrentWriteLength = 0;
5231bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    mBytesRemaining = 0;
5232bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    mPausedWriteLength = 0;
5233bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    mPausedBytesRemaining = 0;
52340f02f265123b7ef2fd6ac09ff70cde26eb5559adHaynes Mathew George
5235bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if (mUseAsyncWrite) {
52363b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        // discard any pending drain or write ack by incrementing sequence
52373b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mWriteAckSequence = (mWriteAckSequence + 2) & ~1;
52383b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mDrainSequence = (mDrainSequence + 2) & ~1;
5239bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        ALOG_ASSERT(mCallbackThread != 0);
52403b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mCallbackThread->setWriteBlocked(mWriteAckSequence);
52413b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mCallbackThread->setDraining(mDrainSequence);
5242bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
5243bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
5244bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
5245bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// ----------------------------------------------------------------------------
5246bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
524781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger,
524872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        AudioFlinger::MixerThread* mainThread, audio_io_handle_t id, bool systemReady)
524981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    :   MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->outDevice(),
525072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                    systemReady, DUPLICATING),
525181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mWaitTimeMs(UINT_MAX)
525281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
525381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    addOutputTrack(mainThread);
525481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
525581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
525681784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::DuplicatingThread::~DuplicatingThread()
525781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
525881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mOutputTracks.size(); i++) {
525981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mOutputTracks[i]->destroy();
526081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
526181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
526281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
526381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::threadLoop_mix()
526481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
526581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mix buffers...
526681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (outputsReady(outputTracks)) {
526781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mAudioMixer->process(AudioBufferProvider::kInvalidPTS);
526881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
526902b5708776ba2a9b4ff8c09008483aef7dbe38c7Eric Laurent        if (mMixerBufferValid) {
527002b5708776ba2a9b4ff8c09008483aef7dbe38c7Eric Laurent            memset(mMixerBuffer, 0, mMixerBufferSize);
527102b5708776ba2a9b4ff8c09008483aef7dbe38c7Eric Laurent        } else {
527202b5708776ba2a9b4ff8c09008483aef7dbe38c7Eric Laurent            memset(mSinkBuffer, 0, mSinkBufferSize);
527302b5708776ba2a9b4ff8c09008483aef7dbe38c7Eric Laurent        }
527481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
5275ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    mSleepTimeUs = 0;
527681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    writeFrames = mNormalFrameCount;
527725c2dac12114699e90deb1c579cadebce7b91a97Andy Hung    mCurrentWriteLength = mSinkBufferSize;
5278ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    mStandbyTimeNs = systemTime() + mStandbyDelayNs;
527981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
528081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
528181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::threadLoop_sleepTime()
528281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
5283ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    if (mSleepTimeUs == 0) {
528481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (mMixerStatus == MIXER_TRACKS_ENABLED) {
5285ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent            mSleepTimeUs = mActiveSleepTimeUs;
528681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
5287ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent            mSleepTimeUs = mIdleSleepTimeUs;
528881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
528981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else if (mBytesWritten != 0) {
529081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (mMixerStatus == MIXER_TRACKS_ENABLED) {
529181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            writeFrames = mNormalFrameCount;
529225c2dac12114699e90deb1c579cadebce7b91a97Andy Hung            memset(mSinkBuffer, 0, mSinkBufferSize);
529381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
529481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // flush remaining overflow buffers in output tracks
529581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            writeFrames = 0;
529681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
5297ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent        mSleepTimeUs = 0;
529881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
529981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
530081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5301bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentssize_t AudioFlinger::DuplicatingThread::threadLoop_write()
530281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
530381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < outputTracks.size(); i++) {
5304c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung        outputTracks[i]->write(mSinkBuffer, writeFrames);
530581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
53062c3740f01acca69c3e0bcc5e01bb0edc51b6777fEric Laurent    mStandby = false;
530725c2dac12114699e90deb1c579cadebce7b91a97Andy Hung    return (ssize_t)mSinkBufferSize;
530881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
530981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
531081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::threadLoop_standby()
531181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
531281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // DuplicatingThread implements standby by stopping all tracks
531381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < outputTracks.size(); i++) {
531481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        outputTracks[i]->stop();
531581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
531681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
531781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
531881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::saveOutputTracks()
531981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
532081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    outputTracks = mOutputTracks;
532181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
532281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
532381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::clearOutputTracks()
532481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
532581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    outputTracks.clear();
532681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
532781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
532881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread)
532981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
533081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
5331c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung    // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass.
5332c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung    // Adjust for thread->sampleRate() to determine minimum buffer frame count.
5333c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung    // Then triple buffer because Threads do not run synchronously and may not be clock locked.
5334c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung    const size_t frameCount =
5335c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung            3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate());
5336c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung    // TODO: Consider asynchronous sample rate conversion to handle clock disparity
5337c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung    // from different OutputTracks and their associated MixerThreads (e.g. one may
5338c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung    // nearly empty and the other may be dropping data).
5339c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung
5340c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung    sp<OutputTrack> outputTrack = new OutputTrack(thread,
534181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                            this,
534281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                            mSampleRate,
5343c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung                                            mFormat,
534481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                            mChannelMask,
5345462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                                            frameCount,
5346462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                                            IPCThreadState::self()->getCallingUid());
534781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (outputTrack->cblk() != NULL) {
5348223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent        thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f);
534981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mOutputTracks.add(outputTrack);
5350c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung        ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread);
535181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        updateWaitTime_l();
535281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
535381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
535481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
535581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread)
535681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
535781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
535881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mOutputTracks.size(); i++) {
535981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (mOutputTracks[i]->thread() == thread) {
536081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mOutputTracks[i]->destroy();
536181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mOutputTracks.removeAt(i);
536281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            updateWaitTime_l();
5363f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent            if (thread->getOutput() == mOutput) {
5364f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent                mOutput = NULL;
5365f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent            }
536681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return;
536781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
536881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
5369f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent    ALOGV("removeOutputTrack(): unknown thread: %p", thread);
537081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
537181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
537281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// caller must hold mLock
537381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::updateWaitTime_l()
537481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
537581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mWaitTimeMs = UINT_MAX;
537681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mOutputTracks.size(); i++) {
537781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<ThreadBase> strong = mOutputTracks[i]->thread().promote();
537881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (strong != 0) {
537981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate();
538081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (waitTimeMs < mWaitTimeMs) {
538181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mWaitTimeMs = waitTimeMs;
538281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
538381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
538481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
538581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
538681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
538781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
538881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::DuplicatingThread::outputsReady(
538981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const SortedVector< sp<OutputTrack> > &outputTracks)
539081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
539181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < outputTracks.size(); i++) {
539281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<ThreadBase> thread = outputTracks[i]->thread().promote();
539381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == 0) {
539481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p",
539581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    outputTracks[i].get());
539681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return false;
539781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
539881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
539981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // see note at standby() declaration
540081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (playbackThread->standby() && !playbackThread->isSuspended()) {
540181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(),
540281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    thread.get());
540381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return false;
540481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
540581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
540681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return true;
540781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
540881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
540981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const
541081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
541181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return (mWaitTimeMs * 1000) / 2;
541281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
541381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
541481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::cacheParameters_l()
541581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
541681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first
541781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    updateWaitTime_l();
541881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
541981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    MixerThread::cacheParameters_l();
542081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
542181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
542281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
542381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent//      Record
542481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
542581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
542681784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger,
542781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                         AudioStreamIn *input,
542881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                         audio_io_handle_t id,
5429d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent                                         audio_devices_t outDevice,
543072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                                         audio_devices_t inDevice,
543172e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                                         bool systemReady
543246909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
543346909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                                         , const sp<NBAIO_Sink>& teeSink
543446909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
543546909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                                         ) :
543672e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    ThreadBase(audioFlinger, id, outDevice, inDevice, RECORD, systemReady),
54376dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    mInput(input), mActiveTracksGen(0), mRsmpInBuffer(NULL),
5438deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten    // mRsmpInFrames and mRsmpInFramesP2 are set by readInputParameters_l()
54394cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten    mRsmpInRear(0)
544046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
544146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten    , mTeeSink(teeSink)
544246909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
5443b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten    , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize,
5444b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten            "RecordThreadRO", MemoryHeapBase::READ_ONLY))
54456dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    // mFastCapture below
54466dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    , mFastCaptureFutex(0)
54476dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    // mInputSource
54486dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    // mPipeSink
54496dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    // mPipeSource
54506dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    , mPipeFramesP2(0)
54516dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    // mPipeMemory
54526dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    // mFastCaptureNBLogWriter
54536e6704c06d61bc356e30c164081e5bcffb37920cGlenn Kasten    , mFastTrackAvail(false)
545481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
5455d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten    snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id);
5456d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten    mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName);
545781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5458deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten    readInputParameters_l();
54596dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
54606dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    // create an NBAIO source for the HAL input stream, and negotiate
54616dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    mInputSource = new AudioStreamInSource(input->stream);
54626dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    size_t numCounterOffers = 0;
54636dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)};
54646dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    ssize_t index = mInputSource->negotiate(offers, 1, NULL, numCounterOffers);
54656dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    ALOG_ASSERT(index == 0);
54666dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
54676dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    // initialize fast capture depending on configuration
54686dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    bool initFastCapture;
54696dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    switch (kUseFastCapture) {
54706dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    case FastCapture_Never:
54716dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        initFastCapture = false;
54726dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        break;
54736dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    case FastCapture_Always:
54746dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        initFastCapture = true;
54756dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        break;
54766dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    case FastCapture_Static:
54776dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        uint32_t primaryOutputSampleRate;
54786dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        {
54796dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            AutoMutex _l(audioFlinger->mHardwareLock);
54806dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            primaryOutputSampleRate = audioFlinger->mPrimaryOutputSampleRate;
54816dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        }
54826dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        initFastCapture =
54836dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                // either capture sample rate is same as (a reasonable) primary output sample rate
5484db4c031f518ae5806af73756273ff32cd8d0e4f8Andy Hung                ((isMusicRate(primaryOutputSampleRate) &&
54856dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                    (mSampleRate == primaryOutputSampleRate)) ||
54866dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                // or primary output sample rate is unknown, and capture sample rate is reasonable
54876dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                ((primaryOutputSampleRate == 0) &&
5488db4c031f518ae5806af73756273ff32cd8d0e4f8Andy Hung                        isMusicRate(mSampleRate))) &&
54899f81de3452dfb2385bd57dc05456a045174a1ab1Glenn Kasten                // and the buffer size is < 12 ms
54909f81de3452dfb2385bd57dc05456a045174a1ab1Glenn Kasten                (mFrameCount * 1000) / mSampleRate < 12;
54916dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        break;
54926dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    // case FastCapture_Dynamic:
54936dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    }
54946dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
54956dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    if (initFastCapture) {
5496d198b85a163330b03e7507c9e8bfeb5f4d958a6cGlenn Kasten        // create a Pipe for FastCapture to write to, and for us and fast tracks to read from
54976dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        NBAIO_Format format = mInputSource->format();
549849d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        size_t pipeFramesP2 = roundup(mSampleRate / 25);    // double-buffering of 20 ms each
54996dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        size_t pipeSize = pipeFramesP2 * Format_frameSize(format);
55006dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        void *pipeBuffer;
55016dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        const sp<MemoryDealer> roHeap(readOnlyHeap());
55026dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        sp<IMemory> pipeMemory;
55036dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        if ((roHeap == 0) ||
55046dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                (pipeMemory = roHeap->allocate(pipeSize)) == 0 ||
55056dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                (pipeBuffer = pipeMemory->pointer()) == NULL) {
55066dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            ALOGE("not enough memory for pipe buffer size=%zu", pipeSize);
55076dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            goto failed;
55086dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        }
55096dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        // pipe will be shared directly with fast clients, so clear to avoid leaking old information
55106dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        memset(pipeBuffer, 0, pipeSize);
55116dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer);
55126dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        const NBAIO_Format offers[1] = {format};
55136dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        size_t numCounterOffers = 0;
55146dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
55156dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        ALOG_ASSERT(index == 0);
55166dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        mPipeSink = pipe;
55176dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        PipeReader *pipeReader = new PipeReader(*pipe);
55186dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        numCounterOffers = 0;
55196dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
55206dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        ALOG_ASSERT(index == 0);
55216dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        mPipeSource = pipeReader;
55226dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        mPipeFramesP2 = pipeFramesP2;
55236dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        mPipeMemory = pipeMemory;
55246dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
55256dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        // create fast capture
55266dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        mFastCapture = new FastCapture();
55276dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        FastCaptureStateQueue *sq = mFastCapture->sq();
55286dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef STATE_QUEUE_DUMP
55296dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        // FIXME
55306dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif
55316dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        FastCaptureState *state = sq->begin();
55326dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        state->mCblk = NULL;
55336dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        state->mInputSource = mInputSource.get();
55346dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        state->mInputSourceGen++;
55356dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        state->mPipeSink = pipe;
55366dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        state->mPipeSinkGen++;
55376dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        state->mFrameCount = mFrameCount;
55386dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        state->mCommand = FastCaptureState::COLD_IDLE;
55396dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        // already done in constructor initialization list
55406dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        //mFastCaptureFutex = 0;
55416dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        state->mColdFutexAddr = &mFastCaptureFutex;
55426dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        state->mColdGen++;
55436dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        state->mDumpState = &mFastCaptureDumpState;
55446dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef TEE_SINK
55456dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        // FIXME
55466dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif
55476dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture");
55486dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        state->mNBLogWriter = mFastCaptureNBLogWriter.get();
55496dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        sq->end();
55506dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED);
55516dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
55526dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        // start the fast capture
55536dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO);
55546dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        pid_t tid = mFastCapture->getTid();
555572e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer);
55566dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef AUDIO_WATCHDOG
55576dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        // FIXME
55586dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif
55596dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
55606e6704c06d61bc356e30c164081e5bcffb37920cGlenn Kasten        mFastTrackAvail = true;
55616dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    }
55626dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kastenfailed: ;
55636dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
55646dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    // FIXME mNormalSource
556581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
556681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
556781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::RecordThread::~RecordThread()
556881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
55696dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    if (mFastCapture != 0) {
55706dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        FastCaptureStateQueue *sq = mFastCapture->sq();
55716dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        FastCaptureState *state = sq->begin();
55726dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        if (state->mCommand == FastCaptureState::COLD_IDLE) {
55736dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            int32_t old = android_atomic_inc(&mFastCaptureFutex);
55746dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            if (old == -1) {
55756dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1);
55766dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            }
55776dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        }
55786dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        state->mCommand = FastCaptureState::EXIT;
55796dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        sq->end();
55806dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED);
55816dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        mFastCapture->join();
55826dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        mFastCapture.clear();
55836dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    }
55846dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter);
5585481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    mAudioFlinger->unregisterWriter(mNBLogWriter);
55865744661e85981f8a9456bf470e2761235fc026daAndy Hung    free(mRsmpInBuffer);
558781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
558881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
558981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::onFirstRef()
559081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
5591d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten    run(mThreadName, PRIORITY_URGENT_AUDIO);
559281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
559381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
559481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::RecordThread::threadLoop()
559581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
559681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    nsecs_t lastWarning = 0;
559781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
559881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    inputStandBy();
559981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5600f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kastenreacquire_wakelock:
5601f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten    sp<RecordTrack> activeTrack;
56022b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten    int activeTracksGen;
5603f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten    {
5604f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten        Mutex::Autolock _l(mLock);
56052b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten        size_t size = mActiveTracks.size();
56062b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten        activeTracksGen = mActiveTracksGen;
56072b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten        if (size > 0) {
56082b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            // FIXME an arbitrary choice
56092b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            activeTrack = mActiveTracks[0];
56102b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            acquireWakeLock_l(activeTrack->uid());
56112b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            if (size > 1) {
56122b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten                SortedVector<int> tmp;
56132b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten                for (size_t i = 0; i < size; i++) {
56142b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten                    tmp.add(mActiveTracks[i]->uid());
56152b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten                }
56162b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten                updateWakeLockUids_l(tmp);
56172b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            }
56182b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten        } else {
56192b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            acquireWakeLock_l(-1);
56202b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten        }
5621f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten    }
5622f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten
56236dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    // used to request a deferred sleep, to be executed later while mutex is unlocked
56246dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    uint32_t sleepUs = 0;
56256dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
56266dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    // loop while there is work to do
56274ef0b463a56c19bad9197aa9f90d792090461429Glenn Kasten    for (;;) {
5628c527a7c2b1bfd26e8f3086e1b653d56e521379d9Glenn Kasten        Vector< sp<EffectChain> > effectChains;
56292cfbf88b89854f30b295e8ae26a031edb8d712f8Glenn Kasten
56305edadd46c76c5ff1c3edabf2ea943c2278e82e1cGlenn Kasten        // sleep with mutex unlocked
56316dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        if (sleepUs > 0) {
5632e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten            ATRACE_BEGIN("sleep");
56336dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            usleep(sleepUs);
5634e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten            ATRACE_END();
56356dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            sleepUs = 0;
56365edadd46c76c5ff1c3edabf2ea943c2278e82e1cGlenn Kasten        }
56375edadd46c76c5ff1c3edabf2ea943c2278e82e1cGlenn Kasten
56386dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // activeTracks accumulates a copy of a subset of mActiveTracks
56396dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        Vector< sp<RecordTrack> > activeTracks;
56406dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
5641735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten        // reference to the (first and only) active fast track
56426dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        sp<RecordTrack> fastTrack;
56431035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
5644735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten        // reference to a fast track which is about to be removed
5645735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten        sp<RecordTrack> fastTrackToRemove;
5646735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten
564781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        { // scope for mLock
564881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            Mutex::Autolock _l(mLock);
5649000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent
5650021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            processConfigEvents_l();
5651f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten
5652000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent            // check exitPending here because checkForNewParameters_l() and
5653000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent            // checkForNewParameters_l() can temporarily release mLock
5654000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent            if (exitPending()) {
5655000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent                break;
5656000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent            }
5657000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent
56582b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            // if no active track(s), then standby and release wakelock
56592b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            size_t size = mActiveTracks.size();
56602b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            if (size == 0) {
566193e471f620454f7de73d190521568b1e25879767Glenn Kasten                standbyIfNotAlreadyInStandby();
56624ef0b463a56c19bad9197aa9f90d792090461429Glenn Kasten                // exitPending() can't become true here
566381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                releaseWakeLock_l();
566481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGV("RecordThread: loop stopping");
566581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // go to sleep
566681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mWaitWorkCV.wait(mLock);
566781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGV("RecordThread: loop starting");
5668f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten                goto reacquire_wakelock;
5669f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten            }
5670f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten
56712b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            if (mActiveTracksGen != activeTracksGen) {
56722b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten                activeTracksGen = mActiveTracksGen;
5673f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten                SortedVector<int> tmp;
56742b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten                for (size_t i = 0; i < size; i++) {
56752b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten                    tmp.add(mActiveTracks[i]->uid());
56762b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten                }
5677f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten                updateWakeLockUids_l(tmp);
567881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
56799e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten
56806dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            bool doBroadcast = false;
56816dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            for (size_t i = 0; i < size; ) {
56829e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten
56836dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                activeTrack = mActiveTracks[i];
56846dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                if (activeTrack->isTerminated()) {
5685735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten                    if (activeTrack->isFastTrack()) {
5686735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten                        ALOG_ASSERT(fastTrackToRemove == 0);
5687735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten                        fastTrackToRemove = activeTrack;
5688735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten                    }
56896dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    removeTrack_l(activeTrack);
56902b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten                    mActiveTracks.remove(activeTrack);
56912b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten                    mActiveTracksGen++;
56926dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    size--;
56939e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten                    continue;
56949e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten                }
56956dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
56966dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                TrackBase::track_state activeTrackState = activeTrack->mState;
56976dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                switch (activeTrackState) {
56986dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
56996dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                case TrackBase::PAUSING:
57006dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    mActiveTracks.remove(activeTrack);
57016dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    mActiveTracksGen++;
57026dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    doBroadcast = true;
57036dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    size--;
57046dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    continue;
57056dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
57066dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                case TrackBase::STARTING_1:
57076dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    sleepUs = 10000;
57086dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    i++;
57096dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    continue;
57106dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
57116dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                case TrackBase::STARTING_2:
57126dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    doBroadcast = true;
57136dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    mStandby = false;
57149e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten                    activeTrack->mState = TrackBase::ACTIVE;
57156dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    break;
57166dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
57176dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                case TrackBase::ACTIVE:
57186dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    break;
57196dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
57206dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                case TrackBase::IDLE:
57216dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    i++;
57226dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    continue;
57236dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
57246dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                default:
5725adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten                    LOG_ALWAYS_FATAL("Unexpected activeTrackState %d", activeTrackState);
57269e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten                }
57272d94426cd3302cb1215c92c5f1c4b90c24ceb72bGlenn Kasten
57286dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                activeTracks.add(activeTrack);
57296dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                i++;
57302d94426cd3302cb1215c92c5f1c4b90c24ceb72bGlenn Kasten
57316dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                if (activeTrack->isFastTrack()) {
57326dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                    ALOG_ASSERT(!mFastTrackAvail);
57336dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                    ALOG_ASSERT(fastTrack == 0);
57346dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                    fastTrack = activeTrack;
57356dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                }
57366dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            }
57376dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            if (doBroadcast) {
57386dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                mStartStopCond.broadcast();
57396dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            }
5740d9fc34fb0fcfcc739f868b116edf50c62af19d5eGlenn Kasten
57416dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            // sleep if there are no active tracks to process
57426dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            if (activeTracks.size() == 0) {
57436dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                if (sleepUs == 0) {
57446dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    sleepUs = kRecordThreadSleepUs;
57456dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                }
57466dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                continue;
574781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
57486dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            sleepUs = 0;
57499e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten
575081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lockEffectChains_l(effectChains);
575181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
575281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
57536dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0
57547165268ffa6c7b6b405b6afad82e2a346500e8eeGlenn Kasten
57556dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        size_t size = effectChains.size();
57566dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        for (size_t i = 0; i < size; i++) {
57571ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten            // thread mutex is not locked, but effect chain is locked
57581ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten            effectChains[i]->process_l();
57591ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten        }
576081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5761735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten        // Push a new fast capture state if fast capture is not already running, or cblk change
57626dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        if (mFastCapture != 0) {
57636dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            FastCaptureStateQueue *sq = mFastCapture->sq();
57646dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            FastCaptureState *state = sq->begin();
5765735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten            bool didModify = false;
5766735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten            FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED;
57676dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME &&
57686dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                    (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) {
57696dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                if (state->mCommand == FastCaptureState::COLD_IDLE) {
57706dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                    int32_t old = android_atomic_inc(&mFastCaptureFutex);
57716dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                    if (old == -1) {
57726dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                        (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1);
57736dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                    }
57746dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                }
57756dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                state->mCommand = FastCaptureState::READ_WRITE;
57766dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#if 0   // FIXME
57776dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ?
5778fbdb2aceab7317aa44bc8f301a93eb49e17b2bceGlenn Kasten                        FastThreadDumpState::kSamplingNforLowRamDevice :
5779fbdb2aceab7317aa44bc8f301a93eb49e17b2bceGlenn Kasten                        FastThreadDumpState::kSamplingN);
57806dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif
5781735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten                didModify = true;
5782735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten            }
5783735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten            audio_track_cblk_t *cblkOld = state->mCblk;
5784735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten            audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL;
5785735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten            if (cblkNew != cblkOld) {
5786735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten                state->mCblk = cblkNew;
5787735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten                // block until acked if removing a fast track
5788735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten                if (cblkOld != NULL) {
5789735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten                    block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED;
5790735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten                }
5791735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten                didModify = true;
5792735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten            }
5793735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten            sq->end(didModify);
5794735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten            if (didModify) {
5795735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten                sq->push(block);
57966dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#if 0
57976dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                if (kUseFastCapture == FastCapture_Dynamic) {
57986dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                    mNormalSource = mPipeSource;
57996dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                }
58006dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif
58016dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            }
58026dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        }
58036dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
5804735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten        // now run the fast track destructor with thread mutex unlocked
5805735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten        fastTrackToRemove.clear();
5806735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten
58076dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one.
58086dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // Only the client(s) that are too slow will overrun. But if even the fastest client is too
58096dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // slow, then this RecordThread will overrun by not calling HAL read often enough.
58106dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // If destination is non-contiguous, first read past the nominal end of buffer, then
58116dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // copy to the right place.  Permitted because mRsmpInBuffer was over-allocated.
58126dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
58136dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1);
58146dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        ssize_t framesRead;
58156dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
58166dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        // If an NBAIO source is present, use it to read the normal capture's data
58176dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        if (mPipeSource != 0) {
58186dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            size_t framesToRead = mBufferSize / mFrameSize;
58195744661e85981f8a9456bf470e2761235fc026daAndy Hung            framesRead = mPipeSource->read((uint8_t*)mRsmpInBuffer + rear * mFrameSize,
58206dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                    framesToRead, AudioBufferProvider::kInvalidPTS);
58216dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            if (framesRead == 0) {
58226dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                // since pipe is non-blocking, simulate blocking input
58236dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                sleepUs = (framesToRead * 1000000LL) / mSampleRate;
58246dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            }
58256dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        // otherwise use the HAL / AudioStreamIn directly
58266dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        } else {
58276dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            ssize_t bytesRead = mInput->stream->read(mInput->stream,
58285744661e85981f8a9456bf470e2761235fc026daAndy Hung                    (uint8_t*)mRsmpInBuffer + rear * mFrameSize, mBufferSize);
58296dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            if (bytesRead < 0) {
58306dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                framesRead = bytesRead;
58316dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            } else {
58326dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                framesRead = bytesRead / mFrameSize;
58336dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            }
58346dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        }
58356dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
58366dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) {
58376dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            ALOGE("read failed: framesRead=%d", framesRead);
58386dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            // Force input into standby so that it tries to recover at next read attempt
58396dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            inputStandBy();
58406dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            sleepUs = kRecordThreadSleepUs;
58416dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        }
58426dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        if (framesRead <= 0) {
58433d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten            goto unlock;
58446dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        }
58456dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        ALOG_ASSERT(framesRead > 0);
58466dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
58476dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        if (mTeeSink != 0) {
58485744661e85981f8a9456bf470e2761235fc026daAndy Hung            (void) mTeeSink->write((uint8_t*)mRsmpInBuffer + rear * mFrameSize, framesRead);
58496dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        }
58506dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // If destination is non-contiguous, we now correct for reading past end of buffer.
58513d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten        {
58523d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten            size_t part1 = mRsmpInFramesP2 - rear;
58533d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten            if ((size_t) framesRead > part1) {
58545744661e85981f8a9456bf470e2761235fc026daAndy Hung                memcpy(mRsmpInBuffer, (uint8_t*)mRsmpInBuffer + mRsmpInFramesP2 * mFrameSize,
58553d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten                        (framesRead - part1) * mFrameSize);
58563d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten            }
58576dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        }
58586dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        rear = mRsmpInRear += framesRead;
58596dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
58606dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        size = activeTracks.size();
58616dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // loop over each active track
58626dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        for (size_t i = 0; i < size; i++) {
58636dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            activeTrack = activeTracks[i];
58646dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
58656dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // skip fast tracks, as those are handled directly by FastCapture
58666dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            if (activeTrack->isFastTrack()) {
58676dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                continue;
58686dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            }
58696dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
587073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung            // TODO: This code probably should be moved to RecordTrack.
587197a893eb34f8687485c88eaf15917974a203f20bAndy Hung            // TODO: Update the activeTrack buffer converter in case of reconfigure.
587297a893eb34f8687485c88eaf15917974a203f20bAndy Hung
58736dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            enum {
58746dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                OVERRUN_UNKNOWN,
58756dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                OVERRUN_TRUE,
58766dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                OVERRUN_FALSE
58776dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            } overrun = OVERRUN_UNKNOWN;
58786dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
58796dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            // loop over getNextBuffer to handle circular sink
58806dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            for (;;) {
58816dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
58826dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                activeTrack->mSink.frameCount = ~0;
58836dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                status_t status = activeTrack->getNextBuffer(&activeTrack->mSink);
58846dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                size_t framesOut = activeTrack->mSink.frameCount;
58856dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0));
58866dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
588773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung                // check available frames and handle overrun conditions
588873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung                // if the record track isn't draining fast enough.
588973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung                bool hasOverrun;
58906dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                size_t framesIn;
589173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung                activeTrack->mResamplerBufferProvider->sync(&framesIn, &hasOverrun);
589273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung                if (hasOverrun) {
58936dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    overrun = OVERRUN_TRUE;
58946dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                }
58954cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten                if (framesOut == 0 || framesIn == 0) {
58964cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten                    break;
58974cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten                }
58984cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten
58996770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung                // Don't allow framesOut to be larger than what is possible with resampling
59006770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung                // from framesIn.
59016770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung                // This isn't strictly necessary but helps limit buffer resizing in
59026770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung                // RecordBufferConverter.  TODO: remove when no longer needed.
59036770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung                framesOut = min(framesOut,
59046770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung                        destinationFramesPossible(
59056770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung                                framesIn, mSampleRate, activeTrack->mSampleRate));
590697a893eb34f8687485c88eaf15917974a203f20bAndy Hung                // process frames from the RecordThread buffer provider to the RecordTrack buffer
590797a893eb34f8687485c88eaf15917974a203f20bAndy Hung                framesOut = activeTrack->mRecordBufferConverter->convert(
590897a893eb34f8687485c88eaf15917974a203f20bAndy Hung                        activeTrack->mSink.raw, activeTrack->mResamplerBufferProvider, framesOut);
59098594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten
59106dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) {
59116dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    overrun = OVERRUN_FALSE;
59121ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten                }
591381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
59146dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                if (activeTrack->mFramesToDrop == 0) {
59156dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    if (framesOut > 0) {
59166dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        activeTrack->mSink.frameCount = framesOut;
59176dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        activeTrack->releaseBuffer(&activeTrack->mSink);
591881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    }
591981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                } else {
59206dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    // FIXME could do a partial drop of framesOut
59216dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    if (activeTrack->mFramesToDrop > 0) {
59226dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        activeTrack->mFramesToDrop -= framesOut;
59236dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        if (activeTrack->mFramesToDrop <= 0) {
592425f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten                            activeTrack->clearSyncStartEvent();
59256dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        }
59266dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    } else {
59276dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        activeTrack->mFramesToDrop += framesOut;
59286dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 ||
59296dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                                activeTrack->mSyncStartEvent->isCancelled()) {
59306dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                            ALOGW("Synced record %s, session %d, trigger session %d",
59316dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                                  (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled",
59326dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                                  activeTrack->sessionId(),
59336dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                                  (activeTrack->mSyncStartEvent != 0) ?
59346dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                                          activeTrack->mSyncStartEvent->triggerSession() : 0);
593525f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten                            activeTrack->clearSyncStartEvent();
59366dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        }
593781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    }
593881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
59396dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
59406dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                if (framesOut == 0) {
59416dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    break;
59426dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                }
594381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
59446dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
59456dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            switch (overrun) {
59466dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            case OVERRUN_TRUE:
59476dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                // client isn't retrieving buffers fast enough
59486dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                if (!activeTrack->setOverflow()) {
59496dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    nsecs_t now = systemTime();
59506dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    // FIXME should lastWarning per track?
59516dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    if ((now - lastWarning) > kWarningThrottleNs) {
59526dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        ALOGW("RecordThread: buffer overflow");
59536dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        lastWarning = now;
59546dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    }
595581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
59566dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                break;
59576dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            case OVERRUN_FALSE:
59586dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                activeTrack->clearOverflow();
59596dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                break;
59606dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            case OVERRUN_UNKNOWN:
59616dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                break;
596281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
59636dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
596481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
59651ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten
59663d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kastenunlock:
596781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // enable changes in effect chain
596881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        unlockEffectChains(effectChains);
5969c527a7c2b1bfd26e8f3086e1b653d56e521379d9Glenn Kasten        // effectChains doesn't need to be cleared, since it is cleared by destructor at scope end
597081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
597181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
597293e471f620454f7de73d190521568b1e25879767Glenn Kasten    standbyIfNotAlreadyInStandby();
597381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
597481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
597581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
59769a54bc27876acd5d8be5b1fc3dc46701fe76fbb3Eric Laurent        for (size_t i = 0; i < mTracks.size(); i++) {
59779a54bc27876acd5d8be5b1fc3dc46701fe76fbb3Eric Laurent            sp<RecordTrack> track = mTracks[i];
59789a54bc27876acd5d8be5b1fc3dc46701fe76fbb3Eric Laurent            track->invalidate();
59799a54bc27876acd5d8be5b1fc3dc46701fe76fbb3Eric Laurent        }
59802b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten        mActiveTracks.clear();
59812b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten        mActiveTracksGen++;
598281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mStartStopCond.broadcast();
598381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
598481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
598581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    releaseWakeLock();
598681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
598781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("RecordThread %p exiting", this);
598881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return false;
598981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
599081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
599193e471f620454f7de73d190521568b1e25879767Glenn Kastenvoid AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby()
599281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
599381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!mStandby) {
599481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        inputStandBy();
599581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mStandby = true;
599681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
599781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
599881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
599981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::inputStandBy()
600081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
60016dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    // Idle the fast capture if it's currently running
60026dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    if (mFastCapture != 0) {
60036dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        FastCaptureStateQueue *sq = mFastCapture->sq();
60046dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        FastCaptureState *state = sq->begin();
60056dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        if (!(state->mCommand & FastCaptureState::IDLE)) {
60066dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            state->mCommand = FastCaptureState::COLD_IDLE;
60076dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            state->mColdFutexAddr = &mFastCaptureFutex;
60086dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            state->mColdGen++;
60096dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            mFastCaptureFutex = 0;
60106dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sq->end();
60116dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now
60126dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED);
60136dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#if 0
60146dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            if (kUseFastCapture == FastCapture_Dynamic) {
60156dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                // FIXME
60166dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            }
60176dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif
60186dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef AUDIO_WATCHDOG
60196dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // FIXME
60206dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif
60216dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        } else {
60226dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sq->end(false /*didModify*/);
60236dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        }
60246dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    }
602581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mInput->stream->common.standby(&mInput->stream->common);
602681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
602781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
602805997e21af6c4517f375def6563af4b9ebe95f39Glenn Kasten// RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held
6029e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kastensp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l(
603081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const sp<AudioFlinger::Client>& client,
603181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t sampleRate,
603281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_format_t format,
603381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_channel_mask_t channelMask,
603474935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten        size_t *pFrameCount,
603581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int sessionId,
60367df8c0b799d8f52d6386e03313286dbd7d5cdc7cGlenn Kasten        size_t *notificationFrames,
6037462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        int uid,
6038ddb0ccf3fb6fe8da8c71a6deb30561b821f3c0a2Glenn Kasten        IAudioFlinger::track_flags_t *flags,
603981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        pid_t tid,
604081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status_t *status)
604181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
604274935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten    size_t frameCount = *pFrameCount;
604381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<RecordTrack> track;
604481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t lStatus;
604581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
604690e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten    // client expresses a preference for FAST, but we get the final say
604790e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten    if (*flags & IAudioFlinger::TRACK_FAST) {
604890e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten      if (
6049b7fbf7ecc6b034243ec64f79f3113675b5e3c941Glenn Kasten            // we formerly checked for a callback handler (non-0 tid),
6050b7fbf7ecc6b034243ec64f79f3113675b5e3c941Glenn Kasten            // but that is no longer required for TRANSFER_OBTAIN mode
6051b7fbf7ecc6b034243ec64f79f3113675b5e3c941Glenn Kasten            //
60527410591dad836434c72ddee66680802708b70c10Glenn Kasten            // frame count is not specified, or is exactly the pipe depth
60537410591dad836434c72ddee66680802708b70c10Glenn Kasten            ((frameCount == 0) || (frameCount == mPipeFramesP2)) &&
60543a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten            // PCM data
60553a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten            audio_is_linear_pcm(format) &&
60566dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // native format
60576dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            (format == mFormat) &&
60586dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // native channel mask
60596dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            (channelMask == mChannelMask) &&
60606dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // native hardware sample rate
606190e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten            (sampleRate == mSampleRate) &&
60623a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten            // record thread has an associated fast capture
60636dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            hasFastCapture() &&
60646dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // there are sufficient fast track slots available
60656dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            mFastTrackAvail
606690e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten        ) {
60677410591dad836434c72ddee66680802708b70c10Glenn Kasten        ALOGV("AUDIO_INPUT_FLAG_FAST accepted: frameCount=%u mFrameCount=%u",
606890e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten                frameCount, mFrameCount);
606990e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten      } else {
60707410591dad836434c72ddee66680802708b70c10Glenn Kasten        ALOGV("AUDIO_INPUT_FLAG_FAST denied: frameCount=%u mFrameCount=%u mPipeFramesP2=%u "
60717410591dad836434c72ddee66680802708b70c10Glenn Kasten                "format=%#x isLinear=%d channelMask=%#x sampleRate=%u mSampleRate=%u "
60726dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                "hasFastCapture=%d tid=%d mFastTrackAvail=%d",
60737410591dad836434c72ddee66680802708b70c10Glenn Kasten                frameCount, mFrameCount, mPipeFramesP2,
60747410591dad836434c72ddee66680802708b70c10Glenn Kasten                format, audio_is_linear_pcm(format), channelMask, sampleRate, mSampleRate,
60757410591dad836434c72ddee66680802708b70c10Glenn Kasten                hasFastCapture(), tid, mFastTrackAvail);
607690e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten        *flags &= ~IAudioFlinger::TRACK_FAST;
60777410591dad836434c72ddee66680802708b70c10Glenn Kasten      }
60787410591dad836434c72ddee66680802708b70c10Glenn Kasten    }
60797410591dad836434c72ddee66680802708b70c10Glenn Kasten
60807410591dad836434c72ddee66680802708b70c10Glenn Kasten    // compute track buffer size in frames, and suggest the notification frame count
60817410591dad836434c72ddee66680802708b70c10Glenn Kasten    if (*flags & IAudioFlinger::TRACK_FAST) {
60827410591dad836434c72ddee66680802708b70c10Glenn Kasten        // fast track: frame count is exactly the pipe depth
60837410591dad836434c72ddee66680802708b70c10Glenn Kasten        frameCount = mPipeFramesP2;
60847410591dad836434c72ddee66680802708b70c10Glenn Kasten        // ignore requested notificationFrames, and always notify exactly once every HAL buffer
60857410591dad836434c72ddee66680802708b70c10Glenn Kasten        *notificationFrames = mFrameCount;
60867410591dad836434c72ddee66680802708b70c10Glenn Kasten    } else {
608749d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        // not fast track: max notification period is resampled equivalent of one HAL buffer time
608849d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        //                 or 20 ms if there is a fast capture
608949d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        // TODO This could be a roundupRatio inline, and const
609049d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount)
609149d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten                * sampleRate + mSampleRate - 1) / mSampleRate;
609249d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        // minimum number of notification periods is at least kMinNotifications,
609349d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs)
609449d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        static const size_t kMinNotifications = 3;
609549d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        static const uint32_t kMinMs = 30;
609649d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        // TODO This could be a roundupRatio inline
609749d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000;
609849d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        // TODO This could be a roundupRatio inline
609949d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) /
610049d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten                maxNotificationFrames;
610149d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        const size_t minFrameCount = maxNotificationFrames *
610249d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten                max(kMinNotifications, minNotificationsByMs);
610349d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        frameCount = max(frameCount, minFrameCount);
610449d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        if (*notificationFrames == 0 || *notificationFrames > maxNotificationFrames) {
610549d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten            *notificationFrames = maxNotificationFrames;
61067410591dad836434c72ddee66680802708b70c10Glenn Kasten        }
610790e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten    }
610874935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten    *pFrameCount = frameCount;
610990e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten
611015e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten    lStatus = initCheck();
611115e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten    if (lStatus != NO_ERROR) {
611215e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten        ALOGE("createRecordTrack_l() audio driver not initialized");
611315e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten        goto Exit;
611415e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten    }
611581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
611681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    { // scope for mLock
611781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
611881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
611981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        track = new RecordTrack(this, client, sampleRate,
612083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                      format, channelMask, frameCount, NULL, sessionId, uid,
612183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                      *flags, TrackBase::TYPE_DEFAULT);
612281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6123030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten        lStatus = track->initCheck();
6124030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten        if (lStatus != NO_ERROR) {
612535295078ab59c8c5d143a54d5a55557c3ca62c51Glenn Kasten            ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus);
612603e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George            // track must be cleared from the caller as the caller has the AF lock
612781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
612881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
612981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mTracks.add(track);
613081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
613181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
613281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        bool suspend = audio_is_bluetooth_sco_device(mInDevice) &&
613381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        mAudioFlinger->btNrecIsOff();
613481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        setEffectSuspended_l(FX_IID_AEC, suspend, sessionId);
613581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        setEffectSuspended_l(FX_IID_NS, suspend, sessionId);
613690e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten
613790e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten        if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) {
613890e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten            pid_t callingPid = IPCThreadState::self()->getCallingPid();
613990e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten            // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful,
614090e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten            // so ask activity manager to do this on our behalf
614190e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten            sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp);
614290e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten        }
614381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
614405997e21af6c4517f375def6563af4b9ebe95f39Glenn Kasten
614581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    lStatus = NO_ERROR;
614681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
614781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit:
61489156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
614981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return track;
615081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
615181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
615281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack,
615381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                           AudioSystem::sync_event_t event,
615481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                           int triggerSession)
615581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
615681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession);
615781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<ThreadBase> strongMe = this;
615881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t status = NO_ERROR;
615981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
616081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (event == AudioSystem::SYNC_EVENT_NONE) {
616125f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten        recordTrack->clearSyncStartEvent();
616281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else if (event != AudioSystem::SYNC_EVENT_SAME) {
61636dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event,
616481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                       triggerSession,
616581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                       recordTrack->sessionId(),
616681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                       syncStartEventCallback,
61676dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                                       recordTrack);
616881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Sync event can be cancelled by the trigger session if the track is not in a
616981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // compatible state in which case we start record immediately
61706dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        if (recordTrack->mSyncStartEvent->isCancelled()) {
617125f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten            recordTrack->clearSyncStartEvent();
617281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
617381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs
61746dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            recordTrack->mFramesToDrop = -
61754cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten                    ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000);
617681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
617781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
617881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
617981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
618047c2070b2ce8aedb7300c0aad91caccf3c383841Glenn Kasten        // This section is a rendezvous between binder thread executing start() and RecordThread
618181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AutoMutex lock(mLock);
61826dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        if (mActiveTracks.indexOf(recordTrack) >= 0) {
61836dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            if (recordTrack->mState == TrackBase::PAUSING) {
61846dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                ALOGV("active record track PAUSING -> ACTIVE");
6185f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten                recordTrack->mState = TrackBase::ACTIVE;
61866dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            } else {
61876dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                ALOGV("active record track state %d", recordTrack->mState);
618881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
618981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return status;
619081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
619181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
61924cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten        // TODO consider other ways of handling this, such as changing the state to :STARTING and
61934cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten        //      adding the track to mActiveTracks after returning from AudioSystem::startInput(),
61944cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten        //      or using a separate command thread
61956dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        recordTrack->mState = TrackBase::STARTING_1;
61962b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten        mActiveTracks.add(recordTrack);
61972b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten        mActiveTracksGen++;
619883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        status_t status = NO_ERROR;
619983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        if (recordTrack->isExternalTrack()) {
620083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            mLock.unlock();
62014dc680607181e6a76f4e91a39366c4f5dfb7b03eEric Laurent            status = AudioSystem::startInput(mId, (audio_session_t)recordTrack->sessionId());
620283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            mLock.lock();
620383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            // FIXME should verify that recordTrack is still in mActiveTracks
620483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            if (status != NO_ERROR) {
620583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                mActiveTracks.remove(recordTrack);
620683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                mActiveTracksGen++;
620783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                recordTrack->clearSyncStartEvent();
620883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                ALOGV("RecordThread::start error %d", status);
620983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                return status;
621083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            }
621181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
62126dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // Catch up with current buffer indices if thread is already running.
62136dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // This is what makes a new client discard all buffered data.  If the track's mRsmpInFront
62146dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // was initialized to some value closer to the thread's mRsmpInFront, then the track could
62156dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // see previously buffered data before it called start(), but with greater risk of overrun.
62166dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
621773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        recordTrack->mResamplerBufferProvider->reset();
621897a893eb34f8687485c88eaf15917974a203f20bAndy Hung        // clear any converter state as new data will be discontinuous
621997a893eb34f8687485c88eaf15917974a203f20bAndy Hung        recordTrack->mRecordBufferConverter->reset();
62206dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        recordTrack->mState = TrackBase::STARTING_2;
622181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // signal thread to start
622281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mWaitWorkCV.broadcast();
62232b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten        if (mActiveTracks.indexOf(recordTrack) < 0) {
622481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("Record failed to start");
622581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status = BAD_VALUE;
622681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto startError;
622781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
622881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return status;
622981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
62307c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten
623181784c37c61b09289654b979567a42bf73cd2b12Eric LaurentstartError:
623283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    if (recordTrack->isExternalTrack()) {
62334dc680607181e6a76f4e91a39366c4f5dfb7b03eEric Laurent        AudioSystem::stopInput(mId, (audio_session_t)recordTrack->sessionId());
623483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    }
623525f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten    recordTrack->clearSyncStartEvent();
62366dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    // FIXME I wonder why we do not reset the state here?
623781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return status;
623881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
623981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
624081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event)
624181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
624281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<SyncEvent> strongEvent = event.promote();
624381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
624481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (strongEvent != 0) {
62458ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent        sp<RefBase> ptr = strongEvent->cookie().promote();
62468ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent        if (ptr != 0) {
62478ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent            RecordTrack *recordTrack = (RecordTrack *)ptr.get();
62488ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent            recordTrack->handleSyncStartEvent(strongEvent);
62498ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent        }
625081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
625181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
625281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6253a8356f663014e7d4c27869629af83d8bb3441e19Glenn Kastenbool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) {
625481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("RecordThread::stop");
6255a8356f663014e7d4c27869629af83d8bb3441e19Glenn Kasten    AutoMutex _l(mLock);
62562b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten    if (mActiveTracks.indexOf(recordTrack) != 0 || recordTrack->mState == TrackBase::PAUSING) {
625781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return false;
625881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
625947c2070b2ce8aedb7300c0aad91caccf3c383841Glenn Kasten    // note that threadLoop may still be processing the track at this point [without lock]
626081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    recordTrack->mState = TrackBase::PAUSING;
626181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // do not wait for mStartStopCond if exiting
626281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (exitPending()) {
626381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return true;
626481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
626547c2070b2ce8aedb7300c0aad91caccf3c383841Glenn Kasten    // FIXME incorrect usage of wait: no explicit predicate or loop
626681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mStartStopCond.wait(mLock);
62672b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten    // if we have been restarted, recordTrack is in mActiveTracks here
62682b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten    if (exitPending() || mActiveTracks.indexOf(recordTrack) != 0) {
626981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("Record stopped OK");
627081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return true;
627181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
627281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return false;
627381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
627481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
62750f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenbool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const
627681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
627781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return false;
627881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
627981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
62800f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenstatus_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused)
628181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
628281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#if 0   // This branch is currently dead code, but is preserved in case it will be needed in future
628381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!isValidSyncEvent(event)) {
628481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
628581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
628681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
628781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int eventSession = event->triggerSession();
628881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t ret = NAME_NOT_FOUND;
628981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
629081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
629181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
629281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mTracks.size(); i++) {
629381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<RecordTrack> track = mTracks[i];
629481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (eventSession == track->sessionId()) {
629581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (void) track->setSyncEvent(event);
629681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ret = NO_ERROR;
629781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
629881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
629981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return ret;
630081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#else
630181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return BAD_VALUE;
630281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
630381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
630481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
630581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// destroyTrack_l() must be called with ThreadBase::mLock held
630681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track)
630781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
6308bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    track->terminate();
6309bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    track->mState = TrackBase::STOPPED;
631081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // active tracks are removed by threadLoop()
63112b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten    if (mActiveTracks.indexOf(track) < 0) {
631281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        removeTrack_l(track);
631381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
631481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
631581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
631681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track)
631781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
631881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mTracks.remove(track);
631981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // need anything related to effects here?
63206dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    if (track->isFastTrack()) {
63216dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        ALOG_ASSERT(!mFastTrackAvail);
63226dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        mFastTrackAvail = true;
63236dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    }
632481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
632581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
632681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args)
632781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
632881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    dumpInternals(fd, args);
632981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    dumpTracks(fd, args);
633081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    dumpEffectChains(fd, args);
633181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
633281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
633381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args)
633481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
633587cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "\nInput thread %p:\n", this);
633681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
633744182c206f7c5584ef2cf504da6be98fab665dbfGlenn Kasten    dumpBase(fd, args);
633844182c206f7c5584ef2cf504da6be98fab665dbfGlenn Kasten
633944182c206f7c5584ef2cf504da6be98fab665dbfGlenn Kasten    if (mActiveTracks.size() == 0) {
634087cebadd48710e42474756fc3513df678de045ceElliott Hughes        dprintf(fd, "  No active record clients\n");
634181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
63426e6704c06d61bc356e30c164081e5bcffb37920cGlenn Kasten    dprintf(fd, "  Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no");
63436dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    dprintf(fd, "  Fast track available: %s\n", mFastTrackAvail ? "yes" : "no");
634417c9c998afed5ed9df7495eeed5822f3ed53ebecGlenn Kasten
634517c9c998afed5ed9df7495eeed5822f3ed53ebecGlenn Kasten    //  Make a non-atomic copy of fast capture dump state so it won't change underneath us
634617c9c998afed5ed9df7495eeed5822f3ed53ebecGlenn Kasten    const FastCaptureDumpState copy(mFastCaptureDumpState);
634717c9c998afed5ed9df7495eeed5822f3ed53ebecGlenn Kasten    copy.dump(fd);
634881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
634981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
63500f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args __unused)
635181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
635281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const size_t SIZE = 256;
635381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    char buffer[SIZE];
635481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    String8 result;
635581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6356b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    size_t numtracks = mTracks.size();
6357b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    size_t numactive = mActiveTracks.size();
6358b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    size_t numactiveseen = 0;
635987cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  %d Tracks", numtracks);
6360b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    if (numtracks) {
636187cebadd48710e42474756fc3513df678de045ceElliott Hughes        dprintf(fd, " of which %d are active\n", numactive);
6362b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        RecordTrack::appendDumpHeader(result);
6363b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        for (size_t i = 0; i < numtracks ; ++i) {
6364b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            sp<RecordTrack> track = mTracks[i];
6365b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            if (track != 0) {
6366b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                bool active = mActiveTracks.indexOf(track) >= 0;
6367b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                if (active) {
6368b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                    numactiveseen++;
6369b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                }
6370b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                track->dump(buffer, SIZE, active);
6371b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                result.append(buffer);
6372b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            }
637381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
6374b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    } else {
637587cebadd48710e42474756fc3513df678de045ceElliott Hughes        dprintf(fd, "\n");
637681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
637781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6378b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    if (numactiveseen != numactive) {
6379b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        snprintf(buffer, SIZE, "  The following tracks are in the active list but"
6380b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                " not in the track list\n");
638181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        result.append(buffer);
638281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        RecordTrack::appendDumpHeader(result);
6383b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        for (size_t i = 0; i < numactive; ++i) {
63842b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            sp<RecordTrack> track = mActiveTracks[i];
6385b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            if (mTracks.indexOf(track) < 0) {
6386b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                track->dump(buffer, SIZE, true);
6387b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                result.append(buffer);
6388b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            }
63892b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten        }
639081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
639181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
639281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    write(fd, result.string(), result.size());
639381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
639481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
639573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung
639673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hungvoid AudioFlinger::RecordThread::ResamplerBufferProvider::reset()
639773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung{
639873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    sp<ThreadBase> threadBase = mRecordTrack->mThread.promote();
639973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    RecordThread *recordThread = (RecordThread *) threadBase.get();
640073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    mRsmpInFront = recordThread->mRsmpInRear;
640173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    mRsmpInUnrel = 0;
640273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung}
640373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung
640473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hungvoid AudioFlinger::RecordThread::ResamplerBufferProvider::sync(
640573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        size_t *framesAvailable, bool *hasOverrun)
640673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung{
640773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    sp<ThreadBase> threadBase = mRecordTrack->mThread.promote();
640873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    RecordThread *recordThread = (RecordThread *) threadBase.get();
640973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    const int32_t rear = recordThread->mRsmpInRear;
641073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    const int32_t front = mRsmpInFront;
641173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    const ssize_t filled = rear - front;
641273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung
641373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    size_t framesIn;
641473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    bool overrun = false;
641573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    if (filled < 0) {
641673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        // should not happen, but treat like a massive overrun and re-sync
641773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        framesIn = 0;
641873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        mRsmpInFront = rear;
641973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        overrun = true;
642073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    } else if ((size_t) filled <= recordThread->mRsmpInFrames) {
642173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        framesIn = (size_t) filled;
642273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    } else {
642373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        // client is not keeping up with server, but give it latest data
642473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        framesIn = recordThread->mRsmpInFrames;
642573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        mRsmpInFront = /* front = */ rear - framesIn;
642673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        overrun = true;
642773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    }
642873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    if (framesAvailable != NULL) {
642973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        *framesAvailable = framesIn;
643073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    }
643173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    if (hasOverrun != NULL) {
643273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        *hasOverrun = overrun;
643373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    }
643473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung}
643573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung
643681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// AudioBufferProvider interface
64376dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kastenstatus_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer(
64386dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        AudioBufferProvider::Buffer* buffer, int64_t pts __unused)
643981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
644073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    sp<ThreadBase> threadBase = mRecordTrack->mThread.promote();
64416dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    if (threadBase == 0) {
64426dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        buffer->frameCount = 0;
6443607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten        buffer->raw = NULL;
64446dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        return NOT_ENOUGH_DATA;
64456dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    }
64466dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    RecordThread *recordThread = (RecordThread *) threadBase.get();
64476dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    int32_t rear = recordThread->mRsmpInRear;
644873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    int32_t front = mRsmpInFront;
64498594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    ssize_t filled = rear - front;
64506dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    // FIXME should not be P2 (don't want to increase latency)
64516dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    // FIXME if client not keeping up, discard
6452607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten    LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames));
64538594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    // 'filled' may be non-contiguous, so return only the first contiguous chunk
64546dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    front &= recordThread->mRsmpInFramesP2 - 1;
64556dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    size_t part1 = recordThread->mRsmpInFramesP2 - front;
64568594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    if (part1 > (size_t) filled) {
64578594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        part1 = filled;
64588594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    }
64598594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    size_t ask = buffer->frameCount;
64608594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    ALOG_ASSERT(ask > 0);
64618594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    if (part1 > ask) {
64628594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        part1 = ask;
64638594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    }
64648594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    if (part1 == 0) {
646573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        // out of data is fine since the resampler will return a short-count.
64668594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        buffer->raw = NULL;
64678594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        buffer->frameCount = 0;
646873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        mRsmpInUnrel = 0;
64698594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        return NOT_ENOUGH_DATA;
64708594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    }
64718594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten
64725744661e85981f8a9456bf470e2761235fc026daAndy Hung    buffer->raw = (uint8_t*)recordThread->mRsmpInBuffer + front * recordThread->mFrameSize;
64738594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    buffer->frameCount = part1;
647473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    mRsmpInUnrel = part1;
647581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
647681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
647781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
647881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// AudioBufferProvider interface
64796dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kastenvoid AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer(
64806dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        AudioBufferProvider::Buffer* buffer)
648181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
64828594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    size_t stepCount = buffer->frameCount;
64838594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    if (stepCount == 0) {
64848594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        return;
64858594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    }
648673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    ALOG_ASSERT(stepCount <= mRsmpInUnrel);
648773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    mRsmpInUnrel -= stepCount;
648873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    mRsmpInFront += stepCount;
64898594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    buffer->raw = NULL;
649081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    buffer->frameCount = 0;
649181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
649281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
649397a893eb34f8687485c88eaf15917974a203f20bAndy HungAudioFlinger::RecordThread::RecordBufferConverter::RecordBufferConverter(
649497a893eb34f8687485c88eaf15917974a203f20bAndy Hung        audio_channel_mask_t srcChannelMask, audio_format_t srcFormat,
649597a893eb34f8687485c88eaf15917974a203f20bAndy Hung        uint32_t srcSampleRate,
649697a893eb34f8687485c88eaf15917974a203f20bAndy Hung        audio_channel_mask_t dstChannelMask, audio_format_t dstFormat,
649797a893eb34f8687485c88eaf15917974a203f20bAndy Hung        uint32_t dstSampleRate) :
649897a893eb34f8687485c88eaf15917974a203f20bAndy Hung            mSrcChannelMask(AUDIO_CHANNEL_INVALID), // updateParameters will set following vars
649997a893eb34f8687485c88eaf15917974a203f20bAndy Hung            // mSrcFormat
650097a893eb34f8687485c88eaf15917974a203f20bAndy Hung            // mSrcSampleRate
650197a893eb34f8687485c88eaf15917974a203f20bAndy Hung            // mDstChannelMask
650297a893eb34f8687485c88eaf15917974a203f20bAndy Hung            // mDstFormat
650397a893eb34f8687485c88eaf15917974a203f20bAndy Hung            // mDstSampleRate
650497a893eb34f8687485c88eaf15917974a203f20bAndy Hung            // mSrcChannelCount
650597a893eb34f8687485c88eaf15917974a203f20bAndy Hung            // mDstChannelCount
650697a893eb34f8687485c88eaf15917974a203f20bAndy Hung            // mDstFrameSize
650797a893eb34f8687485c88eaf15917974a203f20bAndy Hung            mBuf(NULL), mBufFrames(0), mBufFrameSize(0),
6508d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung            mResampler(NULL),
6509d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung            mIsLegacyDownmix(false),
6510d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung            mIsLegacyUpmix(false),
6511d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung            mRequiresFloat(false),
6512d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung            mInputConverterProvider(NULL)
651397a893eb34f8687485c88eaf15917974a203f20bAndy Hung{
651497a893eb34f8687485c88eaf15917974a203f20bAndy Hung    (void)updateParameters(srcChannelMask, srcFormat, srcSampleRate,
651597a893eb34f8687485c88eaf15917974a203f20bAndy Hung            dstChannelMask, dstFormat, dstSampleRate);
651697a893eb34f8687485c88eaf15917974a203f20bAndy Hung}
651797a893eb34f8687485c88eaf15917974a203f20bAndy Hung
651897a893eb34f8687485c88eaf15917974a203f20bAndy HungAudioFlinger::RecordThread::RecordBufferConverter::~RecordBufferConverter() {
651997a893eb34f8687485c88eaf15917974a203f20bAndy Hung    free(mBuf);
652097a893eb34f8687485c88eaf15917974a203f20bAndy Hung    delete mResampler;
6521d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    delete mInputConverterProvider;
652297a893eb34f8687485c88eaf15917974a203f20bAndy Hung}
652397a893eb34f8687485c88eaf15917974a203f20bAndy Hung
652497a893eb34f8687485c88eaf15917974a203f20bAndy Hungsize_t AudioFlinger::RecordThread::RecordBufferConverter::convert(void *dst,
652597a893eb34f8687485c88eaf15917974a203f20bAndy Hung        AudioBufferProvider *provider, size_t frames)
652697a893eb34f8687485c88eaf15917974a203f20bAndy Hung{
6527d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    if (mInputConverterProvider != NULL) {
6528d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        mInputConverterProvider->setBufferProvider(provider);
6529d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        provider = mInputConverterProvider;
6530d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    }
6531d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung
6532d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    if (mResampler == NULL) {
653397a893eb34f8687485c88eaf15917974a203f20bAndy Hung        ALOGVV("NO RESAMPLING sampleRate:%u mSrcFormat:%#x mDstFormat:%#x",
653497a893eb34f8687485c88eaf15917974a203f20bAndy Hung                mSrcSampleRate, mSrcFormat, mDstFormat);
653597a893eb34f8687485c88eaf15917974a203f20bAndy Hung
653697a893eb34f8687485c88eaf15917974a203f20bAndy Hung        AudioBufferProvider::Buffer buffer;
653797a893eb34f8687485c88eaf15917974a203f20bAndy Hung        for (size_t i = frames; i > 0; ) {
653897a893eb34f8687485c88eaf15917974a203f20bAndy Hung            buffer.frameCount = i;
653997a893eb34f8687485c88eaf15917974a203f20bAndy Hung            status_t status = provider->getNextBuffer(&buffer, 0);
654097a893eb34f8687485c88eaf15917974a203f20bAndy Hung            if (status != OK || buffer.frameCount == 0) {
654197a893eb34f8687485c88eaf15917974a203f20bAndy Hung                frames -= i; // cannot fill request.
654297a893eb34f8687485c88eaf15917974a203f20bAndy Hung                break;
654397a893eb34f8687485c88eaf15917974a203f20bAndy Hung            }
6544d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung            // format convert to destination buffer
6545d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung            convertNoResampler(dst, buffer.raw, buffer.frameCount);
654697a893eb34f8687485c88eaf15917974a203f20bAndy Hung
654797a893eb34f8687485c88eaf15917974a203f20bAndy Hung            dst = (int8_t*)dst + buffer.frameCount * mDstFrameSize;
654897a893eb34f8687485c88eaf15917974a203f20bAndy Hung            i -= buffer.frameCount;
654997a893eb34f8687485c88eaf15917974a203f20bAndy Hung            provider->releaseBuffer(&buffer);
655097a893eb34f8687485c88eaf15917974a203f20bAndy Hung        }
655197a893eb34f8687485c88eaf15917974a203f20bAndy Hung    } else {
655297a893eb34f8687485c88eaf15917974a203f20bAndy Hung         ALOGVV("RESAMPLING mSrcSampleRate:%u mDstSampleRate:%u mSrcFormat:%#x mDstFormat:%#x",
655397a893eb34f8687485c88eaf15917974a203f20bAndy Hung                 mSrcSampleRate, mDstSampleRate, mSrcFormat, mDstFormat);
655497a893eb34f8687485c88eaf15917974a203f20bAndy Hung
6555d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung         // reallocate buffer if needed
6556d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung         if (mBufFrameSize != 0 && mBufFrames < frames) {
6557d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung             free(mBuf);
6558d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung             mBufFrames = frames;
6559d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung             (void)posix_memalign(&mBuf, 32, mBufFrames * mBufFrameSize);
6560d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung         }
656197a893eb34f8687485c88eaf15917974a203f20bAndy Hung        // resampler accumulates, but we only have one source track
6562d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        memset(mBuf, 0, frames * mBufFrameSize);
6563d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        frames = mResampler->resample((int32_t*)mBuf, frames, provider);
6564d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        // format convert to destination buffer
6565d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        convertResampler(dst, mBuf, frames);
656697a893eb34f8687485c88eaf15917974a203f20bAndy Hung    }
656797a893eb34f8687485c88eaf15917974a203f20bAndy Hung    return frames;
656897a893eb34f8687485c88eaf15917974a203f20bAndy Hung}
656997a893eb34f8687485c88eaf15917974a203f20bAndy Hung
657097a893eb34f8687485c88eaf15917974a203f20bAndy Hungstatus_t AudioFlinger::RecordThread::RecordBufferConverter::updateParameters(
657197a893eb34f8687485c88eaf15917974a203f20bAndy Hung        audio_channel_mask_t srcChannelMask, audio_format_t srcFormat,
657297a893eb34f8687485c88eaf15917974a203f20bAndy Hung        uint32_t srcSampleRate,
657397a893eb34f8687485c88eaf15917974a203f20bAndy Hung        audio_channel_mask_t dstChannelMask, audio_format_t dstFormat,
657497a893eb34f8687485c88eaf15917974a203f20bAndy Hung        uint32_t dstSampleRate)
657597a893eb34f8687485c88eaf15917974a203f20bAndy Hung{
657697a893eb34f8687485c88eaf15917974a203f20bAndy Hung    // quick evaluation if there is any change.
657797a893eb34f8687485c88eaf15917974a203f20bAndy Hung    if (mSrcFormat == srcFormat
657897a893eb34f8687485c88eaf15917974a203f20bAndy Hung            && mSrcChannelMask == srcChannelMask
657997a893eb34f8687485c88eaf15917974a203f20bAndy Hung            && mSrcSampleRate == srcSampleRate
658097a893eb34f8687485c88eaf15917974a203f20bAndy Hung            && mDstFormat == dstFormat
658197a893eb34f8687485c88eaf15917974a203f20bAndy Hung            && mDstChannelMask == dstChannelMask
658297a893eb34f8687485c88eaf15917974a203f20bAndy Hung            && mDstSampleRate == dstSampleRate) {
658397a893eb34f8687485c88eaf15917974a203f20bAndy Hung        return NO_ERROR;
658497a893eb34f8687485c88eaf15917974a203f20bAndy Hung    }
658597a893eb34f8687485c88eaf15917974a203f20bAndy Hung
6586db4c031f518ae5806af73756273ff32cd8d0e4f8Andy Hung    ALOGV("RecordBufferConverter updateParameters srcMask:%#x dstMask:%#x"
6587db4c031f518ae5806af73756273ff32cd8d0e4f8Andy Hung            "  srcFormat:%#x dstFormat:%#x  srcRate:%u dstRate:%u",
6588db4c031f518ae5806af73756273ff32cd8d0e4f8Andy Hung            srcChannelMask, dstChannelMask, srcFormat, dstFormat, srcSampleRate, dstSampleRate);
658997a893eb34f8687485c88eaf15917974a203f20bAndy Hung    const bool valid =
659097a893eb34f8687485c88eaf15917974a203f20bAndy Hung            audio_is_input_channel(srcChannelMask)
659197a893eb34f8687485c88eaf15917974a203f20bAndy Hung            && audio_is_input_channel(dstChannelMask)
659297a893eb34f8687485c88eaf15917974a203f20bAndy Hung            && audio_is_valid_format(srcFormat) && audio_is_linear_pcm(srcFormat)
659397a893eb34f8687485c88eaf15917974a203f20bAndy Hung            && audio_is_valid_format(dstFormat) && audio_is_linear_pcm(dstFormat)
659497a893eb34f8687485c88eaf15917974a203f20bAndy Hung            && (srcSampleRate <= dstSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX)
659597a893eb34f8687485c88eaf15917974a203f20bAndy Hung            ; // no upsampling checks for now
659697a893eb34f8687485c88eaf15917974a203f20bAndy Hung    if (!valid) {
659797a893eb34f8687485c88eaf15917974a203f20bAndy Hung        return BAD_VALUE;
659897a893eb34f8687485c88eaf15917974a203f20bAndy Hung    }
659997a893eb34f8687485c88eaf15917974a203f20bAndy Hung
660097a893eb34f8687485c88eaf15917974a203f20bAndy Hung    mSrcFormat = srcFormat;
660197a893eb34f8687485c88eaf15917974a203f20bAndy Hung    mSrcChannelMask = srcChannelMask;
660297a893eb34f8687485c88eaf15917974a203f20bAndy Hung    mSrcSampleRate = srcSampleRate;
660397a893eb34f8687485c88eaf15917974a203f20bAndy Hung    mDstFormat = dstFormat;
660497a893eb34f8687485c88eaf15917974a203f20bAndy Hung    mDstChannelMask = dstChannelMask;
660597a893eb34f8687485c88eaf15917974a203f20bAndy Hung    mDstSampleRate = dstSampleRate;
660697a893eb34f8687485c88eaf15917974a203f20bAndy Hung
660797a893eb34f8687485c88eaf15917974a203f20bAndy Hung    // compute derived parameters
660897a893eb34f8687485c88eaf15917974a203f20bAndy Hung    mSrcChannelCount = audio_channel_count_from_in_mask(srcChannelMask);
660997a893eb34f8687485c88eaf15917974a203f20bAndy Hung    mDstChannelCount = audio_channel_count_from_in_mask(dstChannelMask);
661097a893eb34f8687485c88eaf15917974a203f20bAndy Hung    mDstFrameSize = mDstChannelCount * audio_bytes_per_sample(mDstFormat);
661197a893eb34f8687485c88eaf15917974a203f20bAndy Hung
6612d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    // do we need to resample?
6613d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    delete mResampler;
6614d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    mResampler = NULL;
6615d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    if (mSrcSampleRate != mDstSampleRate) {
6616d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        mResampler = AudioResampler::create(AUDIO_FORMAT_PCM_FLOAT,
6617d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung                mSrcChannelCount, mDstSampleRate);
6618d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        mResampler->setSampleRate(mSrcSampleRate);
6619d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        mResampler->setVolume(AudioMixer::UNITY_GAIN_FLOAT, AudioMixer::UNITY_GAIN_FLOAT);
6620d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    }
6621d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung
6622d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    // are we running legacy channel conversion modes?
6623d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    mIsLegacyDownmix = (mSrcChannelMask == AUDIO_CHANNEL_IN_STEREO
6624d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung                            || mSrcChannelMask == AUDIO_CHANNEL_IN_FRONT_BACK)
6625d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung                   && mDstChannelMask == AUDIO_CHANNEL_IN_MONO;
6626d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    mIsLegacyUpmix = mSrcChannelMask == AUDIO_CHANNEL_IN_MONO
6627d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung                   && (mDstChannelMask == AUDIO_CHANNEL_IN_STEREO
6628d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung                            || mDstChannelMask == AUDIO_CHANNEL_IN_FRONT_BACK);
6629d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung
6630d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    // do we need to process in float?
6631d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    mRequiresFloat = mResampler != NULL || mIsLegacyDownmix || mIsLegacyUpmix;
6632d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung
6633d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    // do we need a staging buffer to convert for destination (we can still optimize this)?
6634d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    // we use mBufFrameSize > 0 to indicate both frame size as well as buffer necessity
6635d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    if (mResampler != NULL) {
6636d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        mBufFrameSize = max(mSrcChannelCount, FCC_2)
6637d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung                * audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT);
6638d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    } else if ((mIsLegacyUpmix || mIsLegacyDownmix) && mDstFormat != AUDIO_FORMAT_PCM_FLOAT) {
6639d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        mBufFrameSize = mDstChannelCount * audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT);
6640d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    } else if (mSrcChannelMask != mDstChannelMask && mDstFormat != mSrcFormat) {
664197a893eb34f8687485c88eaf15917974a203f20bAndy Hung        mBufFrameSize = mDstChannelCount * audio_bytes_per_sample(mSrcFormat);
664297a893eb34f8687485c88eaf15917974a203f20bAndy Hung    } else {
664397a893eb34f8687485c88eaf15917974a203f20bAndy Hung        mBufFrameSize = 0;
664497a893eb34f8687485c88eaf15917974a203f20bAndy Hung    }
664597a893eb34f8687485c88eaf15917974a203f20bAndy Hung    mBufFrames = 0; // force the buffer to be resized.
664697a893eb34f8687485c88eaf15917974a203f20bAndy Hung
6647d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    // do we need an input converter buffer provider to give us float?
6648d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    delete mInputConverterProvider;
6649d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    mInputConverterProvider = NULL;
6650d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    if (mRequiresFloat && mSrcFormat != AUDIO_FORMAT_PCM_FLOAT) {
6651d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        mInputConverterProvider = new ReformatBufferProvider(
6652d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung                audio_channel_count_from_in_mask(mSrcChannelMask),
6653d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung                mSrcFormat,
6654d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung                AUDIO_FORMAT_PCM_FLOAT,
6655d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung                256 /* provider buffer frame count */);
6656d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    }
6657d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung
6658d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    // do we need a remixer to do channel mask conversion
6659d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    if (!mIsLegacyDownmix && !mIsLegacyUpmix && mSrcChannelMask != mDstChannelMask) {
6660d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        (void) memcpy_by_index_array_initialization_from_channel_mask(
6661d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung                mIdxAry, ARRAY_SIZE(mIdxAry), mDstChannelMask, mSrcChannelMask);
666297a893eb34f8687485c88eaf15917974a203f20bAndy Hung    }
666397a893eb34f8687485c88eaf15917974a203f20bAndy Hung    return NO_ERROR;
666497a893eb34f8687485c88eaf15917974a203f20bAndy Hung}
666597a893eb34f8687485c88eaf15917974a203f20bAndy Hung
6666d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hungvoid AudioFlinger::RecordThread::RecordBufferConverter::convertNoResampler(
6667d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        void *dst, const void *src, size_t frames)
666897a893eb34f8687485c88eaf15917974a203f20bAndy Hung{
6669d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    // src is native type unless there is legacy upmix or downmix, whereupon it is float.
667097a893eb34f8687485c88eaf15917974a203f20bAndy Hung    if (mBufFrameSize != 0 && mBufFrames < frames) {
667197a893eb34f8687485c88eaf15917974a203f20bAndy Hung        free(mBuf);
667297a893eb34f8687485c88eaf15917974a203f20bAndy Hung        mBufFrames = frames;
667397a893eb34f8687485c88eaf15917974a203f20bAndy Hung        (void)posix_memalign(&mBuf, 32, mBufFrames * mBufFrameSize);
667497a893eb34f8687485c88eaf15917974a203f20bAndy Hung    }
6675d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    // do we need to do legacy upmix and downmix?
6676d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    if (mIsLegacyUpmix || mIsLegacyDownmix) {
667797a893eb34f8687485c88eaf15917974a203f20bAndy Hung        void *dstBuf = mBuf != NULL ? mBuf : dst;
6678d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        if (mIsLegacyUpmix) {
6679d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung            upmix_to_stereo_float_from_mono_float((float *)dstBuf,
6680d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung                    (const float *)src, frames);
6681d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        } else /*mIsLegacyDownmix */ {
6682d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung            downmix_to_mono_float_from_stereo_float((float *)dstBuf,
6683d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung                    (const float *)src, frames);
668497a893eb34f8687485c88eaf15917974a203f20bAndy Hung        }
6685d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        if (mBuf != NULL) {
6686d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung            memcpy_by_audio_format(dst, mDstFormat, mBuf, AUDIO_FORMAT_PCM_FLOAT,
6687d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung                    frames * mDstChannelCount);
6688d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        }
6689d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        return;
6690d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    }
6691d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    // do we need to do channel mask conversion?
6692d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    if (mSrcChannelMask != mDstChannelMask) {
669397a893eb34f8687485c88eaf15917974a203f20bAndy Hung        void *dstBuf = mBuf != NULL ? mBuf : dst;
6694d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        memcpy_by_index_array(dstBuf, mDstChannelCount,
6695d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung                src, mSrcChannelCount, mIdxAry, audio_bytes_per_sample(mSrcFormat), frames);
6696d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        if (dstBuf == dst) {
6697d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung            return; // format is the same
6698d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        }
6699d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    }
6700d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    // convert to destination buffer
6701d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    const void *convertBuf = mBuf != NULL ? mBuf : src;
6702d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    memcpy_by_audio_format(dst, mDstFormat, convertBuf, mSrcFormat,
6703d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung            frames * mDstChannelCount);
6704d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung}
6705d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung
6706d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hungvoid AudioFlinger::RecordThread::RecordBufferConverter::convertResampler(
6707d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        void *dst, /*not-a-const*/ void *src, size_t frames)
6708d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung{
6709d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    // src buffer format is ALWAYS float when entering this routine
6710d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    if (mIsLegacyUpmix) {
6711d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        ; // mono to stereo already handled by resampler
6712d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    } else if (mIsLegacyDownmix
6713d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung            || (mSrcChannelMask == mDstChannelMask && mSrcChannelCount == 1)) {
6714d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        // the resampler outputs stereo for mono input channel (a feature?)
6715d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        // must convert to mono
6716d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        downmix_to_mono_float_from_stereo_float((float *)src,
6717d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung                (const float *)src, frames);
6718d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    } else if (mSrcChannelMask != mDstChannelMask) {
6719d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        // convert to mono channel again for channel mask conversion (could be skipped
6720d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        // with further optimization).
672197a893eb34f8687485c88eaf15917974a203f20bAndy Hung        if (mSrcChannelCount == 1) {
6722d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung            downmix_to_mono_float_from_stereo_float((float *)src,
6723d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung                (const float *)src, frames);
672497a893eb34f8687485c88eaf15917974a203f20bAndy Hung        }
6725d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        // convert to destination format (in place, OK as float is larger than other types)
6726d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        if (mDstFormat != AUDIO_FORMAT_PCM_FLOAT) {
6727d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung            memcpy_by_audio_format(src, mDstFormat, src, AUDIO_FORMAT_PCM_FLOAT,
6728d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung                    frames * mSrcChannelCount);
6729d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        }
6730d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        // channel convert and save to dst
6731d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        memcpy_by_index_array(dst, mDstChannelCount,
6732d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung                src, mSrcChannelCount, mIdxAry, audio_bytes_per_sample(mDstFormat), frames);
6733d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        return;
673497a893eb34f8687485c88eaf15917974a203f20bAndy Hung    }
6735d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    // convert to destination format and save to dst
6736d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    memcpy_by_audio_format(dst, mDstFormat, src, AUDIO_FORMAT_PCM_FLOAT,
6737d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung            frames * mDstChannelCount);
673897a893eb34f8687485c88eaf15917974a203f20bAndy Hung}
673997a893eb34f8687485c88eaf15917974a203f20bAndy Hung
67401035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentbool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair,
67411035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                        status_t& status)
674281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
674381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool reconfig = false;
674481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
67451035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    status = NO_ERROR;
67461035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
67471035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    audio_format_t reqFormat = mFormat;
67481035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    uint32_t samplingRate = mSampleRate;
6749e1635ec096d1110c33a5aa46847af59c261fb7faGlenn Kasten    // TODO this may change if we want to support capture from HDMI PCM multi channel (e.g on TVs).
67501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount);
67511035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
67521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    AudioParameter param = AudioParameter(keyValuePair);
67531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    int value;
67541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // TODO Investigate when this code runs. Check with audio policy when a sample rate and
67551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //      channel count change can be requested. Do we mandate the first client defines the
67561035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //      HAL sampling rate and channel count or do we allow changes on the fly?
67571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
67581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        samplingRate = value;
67591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        reconfig = true;
67601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
67611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
676297a893eb34f8687485c88eaf15917974a203f20bAndy Hung        if (!audio_is_linear_pcm((audio_format_t) value)) {
67631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            status = BAD_VALUE;
67641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        } else {
67651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            reqFormat = (audio_format_t) value;
676681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            reconfig = true;
676781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
67681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
67691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
67701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        audio_channel_mask_t mask = (audio_channel_mask_t) value;
6771d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        if (!audio_is_input_channel(mask) ||
6772d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung                audio_channel_count_from_in_mask(mask) > FCC_8) {
67731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            status = BAD_VALUE;
67741035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        } else {
67751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            channelMask = mask;
67761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            reconfig = true;
677781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
67781035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
67791035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
67801035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // do not accept frame count changes if tracks are open as the track buffer
67811035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // size depends on frame count and correct behavior would not be guaranteed
67821035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // if frame count is changed after track creation
67831035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (mActiveTracks.size() > 0) {
67841035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            status = INVALID_OPERATION;
67851035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        } else {
67861035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            reconfig = true;
678781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
67881035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
67891035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
67901035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // forward device change to effects that have requested to be
67911035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // aware of attached audio device.
67921035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        for (size_t i = 0; i < mEffectChains.size(); i++) {
67931035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mEffectChains[i]->setDevice_l(value);
679481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
679581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
67961035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // store input device and output device but do not forward output device to audio HAL.
67971035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // Note that status is ignored by the caller for output device
67981035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // (see AudioFlinger::setParameters()
67991035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (audio_is_output_devices(value)) {
68001035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mOutDevice = value;
68011035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            status = BAD_VALUE;
68021035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        } else {
68031035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mInDevice = value;
6804e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent            if (value != AUDIO_DEVICE_NONE) {
6805e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent                mPrevInDevice = value;
6806e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent            }
68071035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            // disable AEC and NS if the device is a BT SCO headset supporting those
68081035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            // pre processings
68091035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            if (mTracks.size() > 0) {
68101035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                bool suspend = audio_is_bluetooth_sco_device(mInDevice) &&
68111035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                    mAudioFlinger->btNrecIsOff();
68121035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                for (size_t i = 0; i < mTracks.size(); i++) {
68131035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                    sp<RecordTrack> track = mTracks[i];
68141035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                    setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId());
68151035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                    setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId());
681681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
681781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
681881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
68191035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
68201035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR &&
68211035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mAudioSource != (audio_source_t)value) {
68221035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // forward device change to effects that have requested to be
68231035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // aware of attached audio device.
68241035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        for (size_t i = 0; i < mEffectChains.size(); i++) {
68251035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mEffectChains[i]->setAudioSource_l((audio_source_t)value);
682681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
68271035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        mAudioSource = (audio_source_t)value;
68281035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
6829e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
68301035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (status == NO_ERROR) {
68311035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        status = mInput->stream->common.set_parameters(&mInput->stream->common,
68321035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                keyValuePair.string());
68331035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (status == INVALID_OPERATION) {
68341035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            inputStandBy();
683581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status = mInput->stream->common.set_parameters(&mInput->stream->common,
683681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    keyValuePair.string());
68371035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
68381035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (reconfig) {
68391035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            if (status == BAD_VALUE &&
684097a893eb34f8687485c88eaf15917974a203f20bAndy Hung                audio_is_linear_pcm(mInput->stream->common.get_format(&mInput->stream->common)) &&
684197a893eb34f8687485c88eaf15917974a203f20bAndy Hung                audio_is_linear_pcm(reqFormat) &&
68421035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                (mInput->stream->common.get_sample_rate(&mInput->stream->common)
684397a893eb34f8687485c88eaf15917974a203f20bAndy Hung                        <= (AUDIO_RESAMPLER_DOWN_RATIO_MAX * samplingRate)) &&
6844e541269be94f3a1072932d51537905b120ef4733Andy Hung                audio_channel_count_from_in_mask(
6845d1abb8f94d7ed749ee959655db1e07d26dad074dAndy Hung                        mInput->stream->common.get_channels(&mInput->stream->common)) <= FCC_8) {
68461035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                status = NO_ERROR;
684781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
68481035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            if (status == NO_ERROR) {
68491035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                readInputParameters_l();
685073e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent                sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED);
685181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
685281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
685381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
68541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
685581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return reconfig;
685681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
685781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
685881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentString8 AudioFlinger::RecordThread::getParameters(const String8& keys)
685981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
686081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
686181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (initCheck() != NO_ERROR) {
6862d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten        return String8();
686381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
686481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6865d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten    char *s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string());
6866d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten    const String8 out_s8(s);
686781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    free(s);
686881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return out_s8;
686981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
687081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
687173e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurentvoid AudioFlinger::RecordThread::ioConfigChanged(audio_io_config_event event) {
687273e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    sp<AudioIoDescriptor> desc = new AudioIoDescriptor();
687373e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent
687473e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    desc->mIoHandle = mId;
687581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
687681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    switch (event) {
687773e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    case AUDIO_INPUT_OPENED:
687873e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    case AUDIO_INPUT_CONFIG_CHANGED:
6879296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        desc->mPatch = mPatch;
688073e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        desc->mChannelMask = mChannelMask;
688173e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        desc->mSamplingRate = mSampleRate;
688273e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        desc->mFormat = mFormat;
688373e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        desc->mFrameCount = mFrameCount;
688473e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        desc->mLatency = 0;
688581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        break;
688681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
688773e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    case AUDIO_INPUT_CLOSED:
688881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    default:
688981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        break;
689081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
689173e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    mAudioFlinger->ioConfigChanged(event, desc);
689281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
689381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6894deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kastenvoid AudioFlinger::RecordThread::readInputParameters_l()
689581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
689681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common);
689781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common);
6898e541269be94f3a1072932d51537905b120ef4733Andy Hung    mChannelCount = audio_channel_count_from_in_mask(mChannelMask);
6899d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    if (mChannelCount > FCC_8) {
6900d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        ALOGE("HAL channel count %d > %d", mChannelCount, FCC_8);
6901d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    }
6902463be250de73907965faa6a216c00312bf81e049Andy Hung    mHALFormat = mInput->stream->common.get_format(&mInput->stream->common);
6903463be250de73907965faa6a216c00312bf81e049Andy Hung    mFormat = mHALFormat;
6904d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung    if (!audio_is_linear_pcm(mFormat)) {
6905d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        ALOGE("HAL format %#x is not linear pcm", mFormat);
6906291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten    }
6907665470b36f202bcc8ee2f7417f68fd2608dd07c1Eric Laurent    mFrameSize = audio_stream_in_frame_size(mInput->stream);
6908548efc94813c1dec6e8cf6c085ae41ccb04827f1Glenn Kasten    mBufferSize = mInput->stream->common.get_buffer_size(&mInput->stream->common);
6909548efc94813c1dec6e8cf6c085ae41ccb04827f1Glenn Kasten    mFrameCount = mBufferSize / mFrameSize;
69106dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    // This is the formula for calculating the temporary buffer size.
6911e842614837e5401adf77e90485300c288b9a7876Glenn Kasten    // With 7 HAL buffers, we can guarantee ability to down-sample the input by ratio of 6:1 to
69128594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    // 1 full output buffer, regardless of the alignment of the available input.
6913e842614837e5401adf77e90485300c288b9a7876Glenn Kasten    // The value is somewhat arbitrary, and could probably be even larger.
69146dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    // A larger value should allow more old data to be read after a track calls start(),
69156dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    // without increasing latency.
691697a893eb34f8687485c88eaf15917974a203f20bAndy Hung    //
691797a893eb34f8687485c88eaf15917974a203f20bAndy Hung    // Note this is independent of the maximum downsampling ratio permitted for capture.
6918e842614837e5401adf77e90485300c288b9a7876Glenn Kasten    mRsmpInFrames = mFrameCount * 7;
69198594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    mRsmpInFramesP2 = roundup(mRsmpInFrames);
69205744661e85981f8a9456bf470e2761235fc026daAndy Hung    free(mRsmpInBuffer);
692149d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten
692249d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten    // TODO optimize audio capture buffer sizes ...
692349d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten    // Here we calculate the size of the sliding buffer used as a source
692449d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten    // for resampling.  mRsmpInFramesP2 is currently roundup(mFrameCount * 7).
692549d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten    // For current HAL frame counts, this is usually 2048 = 40 ms.  It would
692649d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten    // be better to have it derived from the pipe depth in the long term.
692749d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten    // The current value is higher than necessary.  However it should not add to latency.
692849d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten
69298594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    // Over-allocate beyond mRsmpInFramesP2 to permit a HAL read past end of buffer
69305744661e85981f8a9456bf470e2761235fc026daAndy Hung    (void)posix_memalign(&mRsmpInBuffer, 32, (mRsmpInFramesP2 + mFrameCount - 1) * mFrameSize);
69316dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
69324cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten    // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints.
69334cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten    // But if thread's mSampleRate or mChannelCount changes, how will that affect active tracks?
693481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
693581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
69365f972c031d4061f4f037c9fda1ea4bd9b6a756cdGlenn Kastenuint32_t AudioFlinger::RecordThread::getInputFramesLost()
693781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
693881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
693981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (initCheck() != NO_ERROR) {
694081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
694181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
694281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
694381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mInput->stream->get_input_frames_lost(mInput->stream);
694481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
694581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
694681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) const
694781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
694881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
694981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t result = 0;
695081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (getEffectChain_l(sessionId) != 0) {
695181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        result = EFFECT_SESSION;
695281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
695381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
695481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mTracks.size(); ++i) {
695581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (sessionId == mTracks[i]->sessionId()) {
695681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            result |= TRACK_SESSION;
695781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
695881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
695981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
696081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
696181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return result;
696281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
696381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
696481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentKeyedVector<int, bool> AudioFlinger::RecordThread::sessionIds() const
696581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
696681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    KeyedVector<int, bool> ids;
696781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
696881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t j = 0; j < mTracks.size(); ++j) {
696981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<RecordThread::RecordTrack> track = mTracks[j];
697081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int sessionId = track->sessionId();
697181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (ids.indexOfKey(sessionId) < 0) {
697281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ids.add(sessionId, true);
697381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
697481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
697581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return ids;
697681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
697781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
697881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput()
697981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
698081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
698181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioStreamIn *input = mInput;
698281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mInput = NULL;
698381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return input;
698481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
698581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
698681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// this method must always be called either with ThreadBase mLock held or inside the thread loop
698781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_stream_t* AudioFlinger::RecordThread::stream() const
698881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
698981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mInput == NULL) {
699081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return NULL;
699181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
699281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return &mInput->stream->common;
699381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
699481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
699581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain)
699681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
699781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // only one chain per input thread
699881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mEffectChains.size() != 0) {
6999aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        ALOGW("addEffectChain_l() already one chain %p on thread %p", chain.get(), this);
700081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return INVALID_OPERATION;
700181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
700281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this);
7003aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    chain->setThread(this);
700481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    chain->setInBuffer(NULL);
700581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    chain->setOutBuffer(NULL);
700681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
700781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    checkSuspendOnAddEffectChain_l(chain);
700881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
70091b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent    // make sure enabled pre processing effects state is communicated to the HAL as we
70101b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent    // just moved them to a new input stream.
70111b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent    chain->syncHalEffectsState();
70121b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent
701381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mEffectChains.add(chain);
701481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
701581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
701681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
701781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
701881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsize_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain)
701981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
702081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this);
702181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGW_IF(mEffectChains.size() != 1,
702281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            "removeEffectChain_l() %p invalid chain size %d on thread %p",
702381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chain.get(), mEffectChains.size(), this);
702481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mEffectChains.size() == 1) {
702581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mEffectChains.removeAt(0);
702681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
702781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return 0;
702881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
702981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
70301c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch,
70311c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                                          audio_patch_handle_t *handle)
70321c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{
70331c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    status_t status = NO_ERROR;
7034054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
7035054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    // store new device and send to effects
7036054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    mInDevice = patch->sources[0].ext.device.type;
7037296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    mPatch = *patch;
7038054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    for (size_t i = 0; i < mEffectChains.size(); i++) {
7039054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        mEffectChains[i]->setDevice_l(mInDevice);
7040054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    }
7041054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
7042054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    // disable AEC and NS if the device is a BT SCO headset supporting those
7043054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    // pre processings
7044054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    if (mTracks.size() > 0) {
7045054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        bool suspend = audio_is_bluetooth_sco_device(mInDevice) &&
7046054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent                            mAudioFlinger->btNrecIsOff();
7047054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        for (size_t i = 0; i < mTracks.size(); i++) {
7048054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent            sp<RecordTrack> track = mTracks[i];
7049054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent            setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId());
7050054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent            setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId());
70511c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
7052054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    }
70531c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
7054054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    // store new source and send to effects
7055054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) {
7056054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        mAudioSource = patch->sinks[0].ext.mix.usecase.source;
7057054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        for (size_t i = 0; i < mEffectChains.size(); i++) {
7058054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent            mEffectChains[i]->setAudioSource_l(mAudioSource);
70591c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
7060054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    }
70611c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
7062054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) {
70631c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice();
70641c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        status = hwDevice->create_audio_patch(hwDevice,
70651c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                               patch->num_sources,
70661c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                               patch->sources,
70671c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                               patch->num_sinks,
70681c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                               patch->sinks,
70691c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                               handle);
70701c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    } else {
7071054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        char *address;
7072054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        if (strcmp(patch->sources[0].ext.device.address, "") != 0) {
7073054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent            address = audio_device_address_to_parameter(
7074054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent                                                patch->sources[0].ext.device.type,
7075054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent                                                patch->sources[0].ext.device.address);
7076054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        } else {
7077054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent            address = (char *)calloc(1, 1);
7078054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        }
7079054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        AudioParameter param = AudioParameter(String8(address));
7080054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        free(address);
7081054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING),
7082054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent                     (int)patch->sources[0].ext.device.type);
7083054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        param.addInt(String8(AUDIO_PARAMETER_STREAM_INPUT_SOURCE),
7084054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent                                         (int)patch->sinks[0].ext.mix.usecase.source);
7085054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        status = mInput->stream->common.set_parameters(&mInput->stream->common,
7086054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent                param.toString().string());
7087054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        *handle = AUDIO_PATCH_HANDLE_NONE;
70881c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    }
7089054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
7090e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent    if (mInDevice != mPrevInDevice) {
7091e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent        sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED);
7092e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent        mPrevInDevice = mInDevice;
7093e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent    }
7094296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent
70951c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    return status;
70961c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent}
70971c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
70981c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle)
70991c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{
71001c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    status_t status = NO_ERROR;
7101054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
7102054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    mInDevice = AUDIO_DEVICE_NONE;
7103054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
71041c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) {
71051c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice();
71061c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        status = hwDevice->release_audio_patch(hwDevice, handle);
71071c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    } else {
7108054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        AudioParameter param;
7109054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0);
7110054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        status = mInput->stream->common.set_parameters(&mInput->stream->common,
7111054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent                param.toString().string());
71121c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    }
71131c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    return status;
71141c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent}
71151c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
711683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::RecordThread::addPatchRecord(const sp<PatchRecord>& record)
711783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
711883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    Mutex::Autolock _l(mLock);
711983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    mTracks.add(record);
712083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
712183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
712283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::RecordThread::deletePatchRecord(const sp<PatchRecord>& record)
712383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
712483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    Mutex::Autolock _l(mLock);
712583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    destroyTrack_l(record);
712683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
712783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
712883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::RecordThread::getAudioPortConfig(struct audio_port_config *config)
712983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
713083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    ThreadBase::getAudioPortConfig(config);
713183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    config->role = AUDIO_PORT_ROLE_SINK;
713283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    config->ext.mix.hw_module = mInput->audioHwDev->handle();
713383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    config->ext.mix.usecase.source = mAudioSource;
713483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
71351c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
713663238efb0d674758902918e3cdaac322126484b7Glenn Kasten} // namespace android
7137