Threads.cpp revision 4dc680607181e6a76f4e91a39366c4f5dfb7b03e
181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent/* 281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** 381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** Copyright 2012, The Android Open Source Project 481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** 581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** Licensed under the Apache License, Version 2.0 (the "License"); 681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** you may not use this file except in compliance with the License. 781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** You may obtain a copy of the License at 881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** 981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** http://www.apache.org/licenses/LICENSE-2.0 1081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** 1181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** Unless required by applicable law or agreed to in writing, software 1281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** distributed under the License is distributed on an "AS IS" BASIS, 1381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** See the License for the specific language governing permissions and 1581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** limitations under the License. 1681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent*/ 1781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#define LOG_TAG "AudioFlinger" 2081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent//#define LOG_NDEBUG 0 21371eb9756c32109ea572b91216b19bb623f6d3fdAlex Ray#define ATRACE_TAG ATRACE_TAG_AUDIO 2281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 23153b9fe667e6e78e0218ff0159353097428c7657Glenn Kasten#include "Configuration.h" 2481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <math.h> 2581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <fcntl.h> 2681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <sys/stat.h> 2781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <cutils/properties.h> 281ab85ec401801ef9a9184650d0f5a1639b45eeb9Glenn Kasten#include <media/AudioParameter.h> 2981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <utils/Log.h> 30371eb9756c32109ea572b91216b19bb623f6d3fdAlex Ray#include <utils/Trace.h> 3181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <private/media/AudioTrackShared.h> 3381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <hardware/audio.h> 3481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <audio_effects/effect_ns.h> 3581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <audio_effects/effect_aec.h> 3681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <audio_utils/primitives.h> 3798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung#include <audio_utils/format.h> 38c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten#include <audio_utils/minifloat.h> 3981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// NBAIO implementations 416dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#include <media/nbaio/AudioStreamInSource.h> 4281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/AudioStreamOutSink.h> 4381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/MonoPipe.h> 4481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/MonoPipeReader.h> 4581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/Pipe.h> 4681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/PipeReader.h> 4781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/SourceAudioBufferProvider.h> 4881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <powermanager/PowerManager.h> 5081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <common_time/cc_helper.h> 5281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <common_time/local_clock.h> 5381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "AudioFlinger.h" 5581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "AudioMixer.h" 5681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "FastMixer.h" 576dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#include "FastCapture.h" 5881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "ServiceUtilities.h" 5981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "SchedulingPolicyService.h" 6081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 6181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef ADD_BATTERY_DATA 6281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/IMediaPlayerService.h> 6381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/IMediaDeathNotifier.h> 6481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 6581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 6681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef DEBUG_CPU_USAGE 6781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <cpustats/CentralTendencyStatistics.h> 6881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <cpustats/ThreadCpuUsage.h> 6981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 7081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 7181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 7281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 7381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Note: the following macro is used for extremely verbose logging message. In 7481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to 7581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// 0; but one side effect of this is to turn all LOGV's as well. Some messages 7681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// are so verbose that we want to suppress them even when we have ALOG_ASSERT 7781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// turned on. Do not uncomment the #def below unless you really know what you 7881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// are doing and want to see all of the extremely verbose messages. 7981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent//#define VERY_VERY_VERBOSE_LOGGING 8081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef VERY_VERY_VERBOSE_LOGGING 8181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#define ALOGVV ALOGV 8281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#else 8381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#define ALOGVV(a...) do { } while(0) 8481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 8581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 8681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentnamespace android { 8781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 8881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// retry counts for buffer fill timeout 8981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// 50 * ~20msecs = 1 second 9081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int8_t kMaxTrackRetries = 50; 9181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int8_t kMaxTrackStartupRetries = 50; 9281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// allow less retry attempts on direct output thread. 9381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// direct outputs can be a scarce resource in audio hardware and should 9481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// be released as quickly as possible. 9581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int8_t kMaxTrackRetriesDirect = 2; 9681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 9781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// don't warn about blocked writes or record buffer overflows more often than this 9881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const nsecs_t kWarningThrottleNs = seconds(5); 9981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 10081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// RecordThread loop sleep time upon application overrun or audio HAL read error 10181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int kRecordThreadSleepUs = 5000; 10281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1031035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// maximum time to wait in sendConfigEvent_l() for a status to be received 1041035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentstatic const nsecs_t kConfigEventTimeoutNs = seconds(2); 10581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 10681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// minimum sleep time for the mixer thread loop when tracks are active but in underrun 10781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const uint32_t kMinThreadSleepTimeUs = 5000; 10881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// maximum divider applied to the active sleep time in the mixer thread loop 10981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const uint32_t kMaxThreadSleepTimeShift = 2; 11081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 11109a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung// minimum normal sink buffer size, expressed in milliseconds rather than frames 11209a5007b17acb49d25cfa386a2e2534d942e8854Andy Hungstatic const uint32_t kMinNormalSinkBufferSizeMs = 20; 11309a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung// maximum normal sink buffer size 11409a5007b17acb49d25cfa386a2e2534d942e8854Andy Hungstatic const uint32_t kMaxNormalSinkBufferSizeMs = 24; 11581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 116972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent// Offloaded output thread standby delay: allows track transition without going to standby 117972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurentstatic const nsecs_t kOffloadStandbyDelayNs = seconds(1); 118972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent 11981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Whether to use fast mixer 12081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const enum { 12181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixer_Never, // never initialize or use: for debugging only 12281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixer_Always, // always initialize and use, even if not needed: for debugging only 12381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // normal mixer multiplier is 1 12481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixer_Static, // initialize if needed, then use all the time if initialized, 12581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // multiplier is calculated based on min & max normal mixer buffer size 12681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load, 12781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // multiplier is calculated based on min & max normal mixer buffer size 12881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME for FastMixer_Dynamic: 12981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Supporting this option will require fixing HALs that can't handle large writes. 13081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // For example, one HAL implementation returns an error from a large write, 13181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // and another HAL implementation corrupts memory, possibly in the sample rate converter. 13281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // We could either fix the HAL implementations, or provide a wrapper that breaks 13381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // up large writes into smaller ones, and the wrapper would need to deal with scheduler. 13481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} kUseFastMixer = FastMixer_Static; 13581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1366dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten// Whether to use fast capture 1376dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kastenstatic const enum { 1386dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCapture_Never, // never initialize or use: for debugging only 1396dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCapture_Always, // always initialize and use, even if not needed: for debugging only 1406dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCapture_Static, // initialize if needed, then use all the time if initialized 1416dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten} kUseFastCapture = FastCapture_Static; 1426dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 14381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Priorities for requestPriority 14481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int kPriorityAudioApp = 2; 14581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int kPriorityFastMixer = 3; 1466dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kastenstatic const int kPriorityFastCapture = 3; 14781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 14881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// IAudioFlinger::createTrack() reports back to client the total size of shared memory area 14981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// for the track. The client then sub-divides this into smaller buffers for its use. 150b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten// Currently the client uses N-buffering by default, but doesn't tell us about the value of N. 151b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten// So for now we just assume that client is double-buffered for fast tracks. 152b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten// FIXME It would be better for client to tell AudioFlinger the value of N, 153b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten// so AudioFlinger could allocate the right amount of memory. 15481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// See the client's minBufCount and mNotificationFramesAct calculations for details. 1550349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten 1560349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten// This is the default value, if not specified by property. 157b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kastenstatic const int kFastTrackMultiplier = 2; 15881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1590349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten// The minimum and maximum allowed values 1600349009fd19f89f8414c428f6b71b369f7546085Glenn Kastenstatic const int kFastTrackMultiplierMin = 1; 1610349009fd19f89f8414c428f6b71b369f7546085Glenn Kastenstatic const int kFastTrackMultiplierMax = 2; 1620349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten 1630349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten// The actual value to use, which can be specified per-device via property af.fast_track_multiplier. 1640349009fd19f89f8414c428f6b71b369f7546085Glenn Kastenstatic int sFastTrackMultiplier = kFastTrackMultiplier; 1650349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten 166b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten// See Thread::readOnlyHeap(). 167b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten// Initially this heap is used to allocate client buffers for "fast" AudioRecord. 168b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten// Eventually it will be the single buffer that FastCapture writes into via HAL read(), 169b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten// and that all "fast" AudioRecord clients read from. In either case, the size can be small. 1709f81de3452dfb2385bd57dc05456a045174a1ab1Glenn Kastenstatic const size_t kRecordThreadReadOnlyHeapSize = 0x2000; 171b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten 17281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 17381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1740349009fd19f89f8414c428f6b71b369f7546085Glenn Kastenstatic pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT; 1750349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten 1760349009fd19f89f8414c428f6b71b369f7546085Glenn Kastenstatic void sFastTrackMultiplierInit() 1770349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten{ 1780349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten char value[PROPERTY_VALUE_MAX]; 1790349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten if (property_get("af.fast_track_multiplier", value, NULL) > 0) { 1800349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten char *endptr; 1810349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten unsigned long ul = strtoul(value, &endptr, 0); 1820349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) { 1830349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten sFastTrackMultiplier = (int) ul; 1840349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten } 1850349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten } 1860349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten} 1870349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten 1880349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten// ---------------------------------------------------------------------------- 1890349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten 19081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef ADD_BATTERY_DATA 19181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// To collect the amplifier usage 19281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic void addBatteryData(uint32_t params) { 19381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); 19481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (service == NULL) { 19581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it already logged 19681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 19781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 19881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 19981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent service->addBatteryData(params); 20081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 20181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 20281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 20381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 20481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 20581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// CPU Stats 20681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 20781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 20881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass CpuStats { 20981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic: 21081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent CpuStats(); 21181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void sample(const String8 &title); 21281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef DEBUG_CPU_USAGE 21381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate: 21481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns 21581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns 21681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 21781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles 21881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 21981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int mCpuNum; // thread's current CPU number 22081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int mCpukHz; // frequency of thread's current CPU in kHz 22181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 22281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}; 22381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 22481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentCpuStats::CpuStats() 22581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef DEBUG_CPU_USAGE 22681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent : mCpuNum(-1), mCpukHz(-1) 22781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 22881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 22981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 23081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2310f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid CpuStats::sample(const String8 &title 2320f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten#ifndef DEBUG_CPU_USAGE 2330f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten __unused 2340f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten#endif 2350f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten ) { 23681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef DEBUG_CPU_USAGE 23781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // get current thread's delta CPU time in wall clock ns 23881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double wcNs; 23981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool valid = mCpuUsage.sampleAndEnable(wcNs); 24081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 24181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // record sample for wall clock statistics 24281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (valid) { 24381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWcStats.sample(wcNs); 24481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 24581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 24681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // get the current CPU number 24781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int cpuNum = sched_getcpu(); 24881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 24981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // get the current CPU frequency in kHz 25081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int cpukHz = mCpuUsage.getCpukHz(cpuNum); 25181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 25281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // check if either CPU number or frequency changed 25381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (cpuNum != mCpuNum || cpukHz != mCpukHz) { 25481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mCpuNum = cpuNum; 25581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mCpukHz = cpukHz; 25681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // ignore sample for purposes of cycles 25781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent valid = false; 25881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 25981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 26081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if no change in CPU number or frequency, then record sample for cycle statistics 26181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (valid && mCpukHz > 0) { 26281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double cycles = wcNs * cpukHz * 0.000001; 26381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHzStats.sample(cycles); 26481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 26581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 26681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent unsigned n = mWcStats.n(); 26781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mCpuUsage.elapsed() is expensive, so don't call it every loop 26881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((n & 127) == 1) { 26981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent long long elapsed = mCpuUsage.elapsed(); 27081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { 27181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double perLoop = elapsed / (double) n; 27281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double perLoop100 = perLoop * 0.01; 27381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double perLoop1k = perLoop * 0.001; 27481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double mean = mWcStats.mean(); 27581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double stddev = mWcStats.stddev(); 27681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double minimum = mWcStats.minimum(); 27781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double maximum = mWcStats.maximum(); 27881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double meanCycles = mHzStats.mean(); 27981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double stddevCycles = mHzStats.stddev(); 28081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double minCycles = mHzStats.minimum(); 28181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double maxCycles = mHzStats.maximum(); 28281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mCpuUsage.resetElapsed(); 28381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWcStats.reset(); 28481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHzStats.reset(); 28581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGD("CPU usage for %s over past %.1f secs\n" 28681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent " (%u mixer loops at %.1f mean ms per loop):\n" 28781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" 28881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" 28981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", 29081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent title.string(), 29181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent elapsed * .000000001, n, perLoop * .000001, 29281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mean * .001, 29381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent stddev * .001, 29481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minimum * .001, 29581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent maximum * .001, 29681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mean / perLoop100, 29781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent stddev / perLoop100, 29881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minimum / perLoop100, 29981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent maximum / perLoop100, 30081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent meanCycles / perLoop1k, 30181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent stddevCycles / perLoop1k, 30281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minCycles / perLoop1k, 30381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent maxCycles / perLoop1k); 30481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 30581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 30681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 30781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 30881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}; 30981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 31081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 31181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ThreadBase 31281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 31381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 31481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, 31581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_devices_t outDevice, audio_devices_t inDevice, type_t type) 31681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent : Thread(false /*canCallJava*/), 31781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mType(type), 3189b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten mAudioFlinger(audioFlinger), 31970949c47fbae3f836d15f040551d7631be3ed7c2Glenn Kasten // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize 320deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten // are set by PlaybackThread::readOutputParameters_l() or 321deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten // RecordThread::readInputParameters_l() 322fd4779740ec3e9e865d5514464df26d015354388Eric Laurent //FIXME: mStandby should be true here. Is this some kind of hack? 32381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStandby(false), mOutDevice(outDevice), mInDevice(inDevice), 32481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id), 32581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mName will be set by concrete (non-virtual) subclass 32681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mDeathRecipient(new PMDeathRecipient(this)) 32781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 32881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 32981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 33081784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::ThreadBase::~ThreadBase() 33181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 332c6ae3c8a261794fd4445e4e152d1ada074a3f92fGlenn Kasten // mConfigEvents should be empty, but just in case it isn't, free the memory it owns 333c6ae3c8a261794fd4445e4e152d1ada074a3f92fGlenn Kasten mConfigEvents.clear(); 334c6ae3c8a261794fd4445e4e152d1ada074a3f92fGlenn Kasten 33581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // do not lock the mutex in destructor 33681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock_l(); 33781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mPowerManager != 0) { 33881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<IBinder> binder = mPowerManager->asBinder(); 33981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent binder->unlinkToDeath(mDeathRecipient); 34081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 34181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 34281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 343cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kastenstatus_t AudioFlinger::ThreadBase::readyToRun() 344cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten{ 345cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten status_t status = initCheck(); 346cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten if (status == NO_ERROR) { 347cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten ALOGI("AudioFlinger's thread %p ready to run", this); 348cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten } else { 349cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten ALOGE("No working audio driver found."); 350cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten } 351cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten return status; 352cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten} 353cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten 35481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::exit() 35581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 35681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("ThreadBase::exit"); 35781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // do any cleanup required for exit to succeed 35881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent preExit(); 35981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 36081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // This lock prevents the following race in thread (uniprocessor for illustration): 36181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if (!exitPending()) { 36281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // // context switch from here to exit() 36381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // // exit() calls requestExit(), what exitPending() observes 36481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // // exit() calls signal(), which is dropped since no waiters 36581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // // context switch back from exit() to here 36681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mWaitWorkCV.wait(...); 36781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // // now thread is hung 36881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // } 36981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AutoMutex lock(mLock); 37081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent requestExit(); 37181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitWorkCV.broadcast(); 37281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 37381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // When Thread::requestExitAndWait is made virtual and this method is renamed to 37481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" 37581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent requestExitAndWait(); 37681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 37781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 37881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) 37981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 38081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status; 38181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 38281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); 38381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 38481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3851035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent return sendSetParameterConfigEvent_l(keyValuePairs); 3861035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent} 3871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 3881035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// sendConfigEvent_l() must be called with ThreadBase::mLock held 3891035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// Can temporarily release the lock if waiting for a reply from processConfigEvents_l(). 3901035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentstatus_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event) 3911035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent{ 3921035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status_t status = NO_ERROR; 3931035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 3941035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mConfigEvents.add(event); 3951035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent ALOGV("sendConfigEvent_l() num events %d event %d", mConfigEvents.size(), event->mType); 39681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitWorkCV.signal(); 3971035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mLock.unlock(); 3981035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent { 3991035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent Mutex::Autolock _l(event->mLock); 4001035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent while (event->mWaitStatus) { 4011035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) { 4021035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent event->mStatus = TIMED_OUT; 4031035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent event->mWaitStatus = false; 4041035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 4051035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 4061035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = event->mStatus; 40781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 4081035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mLock.lock(); 40981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 41081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 41181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 41281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::sendIoConfigEvent(int event, int param) 41381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 41481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 41581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sendIoConfigEvent_l(event, param); 41681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 41781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 41881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// sendIoConfigEvent_l() must be called with ThreadBase::mLock held 41981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::sendIoConfigEvent_l(int event, int param) 42081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 4211035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, param); 4221035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sendConfigEvent_l(configEvent); 42381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 42481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 42581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// sendPrioConfigEvent_l() must be called with ThreadBase::mLock held 42681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio) 42781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 4281035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio); 4291035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sendConfigEvent_l(configEvent); 43081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 43181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4321035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held 4331035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentstatus_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair) 43481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 4351035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sp<ConfigEvent> configEvent = (ConfigEvent *)new SetParameterConfigEvent(keyValuePair); 4361035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent return sendConfigEvent_l(configEvent); 437f777331418a86cd9fd709af898ef24a69967aeb4Glenn Kasten} 438f777331418a86cd9fd709af898ef24a69967aeb4Glenn Kasten 4391c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::ThreadBase::sendCreateAudioPatchConfigEvent( 4401c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent const struct audio_patch *patch, 4411c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_patch_handle_t *handle) 4421c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{ 4431c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent Mutex::Autolock _l(mLock); 4441c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle); 4451c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status_t status = sendConfigEvent_l(configEvent); 4461c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent if (status == NO_ERROR) { 4471c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent CreateAudioPatchConfigEventData *data = 4481c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent (CreateAudioPatchConfigEventData *)configEvent->mData.get(); 4491c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent *handle = data->mHandle; 4501c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 4511c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent return status; 4521c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent} 4531c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 4541c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::ThreadBase::sendReleaseAudioPatchConfigEvent( 4551c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent const audio_patch_handle_t handle) 4561c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{ 4571c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent Mutex::Autolock _l(mLock); 4581c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle); 4591c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent return sendConfigEvent_l(configEvent); 4601c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent} 4611c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 4621c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 4632cfbf88b89854f30b295e8ae26a031edb8d712f8Glenn Kasten// post condition: mConfigEvents.isEmpty() 464021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurentvoid AudioFlinger::ThreadBase::processConfigEvents_l() 465f777331418a86cd9fd709af898ef24a69967aeb4Glenn Kasten{ 4661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent bool configChanged = false; 4671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 46881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent while (!mConfigEvents.isEmpty()) { 4691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent ALOGV("processConfigEvents_l() remaining events %d", mConfigEvents.size()); 4701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sp<ConfigEvent> event = mConfigEvents[0]; 47181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mConfigEvents.removeAt(0); 4721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent switch (event->mType) { 4733468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten case CFG_EVENT_PRIO: { 4741035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get(); 4751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // FIXME Need to understand why this has to be done asynchronously 4761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent int err = requestPriority(data->mPid, data->mTid, data->mPrio, 4773468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten true /*asynchronous*/); 4783468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten if (err != 0) { 4793468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", 4801035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent data->mPrio, data->mPid, data->mTid, err); 4813468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten } 4823468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten } break; 4833468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten case CFG_EVENT_IO: { 4841035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent IoConfigEventData *data = (IoConfigEventData *)event->mData.get(); 485021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent audioConfigChanged(data->mEvent, data->mParam); 4861035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } break; 4871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent case CFG_EVENT_SET_PARAMETER: { 4881035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get(); 4891035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) { 4901035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent configChanged = true; 491d5418eb594435c958d6c37fa9938161a0112adbdGlenn Kasten } 4923468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten } break; 4931c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent case CFG_EVENT_CREATE_AUDIO_PATCH: { 4941c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent CreateAudioPatchConfigEventData *data = 4951c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent (CreateAudioPatchConfigEventData *)event->mData.get(); 4961c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle); 4971c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } break; 4981c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent case CFG_EVENT_RELEASE_AUDIO_PATCH: { 4991c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent ReleaseAudioPatchConfigEventData *data = 5001c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent (ReleaseAudioPatchConfigEventData *)event->mData.get(); 5011c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent event->mStatus = releaseAudioPatch_l(data->mHandle); 5021c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } break; 5033468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten default: 5041035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType); 5053468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten break; 50681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 5071035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent { 5081035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent Mutex::Autolock _l(event->mLock); 5091035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (event->mWaitStatus) { 5101035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent event->mWaitStatus = false; 5111035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent event->mCond.signal(); 5121035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 5131035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 5141035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this); 5151035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 5161035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 5171035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (configChanged) { 5181035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent cacheParameters_l(); 51981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 52081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 52181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 522b220884bf3129253cc5bc8d030bc475411ea4911Marco NelissenString8 channelMaskToString(audio_channel_mask_t mask, bool output) { 523b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen String8 s; 524b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (output) { 525b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, "); 526b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, "); 527b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, "); 528b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, "); 529b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, "); 530b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, "); 531b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, "); 532b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, "); 533b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, "); 534b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, "); 535b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, "); 536b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,"); 537b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, "); 538b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, "); 539b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, "); 540b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, "); 541b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " ); 542b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " ); 543b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown, "); 544b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } else { 545b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, "); 546b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, "); 547b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, "); 548b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, "); 549b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, "); 550b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, "); 551b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, "); 552b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, "); 553b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, "); 554b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, "); 555b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, "); 556b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, "); 557b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, "); 558b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, "); 559b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown, "); 560b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 561b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen int len = s.length(); 562b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (s.length() > 2) { 563b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen char *str = s.lockBuffer(len); 564b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen s.unlockBuffer(len - 2); 565b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 566b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen return s; 567b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen} 568b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen 5690f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args __unused) 57081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 57181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const size_t SIZE = 256; 57281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char buffer[SIZE]; 57381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent String8 result; 57481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 57581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool locked = AudioFlinger::dumpTryLock(mLock); 57681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!locked) { 57787cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, "thread %p maybe dead locked\n", this); 578b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 579b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen 58087cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " I/O handle: %d\n", mId); 58187cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " TID: %d\n", getTid()); 58287cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Standby: %s\n", mStandby ? "yes" : "no"); 58387cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Sample rate: %u\n", mSampleRate); 58487cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " HAL frame count: %zu\n", mFrameCount); 58587cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " HAL buffer size: %u bytes\n", mBufferSize); 58687cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Channel Count: %u\n", mChannelCount); 58787cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Channel Mask: 0x%08x (%s)\n", mChannelMask, 588b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen channelMaskToString(mChannelMask, mType != RECORD).string()); 589463be250de73907965faa6a216c00312bf81e049Andy Hung dprintf(fd, " Format: 0x%x (%s)\n", mHALFormat, formatToString(mHALFormat)); 59087cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Frame size: %zu\n", mFrameSize); 59187cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Pending config events:"); 592b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numConfig = mConfigEvents.size(); 593b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (numConfig) { 594b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen for (size_t i = 0; i < numConfig; i++) { 595b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen mConfigEvents[i]->dump(buffer, SIZE); 59687cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, "\n %s", buffer); 597b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 59887cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, "\n"); 599b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } else { 60087cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " none\n"); 60181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 60281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 60381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (locked) { 60481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mLock.unlock(); 60581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 60681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 60781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 60881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) 60981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 61081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const size_t SIZE = 256; 61181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char buffer[SIZE]; 61281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent String8 result; 61381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 614b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numEffectChains = mEffectChains.size(); 6151d6fa7af1288b550faabe4ec2cf98684236723dbNarayan Kamath snprintf(buffer, SIZE, " %zu Effect Chains\n", numEffectChains); 61681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent write(fd, buffer, strlen(buffer)); 61781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 618b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen for (size_t i = 0; i < numEffectChains; ++i) { 61981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = mEffectChains[i]; 62081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 62181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->dump(fd, args); 62281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 62381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 62481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 62581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 626e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissenvoid AudioFlinger::ThreadBase::acquireWakeLock(int uid) 62781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 62881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 629e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen acquireWakeLock_l(uid); 63081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 63181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 632014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan KamathString16 AudioFlinger::ThreadBase::getWakeLockTag() 633014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath{ 634014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath switch (mType) { 635014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath case MIXER: 636014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath return String16("AudioMix"); 637014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath case DIRECT: 638014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath return String16("AudioDirectOut"); 639014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath case DUPLICATING: 640014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath return String16("AudioDup"); 641014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath case RECORD: 642014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath return String16("AudioIn"); 643014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath case OFFLOAD: 644014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath return String16("AudioOffload"); 645014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath default: 646014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath ALOG_ASSERT(false); 647014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath return String16("AudioUnknown"); 648014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath } 649014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath} 650014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath 651e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissenvoid AudioFlinger::ThreadBase::acquireWakeLock_l(int uid) 65281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 653462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen getPowerManager_l(); 65481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mPowerManager != 0) { 65581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<IBinder> binder = new BBinder(); 656e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen status_t status; 657e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen if (uid >= 0) { 658547789d25dc6bd6561553bcf6b384fb0d4fee834Eric Laurent status = mPowerManager->acquireWakeLockWithUid(POWERMANAGER_PARTIAL_WAKE_LOCK, 659e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen binder, 660014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath getWakeLockTag(), 661e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen String16("media"), 662e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen uid); 663e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen } else { 664547789d25dc6bd6561553bcf6b384fb0d4fee834Eric Laurent status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, 665e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen binder, 666014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath getWakeLockTag(), 667e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen String16("media")); 668e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen } 66981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (status == NO_ERROR) { 67081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWakeLockToken = binder; 67181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 67281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("acquireWakeLock_l() %s status %d", mName, status); 67381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 67481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 67581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 67681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::releaseWakeLock() 67781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 67881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 67981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock_l(); 68081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 68181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 68281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::releaseWakeLock_l() 68381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 68481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mWakeLockToken != 0) { 68581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("releaseWakeLock_l() %s", mName); 68681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mPowerManager != 0) { 68781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPowerManager->releaseWakeLock(mWakeLockToken, 0); 68881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 68981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWakeLockToken.clear(); 69081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 69181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 69281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 693462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissenvoid AudioFlinger::ThreadBase::updateWakeLockUids(const SortedVector<int> &uids) { 694462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen Mutex::Autolock _l(mLock); 695462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen updateWakeLockUids_l(uids); 696462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen} 697462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 698462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissenvoid AudioFlinger::ThreadBase::getPowerManager_l() { 699462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 700462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen if (mPowerManager == 0) { 701462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen // use checkService() to avoid blocking if power service is not up yet 702462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen sp<IBinder> binder = 703462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen defaultServiceManager()->checkService(String16("power")); 704462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen if (binder == 0) { 705462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen ALOGW("Thread %s cannot connect to the power manager service", mName); 706462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } else { 707462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mPowerManager = interface_cast<IPowerManager>(binder); 708462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen binder->linkToDeath(mDeathRecipient); 709462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } 710462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } 711462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen} 712462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 713462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissenvoid AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<int> &uids) { 714462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 715462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen getPowerManager_l(); 716462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen if (mWakeLockToken == NULL) { 717462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen ALOGE("no wake lock to update!"); 718462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen return; 719462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } 720462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen if (mPowerManager != 0) { 721462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen sp<IBinder> binder = new BBinder(); 722462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen status_t status; 723462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen status = mPowerManager->updateWakeLockUids(mWakeLockToken, uids.size(), uids.array()); 724462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen ALOGV("acquireWakeLock_l() %s status %d", mName, status); 725462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } 726462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen} 727462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 72881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::clearPowerManager() 72981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 73081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 73181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock_l(); 73281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPowerManager.clear(); 73381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 73481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 7350f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) 73681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 73781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<ThreadBase> thread = mThread.promote(); 73881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread != 0) { 73981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread->clearPowerManager(); 74081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 74181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("power manager service died !!!"); 74281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 74381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 74481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::setEffectSuspended( 74581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const effect_uuid_t *type, bool suspend, int sessionId) 74681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 74781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 74881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent setEffectSuspended_l(type, suspend, sessionId); 74981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 75081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 75181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::setEffectSuspended_l( 75281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const effect_uuid_t *type, bool suspend, int sessionId) 75381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 75481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(sessionId); 75581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 75681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (type != NULL) { 75781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setEffectSuspended_l(type, suspend); 75881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 75981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setEffectSuspendedAll_l(suspend); 76081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 76181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 76281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 76381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent updateSuspendedSessions_l(type, suspend, sessionId); 76481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 76581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 76681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) 76781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 76881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); 76981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (index < 0) { 77081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 77181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 77281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 77381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = 77481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSuspendedSessions.valueAt(index); 77581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 77681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < sessionEffects.size(); i++) { 77781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<SuspendedSessionDesc> desc = sessionEffects.valueAt(i); 77881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (int j = 0; j < desc->mRefCount; j++) { 77981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { 78081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setEffectSuspendedAll_l(true); 78181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 78281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", 78381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc->mType.timeLow); 78481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setEffectSuspended_l(&desc->mType, true); 78581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 78681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 78781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 78881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 78981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 79081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, 79181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool suspend, 79281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId) 79381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 79481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ssize_t index = mSuspendedSessions.indexOfKey(sessionId); 79581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 79681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; 79781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 79881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (suspend) { 79981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (index >= 0) { 80081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionEffects = mSuspendedSessions.valueAt(index); 80181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 80281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSuspendedSessions.add(sessionId, sessionEffects); 80381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 80481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 80581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (index < 0) { 80681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 80781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 80881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionEffects = mSuspendedSessions.valueAt(index); 80981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 81081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 81181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 81281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int key = EffectChain::kKeyForSuspendAll; 81381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (type != NULL) { 81481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent key = type->timeLow; 81581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 81681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent index = sessionEffects.indexOfKey(key); 81781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 81881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<SuspendedSessionDesc> desc; 81981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (suspend) { 82081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (index >= 0) { 82181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc = sessionEffects.valueAt(index); 82281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 82381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc = new SuspendedSessionDesc(); 82481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (type != NULL) { 82581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc->mType = *type; 82681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 82781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionEffects.add(key, desc); 82881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); 82981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 83081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc->mRefCount++; 83181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 83281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (index < 0) { 83381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 83481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 83581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc = sessionEffects.valueAt(index); 83681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (--desc->mRefCount == 0) { 83781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); 83881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionEffects.removeItemsAt(index); 83981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionEffects.isEmpty()) { 84081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("updateSuspendedSessions_l() restore removing session %d", 84181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionId); 84281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSuspendedSessions.removeItem(sessionId); 84381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 84481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 84581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 84681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!sessionEffects.isEmpty()) { 84781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); 84881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 84981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 85081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 85181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, 85281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool enabled, 85381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId) 85481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 85581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 85681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); 85781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 85881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 85981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, 86081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool enabled, 86181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId) 86281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 86381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mType != RECORD) { 86481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on 86581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // another session. This gives the priority to well behaved effect control panels 86681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // and applications not using global effects. 86781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect 86881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // global effects 86981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) { 87081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); 87181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 87281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 87381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 87481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(sessionId); 87581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 87681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->checkSuspendOnEffectEnabled(effect, enabled); 87781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 87881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 87981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 88081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held 88181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( 88281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<AudioFlinger::Client>& client, 88381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<IEffectClient>& effectClient, 88481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int32_t priority, 88581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId, 88681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect_descriptor_t *desc, 88781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int *enabled, 8889156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten status_t *status) 88981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 89081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectModule> effect; 89181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectHandle> handle; 89281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t lStatus; 89381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain; 89481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool chainCreated = false; 89581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool effectCreated = false; 89681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool effectRegistered = false; 89781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 89881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = initCheck(); 89981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR) { 90081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("createEffect_l() Audio driver not initialized."); 90181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 90281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 90381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 90498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Reject any effect on Direct output threads for now, since the format of 90598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo). 90698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mType == DIRECT) { 90798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung ALOGW("createEffect_l() Cannot add effect %s on Direct output type thread %s", 90898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung desc->name, mName); 90998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung lStatus = BAD_VALUE; 91098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung goto Exit; 91198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 91298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung 9139a59276fb465e492138e0576523b54079671e8f4Andy Hung // Reject any effect on multichannel sinks. 9149a59276fb465e492138e0576523b54079671e8f4Andy Hung // TODO: fix both format and multichannel issues with effects. 9159a59276fb465e492138e0576523b54079671e8f4Andy Hung if (mChannelCount != FCC_2) { 9169a59276fb465e492138e0576523b54079671e8f4Andy Hung ALOGW("createEffect_l() Cannot add effect %s for multichannel(%d) thread", 9179a59276fb465e492138e0576523b54079671e8f4Andy Hung desc->name, mChannelCount); 9189a59276fb465e492138e0576523b54079671e8f4Andy Hung lStatus = BAD_VALUE; 9199a59276fb465e492138e0576523b54079671e8f4Andy Hung goto Exit; 9209a59276fb465e492138e0576523b54079671e8f4Andy Hung } 9219a59276fb465e492138e0576523b54079671e8f4Andy Hung 9225baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent // Allow global effects only on offloaded and mixer threads 9235baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { 9245baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent switch (mType) { 9255baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent case MIXER: 9265baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent case OFFLOAD: 9275baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent break; 9285baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent case DIRECT: 9295baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent case DUPLICATING: 9305baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent case RECORD: 9315baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent default: 9325baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent ALOGW("createEffect_l() Cannot add global effect %s on thread %s", desc->name, mName); 9335baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent lStatus = BAD_VALUE; 9345baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent goto Exit; 9355baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent } 93681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 9375baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent 93881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Only Pre processor effects are allowed on input threads and only on input threads 93981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { 94081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d", 94181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc->name, desc->flags, mType); 94281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 94381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 94481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 94581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 94681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); 94781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 94881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // scope for mLock 94981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 95081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 95181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // check for existing effect chain with the requested audio session 95281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain = getEffectChain_l(sessionId); 95381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain == 0) { 95481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create a new chain for this session 95581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("createEffect_l() new effect chain for session %d", sessionId); 95681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain = new EffectChain(this, sessionId); 95781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent addEffectChain_l(chain); 95881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setStrategy(getStrategyForSession_l(sessionId)); 95981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chainCreated = true; 96081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 96181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect = chain->getEffectFromDesc_l(desc); 96281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 96381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 96481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); 96581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 96681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (effect == 0) { 96781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int id = mAudioFlinger->nextUniqueId(); 96881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Check CPU and memory usage 96981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); 97081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR) { 97181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 97281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 97381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effectRegistered = true; 97481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create a new effect module if none present in the chain 97581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect = new EffectModule(this, chain, desc, id, sessionId); 97681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = effect->status(); 97781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR) { 97881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 97981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 9805baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent effect->setOffloaded(mType == OFFLOAD, mId); 9815baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent 98281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = chain->addEffect_l(effect); 98381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR) { 98481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 98581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 98681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effectCreated = true; 98781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 98881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setDevice(mOutDevice); 98981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setDevice(mInDevice); 99081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setMode(mAudioFlinger->getMode()); 99181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setAudioSource(mAudioSource); 99281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 99381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create effect handle and connect it to effect module 99481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent handle = new EffectHandle(effect, client, effectClient, priority); 995e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten lStatus = handle->initCheck(); 996e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten if (lStatus == OK) { 997e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten lStatus = effect->addHandle(handle.get()); 998e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten } 99981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (enabled != NULL) { 100081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *enabled = (int)effect->isEnabled(); 100181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 100281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 100381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 100481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit: 100581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { 100681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 100781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (effectCreated) { 100881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->removeEffect_l(effect); 100981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 101081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (effectRegistered) { 101181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSystem::unregisterEffect(effect->id()); 101281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 101381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chainCreated) { 101481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent removeEffectChain_l(chain); 101581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 101681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent handle.clear(); 101781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 101881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 10199156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten *status = lStatus; 102081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return handle; 102181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 102281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 102381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(int sessionId, int effectId) 102481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 102581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 102681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return getEffect_l(sessionId, effectId); 102781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 102881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 102981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId) 103081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 103181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(sessionId); 103281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; 103381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 103481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 103581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and 103681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// PlaybackThread::mLock held 103781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) 103881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 103981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // check for existing effect chain with the requested audio session 104081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId = effect->sessionId(); 104181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(sessionId); 104281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool chainCreated = false; 104381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 10445baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(), 10455baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %x", 10465baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent this, effect->desc().name, effect->desc().flags); 10475baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent 104881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain == 0) { 104981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create a new chain for this session 105081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffect_l() new effect chain for session %d", sessionId); 105181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain = new EffectChain(this, sessionId); 105281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent addEffectChain_l(chain); 105381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setStrategy(getStrategyForSession_l(sessionId)); 105481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chainCreated = true; 105581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 105681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); 105781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 105881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain->getEffectFromId_l(effect->id()) != 0) { 105981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("addEffect_l() %p effect %s already present in chain %p", 106081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent this, effect->desc().name, chain.get()); 106181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 106281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 106381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 10645baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent effect->setOffloaded(mType == OFFLOAD, mId); 10655baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent 106681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status = chain->addEffect_l(effect); 106781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (status != NO_ERROR) { 106881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chainCreated) { 106981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent removeEffectChain_l(chain); 107081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 107181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 107281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 107381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 107481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setDevice(mOutDevice); 107581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setDevice(mInDevice); 107681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setMode(mAudioFlinger->getMode()); 107781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setAudioSource(mAudioSource); 107881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 107981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 108081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 108181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { 108281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 108381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("removeEffect_l() %p effect %p", this, effect.get()); 108481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect_descriptor_t desc = effect->desc(); 108581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 108681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent detachAuxEffect_l(effect->id()); 108781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 108881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 108981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = effect->chain().promote(); 109081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 109181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // remove effect chain if removing last effect 109281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain->removeEffect_l(effect) == 0) { 109381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent removeEffectChain_l(chain); 109481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 109581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 109681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); 109781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 109881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 109981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 110081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::lockEffectChains_l( 110181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<AudioFlinger::EffectChain> >& effectChains) 110281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 110381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effectChains = mEffectChains; 110481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 110581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains[i]->lock(); 110681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 110781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 110881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 110981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::unlockEffectChains( 111081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const Vector< sp<AudioFlinger::EffectChain> >& effectChains) 111181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 111281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < effectChains.size(); i++) { 111381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effectChains[i]->unlock(); 111481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 111581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 111681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 111781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId) 111881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 111981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 112081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return getEffectChain_l(sessionId); 112181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 112281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 112381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) const 112481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 112581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t size = mEffectChains.size(); 112681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < size; i++) { 112781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mEffectChains[i]->sessionId() == sessionId) { 112881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mEffectChains[i]; 112981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 113081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 113181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 113281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 113381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 113481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::setMode(audio_mode_t mode) 113581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 113681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 113781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t size = mEffectChains.size(); 113881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < size; i++) { 113981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains[i]->setMode_l(mode); 114081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 114181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 114281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 114381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::disconnectEffect(const sp<EffectModule>& effect, 114481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent EffectHandle *handle, 114581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool unpinIfLast) { 114681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 114781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 114881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("disconnectEffect() %p effect %p", this, effect.get()); 114981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // delete the effect module if removing last handle on it 115081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (effect->removeHandle(handle) == 0) { 115181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!effect->isPinned() || unpinIfLast) { 115281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent removeEffect_l(effect); 115381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSystem::unregisterEffect(effect->id()); 115481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 115581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 115681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 115781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 115883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::ThreadBase::getAudioPortConfig(struct audio_port_config *config) 115983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{ 116083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->type = AUDIO_PORT_TYPE_MIX; 116183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->ext.mix.handle = mId; 116283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->sample_rate = mSampleRate; 116383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->format = mFormat; 116483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->channel_mask = mChannelMask; 116583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| 116683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent AUDIO_PORT_CONFIG_FORMAT; 116783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent} 116883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 116983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 117081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 117181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Playback 117281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 117381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 117481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, 117581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamOut* output, 117681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t id, 117781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_devices_t device, 117881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent type_t type) 117981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent : ThreadBase(audioFlinger, id, device, AUDIO_DEVICE_NONE, type), 11802098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung mNormalFrameCount(0), mSinkBuffer(NULL), 11816146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision), 118269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBuffer(NULL), 118369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferSize(0), 118469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferFormat(AUDIO_FORMAT_INVALID), 118569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferValid(false), 11866146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision), 118798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBuffer(NULL), 118898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferSize(0), 118998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferFormat(AUDIO_FORMAT_INVALID), 119098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferValid(false), 1191c1fac191069774c7bfcb062edbb821ea56e7dbc0Glenn Kasten mSuspended(0), mBytesWritten(0), 1192462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mActiveTracksGeneration(0), 119381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mStreamTypes[] initialized in constructor body 119481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutput(output), 119581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), 119681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMixerStatus(MIXER_IDLE), 119781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMixerStatusIgnoringFastTracks(MIXER_IDLE), 119881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent standbyDelay(AudioFlinger::mStandbyTimeInNsecs), 1199bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining(0), 1200bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mCurrentWriteLength(0), 1201bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mUseAsyncWrite(false), 12023b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence(0), 12033b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence(0), 1204ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent mSignalPending(false), 120581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mScreenState(AudioFlinger::mScreenState), 120681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // index 0 is reserved for normal mixer's submix 1207bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mFastTrackAvailMask(((1 << FastMixerState::kMaxFastTracks) - 1) & ~1), 1208bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten // mLatchD, mLatchQ, 1209bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mLatchDValid(false), mLatchQValid(false) 121081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 121181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent snprintf(mName, kNameLength, "AudioOut_%X", id); 12129e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mName); 121381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 121481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Assumes constructor is called by AudioFlinger with it's mLock held, but 121581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it would be safer to explicitly pass initial masterVolume/masterMute as 121681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // parameter. 121781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // 121881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If the HAL we are using has support for master volume or master mute, 121981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // then do not attenuate or mute during mixing (just leave the volume at 1.0 122081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // and the mute set to false). 122181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterVolume = audioFlinger->masterVolume_l(); 122281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterMute = audioFlinger->masterMute_l(); 122381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutput && mOutput->audioHwDev) { 122481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutput->audioHwDev->canSetMasterVolume()) { 122581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterVolume = 1.0; 122681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 122781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 122881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutput->audioHwDev->canSetMasterMute()) { 122981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterMute = false; 123081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 123181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 123281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1233deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten readOutputParameters_l(); 123481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 123581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mStreamTypes[AUDIO_STREAM_CNT] is initialized by stream_type_t default constructor 123681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // There is no AUDIO_STREAM_MIN, and ++ operator does not compile 123766e4635cb09fadcaccf912f37c387396c428378aGlenn Kasten for (audio_stream_type_t stream = AUDIO_STREAM_MIN; stream < AUDIO_STREAM_CNT; 123881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent stream = (audio_stream_type_t) (stream + 1)) { 123981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream); 124081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); 124181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 124281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mStreamTypes[AUDIO_STREAM_CNT] exists but isn't explicitly initialized here, 124381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // because mAudioFlinger doesn't have one to copy from 124481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 124581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 124681784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread::~PlaybackThread() 124781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 12489e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten mAudioFlinger->unregisterWriter(mNBLogWriter); 1249010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung free(mSinkBuffer); 125069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung free(mMixerBuffer); 125198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung free(mEffectBuffer); 125281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 125381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 125481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) 125581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 125681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpInternals(fd, args); 125781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpTracks(fd, args); 125881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpEffectChains(fd, args); 125981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 126081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 12610f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args __unused) 126281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 126381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const size_t SIZE = 256; 126481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char buffer[SIZE]; 126581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent String8 result; 126681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1267b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.appendFormat(" Stream volumes in dB: "); 126881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { 126981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const stream_type_t *st = &mStreamTypes[i]; 127081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (i > 0) { 127181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result.appendFormat(", "); 127281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 127381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); 127481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (st->mute) { 127581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result.append("M"); 127681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 127781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 127881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result.append("\n"); 127981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent write(fd, result.string(), result.length()); 128081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result.clear(); 128181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1282b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen // These values are "raw"; they will wrap around. See prepareTracks_l() for a better way. 1283b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen FastTrackUnderruns underruns = getFastTrackUnderruns(0); 128487cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Normal mixer raw underrun counters: partial=%u empty=%u\n", 1285b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); 1286b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen 1287b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numtracks = mTracks.size(); 1288b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numactive = mActiveTracks.size(); 128987cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " %d Tracks", numtracks); 1290b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numactiveseen = 0; 1291b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (numtracks) { 129287cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " of which %d are active\n", numactive); 1293b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen Track::appendDumpHeader(result); 1294b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen for (size_t i = 0; i < numtracks; ++i) { 1295b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen sp<Track> track = mTracks[i]; 1296b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (track != 0) { 1297b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen bool active = mActiveTracks.indexOf(track) >= 0; 1298b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (active) { 1299b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen numactiveseen++; 1300b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 1301b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen track->dump(buffer, SIZE, active); 1302b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append(buffer); 1303b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 130481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1305b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } else { 1306b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append("\n"); 130781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1308b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (numactiveseen != numactive) { 1309b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen // some tracks in the active list were not in the tracks list 1310b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen snprintf(buffer, SIZE, " The following tracks are in the active list but" 1311b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen " not in the track list\n"); 1312b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append(buffer); 1313b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen Track::appendDumpHeader(result); 1314b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen for (size_t i = 0; i < numactive; ++i) { 1315b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen sp<Track> track = mActiveTracks[i].promote(); 1316b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (track != 0 && mTracks.indexOf(track) < 0) { 1317b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen track->dump(buffer, SIZE, true); 1318b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append(buffer); 1319b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 132081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 132181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1322b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen 132381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent write(fd, result.string(), result.size()); 132481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 132581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 132681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) 132781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 132887cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, "\nOutput thread %p:\n", this); 132987cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Normal frame count: %zu\n", mNormalFrameCount); 133087cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime)); 133187cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Total writes: %d\n", mNumWrites); 133287cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Delayed writes: %d\n", mNumDelayedWrites); 133387cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Blocked in write: %s\n", mInWrite ? "yes" : "no"); 133487cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Suspend count: %d\n", mSuspended); 133587cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Sink buffer : %p\n", mSinkBuffer); 133687cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Mixer buffer: %p\n", mMixerBuffer); 133787cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Effect buffer: %p\n", mEffectBuffer); 133887cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Fast track availMask=%#x\n", mFastTrackAvailMask); 133981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 134081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpBase(fd, args); 134181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 134281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 134381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Thread virtuals 134481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 134581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::onFirstRef() 134681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 134781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent run(mName, ANDROID_PRIORITY_URGENT_AUDIO); 134881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 134981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 135081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ThreadBase virtuals 135181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::preExit() 135281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 135381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV(" preExit()"); 135481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME this is using hard-coded strings but in the future, this functionality will be 135581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // converted to use audio HAL extensions required to support tunneling 135681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutput->stream->common.set_parameters(&mOutput->stream->common, "exiting=1"); 135781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 135881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 135981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held 136081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( 136181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<AudioFlinger::Client>& client, 136281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_stream_type_t streamType, 136381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t sampleRate, 136481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_format_t format, 136581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_channel_mask_t channelMask, 136674935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten size_t *pFrameCount, 136781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<IMemory>& sharedBuffer, 136881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId, 136981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent IAudioFlinger::track_flags_t *flags, 137081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t tid, 1371462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen int uid, 137281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t *status) 137381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 137474935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten size_t frameCount = *pFrameCount; 137581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track; 137681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t lStatus; 137781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 137881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool isTimed = (*flags & IAudioFlinger::TRACK_TIMED) != 0; 137981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 138081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // client expresses a preference for FAST, but we get the final say 138181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (*flags & IAudioFlinger::TRACK_FAST) { 138281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ( 138381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // not timed 138481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (!isTimed) && 138581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // either of these use cases: 138681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ( 138781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // use case 1: shared buffer with any frame count 138881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ( 138981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (sharedBuffer != 0) 139081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ) || 139181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // use case 2: callback handler and frame count is default or at least as large as HAL 139281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ( 139381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (tid != -1) && 139481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ((frameCount == 0) || 1395b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten (frameCount >= mFrameCount)) 139681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ) 139781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ) && 139881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // PCM data 139981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_is_linear_pcm(format) && 14009a59276fb465e492138e0576523b54079671e8f4Andy Hung // identical channel mask to sink, or mono in and stereo sink 14019a59276fb465e492138e0576523b54079671e8f4Andy Hung (channelMask == mChannelMask || 14029a59276fb465e492138e0576523b54079671e8f4Andy Hung (channelMask == AUDIO_CHANNEL_OUT_MONO && 14039a59276fb465e492138e0576523b54079671e8f4Andy Hung mChannelMask == AUDIO_CHANNEL_OUT_STEREO)) && 140481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // hardware sample rate 140581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (sampleRate == mSampleRate) && 140681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // normal mixer has an associated fast mixer 140781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent hasFastMixer() && 140881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // there are sufficient fast track slots available 140981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (mFastTrackAvailMask != 0) 141081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME test that MixerThread for this fast track has a capable output HAL 141181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME add a permission test also? 141281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ) { 141381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if frameCount not specified, then it defaults to fast mixer (HAL) frame count 141481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (frameCount == 0) { 14150349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten // read the fast track multiplier property the first time it is needed 14160349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit); 14170349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten if (ok != 0) { 14180349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten ALOGE("%s pthread_once failed: %d", __func__, ok); 14190349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten } 14200349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten frameCount = mFrameCount * sFastTrackMultiplier; 142181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 142281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%d mFrameCount=%d", 142381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent frameCount, mFrameCount); 142481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 142581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: isTimed=%d sharedBuffer=%p frameCount=%d " 14266146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung "mFrameCount=%d format=%#x mFormat=%#x isLinear=%d channelMask=%#x " 14276146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung "sampleRate=%u mSampleRate=%u " 142881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", 14296146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung isTimed, sharedBuffer.get(), frameCount, mFrameCount, format, mFormat, 143081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_is_linear_pcm(format), 143181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); 143281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *flags &= ~IAudioFlinger::TRACK_FAST; 143381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // For compatibility with AudioTrack calculation, buffer depth is forced 143481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // to be at least 2 x the normal mixer frame count and cover audio hardware latency. 143581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // This is probably too conservative, but legacy application code may depend on it. 143681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If you change this calculation, also review the start threshold which is related. 143781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream); 143881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate); 143981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (minBufCount < 2) { 144081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minBufCount = 2; 144181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 144281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t minFrameCount = mNormalFrameCount * minBufCount; 144381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (frameCount < minFrameCount) { 144481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent frameCount = minFrameCount; 144581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 144681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 144781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 144874935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten *pFrameCount = frameCount; 144981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1450c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten switch (mType) { 1451c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten 1452c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten case DIRECT: 1453993fa0603707e94ce259e95e56838a85b5ccbdc5Glenn Kasten if (audio_is_linear_pcm(format)) { 145481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { 1455cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x " 1456cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten "for output %p with format %#x", 145781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sampleRate, format, channelMask, mOutput, mFormat); 145881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 145981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 146081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 146181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1462c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten break; 1463c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten 1464c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten case OFFLOAD: 1465bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { 1466cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \"" 1467cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten "for output %p with format %#x", 1468bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent sampleRate, format, channelMask, mOutput, mFormat); 1469bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent lStatus = BAD_VALUE; 1470bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent goto Exit; 1471bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1472c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten break; 1473c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten 1474c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten default: 1475993fa0603707e94ce259e95e56838a85b5ccbdc5Glenn Kasten if (!audio_is_linear_pcm(format)) { 1476cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten ALOGE("createTrack_l() Bad parameter: format %#x \"" 1477cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten "for output %p with format %#x", 1478bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent format, mOutput, mFormat); 1479bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent lStatus = BAD_VALUE; 1480bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent goto Exit; 1481bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 148281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Resampler implementation limits input sampling rate to 2 x output sampling rate. 148381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sampleRate > mSampleRate*2) { 148481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); 148581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 148681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 148781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1488c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten break; 1489c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten 149081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 149181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 149281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = initCheck(); 149381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR) { 149415e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten ALOGE("createTrack_l() audio driver not initialized"); 149581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 149681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 149781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 149881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // scope for mLock 149981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 150081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 150181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // all tracks in same audio session must share the same routing strategy otherwise 150281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // conflicts will happen when tracks are moved from one output to another by audio policy 150381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // manager 150481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t strategy = AudioSystem::getStrategyForStream(streamType); 150581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 150681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> t = mTracks[i]; 150783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent if (t != 0 && t->isExternalTrack()) { 150881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); 150981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId == t->sessionId() && strategy != actual) { 151081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", 151181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent strategy, actual); 151281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 151381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 151481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 151581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 151681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 151781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 151881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!isTimed) { 151981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track = new Track(this, client, streamType, sampleRate, format, 152083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent channelMask, frameCount, NULL, sharedBuffer, 152183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent sessionId, uid, *flags, TrackBase::TYPE_DEFAULT); 152281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 152381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track = TimedTrack::create(this, client, streamType, sampleRate, format, 1524462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen channelMask, frameCount, sharedBuffer, sessionId, uid); 152581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1526030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten 1527030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten // new Track always returns non-NULL, 1528030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten // but TimedTrack::create() is a factory that could fail by returning NULL 1529030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY; 1530030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten if (lStatus != NO_ERROR) { 15310cde076ddb283c84c3801a2df4cc3df99bd1577fGlenn Kasten ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus); 153203e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George // track must be cleared from the caller as the caller has the AF lock 153381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 153481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 153581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mTracks.add(track); 153681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 153781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(sessionId); 153881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 153981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); 154081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->setMainBuffer(chain->inBuffer()); 154181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); 154281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->incTrackCnt(); 154381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 154481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 154581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { 154681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t callingPid = IPCThreadState::self()->getCallingPid(); 154781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, 154881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // so ask activity manager to do this on our behalf 154981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); 155081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 155181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 155281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 155381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = NO_ERROR; 155481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 155581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit: 15569156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten *status = lStatus; 155781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return track; 155881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 155981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 156081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const 156181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 156281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return latency; 156381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 156481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 156581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::latency() const 156681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 156781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 156881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return latency_l(); 156981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 157081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::latency_l() const 157181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 157281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (initCheck() == NO_ERROR) { 157381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return correctLatency_l(mOutput->stream->get_latency(mOutput->stream)); 157481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 157581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 157681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 157781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 157881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 157981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::setMasterVolume(float value) 158081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 158181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 158281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Don't apply master volume in SW if our HAL can do it for us. 158381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutput && mOutput->audioHwDev && 158481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutput->audioHwDev->canSetMasterVolume()) { 158581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterVolume = 1.0; 158681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 158781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterVolume = value; 158881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 158981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 159081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 159181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::setMasterMute(bool muted) 159281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 159381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 159481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Don't apply master mute in SW if our HAL can do it for us. 159581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutput && mOutput->audioHwDev && 159681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutput->audioHwDev->canSetMasterMute()) { 159781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterMute = false; 159881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 159981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterMute = muted; 160081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 160181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 160281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 160381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) 160481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 160581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 160681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStreamTypes[stream].volume = value; 1607ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent broadcast_l(); 160881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 160981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 161081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) 161181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 161281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 161381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStreamTypes[stream].mute = muted; 1614ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent broadcast_l(); 161581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 161681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 161781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentfloat AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const 161881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 161981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 162081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mStreamTypes[stream].volume; 162181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 162281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 162381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// addTrack_l() must be called with ThreadBase::mLock held 162481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) 162581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 162681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status = ALREADY_EXISTS; 162781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 162881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // set retry count for buffer fill 162981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mRetryCount = kMaxTrackStartupRetries; 163081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mActiveTracks.indexOf(track) < 0) { 163181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // the track is newly added, make sure it fills up all its 163281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // buffers before playing. This is to ensure the client will 163381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // effectively get the latency it requested. 163483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent if (track->isExternalTrack()) { 1635bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent TrackBase::track_state state = track->mState; 1636bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mLock.unlock(); 1637bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent status = AudioSystem::startOutput(mId, track->streamType(), track->sessionId()); 1638bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mLock.lock(); 1639bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // abort track was stopped/paused while we released the lock 1640bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (state != track->mState) { 1641bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (status == NO_ERROR) { 1642bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mLock.unlock(); 1643bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioSystem::stopOutput(mId, track->streamType(), track->sessionId()); 1644bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mLock.lock(); 1645bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1646bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return INVALID_OPERATION; 1647bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1648bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // abort if start is rejected by audio policy manager 1649bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (status != NO_ERROR) { 1650bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return PERMISSION_DENIED; 1651bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1652bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent#ifdef ADD_BATTERY_DATA 1653bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // to track the speaker usage 1654bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); 1655bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent#endif 1656bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1657bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 16589f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten track->mFillingUpStatus = track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING; 165981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mResetDone = false; 166081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mPresentationCompleteFrames = 0; 166181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mActiveTracks.add(track); 1662462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mWakeLockUids.add(track->uid()); 1663462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mActiveTracksGeneration++; 1664fd4779740ec3e9e865d5514464df26d015354388Eric Laurent mLatestActiveTrack = track; 1665d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent sp<EffectChain> chain = getEffectChain_l(track->sessionId()); 1666d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent if (chain != 0) { 1667d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), 1668d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent track->sessionId()); 1669d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent chain->incActiveTrackCnt(); 167081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 167181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 167281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = NO_ERROR; 167381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 167481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 16754c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George onAddNewTrack_l(); 167681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 167781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 167881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1679bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) 168081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 1681bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->terminate(); 168281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // active tracks are removed by threadLoop() 1683bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent bool trackActive = (mActiveTracks.indexOf(track) >= 0); 1684bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mState = TrackBase::STOPPED; 1685bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (!trackActive) { 168681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent removeTrack_l(track); 1687ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) { 1688bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mState = TrackBase::STOPPING_1; 168981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1690bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1691bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return trackActive; 169281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 169381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 169481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) 169581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 169681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); 169781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mTracks.remove(track); 169881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent deleteTrackName_l(track->name()); 169981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // redundant as track is about to be destroyed, for dumpsys only 170081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mName = -1; 170181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isFastTrack()) { 170281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int index = track->mFastIndex; 170381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(0 < index && index < (int)FastMixerState::kMaxFastTracks); 170481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); 170581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastTrackAvailMask |= 1 << index; 170681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // redundant as track is about to be destroyed, for dumpsys only 170781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mFastIndex = -1; 170881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 170981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(track->sessionId()); 171081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 171181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->decTrackCnt(); 171281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 171381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 171481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1715ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurentvoid AudioFlinger::PlaybackThread::broadcast_l() 1716bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 1717bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Thread could be blocked waiting for async 1718bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // so signal it to handle state changes immediately 1719bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // If threadLoop is currently unlocked a signal of mWaitWorkCV will 1720bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // be lost so we also flag to prevent it blocking on mWaitWorkCV 1721bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mSignalPending = true; 1722ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent mWaitWorkCV.broadcast(); 1723bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 1724bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 172581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentString8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) 172681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 172781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 172881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (initCheck() != NO_ERROR) { 1729d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten return String8(); 173081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 173181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1732d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten char *s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); 1733d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten const String8 out_s8(s); 173481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent free(s); 173581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return out_s8; 173681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 173781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1738021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurentvoid AudioFlinger::PlaybackThread::audioConfigChanged(int event, int param) { 173981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSystem::OutputDescriptor desc; 174081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void *param2 = NULL; 174181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1742021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent ALOGV("PlaybackThread::audioConfigChanged, thread %p, event %d, param %d", this, event, 174381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent param); 174481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 174581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent switch (event) { 174681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case AudioSystem::OUTPUT_OPENED: 174781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case AudioSystem::OUTPUT_CONFIG_CHANGED: 1748fad226abd12435dbcd232f7de396f1a097b2bd5fGlenn Kasten desc.channelMask = mChannelMask; 174981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc.samplingRate = mSampleRate; 175081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc.format = mFormat; 175181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc.frameCount = mNormalFrameCount; // FIXME see 175281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // AudioFlinger::frameCount(audio_io_handle_t) 17531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent desc.latency = latency_l(); 175481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent param2 = &desc; 175581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 175681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 175781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case AudioSystem::STREAM_CONFIG_CHANGED: 175881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent param2 = ¶m; 175981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case AudioSystem::OUTPUT_CLOSED: 176081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent default: 176181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 176281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1763021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent mAudioFlinger->audioConfigChanged(event, mId, param2); 176481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 176581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1766bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::writeCallback() 1767bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 1768bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 17693b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->resetWriteBlocked(); 1770bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 1771bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1772bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::drainCallback() 1773bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 1774bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 17753b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->resetDraining(); 1776bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 1777bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 17783b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence) 1779bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 1780bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 17813b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // reject out of sequence requests 17823b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) { 17833b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence &= ~1; 1784bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mWaitWorkCV.signal(); 1785bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1786bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 1787bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 17883b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence) 1789bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 1790bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 17913b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // reject out of sequence requests 17923b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if ((mDrainSequence & 1) && (sequence == mDrainSequence)) { 17933b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence &= ~1; 1794bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mWaitWorkCV.signal(); 1795bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1796bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 1797bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1798bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// static 1799bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentint AudioFlinger::PlaybackThread::asyncCallback(stream_callback_event_t event, 18000f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten void *param __unused, 1801bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent void *cookie) 1802bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 1803bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioFlinger::PlaybackThread *me = (AudioFlinger::PlaybackThread *)cookie; 1804bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("asyncCallback() event %d", event); 1805bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent switch (event) { 1806bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent case STREAM_CBK_EVENT_WRITE_READY: 1807bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent me->writeCallback(); 1808bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent break; 1809bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent case STREAM_CBK_EVENT_DRAIN_READY: 1810bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent me->drainCallback(); 1811bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent break; 1812bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent default: 1813bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGW("asyncCallback() unknown event %d", event); 1814bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent break; 1815bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1816bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return 0; 1817bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 1818bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1819deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kastenvoid AudioFlinger::PlaybackThread::readOutputParameters_l() 182081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 1821adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten // unfortunately we have no way of recovering from errors here, hence the LOG_ALWAYS_FATAL 182281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common); 182381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common); 18247fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten if (!audio_is_output_channel(mChannelMask)) { 1825adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask); 18267fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten } 18279a59276fb465e492138e0576523b54079671e8f4Andy Hung if ((mType == MIXER || mType == DUPLICATING) 18289a59276fb465e492138e0576523b54079671e8f4Andy Hung && !isValidPcmSinkChannelMask(mChannelMask)) { 18299a59276fb465e492138e0576523b54079671e8f4Andy Hung LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output", 18309a59276fb465e492138e0576523b54079671e8f4Andy Hung mChannelMask); 18317fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten } 1832e541269be94f3a1072932d51537905b120ef4733Andy Hung mChannelCount = audio_channel_count_from_out_mask(mChannelMask); 1833463be250de73907965faa6a216c00312bf81e049Andy Hung mHALFormat = mOutput->stream->common.get_format(&mOutput->stream->common); 1834463be250de73907965faa6a216c00312bf81e049Andy Hung mFormat = mHALFormat; 18357fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten if (!audio_is_valid_format(mFormat)) { 1836adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat); 18377fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten } 18386146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung if ((mType == MIXER || mType == DUPLICATING) 18396146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung && !isValidPcmSinkFormat(mFormat)) { 18406146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung LOG_FATAL("HAL format %#x not supported for mixed output", 18416146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung mFormat); 18427fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten } 1843665470b36f202bcc8ee2f7417f68fd2608dd07c1Eric Laurent mFrameSize = audio_stream_out_frame_size(mOutput->stream); 184470949c47fbae3f836d15f040551d7631be3ed7c2Glenn Kasten mBufferSize = mOutput->stream->common.get_buffer_size(&mOutput->stream->common); 184570949c47fbae3f836d15f040551d7631be3ed7c2Glenn Kasten mFrameCount = mBufferSize / mFrameSize; 184681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mFrameCount & 15) { 184781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("HAL output buffer size is %u frames but AudioMixer requires multiples of 16 frames", 184881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFrameCount); 184981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 185081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1851bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if ((mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) && 1852bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent (mOutput->stream->set_callback != NULL)) { 1853bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mOutput->stream->set_callback(mOutput->stream, 1854bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioFlinger::PlaybackThread::asyncCallback, this) == 0) { 1855bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mUseAsyncWrite = true; 18564de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent mCallbackThread = new AudioFlinger::AsyncCallbackThread(this); 1857bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1858bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1859bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 186009a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung // Calculate size of normal sink buffer relative to the HAL output buffer size 186181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double multiplier = 1.0; 186281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mType == MIXER && (kUseFastMixer == FastMixer_Static || 186381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent kUseFastMixer == FastMixer_Dynamic)) { 186409a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000; 186509a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000; 186681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer 186781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minNormalFrameCount = (minNormalFrameCount + 15) & ~15; 186881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent maxNormalFrameCount = maxNormalFrameCount & ~15; 186981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (maxNormalFrameCount < minNormalFrameCount) { 187081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent maxNormalFrameCount = minNormalFrameCount; 187181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 187281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent multiplier = (double) minNormalFrameCount / (double) mFrameCount; 187381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (multiplier <= 1.0) { 187481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent multiplier = 1.0; 187581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (multiplier <= 2.0) { 187681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (2 * mFrameCount <= maxNormalFrameCount) { 187781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent multiplier = 2.0; 187881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 187981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent multiplier = (double) maxNormalFrameCount / (double) mFrameCount; 188081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 188181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 188281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // prefer an even multiplier, for compatibility with doubling of fast tracks due to HAL 188309a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung // SRC (it would be unusual for the normal sink buffer size to not be a multiple of fast 188481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track, but we sometimes have to do this to satisfy the maximum frame count 188581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // constraint) 188681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME this rounding up should not be done if no HAL SRC 188781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t truncMult = (uint32_t) multiplier; 188881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((truncMult & 1)) { 188981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((truncMult + 1) * mFrameCount <= maxNormalFrameCount) { 189081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ++truncMult; 189181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 189281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 189381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent multiplier = (double) truncMult; 189481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 189581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 189681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalFrameCount = multiplier * mFrameCount; 189781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // round up to nearest 16 frames to satisfy AudioMixer 1898ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent if (mType == MIXER || mType == DUPLICATING) { 1899ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent mNormalFrameCount = (mNormalFrameCount + 15) & ~15; 1900ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent } 190109a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung ALOGI("HAL output buffer size %u frames, normal sink buffer size %u frames", mFrameCount, 190281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalFrameCount); 190381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1904010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // mSinkBuffer is the sink buffer. Size is always multiple-of-16 frames. 1905010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // Originally this was int16_t[] array, need to remove legacy implications. 1906010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung free(mSinkBuffer); 1907010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung mSinkBuffer = NULL; 19085b10a2037a835e790994b9ebec3c2e55052f1f3bAndy Hung // For sink buffer size, we use the frame size from the downstream sink to avoid problems 19095b10a2037a835e790994b9ebec3c2e55052f1f3bAndy Hung // with non PCM formats for compressed music, e.g. AAC, and Offload threads. 19105b10a2037a835e790994b9ebec3c2e55052f1f3bAndy Hung const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; 1911010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); 191281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 191369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // We resize the mMixerBuffer according to the requirements of the sink buffer which 191469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // drives the output. 191569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung free(mMixerBuffer); 191669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBuffer = NULL; 191769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung if (mMixerBufferEnabled) { 191869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // also valid: AUDIO_FORMAT_PCM_16_BIT. 191969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferSize = mNormalFrameCount * mChannelCount 192069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * audio_bytes_per_sample(mMixerBufferFormat); 192169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize); 192269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung } 192398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung free(mEffectBuffer); 192498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBuffer = NULL; 192598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mEffectBufferEnabled) { 192698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferFormat = AUDIO_FORMAT_PCM_16_BIT; // Note: Effects support 16b only 192798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferSize = mNormalFrameCount * mChannelCount 192898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung * audio_bytes_per_sample(mEffectBufferFormat); 192998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize); 193098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 193169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung 193281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // force reconfiguration of effect chains and engines to take new buffer size and audio 193381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // parameters into account 1934deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten // Note that mLock is not held when readOutputParameters_l() is called from the constructor 193581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // but in this case nothing is done below as no audio sessions have effect yet so it doesn't 193681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // matter. 193781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains 193881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<EffectChain> > effectChains = mEffectChains; 193981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < effectChains.size(); i ++) { 194081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); 194181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 194281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 194381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 194481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1945377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETITstatus_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) 194681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 194781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (halFrames == NULL || dspFrames == NULL) { 194881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 194981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 195081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 195181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (initCheck() != NO_ERROR) { 195281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return INVALID_OPERATION; 195381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 195481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t framesWritten = mBytesWritten / mFrameSize; 195581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *halFrames = framesWritten; 195681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 195781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (isSuspended()) { 195881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // return an estimation of rendered frames when the output is suspended 195981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t latencyFrames = (latency_l() * mSampleRate) / 1000; 196081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *dspFrames = framesWritten >= latencyFrames ? framesWritten - latencyFrames : 0; 196181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 196281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 1963377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT status_t status; 1964377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT uint32_t frames; 1965377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT status = mOutput->stream->get_render_position(mOutput->stream, &frames); 1966377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT *dspFrames = (size_t)frames; 1967377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT return status; 196881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 196981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 197081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 197181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) const 197281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 197381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 197481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t result = 0; 197581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (getEffectChain_l(sessionId) != 0) { 197681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result = EFFECT_SESSION; 197781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 197881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 197981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 198081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mTracks[i]; 19815736c35b841de56ce394b4879389f669b61425e6Glenn Kasten if (sessionId == track->sessionId() && !track->isInvalid()) { 198281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result |= TRACK_SESSION; 198381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 198481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 198581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 198681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 198781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return result; 198881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 198981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 199081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId) 199181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 199281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that 199381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it is moved to correct output by audio policy manager when A2DP is connected or disconnected 199481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { 199581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); 199681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 199781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 199881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mTracks[i]; 19995736c35b841de56ce394b4879389f669b61425e6Glenn Kasten if (sessionId == track->sessionId() && !track->isInvalid()) { 200081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return AudioSystem::getStrategyForStream(track->streamType()); 200181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 200281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 200381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); 200481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 200581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 200681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 200781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const 200881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 200981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 201081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mOutput; 201181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 201281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 201381784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() 201481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 201581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 201681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamOut *output = mOutput; 201781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutput = NULL; 201881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME FastMixer might also have a raw ptr to mOutputSink; 201981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // must push a NULL and wait for ack 202081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutputSink.clear(); 202181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPipeSink.clear(); 202281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink.clear(); 202381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return output; 202481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 202581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 202681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// this method must always be called either with ThreadBase mLock held or inside the thread loop 202781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_stream_t* AudioFlinger::PlaybackThread::stream() const 202881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 202981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutput == NULL) { 203081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NULL; 203181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 203281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return &mOutput->stream->common; 203381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 203481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 203581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const 203681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 203781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); 203881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 203981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 204081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) 204181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 204281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!isValidSyncEvent(event)) { 204381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 204481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 204581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 204681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 204781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 204881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 204981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mTracks[i]; 205081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (event->triggerSession() == track->sessionId()) { 205181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (void) track->setSyncEvent(event); 205281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 205381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 205481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 205581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 205681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NAME_NOT_FOUND; 205781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 205881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 205981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const 206081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 206181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; 206281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 206381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 206481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::threadLoop_removeTracks( 206581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const Vector< sp<Track> >& tracksToRemove) 206681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 206781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t count = tracksToRemove.size(); 206834fca34606b448e6b71c2942f63cb13a0aebd620Glenn Kasten if (count > 0) { 206981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0 ; i < count ; i++) { 207081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<Track>& track = tracksToRemove.itemAt(i); 207183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent if (track->isExternalTrack()) { 207281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSystem::stopOutput(mId, track->streamType(), track->sessionId()); 2073bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent#ifdef ADD_BATTERY_DATA 2074bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // to track the speaker usage 2075bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); 2076bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent#endif 2077bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (track->isTerminated()) { 2078bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioSystem::releaseOutput(mId); 2079bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 208081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 208181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 208281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 208381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 208481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 208581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::checkSilentMode_l() 208681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 208781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!mMasterMute) { 208881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char value[PROPERTY_VALUE_MAX]; 208981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (property_get("ro.audio.silent", value, "0") > 0) { 209081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char *endptr; 209181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent unsigned long ul = strtoul(value, &endptr, 0); 209281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (*endptr == '\0' && ul != 0) { 209381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGD("Silence is golden"); 209481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The setprop command will not allow a property to be changed after 209581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // the first time it is set, so we don't have to worry about un-muting. 209681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent setMasterMute_l(true); 209781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 209881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 209981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 210081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 210181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 210281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// shared by MIXER and DIRECT, overridden by DUPLICATING 2103bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentssize_t AudioFlinger::PlaybackThread::threadLoop_write() 210481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 210581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME rewrite to reduce number of system calls 210681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mLastWriteTime = systemTime(); 210781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mInWrite = true; 2108bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ssize_t bytesWritten; 2109010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung const size_t offset = mCurrentWriteLength - mBytesRemaining; 211081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 211181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If an NBAIO sink is present, use it to write the normal mixer's submix 211281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mNormalSink != 0) { 2113010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung const size_t count = mBytesRemaining / mFrameSize; 2114010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung 21152d590964aa58e137d17a43e095e6443dd0fe2e98Simon Wilson ATRACE_BEGIN("write"); 211681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // update the setpoint when AudioFlinger::mScreenState changes 211781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t screenState = AudioFlinger::mScreenState; 211881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (screenState != mScreenState) { 211981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mScreenState = screenState; 212081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); 212181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (pipe != NULL) { 212281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pipe->setAvgFrames((mScreenState & 1) ? 212381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); 212481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 212581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 2126010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count); 21272d590964aa58e137d17a43e095e6443dd0fe2e98Simon Wilson ATRACE_END(); 212881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (framesWritten > 0) { 2129010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung bytesWritten = framesWritten * mFrameSize; 213081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 213181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bytesWritten = framesWritten; 213281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 2133767094dd98b01baf21de2ad09c27b3c98776cf73Glenn Kasten status_t status = mNormalSink->getTimestamp(mLatchD.mTimestamp); 2134bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten if (status == NO_ERROR) { 2135bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten size_t totalFramesWritten = mNormalSink->framesWritten(); 2136bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten if (totalFramesWritten >= mLatchD.mTimestamp.mPosition) { 2137bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mLatchD.mUnpresentedFrames = totalFramesWritten - mLatchD.mTimestamp.mPosition; 2138bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mLatchDValid = true; 2139bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten } 2140bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten } 214181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // otherwise use the HAL / AudioStreamOut directly 214281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 2143bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Direct output and offload threads 2144010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung 2145bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mUseAsyncWrite) { 21463b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request"); 21473b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence += 2; 21483b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence |= 1; 2149bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 21503b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setWriteBlocked(mWriteAckSequence); 2151bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2152767094dd98b01baf21de2ad09c27b3c98776cf73Glenn Kasten // FIXME We should have an implementation of timestamps for direct output threads. 2153767094dd98b01baf21de2ad09c27b3c98776cf73Glenn Kasten // They are used e.g for multichannel PCM playback over HDMI. 2154bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent bytesWritten = mOutput->stream->write(mOutput->stream, 21552098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung (char *)mSinkBuffer + offset, mBytesRemaining); 2156bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mUseAsyncWrite && 2157bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) { 2158bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // do not wait for async callback in case of error of full write 21593b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence &= ~1; 2160bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 21613b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setWriteBlocked(mWriteAckSequence); 2162bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 216381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 216481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 216581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNumWrites++; 216681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mInWrite = false; 2167fd4779740ec3e9e865d5514464df26d015354388Eric Laurent mStandby = false; 2168bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return bytesWritten; 2169bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 2170bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 2171bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::threadLoop_drain() 2172bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 2173bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mOutput->stream->drain) { 2174bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full"); 2175bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mUseAsyncWrite) { 21763b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request"); 21773b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence |= 1; 2178bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 21793b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setDraining(mDrainSequence); 2180bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2181bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mOutput->stream->drain(mOutput->stream, 2182bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent (mMixerStatus == MIXER_DRAIN_TRACK) ? AUDIO_DRAIN_EARLY_NOTIFY 2183bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent : AUDIO_DRAIN_ALL); 2184bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2185bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 2186bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 2187bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::threadLoop_exit() 2188bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 2189bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Default implementation has nothing to do 219081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 219181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 219281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent/* 219381784c37c61b09289654b979567a42bf73cd2b12Eric LaurentThe derived values that are cached: 219425c2dac12114699e90deb1c579cadebce7b91a97Andy Hung - mSinkBufferSize from frame count * frame size 219581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - activeSleepTime from activeSleepTimeUs() 219681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - idleSleepTime from idleSleepTimeUs() 219781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - standbyDelay from mActiveSleepTimeUs (DIRECT only) 219881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - maxPeriod from frame count and sample rate (MIXER only) 219981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 220081784c37c61b09289654b979567a42bf73cd2b12Eric LaurentThe parameters that affect these derived values are: 220181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - frame count 220281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - frame size 220381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - sample rate 220481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - device type: A2DP or not 220581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - device latency 220681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - format: PCM or not 220781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - active sleep time 220881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - idle sleep time 220981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent*/ 221081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 221181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::cacheParameters_l() 221281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 221325c2dac12114699e90deb1c579cadebce7b91a97Andy Hung mSinkBufferSize = mNormalFrameCount * mFrameSize; 221481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent activeSleepTime = activeSleepTimeUs(); 221581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent idleSleepTime = idleSleepTimeUs(); 221681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 221781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 221881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType) 221981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 22207c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d", 222181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent this, streamType, mTracks.size()); 222281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 222381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 222481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t size = mTracks.size(); 222581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < size; i++) { 222681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> t = mTracks[i]; 222781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (t->streamType() == streamType) { 22285736c35b841de56ce394b4879389f669b61425e6Glenn Kasten t->invalidate(); 222981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 223081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 223181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 223281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 223381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) 223481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 223581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int session = chain->sessionId(); 2236010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung int16_t* buffer = reinterpret_cast<int16_t*>(mEffectBufferEnabled 2237010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung ? mEffectBuffer : mSinkBuffer); 223881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool ownsBuffer = false; 223981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 224081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); 224181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (session > 0) { 224281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Only one effect chain can be present in direct output thread and it uses 22432098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung // the sink buffer as input 224481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mType != DIRECT) { 224581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t numSamples = mNormalFrameCount * mChannelCount; 224681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent buffer = new int16_t[numSamples]; 224781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent memset(buffer, 0, numSamples * sizeof(int16_t)); 224881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); 224981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ownsBuffer = true; 225081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 225181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 225281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Attach all tracks with same session ID to this chain. 225381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 225481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mTracks[i]; 225581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (session == track->sessionId()) { 225681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), 225781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent buffer); 225881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->setMainBuffer(buffer); 225981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->incTrackCnt(); 226081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 226181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 226281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 226381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // indicate all active tracks in the chain 226481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { 226581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mActiveTracks[i].promote(); 226681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track == 0) { 226781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 226881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 226981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (session == track->sessionId()) { 227081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); 227181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->incActiveTrackCnt(); 227281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 227381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 227481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 227581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 227681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setInBuffer(buffer, ownsBuffer); 2277010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung chain->setOutBuffer(reinterpret_cast<int16_t*>(mEffectBufferEnabled 2278010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung ? mEffectBuffer : mSinkBuffer)); 227981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect 228081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // chains list in order to be processed last as it contains output stage effects 228181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before 228281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // session AUDIO_SESSION_OUTPUT_STAGE to be processed 228381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // after track specific effects and before output stage 228481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and 228581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX 228681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Effect chain for other sessions are inserted at beginning of effect 228781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // chains list to be processed before output mix effects. Relative order between other 228881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // sessions is not important 228981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t size = mEffectChains.size(); 229081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t i = 0; 229181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (i = 0; i < size; i++) { 229281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mEffectChains[i]->sessionId() < session) { 229381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 229481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 229581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 229681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains.insertAt(chain, i); 229781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent checkSuspendOnAddEffectChain_l(chain); 229881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 229981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 230081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 230181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 230281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsize_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) 230381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 230481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int session = chain->sessionId(); 230581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 230681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); 230781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 230881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 230981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain == mEffectChains[i]) { 231081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains.removeAt(i); 231181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // detach all active tracks from the chain 231281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { 231381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mActiveTracks[i].promote(); 231481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track == 0) { 231581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 231681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 231781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (session == track->sessionId()) { 231881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", 231981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain.get(), session); 232081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->decActiveTrackCnt(); 232181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 232281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 232381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 232481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // detach all tracks with same session ID from this chain 232581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 232681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mTracks[i]; 232781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (session == track->sessionId()) { 2328010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung track->setMainBuffer(reinterpret_cast<int16_t*>(mSinkBuffer)); 232981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->decTrackCnt(); 233081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 233181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 233281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 233381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 233481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 233581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mEffectChains.size(); 233681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 233781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 233881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::attachAuxEffect( 233981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) 234081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 234181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 234281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return attachAuxEffect_l(track, EffectId); 234381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 234481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 234581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::attachAuxEffect_l( 234681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) 234781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 234881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status = NO_ERROR; 234981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 235081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (EffectId == 0) { 235181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->setAuxBuffer(0, NULL); 235281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 235381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX 235481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); 235581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (effect != 0) { 235681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 235781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); 235881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 235981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = INVALID_OPERATION; 236081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 236181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 236281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = BAD_VALUE; 236381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 236481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 236581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 236681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 236781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 236881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) 236981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 237081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 237181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mTracks[i]; 237281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->auxEffectId() == effectId) { 237381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent attachAuxEffect_l(track, 0); 237481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 237581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 237681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 237781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 237881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::PlaybackThread::threadLoop() 237981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 238081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<Track> > tracksToRemove; 238181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 238281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent standbyTime = systemTime(); 238381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 238481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // MIXER 238581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent nsecs_t lastWarning = 0; 238681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 238781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // DUPLICATING 238881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME could this be made local to while loop? 238981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent writeFrames = 0; 239081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2391462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen int lastGeneration = 0; 2392462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 239381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent cacheParameters_l(); 239481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = idleSleepTime; 239581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 239681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mType == MIXER) { 239781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTimeShift = 0; 239881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 239981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 240081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent CpuStats cpuStats; 240181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); 240281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 240381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent acquireWakeLock(); 240481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 24059e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten // mNBLogWriter->log can only be called while thread mutex mLock is held. 24069e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten // So if you need to log when mutex is unlocked, set logString to a non-NULL string, 24079e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten // and then that string will be logged at the next convenient opportunity. 24089e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten const char *logString = NULL; 24099e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten 2410664539d25180ab8f77e0521533ea2821cf28985fEric Laurent checkSilentMode_l(); 2411664539d25180ab8f77e0521533ea2821cf28985fEric Laurent 241281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent while (!exitPending()) 241381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 241481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent cpuStats.sample(myName); 241581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 241681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<EffectChain> > effectChains; 241781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 241881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // scope for mLock 241981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 242081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 242181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2422021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent processConfigEvents_l(); 24231035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 24249e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten if (logString != NULL) { 24259e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten mNBLogWriter->logTimestamp(); 24269e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten mNBLogWriter->log(logString); 24279e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten logString = NULL; 24289e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten } 24299e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten 2430bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten if (mLatchDValid) { 2431bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mLatchQ = mLatchD; 2432bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mLatchDValid = false; 2433bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mLatchQValid = true; 2434bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten } 2435bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten 243681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent saveOutputTracks(); 2437bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mSignalPending) { 2438bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // A signal was raised while we were unlocked 2439bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mSignalPending = false; 2440bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if (waitingAsyncCallback_l()) { 2441bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (exitPending()) { 2442bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent break; 2443bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2444bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent releaseWakeLock_l(); 2445462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mWakeLockUids.clear(); 2446462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mActiveTracksGeneration++; 2447bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("wait async completion"); 2448bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mWaitWorkCV.wait(mLock); 2449bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("async completion/wake"); 2450bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent acquireWakeLock_l(); 2451972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent standbyTime = systemTime() + standbyDelay; 2452972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent sleepTime = 0; 2453ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent 2454ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent continue; 2455ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent } 2456ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent if ((!mActiveTracks.size() && systemTime() > standbyTime) || 2457bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent isSuspended()) { 2458bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // put audio hardware into standby after short delay 2459bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (shouldStandby_l()) { 246081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 246181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent threadLoop_standby(); 246281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 246381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStandby = true; 246481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 246581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 246681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { 246781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // we're about to wait, flush the binder command buffer 246881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent IPCThreadState::self()->flushCommands(); 246981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 247081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent clearOutputTracks(); 247181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 247281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (exitPending()) { 247381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 247481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 247581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 247681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock_l(); 2477462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mWakeLockUids.clear(); 2478462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mActiveTracksGeneration++; 247981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // wait until we have something to do... 248081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("%s going to sleep", myName.string()); 248181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitWorkCV.wait(mLock); 248281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("%s waking up", myName.string()); 248381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent acquireWakeLock_l(); 248481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 248581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMixerStatus = MIXER_IDLE; 248681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMixerStatusIgnoringFastTracks = MIXER_IDLE; 248781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mBytesWritten = 0; 2488bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining = 0; 248981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent checkSilentMode_l(); 249081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 249181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent standbyTime = systemTime() + standbyDelay; 249281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = idleSleepTime; 249381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mType == MIXER) { 249481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTimeShift = 0; 249581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 249681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 249781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 249881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 249981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 250081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mMixerStatusIgnoringFastTracks is also updated internally 250181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMixerStatus = prepareTracks_l(&tracksToRemove); 250281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2503462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen // compare with previously applied list 2504462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen if (lastGeneration != mActiveTracksGeneration) { 2505462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen // update wakelock 2506462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen updateWakeLockUids_l(mWakeLockUids); 2507462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen lastGeneration = mActiveTracksGeneration; 2508462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } 2509462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 251081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // prevent any changes in effect chain list and in each effect chain 251181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // during mixing and effect process as the audio buffers could be deleted 251281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // or modified if an effect is created or deleted 251381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lockEffectChains_l(effectChains); 2514462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } // mLock scope ends 251581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2516bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mBytesRemaining == 0) { 2517bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mCurrentWriteLength = 0; 2518bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mMixerStatus == MIXER_TRACKS_READY) { 2519bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // threadLoop_mix() sets mCurrentWriteLength 2520bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent threadLoop_mix(); 2521bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if ((mMixerStatus != MIXER_DRAIN_TRACK) 2522bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent && (mMixerStatus != MIXER_DRAIN_ALL)) { 2523bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // threadLoop_sleepTime sets sleepTime to 0 if data 2524bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // must be written to HAL 2525bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent threadLoop_sleepTime(); 2526bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (sleepTime == 0) { 252725c2dac12114699e90deb1c579cadebce7b91a97Andy Hung mCurrentWriteLength = mSinkBufferSize; 2528bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2529bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 253098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Either threadLoop_mix() or threadLoop_sleepTime() should have set 253198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // mMixerBuffer with data if mMixerBufferValid is true and sleepTime == 0. 253298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid) 253398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // or mSinkBuffer (if there are no effects). 253498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // 253598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // This is done pre-effects computation; if effects change to 253698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // support higher precision, this needs to move. 253798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // 253898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // mMixerBufferValid is only set true by MixerThread::prepareTracks_l(). 253998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // TODO use sleepTime == 0 as an additional condition. 254098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mMixerBufferValid) { 254198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer; 254298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat; 254398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung 254498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat, 254598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mNormalFrameCount * mChannelCount); 254698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 254798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung 2548bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining = mCurrentWriteLength; 2549bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (isSuspended()) { 2550bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent sleepTime = suspendSleepTimeUs(); 2551bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // simulate write to HAL when suspended 255225c2dac12114699e90deb1c579cadebce7b91a97Andy Hung mBytesWritten += mSinkBufferSize; 2553bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining = 0; 2554bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 255581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2556bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // only process effects if we're going to write 255759fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent if (sleepTime == 0 && mType != OFFLOAD) { 2558bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent for (size_t i = 0; i < effectChains.size(); i ++) { 2559bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent effectChains[i]->process_l(); 2560bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 256181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 256281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 256359fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent // Process effect chains for offloaded thread even if no audio 256459fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent // was read from audio track: process only updates effect state 256559fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent // and thus does have to be synchronized with audio writes but may have 256659fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent // to be called while waiting for async write callback 256759fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent if (mType == OFFLOAD) { 256859fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent for (size_t i = 0; i < effectChains.size(); i ++) { 256959fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent effectChains[i]->process_l(); 257059fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent } 257159fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent } 257281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 257398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Only if the Effects buffer is enabled and there is data in the 257498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Effects buffer (buffer valid), we need to 257598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // copy into the sink buffer. 257698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // TODO use sleepTime == 0 as an additional condition. 257798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mEffectBufferValid) { 257898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung //ALOGV("writing effect buffer to sink buffer format %#x", mFormat); 257998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat, 258098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mNormalFrameCount * mChannelCount); 258198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 258298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung 258381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // enable changes in effect chain 258481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent unlockEffectChains(effectChains); 258581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2586bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (!waitingAsyncCallback()) { 2587bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // sleepTime == 0 means we must write to audio hardware 2588bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (sleepTime == 0) { 2589bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mBytesRemaining) { 2590bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ssize_t ret = threadLoop_write(); 2591bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (ret < 0) { 2592bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining = 0; 2593bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else { 2594bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesWritten += ret; 2595bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining -= ret; 2596bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2597bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if ((mMixerStatus == MIXER_DRAIN_TRACK) || 2598bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent (mMixerStatus == MIXER_DRAIN_ALL)) { 2599bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent threadLoop_drain(); 2600bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 26014944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten if (mType == MIXER) { 26024944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten // write blocked detection 26034944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten nsecs_t now = systemTime(); 26044944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten nsecs_t delta = now - mLastWriteTime; 26054944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten if (!mStandby && delta > maxPeriod) { 26064944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten mNumDelayedWrites++; 26074944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten if ((now - lastWarning) > kWarningThrottleNs) { 26084944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten ATRACE_NAME("underrun"); 26094944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", 26104944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten ns2ms(delta), mNumDelayedWrites, this); 26114944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten lastWarning = now; 26124944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten } 2613bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 261481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 261581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2616bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else { 2617bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent usleep(sleepTime); 2618bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 261981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 262081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 262181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Finally let go of removed track(s), without the lock held 262281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // since we can't guarantee the destructors won't acquire that 262381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // same lock. This will also mutate and push a new fast mixer state. 262481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent threadLoop_removeTracks(tracksToRemove); 262581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent tracksToRemove.clear(); 262681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 262781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME I don't understand the need for this here; 262881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it was in the original code but maybe the 262981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // assignment in saveOutputTracks() makes this unnecessary? 263081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent clearOutputTracks(); 263181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 263281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Effect chains will be actually deleted here if they were removed from 263381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mEffectChains list during mixing or effects processing 263481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effectChains.clear(); 263581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 263681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME Note that the above .clear() is no longer necessary since effectChains 263781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // is now local to this block, but will keep it for now (at least until merge done). 263881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 263981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2640bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent threadLoop_exit(); 2641bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 264281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // for DuplicatingThread, standby mode is handled by the outputTracks, otherwise ... 2643bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mType == MIXER || mType == DIRECT || mType == OFFLOAD) { 264481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // put output stream into standby mode 264581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!mStandby) { 264681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutput->stream->common.standby(&mOutput->stream->common); 264781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 264881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 264981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 265081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock(); 2651462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mWakeLockUids.clear(); 2652462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mActiveTracksGeneration++; 265381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 265481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("Thread %p type %d exiting", this, mType); 265581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 265681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 265781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2658bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// removeTracks_l() must be called with ThreadBase::mLock held 2659bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove) 2660bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 2661bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent size_t count = tracksToRemove.size(); 266234fca34606b448e6b71c2942f63cb13a0aebd620Glenn Kasten if (count > 0) { 2663bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent for (size_t i=0 ; i<count ; i++) { 2664bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent const sp<Track>& track = tracksToRemove.itemAt(i); 2665bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mActiveTracks.remove(track); 2666462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mWakeLockUids.remove(track->uid()); 2667462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mActiveTracksGeneration++; 2668bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("removeTracks_l removing track on session %d", track->sessionId()); 2669bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent sp<EffectChain> chain = getEffectChain_l(track->sessionId()); 2670bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (chain != 0) { 2671bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("stopping track on chain %p for session Id: %d", chain.get(), 2672bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->sessionId()); 2673bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent chain->decActiveTrackCnt(); 2674bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2675bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (track->isTerminated()) { 2676bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent removeTrack_l(track); 2677bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2678bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2679bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2680bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 2681bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 268281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2683accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurentstatus_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp) 2684accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent{ 2685accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent if (mNormalSink != 0) { 2686accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent return mNormalSink->getTimestamp(timestamp); 2687accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent } 2688ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent if ((mType == OFFLOAD || mType == DIRECT) && mOutput->stream->get_presentation_position) { 2689accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent uint64_t position64; 2690accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent int ret = mOutput->stream->get_presentation_position( 2691accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent mOutput->stream, &position64, ×tamp.mTime); 2692accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent if (ret == 0) { 2693accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent timestamp.mPosition = (uint32_t)position64; 2694accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent return NO_ERROR; 2695accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent } 2696accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent } 2697accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent return INVALID_OPERATION; 2698accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent} 26991c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 27001c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch, 27011c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_patch_handle_t *handle) 27021c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{ 27031c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status_t status = NO_ERROR; 27041c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { 27051c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent // store new device and send to effects 27061c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_devices_t type = AUDIO_DEVICE_NONE; 27071c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent for (unsigned int i = 0; i < patch->num_sinks; i++) { 27081c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent type |= patch->sinks[i].ext.device.type; 27091c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 27101c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent mOutDevice = type; 27111c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 27121c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent mEffectChains[i]->setDevice_l(mOutDevice); 27131c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 27141c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 27151c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); 27161c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status = hwDevice->create_audio_patch(hwDevice, 27171c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent patch->num_sources, 27181c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent patch->sources, 27191c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent patch->num_sinks, 27201c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent patch->sinks, 27211c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent handle); 27221c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } else { 27231c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent ALOG_ASSERT(false, "createAudioPatch_l() called on a pre 3.0 HAL"); 27241c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 27251c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent return status; 27261c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent} 27271c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 27281c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle) 27291c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{ 27301c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status_t status = NO_ERROR; 27311c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { 27321c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); 27331c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status = hwDevice->release_audio_patch(hwDevice, handle); 27341c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } else { 27351c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent ALOG_ASSERT(false, "releaseAudioPatch_l() called on a pre 3.0 HAL"); 27361c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 27371c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent return status; 27381c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent} 27391c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 274083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track) 274183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{ 274283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent Mutex::Autolock _l(mLock); 274383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent mTracks.add(track); 274483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent} 274583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 274683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track) 274783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{ 274883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent Mutex::Autolock _l(mLock); 274983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent destroyTrack_l(track); 275083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent} 275183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 275283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::PlaybackThread::getAudioPortConfig(struct audio_port_config *config) 275383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{ 275483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent ThreadBase::getAudioPortConfig(config); 275583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->role = AUDIO_PORT_ROLE_SOURCE; 275683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->ext.mix.hw_module = mOutput->audioHwDev->handle(); 275783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; 275883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent} 275983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 276081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 276181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 276281784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, 276381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t id, audio_devices_t device, type_t type) 276481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent : PlaybackThread(audioFlinger, output, id, device, type), 276581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mAudioMixer below 276681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mFastMixer below 276781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixerFutex(0) 276881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mOutputSink below 276981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mPipeSink below 277081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mNormalSink below 277181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 277281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type); 2773f6ed423af92a56ef54bba23eba883b1f21448b54Glenn Kasten ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%d, mFrameSize=%u, " 277481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "mFrameCount=%d, mNormalFrameCount=%d", 277581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, 277681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalFrameCount); 277781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); 277881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 277981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create an NBAIO sink for the HAL output stream, and negotiate 278081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutputSink = new AudioStreamOutSink(output->stream); 278181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t numCounterOffers = 0; 2782f69f9869514730aebe5724c461768507084dfff7Glenn Kasten const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; 278381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ssize_t index = mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); 278481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(index == 0); 278581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 278681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // initialize fast mixer depending on configuration 278781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool initFastMixer; 278881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent switch (kUseFastMixer) { 278981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Never: 279081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent initFastMixer = false; 279181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 279281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Always: 279381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent initFastMixer = true; 279481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 279581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Static: 279681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Dynamic: 279781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent initFastMixer = mFrameCount < mNormalFrameCount; 279881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 279981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 280081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (initFastMixer) { 28011258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung audio_format_t fastMixerFormat; 28021258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung if (mMixerBufferEnabled && mEffectBufferEnabled) { 28031258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT; 28041258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung } else { 28051258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT; 28061258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung } 28071258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung if (mFormat != fastMixerFormat) { 28081258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung // change our Sink format to accept our intermediate precision 28091258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung mFormat = fastMixerFormat; 28101258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung free(mSinkBuffer); 28111258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); 28121258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; 28131258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); 28141258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung } 281581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 281681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create a MonoPipe to connect our submix to FastMixer 281781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent NBAIO_Format format = mOutputSink->format(); 28181258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung // adjust format to match that of the Fast Mixer 28191258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung format.mFormat = fastMixerFormat; 28201258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount; 28211258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung 282281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // This pipe depth compensates for scheduling latency of the normal mixer thread. 282381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // When it wakes up after a maximum latency, it runs a few cycles quickly before 282481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // finally blocking. Note the pipe implementation rounds up the request to a power of 2. 282581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); 282681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const NBAIO_Format offers[1] = {format}; 282781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t numCounterOffers = 0; 282881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ssize_t index = monoPipe->negotiate(offers, 1, NULL, numCounterOffers); 282981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(index == 0); 283081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent monoPipe->setAvgFrames((mScreenState & 1) ? 283181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); 283281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPipeSink = monoPipe; 283381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 283446909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 2835da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (mTeeSinkOutputEnabled) { 2836da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten // create a Pipe to archive a copy of FastMixer's output for dumpsys 2837da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten Pipe *teeSink = new Pipe(mTeeSinkOutputFrames, format); 2838da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten numCounterOffers = 0; 2839da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten index = teeSink->negotiate(offers, 1, NULL, numCounterOffers); 2840da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten ALOG_ASSERT(index == 0); 2841da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten mTeeSink = teeSink; 2842da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten PipeReader *teeSource = new PipeReader(*teeSink); 2843da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten numCounterOffers = 0; 2844da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten index = teeSource->negotiate(offers, 1, NULL, numCounterOffers); 2845da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten ALOG_ASSERT(index == 0); 2846da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten mTeeSource = teeSource; 2847da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 284846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 284981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 285081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create fast mixer and configure it initially with just one fast track for our submix 285181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixer = new FastMixer(); 285281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 285381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef STATE_QUEUE_DUMP 285481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->setObserverDump(&mStateQueueObserverDump); 285581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->setMutatorDump(&mStateQueueMutatorDump); 285681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 285781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerState *state = sq->begin(); 285881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastTrack *fastTrack = &state->mFastTracks[0]; 285981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // wrap the source side of the MonoPipe to make it an AudioBufferProvider 286081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); 286181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mVolumeProvider = NULL; 2862e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung fastTrack->mChannelMask = mChannelMask; // mPipeSink channel mask for audio to FastMixer 2863e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer 286481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mGeneration++; 286581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mFastTracksGen++; 286681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mTrackMask = 1; 286781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // fast mixer will use the HAL output sink 286881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mOutputSink = mOutputSink.get(); 286981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mOutputSinkGen++; 287081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mFrameCount = mFrameCount; 287181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand = FastMixerState::COLD_IDLE; 287281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // already done in constructor initialization list 287381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent //mFastMixerFutex = 0; 287481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mColdFutexAddr = &mFastMixerFutex; 287581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mColdGen++; 287681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mDumpState = &mFastMixerDumpState; 287746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 287881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mTeeSink = mTeeSink.get(); 287946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 28809e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer"); 28819e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten state->mNBLogWriter = mFastMixerNBLogWriter.get(); 288281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(); 288381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); 288481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 288581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // start the fast mixer 288681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); 288781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t tid = mFastMixer->getTid(); 288881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int err = requestPriority(getpid_cached, tid, kPriorityFastMixer); 288981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (err != 0) { 289081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", 289181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent kPriorityFastMixer, getpid_cached, tid, err); 289281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 289381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 289481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG 289581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create and start the watchdog 289681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog = new AudioWatchdog(); 289781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->setDump(&mAudioWatchdogDump); 289881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); 289981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent tid = mAudioWatchdog->getTid(); 290081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent err = requestPriority(getpid_cached, tid, kPriorityFastMixer); 290181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (err != 0) { 290281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", 290381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent kPriorityFastMixer, getpid_cached, tid, err); 290481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 290581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 290681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 290781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 290881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 290981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent switch (kUseFastMixer) { 291081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Never: 291181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Dynamic: 291281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink = mOutputSink; 291381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 291481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Always: 291581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink = mPipeSink; 291681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 291781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Static: 291881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink = initFastMixer ? mPipeSink : mOutputSink; 291981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 292081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 292181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 292281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 292381784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::MixerThread::~MixerThread() 292481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 29254d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten if (mFastMixer != 0) { 292681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 292781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerState *state = sq->begin(); 292881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (state->mCommand == FastMixerState::COLD_IDLE) { 292981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int32_t old = android_atomic_inc(&mFastMixerFutex); 293081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (old == -1) { 2931ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); 293281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 293381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 293481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand = FastMixerState::EXIT; 293581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(); 293681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); 293781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixer->join(); 293881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Though the fast mixer thread has exited, it's state queue is still valid. 293981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // We'll use that extract the final state which contains one remaining fast track 294081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // corresponding to our sub-mix. 294181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state = sq->begin(); 294281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(state->mTrackMask == 1); 294381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastTrack *fastTrack = &state->mFastTracks[0]; 294481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(fastTrack->mBufferProvider != NULL); 294581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent delete fastTrack->mBufferProvider; 294681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(false /*didModify*/); 29474d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten mFastMixer.clear(); 294881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG 294981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mAudioWatchdog != 0) { 295081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->requestExit(); 295181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->requestExitAndWait(); 295281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog.clear(); 295381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 295481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 295581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 29569e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter); 295781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent delete mAudioMixer; 295881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 295981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 296081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 296181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const 296281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 29634d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten if (mFastMixer != 0) { 296481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); 296581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent latency += (pipe->getAvgFrames() * 1000) / mSampleRate; 296681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 296781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return latency; 296881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 296981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 297081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 297181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) 297281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 297381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread::threadLoop_removeTracks(tracksToRemove); 297481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 297581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2976bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentssize_t AudioFlinger::MixerThread::threadLoop_write() 297781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 297881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME we should only do one push per cycle; confirm this is true 297981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Start the fast mixer if it's not already running 29804d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten if (mFastMixer != 0) { 298181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 298281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerState *state = sq->begin(); 298381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (state->mCommand != FastMixerState::MIX_WRITE && 298481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { 298581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (state->mCommand == FastMixerState::COLD_IDLE) { 298681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int32_t old = android_atomic_inc(&mFastMixerFutex); 298781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (old == -1) { 2988ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); 298981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 299081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG 299181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mAudioWatchdog != 0) { 299281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->resume(); 299381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 299481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 299581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 299681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand = FastMixerState::MIX_WRITE; 29974182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? 29984182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten FastMixerDumpState::kSamplingNforLowRamDevice : FastMixerDumpState::kSamplingN); 299981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(); 300081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); 300181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (kUseFastMixer == FastMixer_Dynamic) { 300281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink = mPipeSink; 300381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 300481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 300581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(false /*didModify*/); 300681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 300781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 3008bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return PlaybackThread::threadLoop_write(); 300981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 301081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 301181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::threadLoop_standby() 301281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 301381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Idle the fast mixer if it's currently running 30144d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten if (mFastMixer != 0) { 301581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 301681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerState *state = sq->begin(); 301781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!(state->mCommand & FastMixerState::IDLE)) { 301881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand = FastMixerState::COLD_IDLE; 301981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mColdFutexAddr = &mFastMixerFutex; 302081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mColdGen++; 302181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixerFutex = 0; 302281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(); 302381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now 302481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); 302581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (kUseFastMixer == FastMixer_Dynamic) { 302681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink = mOutputSink; 302781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 302881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG 302981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mAudioWatchdog != 0) { 303081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->pause(); 303181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 303281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 303381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 303481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(false /*didModify*/); 303581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 303681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 303781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread::threadLoop_standby(); 303881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 303981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3040bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::PlaybackThread::waitingAsyncCallback_l() 3041bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 3042bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return false; 3043bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 3044bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 3045bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::PlaybackThread::shouldStandby_l() 3046bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 3047bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return !mStandby; 3048bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 3049bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 3050bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::PlaybackThread::waitingAsyncCallback() 3051bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 3052bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 3053bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return waitingAsyncCallback_l(); 3054bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 3055bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 305681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// shared by MIXER and DIRECT, overridden by DUPLICATING 305781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::threadLoop_standby() 305881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 305981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended); 306081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutput->stream->common.standby(&mOutput->stream->common); 3061bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mUseAsyncWrite != 0) { 30623b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // discard any pending drain or write ack by incrementing sequence 30633b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence = (mWriteAckSequence + 2) & ~1; 30643b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence = (mDrainSequence + 2) & ~1; 3065bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 30663b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setWriteBlocked(mWriteAckSequence); 30673b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setDraining(mDrainSequence); 3068bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 306981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 307081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 30714c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew Georgevoid AudioFlinger::PlaybackThread::onAddNewTrack_l() 30724c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George{ 30734c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George ALOGV("signal playback thread"); 30744c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George broadcast_l(); 30754c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George} 30764c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George 307781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::threadLoop_mix() 307881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 307981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // obtain the presentation timestamp of the next output buffer 308081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int64_t pts; 308181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status = INVALID_OPERATION; 308281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 308381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mNormalSink != 0) { 308481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = mNormalSink->getNextWriteTimestamp(&pts); 308581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 308681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = mOutputSink->getNextWriteTimestamp(&pts); 308781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 308881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 308981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (status != NO_ERROR) { 309081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pts = AudioBufferProvider::kInvalidPTS; 309181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 309281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 309381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mix buffers... 309481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->process(pts); 309525c2dac12114699e90deb1c579cadebce7b91a97Andy Hung mCurrentWriteLength = mSinkBufferSize; 309681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // increase sleep time progressively when application underrun condition clears. 309781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Only increase sleep time if the mixer is ready for two consecutive times to avoid 309881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // that a steady state of alternating ready/not ready conditions keeps the sleep time 309981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // such that we would underrun the audio HAL. 310081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((sleepTime == 0) && (sleepTimeShift > 0)) { 310181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTimeShift--; 310281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 310381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = 0; 310481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent standbyTime = systemTime() + standbyDelay; 310581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent //TODO: delay standby when effects have a tail 310681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 310781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 310881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::threadLoop_sleepTime() 310981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 311081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If no tracks are ready, sleep once for the duration of an output 311181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // buffer size, then write 0s to the output 311281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sleepTime == 0) { 311381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mMixerStatus == MIXER_TRACKS_ENABLED) { 311481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = activeSleepTime >> sleepTimeShift; 311581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sleepTime < kMinThreadSleepTimeUs) { 311681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = kMinThreadSleepTimeUs; 311781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 311881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // reduce sleep time in case of consecutive application underruns to avoid 311981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer 312081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // duration we would end up writing less data than needed by the audio HAL if 312181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // the condition persists. 312281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sleepTimeShift < kMaxThreadSleepTimeShift) { 312381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTimeShift++; 312481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 312581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 312681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = idleSleepTime; 312781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 312881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { 312998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared 313098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // before effects processing or output. 313198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mMixerBufferValid) { 313298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung memset(mMixerBuffer, 0, mMixerBufferSize); 313398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } else { 313498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung memset(mSinkBuffer, 0, mSinkBufferSize); 313598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 313681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = 0; 313781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), 313881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "anticipated start"); 313981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 314081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // TODO add standby time extension fct of effect tail 314181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 314281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 314381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// prepareTracks_l() must be called with ThreadBase::mLock held 314481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( 314581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<Track> > *tracksToRemove) 314681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 314781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 314881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixer_state mixerStatus = MIXER_IDLE; 314981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // find out which tracks need to be processed 315081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t count = mActiveTracks.size(); 315181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t mixedTracks = 0; 315281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t tracksWithEffect = 0; 315381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // counts only _active_ fast tracks 315481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t fastTracks = 0; 315581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset 315681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 315781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent float masterVolume = mMasterVolume; 315881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool masterMute = mMasterMute; 315981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 316081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (masterMute) { 316181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent masterVolume = 0; 316281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 316381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Delegate master volume control to effect in output mix effect chain if needed 316481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); 316581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 316681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t v = (uint32_t)(masterVolume * (1 << 24)); 316781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setVolume_l(&v, &v); 316881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent masterVolume = (float)((v + (1 << 23)) >> 24); 316981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain.clear(); 317081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 317181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 317281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // prepare a new state to push 317381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue *sq = NULL; 317481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerState *state = NULL; 317581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool didModify = false; 317681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; 31774d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten if (mFastMixer != 0) { 317881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq = mFastMixer->sq(); 317981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state = sq->begin(); 318081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 318181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 318269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferValid = false; // mMixerBuffer has no valid data until appropriate tracks found. 318398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found. 318469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung 318581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i=0 ; i<count ; i++) { 31869fdcb0a9497ca290bcf364b10868587b6bde3a34Glenn Kasten const sp<Track> t = mActiveTracks[i].promote(); 318781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (t == 0) { 318881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 318981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 319081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 319181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // this const just means the local variable doesn't change 319281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Track* const track = t.get(); 319381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 319481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // process fast tracks 319581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isFastTrack()) { 319681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 319781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // It's theoretically possible (though unlikely) for a fast track to be created 319881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // and then removed within the same normal mix cycle. This is not a problem, as 319981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // the track never becomes active so it's fast mixer slot is never touched. 320081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The converse, of removing an (active) track and then creating a new track 320181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // at the identical fast mixer slot within the same normal mix cycle, 320281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // is impossible because the slot isn't marked available until the end of each cycle. 320381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int j = track->mFastIndex; 320481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(0 < j && j < (int)FastMixerState::kMaxFastTracks); 320581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); 320681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastTrack *fastTrack = &state->mFastTracks[j]; 320781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 320881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Determine whether the track is currently in underrun condition, 320981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // and whether it had a recent underrun. 321081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; 321181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastTrackUnderruns underruns = ftDump->mUnderruns; 321281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t recentFull = (underruns.mBitFields.mFull - 321381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK; 321481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t recentPartial = (underruns.mBitFields.mPartial - 321581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK; 321681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t recentEmpty = (underruns.mBitFields.mEmpty - 321781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK; 321881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t recentUnderruns = recentPartial + recentEmpty; 321981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mObservedUnderruns = underruns; 322081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // don't count underruns that occur while stopping or pausing 322181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // or stopped which can occur when flush() is called while active 322282aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten if (!(track->isStopping() || track->isPausing() || track->isStopped()) && 322382aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten recentUnderruns > 0) { 322482aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun 322582aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten track->mAudioTrackServerProxy->tallyUnderrunFrames(recentUnderruns * mFrameCount); 322681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 322781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 322881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // This is similar to the state machine for normal tracks, 322981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // with a few modifications for fast tracks. 323081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool isActive = true; 323181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent switch (track->mState) { 323281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::STOPPING_1: 323381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track stays active in STOPPING_1 state until first underrun 3234bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (recentUnderruns > 0 || track->isTerminated()) { 323581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mState = TrackBase::STOPPING_2; 323681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 323781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 323881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::PAUSING: 323981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // ramp down is not yet implemented 324081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->setPaused(); 324181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 324281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::RESUMING: 324381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // ramp up is not yet implemented 324481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mState = TrackBase::ACTIVE; 324581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 324681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::ACTIVE: 324781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (recentFull > 0 || recentPartial > 0) { 324881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track has provided at least some frames recently: reset retry count 324981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mRetryCount = kMaxTrackRetries; 325081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 325181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (recentUnderruns == 0) { 325281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // no recent underruns: stay active 325381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 325481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 325581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // there has recently been an underrun of some kind 325681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->sharedBuffer() == 0) { 325781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // were any of the recent underruns "empty" (no frames available)? 325881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (recentEmpty == 0) { 325981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // no, then ignore the partial underruns as they are allowed indefinitely 326081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 326181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 326281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // there has recently been an "empty" underrun: decrement the retry counter 326381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (--(track->mRetryCount) > 0) { 326481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 326581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 326681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // indicate to client process that the track was disabled because of underrun; 326781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it will then automatically call start() when data is available 326896f60d8f04432a1ed503b3e24d5736d28c63c9a2Glenn Kasten android_atomic_or(CBLK_DISABLED, &track->mCblk->mFlags); 326981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // remove from active list, but state remains ACTIVE [confusing but true] 327081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent isActive = false; 327181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 327281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 327381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // fall through 327481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::STOPPING_2: 327581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::PAUSED: 327681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::STOPPED: 327781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::FLUSHED: // flush() while active 327881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Check for presentation complete if track is inactive 327981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // We have consumed all the buffers of this track. 328081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // This would be incomplete if we auto-paused on underrun 328181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 328281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t audioHALFrames = 328381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; 328481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t framesWritten = mBytesWritten / mFrameSize; 328581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { 328681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track stays in active list until presentation is complete 328781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 328881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 328981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 329081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isStopping_2()) { 329181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mState = TrackBase::STOPPED; 329281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 329381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isStopped()) { 329481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Can't reset directly, as fast mixer is still polling this track 329581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track->reset(); 329681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // So instead mark this track as needing to be reset after push with ack 329781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent resetMask |= 1 << i; 329881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 329981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent isActive = false; 330081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 330181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::IDLE: 330281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent default: 3303adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("unexpected track state %d", track->mState); 330481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 330581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 330681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (isActive) { 330781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // was it previously inactive? 330881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!(state->mTrackMask & (1 << j))) { 330981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ExtendedAudioBufferProvider *eabp = track; 331081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent VolumeProvider *vp = track; 331181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mBufferProvider = eabp; 331281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mVolumeProvider = vp; 331381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mChannelMask = track->mChannelMask; 3314e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung fastTrack->mFormat = track->mFormat; 331581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mGeneration++; 331681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mTrackMask |= 1 << j; 331781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent didModify = true; 331881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // no acknowledgement required for newly active tracks 331981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 332081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // cache the combined master volume and stream type volume for fast mixer; this 332181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // lacks any synchronization or barrier so VolumeProvider may read a stale value 3322e4756fe3a387615acb63c6a05788c8db9b5786cbGlenn Kasten track->mCachedVolume = masterVolume * mStreamTypes[track->streamType()].volume; 332381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ++fastTracks; 332481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 332581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // was it previously active? 332681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (state->mTrackMask & (1 << j)) { 332781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mBufferProvider = NULL; 332881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mGeneration++; 332981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mTrackMask &= ~(1 << j); 333081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent didModify = true; 333181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If any fast tracks were removed, we must wait for acknowledgement 333281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // because we're about to decrement the last sp<> on those tracks. 333381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; 333481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 3335adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("fast track %d should have been active", j); 333681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 333781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent tracksToRemove->add(track); 333881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Avoids a misleading display in dumpsys 333981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL; 334081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 334181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 334281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 334381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 334481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // local variable scope to avoid goto warning 334581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 334681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_track_cblk_t* cblk = track->cblk(); 334781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 334881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The first time a track is added we wait 334981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // for all its buffers to be filled before processing it 335081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int name = track->name(); 335181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // make sure that we have enough frames to mix one full buffer. 335281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // enforce this condition only once to enable draining the buffer in case the client 335381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // app does not call stop() and relies on underrun to stop: 335481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed 335581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // during last round 33569f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten size_t desiredFrames; 33579fdcb0a9497ca290bcf364b10868587b6bde3a34Glenn Kasten uint32_t sr = track->sampleRate(); 33589fdcb0a9497ca290bcf364b10868587b6bde3a34Glenn Kasten if (sr == mSampleRate) { 33599f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten desiredFrames = mNormalFrameCount; 33609f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } else { 33619f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // +1 for rounding and +1 for additional sample needed for interpolation 33629fdcb0a9497ca290bcf364b10868587b6bde3a34Glenn Kasten desiredFrames = (mNormalFrameCount * sr) / mSampleRate + 1 + 1; 33639f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // add frames already consumed but not yet released by the resampler 33642fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten // because mAudioTrackServerProxy->framesReady() will include these frames 33659f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten desiredFrames += mAudioMixer->getUnreleasedFrames(track->name()); 336674935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten#if 0 33679f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // the minimum track buffer size is normally twice the number of frames necessary 33689f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // to fill one buffer and the resampler should not leave more than one buffer worth 33699f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // of unreleased frames after each pass, but just in case... 33709f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten ALOG_ASSERT(desiredFrames <= cblk->frameCount_); 337174935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten#endif 33729f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 337381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t minFrames = 1; 337481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && 337581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { 33769f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten minFrames = desiredFrames; 33779f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 337813e4c960ea3db03a43e084fbd85d52aa77f7b871Eric Laurent 337913e4c960ea3db03a43e084fbd85d52aa77f7b871Eric Laurent size_t framesReady = track->framesReady(); 33809f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if ((framesReady >= minFrames) && track->isReady() && 338181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent !track->isPaused() && !track->isTerminated()) 338281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 3383f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten ALOGVV("track %d s=%08x [OK] on thread %p", name, cblk->mServer, this); 338481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 338581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixedTracks++; 338681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 338769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // track->mainBuffer() != mSinkBuffer or mMixerBuffer means 338869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // there is an effect chain connected to the track 338981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain.clear(); 339069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung if (track->mainBuffer() != mSinkBuffer && 339169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung track->mainBuffer() != mMixerBuffer) { 339298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mEffectBufferEnabled) { 339398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferValid = true; // Later can set directly. 339498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 339581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain = getEffectChain_l(track->sessionId()); 339681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Delegate volume control to effect in track effect chain if needed 339781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 339881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent tracksWithEffect++; 339981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 340081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on " 340181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "session %d", 340281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent name, track->sessionId()); 340381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 340481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 340581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 340681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 340781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int param = AudioMixer::VOLUME; 340881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->mFillingUpStatus == Track::FS_FILLED) { 340981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // no ramp for the first volume setting 341081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mFillingUpStatus = Track::FS_ACTIVE; 341181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->mState == TrackBase::RESUMING) { 341281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mState = TrackBase::ACTIVE; 341381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent param = AudioMixer::RAMP_VOLUME; 341481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 341581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); 3416f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten // FIXME should not make a decision based on mServer 3417f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten } else if (cblk->mServer != 0) { 341881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If the track is stopped before the first frame was mixed, 341981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // do not apply ramp 342081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent param = AudioMixer::RAMP_VOLUME; 342181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 342281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 342381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // compute volume for this track 34246be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung uint32_t vl, vr; // in U8.24 integer format 34256be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung float vlf, vrf, vaf; // in [0.0, 1.0] float format 3426e4756fe3a387615acb63c6a05788c8db9b5786cbGlenn Kasten if (track->isPausing() || mStreamTypes[track->streamType()].mute) { 34276be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vl = vr = 0; 34286be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vlf = vrf = vaf = 0.; 342981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isPausing()) { 343081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->setPaused(); 343181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 343281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 343381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 343481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // read original volumes with volume control 343581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent float typeVolume = mStreamTypes[track->streamType()].volume; 343681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent float v = masterVolume * typeVolume; 34379f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; 3438c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten gain_minifloat_packed_t vlr = proxy->getVolumeLR(); 34396be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); 34406be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); 344181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track volumes come from shared memory, so can't be trusted and must be clamped 3442c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten if (vlf > GAIN_FLOAT_UNITY) { 3443c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten ALOGV("Track left volume out of range: %.3g", vlf); 3444c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten vlf = GAIN_FLOAT_UNITY; 344581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 3446c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten if (vrf > GAIN_FLOAT_UNITY) { 3447c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten ALOGV("Track right volume out of range: %.3g", vrf); 3448c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten vrf = GAIN_FLOAT_UNITY; 344981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 345081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // now apply the master volume and stream type volume 34516be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vlf *= v; 34526be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vrf *= v; 345381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // assuming master volume and stream type volume each go up to 1.0, 34546be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung // then derive vl and vr as U8.24 versions for the effect chain 34556be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT; 34566be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vl = (uint32_t) (scaleto8_24 * vlf); 34576be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vr = (uint32_t) (scaleto8_24 * vrf); 34586be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung // vl and vr are now in U8.24 format 3459e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten uint16_t sendLevel = proxy->getSendLevel_U4_12(); 346081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // send level comes from shared memory and so may be corrupt 346181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sendLevel > MAX_GAIN_INT) { 346281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("Track send level out of range: %04X", sendLevel); 346381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sendLevel = MAX_GAIN_INT; 346481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 34656be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung // vaf is represented as [0.0, 1.0] float by rescaling sendLevel 34666be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vaf = v * sendLevel * (1. / MAX_GAIN_INT); 346781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 3468bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 346981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Delegate volume control to effect in track effect chain if needed 347081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0 && chain->setVolume_l(&vl, &vr)) { 347181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Do not ramp volume if volume is controlled by effect 347281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent param = AudioMixer::VOLUME; 3473b6be7f22a82ee3bad8bcc709d21e72fc4727da09Bryant Liu // Update remaining floating point volume levels 3474b6be7f22a82ee3bad8bcc709d21e72fc4727da09Bryant Liu vlf = (float)vl / (1 << 24); 3475b6be7f22a82ee3bad8bcc709d21e72fc4727da09Bryant Liu vrf = (float)vr / (1 << 24); 347681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mHasVolumeController = true; 347781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 347881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // force no volume ramp when volume controller was just disabled or removed 347981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // from effect chain to avoid volume spike 348081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->mHasVolumeController) { 348181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent param = AudioMixer::VOLUME; 348281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 348381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mHasVolumeController = false; 348481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 348581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 348681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // XXX: these things DON'T need to be done each time 348781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->setBufferProvider(name, track); 348881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->enable(name); 348981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 34906be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, &vlf); 34916be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, &vrf); 34926be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, &vaf); 349381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->setParameter( 349481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent name, 349581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::TRACK, 349681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::FORMAT, (void *)track->format()); 349781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->setParameter( 349881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent name, 349981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::TRACK, 3500377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask()); 35019a59276fb465e492138e0576523b54079671e8f4Andy Hung mAudioMixer->setParameter( 35029a59276fb465e492138e0576523b54079671e8f4Andy Hung name, 35039a59276fb465e492138e0576523b54079671e8f4Andy Hung AudioMixer::TRACK, 35049a59276fb465e492138e0576523b54079671e8f4Andy Hung AudioMixer::MIXER_CHANNEL_MASK, (void *)(uintptr_t)mChannelMask); 3505e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten // limit track sample rate to 2 x output sample rate, which changes at re-configuration 3506e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten uint32_t maxSampleRate = mSampleRate * 2; 35079f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten uint32_t reqSampleRate = track->mAudioTrackServerProxy->getSampleRate(); 3508e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten if (reqSampleRate == 0) { 3509e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten reqSampleRate = mSampleRate; 3510e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten } else if (reqSampleRate > maxSampleRate) { 3511e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten reqSampleRate = maxSampleRate; 3512e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten } 351381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->setParameter( 351481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent name, 351581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::RESAMPLE, 351681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::SAMPLE_RATE, 3517377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT (void *)(uintptr_t)reqSampleRate); 351869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung /* 351969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * Select the appropriate output buffer for the track. 352069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * 352198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung * Tracks with effects go into their own effects chain buffer 352298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung * and from there into either mEffectBuffer or mSinkBuffer. 352369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * 352469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * Other tracks can use mMixerBuffer for higher precision 352569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * channel accumulation. If this buffer is enabled 352669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * (mMixerBufferEnabled true), then selected tracks will accumulate 352769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * into it. 352869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * 352969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung */ 353069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung if (mMixerBufferEnabled 353169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung && (track->mainBuffer() == mSinkBuffer 353269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung || track->mainBuffer() == mMixerBuffer)) { 353369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mAudioMixer->setParameter( 353469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung name, 353569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung AudioMixer::TRACK, 3536788207057ed4b8df4719ed8089f376ef52de9ca1Andy Hung AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat); 353769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mAudioMixer->setParameter( 353869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung name, 353969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung AudioMixer::TRACK, 354069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer); 354169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // TODO: override track->mainBuffer()? 354269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferValid = true; 354369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung } else { 354469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mAudioMixer->setParameter( 354569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung name, 354669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung AudioMixer::TRACK, 3547788207057ed4b8df4719ed8089f376ef52de9ca1Andy Hung AudioMixer::MIXER_FORMAT, (void *)AUDIO_FORMAT_PCM_16_BIT); 354869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mAudioMixer->setParameter( 354969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung name, 355069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung AudioMixer::TRACK, 355169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); 355269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung } 355381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->setParameter( 355481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent name, 355581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::TRACK, 355681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); 355781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 355881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // reset retry count 355981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mRetryCount = kMaxTrackRetries; 356081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 356181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If one track is ready, set the mixer ready if: 356281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // - the mixer was not ready during previous round OR 356381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // - no other track is not ready 356481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || 356581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixerStatus != MIXER_TRACKS_ENABLED) { 356681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixerStatus = MIXER_TRACKS_READY; 356781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 356881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 35699f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { 357082aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten track->mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames); 35719f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 357281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // clear effect chain input buffer if an active track underruns to avoid sending 357381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // previous audio buffer again to effects 357481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain = getEffectChain_l(track->sessionId()); 357581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 357681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->clearInputBuffer(); 357781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 357881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3579f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten ALOGVV("track %d s=%08x [NOT READY] on thread %p", name, cblk->mServer, this); 358081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((track->sharedBuffer() != 0) || track->isTerminated() || 358181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->isStopped() || track->isPaused()) { 358281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // We have consumed all the buffers of this track. 358381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Remove it from the list of active tracks. 358481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // TODO: use actual buffer filling status instead of latency when available from 358581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // audio HAL 358681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; 358781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t framesWritten = mBytesWritten / mFrameSize; 358881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { 358981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isStopped()) { 359081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->reset(); 359181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 359281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent tracksToRemove->add(track); 359381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 359481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 359581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // No buffers for this track. Give it a few chances to 359681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // fill a buffer, then remove it from active list. 359781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (--(track->mRetryCount) <= 0) { 3598c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten ALOGI("BUFFER TIMEOUT: remove(%d) from active list on thread %p", name, this); 359981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent tracksToRemove->add(track); 360081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // indicate to client process that the track was disabled because of underrun; 360181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it will then automatically call start() when data is available 360296f60d8f04432a1ed503b3e24d5736d28c63c9a2Glenn Kasten android_atomic_or(CBLK_DISABLED, &cblk->mFlags); 360381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If one track is not ready, mark the mixer also not ready if: 360481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // - the mixer was ready during previous round OR 360581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // - no other track is ready 360681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || 360781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixerStatus != MIXER_TRACKS_READY) { 360881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixerStatus = MIXER_TRACKS_ENABLED; 360981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 361081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 361181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->disable(name); 361281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 361381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 361481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } // local variable scope to avoid goto warning 361581784c37c61b09289654b979567a42bf73cd2b12Eric Laurenttrack_is_ready: ; 361681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 361781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 361881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 361981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Push the new FastMixer state if necessary 362081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool pauseAudioWatchdog = false; 362181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (didModify) { 362281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mFastTracksGen++; 362381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle 362481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (kUseFastMixer == FastMixer_Dynamic && 362581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { 362681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand = FastMixerState::COLD_IDLE; 362781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mColdFutexAddr = &mFastMixerFutex; 362881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mColdGen++; 362981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixerFutex = 0; 363081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (kUseFastMixer == FastMixer_Dynamic) { 363181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink = mOutputSink; 363281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 363381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If we go into cold idle, need to wait for acknowledgement 363481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // so that fast mixer stops doing I/O. 363581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; 363681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pauseAudioWatchdog = true; 363781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 363881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 363981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sq != NULL) { 364081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(didModify); 364181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->push(block); 364281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 364381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG 364481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (pauseAudioWatchdog && mAudioWatchdog != 0) { 364581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->pause(); 364681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 364781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 364881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 364981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Now perform the deferred reset on fast tracks that have stopped 365081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent while (resetMask != 0) { 365181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t i = __builtin_ctz(resetMask); 365281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(i < count); 365381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent resetMask &= ~(1 << i); 365481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> t = mActiveTracks[i].promote(); 365581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (t == 0) { 365681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 365781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 365881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Track* track = t.get(); 365981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(track->isFastTrack() && track->isStopped()); 366081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->reset(); 366181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 366281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 366381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // remove all the tracks that need to be... 3664bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent removeTracks_l(*tracksToRemove); 366581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 366669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // sink or mix buffer must be cleared if all tracks are connected to an 366769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // effect chain as in this case the mixer will not write to the sink or mix buffer 366869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // and track effects will accumulate into it 3669bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || 3670bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent (mixedTracks == 0 && fastTracks > 0))) { 367181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME as a performance optimization, should remember previous zero status 367269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung if (mMixerBufferValid) { 367369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung memset(mMixerBuffer, 0, mMixerBufferSize); 367469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // TODO: In testing, mSinkBuffer below need not be cleared because 367569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer 367669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // after mixing. 367769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // 367869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // To enforce this guarantee: 367969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || 368069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // (mixedTracks == 0 && fastTracks > 0)) 368169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // must imply MIXER_TRACKS_READY. 368269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // Later, we may clear buffers regardless, and skip much of this logic. 368369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung } 368498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // TODO - either mEffectBuffer or mSinkBuffer needs to be cleared. 368598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mEffectBufferValid) { 368698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung memset(mEffectBuffer, 0, mEffectBufferSize); 368798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 368898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // FIXME as a performance optimization, should remember previous zero status 36895567aaf4818007cd8e77329683a91c0f5d7a8837Andy Hung memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize); 369081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 369181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 369281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if any fast tracks, then status is ready 369381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMixerStatusIgnoringFastTracks = mixerStatus; 369481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (fastTracks > 0) { 369581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixerStatus = MIXER_TRACKS_READY; 369681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 369781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mixerStatus; 369881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 369981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 370081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// getTrackName_l() must be called with ThreadBase::mLock held 3701e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hungint AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask, 3702e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung audio_format_t format, int sessionId) 370381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 3704e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung return mAudioMixer->getTrackName(channelMask, format, sessionId); 370581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 370681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 370781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// deleteTrackName_l() must be called with ThreadBase::mLock held 370881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::deleteTrackName_l(int name) 370981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 371081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("remove track (%d) and delete from mixer", name); 371181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->deleteTrackName(name); 371281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 371381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 37141035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// checkForNewParameter_l() must be called with ThreadBase::mLock held 37151035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentbool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair, 37161035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status_t& status) 371781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 371881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool reconfig = false; 371981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 37201035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = NO_ERROR; 372181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 37221035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // if !&IDLE, holds the FastMixer state to restore after new parameters processed 37231035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; 37244d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten if (mFastMixer != 0) { 37251035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 37261035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent FastMixerState *state = sq->begin(); 37271035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (!(state->mCommand & FastMixerState::IDLE)) { 37281035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent previousCommand = state->mCommand; 37291035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent state->mCommand = FastMixerState::HOT_IDLE; 37301035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sq->end(); 37311035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); 37321035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 37331035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sq->end(false /*didModify*/); 373481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 37351035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 373681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 37371035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent AudioParameter param = AudioParameter(keyValuePair); 37381035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent int value; 37391035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { 37401035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 37411035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 37421035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { 37439a59276fb465e492138e0576523b54079671e8f4Andy Hung if (!isValidPcmSinkFormat((audio_format_t) value)) { 37441035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = BAD_VALUE; 37451035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 37461035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // no need to save value, since it's constant 374781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent reconfig = true; 374881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 37491035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 37501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { 37519a59276fb465e492138e0576523b54079671e8f4Andy Hung if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) { 37521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = BAD_VALUE; 37531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 37541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // no need to save value, since it's constant 37551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 375681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 37571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 37581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { 37591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // do not accept frame count changes if tracks are open as the track buffer 37601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // size depends on frame count and correct behavior would not be guaranteed 37611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // if frame count is changed after track creation 37621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (!mTracks.isEmpty()) { 37631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = INVALID_OPERATION; 37641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 37651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 376681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 37671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 37681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { 376981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef ADD_BATTERY_DATA 37701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // when changing the audio output device, call addBatteryData to notify 37711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // the change 37721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (mOutDevice != value) { 37731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent uint32_t params = 0; 37741035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // check whether speaker is on 37751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (value & AUDIO_DEVICE_OUT_SPEAKER) { 37761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent params |= IMediaPlayerService::kBatteryDataSpeakerOn; 37771035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 377881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 37791035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent audio_devices_t deviceWithoutSpeaker 37801035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; 37811035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // check if any other device (except speaker) is on 37821035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (value & deviceWithoutSpeaker ) { 37831035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; 37841035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 378581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 37861035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (params != 0) { 37871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent addBatteryData(params); 378881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 37891035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 379081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 379181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 37921035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // forward device change to effects that have requested to be 37931035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // aware of attached audio device. 37941035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (value != AUDIO_DEVICE_NONE) { 37951035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mOutDevice = value; 37961035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 37971035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mEffectChains[i]->setDevice_l(mOutDevice); 379881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 379981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 38001035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 380181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 38021035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == NO_ERROR) { 38031035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = mOutput->stream->common.set_parameters(&mOutput->stream->common, 38041035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent keyValuePair.string()); 38051035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (!mStandby && status == INVALID_OPERATION) { 38061035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mOutput->stream->common.standby(&mOutput->stream->common); 38071035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mStandby = true; 38081035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mBytesWritten = 0; 380981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = mOutput->stream->common.set_parameters(&mOutput->stream->common, 38101035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent keyValuePair.string()); 38111035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 38121035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == NO_ERROR && reconfig) { 38131035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent readOutputParameters_l(); 38141035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent delete mAudioMixer; 38151035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); 38161035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent for (size_t i = 0; i < mTracks.size() ; i++) { 3817e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung int name = getTrackName_l(mTracks[i]->mChannelMask, 3818e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung mTracks[i]->mFormat, mTracks[i]->mSessionId); 38191035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (name < 0) { 38201035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent break; 382181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 38221035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mTracks[i]->mName = name; 382381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 38241035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sendIoConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); 382581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 382681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 382781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 382881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!(previousCommand & FastMixerState::IDLE)) { 38294d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten ALOG_ASSERT(mFastMixer != 0); 383081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 383181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerState *state = sq->begin(); 383281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); 383381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand = previousCommand; 383481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(); 383581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); 383681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 383781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 383881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return reconfig; 383981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 384081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 384181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 384281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) 384381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 384481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const size_t SIZE = 256; 384581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char buffer[SIZE]; 384681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent String8 result; 384781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 384881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread::dumpInternals(fd, args); 384981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 385087cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " AudioMixer tracks: 0x%08x\n", mAudioMixer->trackNames()); 385181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 385281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Make a non-atomic copy of fast mixer dump state so it won't change underneath us 38534182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten const FastMixerDumpState copy(mFastMixerDumpState); 385481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent copy.dump(fd); 385581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 385681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef STATE_QUEUE_DUMP 385781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Similar for state queue 385881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent StateQueueObserverDump observerCopy = mStateQueueObserverDump; 385981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent observerCopy.dump(fd); 386081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; 386181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mutatorCopy.dump(fd); 386281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 386381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 386446909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 386581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Write the tee output to a .wav file 386681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpTee(fd, mTeeSource, mId); 386746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 386881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 386981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG 387081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mAudioWatchdog != 0) { 387181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Make a non-atomic copy of audio watchdog dump so it won't change underneath us 387281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioWatchdogDump wdCopy = mAudioWatchdogDump; 387381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent wdCopy.dump(fd); 387481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 387581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 387681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 387781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 387881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const 387981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 388081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; 388181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 388281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 388381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const 388481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 388581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); 388681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 388781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 388881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::cacheParameters_l() 388981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 389081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread::cacheParameters_l(); 389181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 389281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME: Relaxed timing because of a certain device that can't meet latency 389381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Should be reduced to 2x after the vendor fixes the driver issue 389481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // increase threshold again due to low power audio mode. The way this warning 389581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // threshold is calculated and its usefulness should be reconsidered anyway. 389681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; 389781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 389881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 389981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 390081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 390181784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, 390281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device) 390381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent : PlaybackThread(audioFlinger, output, id, device, DIRECT) 390481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mLeftVolFloat, mRightVolFloat 390581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 390681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 390781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3908bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, 3909bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioStreamOut* output, audio_io_handle_t id, uint32_t device, 3910bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ThreadBase::type_t type) 3911bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent : PlaybackThread(audioFlinger, output, id, device, type) 3912bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // mLeftVolFloat, mRightVolFloat 3913bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 3914bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 3915bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 391681784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::DirectOutputThread::~DirectOutputThread() 391781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 391881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 391981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3920bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack) 3921bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 3922bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent audio_track_cblk_t* cblk = track->cblk(); 3923bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent float left, right; 3924bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 3925bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mMasterMute || mStreamTypes[track->streamType()].mute) { 3926bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent left = right = 0; 3927bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else { 3928bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent float typeVolume = mStreamTypes[track->streamType()].volume; 3929bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent float v = mMasterVolume * typeVolume; 3930bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; 3931c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten gain_minifloat_packed_t vlr = proxy->getVolumeLR(); 3932c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten left = float_from_gain(gain_minifloat_unpack_left(vlr)); 3933c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten if (left > GAIN_FLOAT_UNITY) { 3934c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten left = GAIN_FLOAT_UNITY; 3935c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten } 3936c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten left *= v; 3937c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten right = float_from_gain(gain_minifloat_unpack_right(vlr)); 3938c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten if (right > GAIN_FLOAT_UNITY) { 3939c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten right = GAIN_FLOAT_UNITY; 3940c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten } 3941c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten right *= v; 3942bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 3943bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 3944bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (lastTrack) { 3945bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (left != mLeftVolFloat || right != mRightVolFloat) { 3946bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mLeftVolFloat = left; 3947bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mRightVolFloat = right; 3948bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 3949bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Convert volumes from float to 8.24 3950bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent uint32_t vl = (uint32_t)(left * (1 << 24)); 3951bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent uint32_t vr = (uint32_t)(right * (1 << 24)); 3952bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 3953bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Delegate volume control to effect in track effect chain if needed 3954bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // only one effect chain can be present on DirectOutputThread, so if 3955bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // there is one, the track is connected to it 3956bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (!mEffectChains.isEmpty()) { 3957bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mEffectChains[0]->setVolume_l(&vl, &vr); 3958bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent left = (float)vl / (1 << 24); 3959bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent right = (float)vr / (1 << 24); 3960bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 3961bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mOutput->stream->set_volume) { 3962bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mOutput->stream->set_volume(mOutput->stream, left, right); 3963bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 3964bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 3965bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 3966bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 3967bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 3968bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 396981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( 397081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<Track> > *tracksToRemove 397181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent) 397281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 3973d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent size_t count = mActiveTracks.size(); 397481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixer_state mixerStatus = MIXER_IDLE; 397581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 397681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // find out which tracks need to be processed 3977d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent for (size_t i = 0; i < count; i++) { 3978d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent sp<Track> t = mActiveTracks[i].promote(); 397981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The track died recently 398081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (t == 0) { 3981d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent continue; 398281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 398381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 398481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Track* const track = t.get(); 398581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_track_cblk_t* cblk = track->cblk(); 3986fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // Only consider last track started for volume and mixer state control. 3987fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // In theory an older track could underrun and restart after the new one starts 3988fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // but as we only care about the transition phase between two tracks on a 3989fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // direct output, it is not a problem to ignore the underrun case. 3990fd4779740ec3e9e865d5514464df26d015354388Eric Laurent sp<Track> l = mLatestActiveTrack.promote(); 3991fd4779740ec3e9e865d5514464df26d015354388Eric Laurent bool last = l.get() == track; 399281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 399381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The first time a track is added we wait 399481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // for all its buffers to be filled before processing it 399581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t minFrames; 3996ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing()) { 399781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minFrames = mNormalFrameCount; 399881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 399981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minFrames = 1; 400081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 4001bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4002ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent ALOGI("prepareTracks_l minFrames %d state %d frames ready %d, ", 4003ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent minFrames, track->mState, track->framesReady()); 4004ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent if ((track->framesReady() >= minFrames) && track->isReady() && !track->isPaused() && 4005ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent !track->isStopping_2() && !track->isStopped()) 400681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 4007f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten ALOGVV("track %d s=%08x [OK]", track->name(), cblk->mServer); 400881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 400981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->mFillingUpStatus == Track::FS_FILLED) { 401081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mFillingUpStatus = Track::FS_ACTIVE; 40111abbdb4429479975718421c4fef3f79ce7c820e3Eric Laurent // make sure processVolume_l() will apply new volume even if 0 40121abbdb4429479975718421c4fef3f79ce7c820e3Eric Laurent mLeftVolFloat = mRightVolFloat = -1.0; 401381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->mState == TrackBase::RESUMING) { 401481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mState = TrackBase::ACTIVE; 401581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 401681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 401781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 401881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // compute volume for this track 4019bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent processVolume_l(track, last); 4020bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (last) { 4021d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent // reset retry count 4022d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent track->mRetryCount = kMaxTrackRetriesDirect; 4023d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent mActiveTrack = t; 4024d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent mixerStatus = MIXER_TRACKS_READY; 4025d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent } 402681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 4027d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent // clear effect chain input buffer if the last active track started underruns 4028d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent // to avoid sending previous audio buffer again to effects 4029fd4779740ec3e9e865d5514464df26d015354388Eric Laurent if (!mEffectChains.isEmpty() && last) { 403081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains[0]->clearInputBuffer(); 403181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 4032ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent if (track->isStopping_1()) { 4033ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent track->mState = TrackBase::STOPPING_2; 4034ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent } 4035ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent if ((track->sharedBuffer() != 0) || track->isStopped() || 4036ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent track->isStopping_2() || track->isPaused()) { 403781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // We have consumed all the buffers of this track. 403881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Remove it from the list of active tracks. 4039ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent size_t audioHALFrames; 4040ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent if (audio_is_linear_pcm(mFormat)) { 4041ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent audioHALFrames = (latency_l() * mSampleRate) / 1000; 4042ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent } else { 4043ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent audioHALFrames = 0; 4044ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent } 4045ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent 404681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t framesWritten = mBytesWritten / mFrameSize; 4047fd4779740ec3e9e865d5514464df26d015354388Eric Laurent if (mStandby || !last || 4048fd4779740ec3e9e865d5514464df26d015354388Eric Laurent track->presentationComplete(framesWritten, audioHALFrames)) { 4049ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent if (track->isStopping_2()) { 4050ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent track->mState = TrackBase::STOPPED; 4051ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent } 405281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isStopped()) { 405381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->reset(); 405481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 4055d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent tracksToRemove->add(track); 405681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 405781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 405881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // No buffers for this track. Give it a few chances to 405981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // fill a buffer, then remove it from active list. 4060d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent // Only consider last track started for mixer state control 406181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (--(track->mRetryCount) <= 0) { 406281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); 4063d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent tracksToRemove->add(track); 4064a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent // indicate to client process that the track was disabled because of underrun; 4065a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent // it will then automatically call start() when data is available 4066a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent android_atomic_or(CBLK_DISABLED, &cblk->mFlags); 4067bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if (last) { 406881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixerStatus = MIXER_TRACKS_ENABLED; 406981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 407081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 407181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 407281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 407381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 407481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // remove all the tracks that need to be... 4075bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent removeTracks_l(*tracksToRemove); 407681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 407781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mixerStatus; 407881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 407981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 408081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DirectOutputThread::threadLoop_mix() 408181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 408281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t frameCount = mFrameCount; 40832098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung int8_t *curBuf = (int8_t *)mSinkBuffer; 408481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // output audio to hardware 408581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent while (frameCount) { 408634542acfa25c6413c87a94b6f7cc315a0c496277Glenn Kasten AudioBufferProvider::Buffer buffer; 408781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent buffer.frameCount = frameCount; 408881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mActiveTrack->getNextBuffer(&buffer); 4089fa319e6d918b84f93fb5457af5d1cca6421ac517Glenn Kasten if (buffer.raw == NULL) { 409081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent memset(curBuf, 0, frameCount * mFrameSize); 409181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 409281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 409381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); 409481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent frameCount -= buffer.frameCount; 409581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent curBuf += buffer.frameCount * mFrameSize; 409681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mActiveTrack->releaseBuffer(&buffer); 409781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 40982098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer; 409981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = 0; 410081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent standbyTime = systemTime() + standbyDelay; 410181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mActiveTrack.clear(); 410281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 410381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 410481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DirectOutputThread::threadLoop_sleepTime() 410581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 410681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sleepTime == 0) { 410781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mMixerStatus == MIXER_TRACKS_ENABLED) { 410881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = activeSleepTime; 410981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 411081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = idleSleepTime; 411181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 411281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) { 41132098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung memset(mSinkBuffer, 0, mFrameCount * mFrameSize); 411481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = 0; 411581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 411681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 411781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 411881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// getTrackName_l() must be called with ThreadBase::mLock held 41190f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenint AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask __unused, 4120e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung audio_format_t format __unused, int sessionId __unused) 412181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 412281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 412381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 412481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 412581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// deleteTrackName_l() must be called with ThreadBase::mLock held 41260f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::DirectOutputThread::deleteTrackName_l(int name __unused) 412781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 412881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 412981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 41301035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// checkForNewParameter_l() must be called with ThreadBase::mLock held 41311035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentbool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair, 41321035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status_t& status) 413381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 413481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool reconfig = false; 413581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 41361035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = NO_ERROR; 41371035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 41381035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent AudioParameter param = AudioParameter(keyValuePair); 41391035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent int value; 41401035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { 41411035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // forward device change to effects that have requested to be 41421035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // aware of attached audio device. 41431035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (value != AUDIO_DEVICE_NONE) { 41441035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mOutDevice = value; 41451035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 41461035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mEffectChains[i]->setDevice_l(mOutDevice); 4147c125f38cd0ae35409a01b98a99e483550daa1313Glenn Kasten } 4148c125f38cd0ae35409a01b98a99e483550daa1313Glenn Kasten } 41491035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 41501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { 41511035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // do not accept frame count changes if tracks are open as the track buffer 41521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // size depends on frame count and correct behavior would not be garantied 41531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // if frame count is changed after track creation 41541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (!mTracks.isEmpty()) { 41551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = INVALID_OPERATION; 41561035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 41571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 415881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 41591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 41601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == NO_ERROR) { 41611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = mOutput->stream->common.set_parameters(&mOutput->stream->common, 41621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent keyValuePair.string()); 41631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (!mStandby && status == INVALID_OPERATION) { 41641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mOutput->stream->common.standby(&mOutput->stream->common); 41651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mStandby = true; 41661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mBytesWritten = 0; 416781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = mOutput->stream->common.set_parameters(&mOutput->stream->common, 41681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent keyValuePair.string()); 41691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 41701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == NO_ERROR && reconfig) { 41711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent readOutputParameters_l(); 41721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sendIoConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); 417381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 417481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 41751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 417681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return reconfig; 417781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 417881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 417981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const 418081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 418181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t time; 418281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (audio_is_linear_pcm(mFormat)) { 418381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent time = PlaybackThread::activeSleepTimeUs(); 418481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 418581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent time = 10000; 418681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 418781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return time; 418881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 418981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 419081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const 419181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 419281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t time; 419381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (audio_is_linear_pcm(mFormat)) { 419481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; 419581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 419681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent time = 10000; 419781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 419881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return time; 419981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 420081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 420181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const 420281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 420381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t time; 420481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (audio_is_linear_pcm(mFormat)) { 420581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); 420681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 420781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent time = 10000; 420881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 420981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return time; 421081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 421181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 421281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DirectOutputThread::cacheParameters_l() 421381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 421481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread::cacheParameters_l(); 421581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 421681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // use shorter standby delay as on normal output to release 421781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // hardware resources as soon as possible 4218972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent if (audio_is_linear_pcm(mFormat)) { 4219972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent standbyDelay = microseconds(activeSleepTime*2); 4220972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent } else { 4221972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent standbyDelay = kOffloadStandbyDelayNs; 4222972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent } 422381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 422481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 422581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 422681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4227bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::AsyncCallbackThread::AsyncCallbackThread( 42284de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent const wp<AudioFlinger::PlaybackThread>& playbackThread) 4229bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent : Thread(false /*canCallJava*/), 42304de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent mPlaybackThread(playbackThread), 42313b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence(0), 42323b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence(0) 4233bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4234bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4235bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4236bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::AsyncCallbackThread::~AsyncCallbackThread() 4237bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4238bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4239bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4240bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::AsyncCallbackThread::onFirstRef() 4241bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4242bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO); 4243bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4244bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4245bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::AsyncCallbackThread::threadLoop() 4246bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4247bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent while (!exitPending()) { 42483b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent uint32_t writeAckSequence; 42493b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent uint32_t drainSequence; 4250bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4251bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent { 4252bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 425324a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George while (!((mWriteAckSequence & 1) || 425424a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George (mDrainSequence & 1) || 425524a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George exitPending())) { 425624a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George mWaitWorkCV.wait(mLock); 425724a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George } 425824a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George 4259bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (exitPending()) { 4260bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent break; 4261bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 42623b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d", 42633b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence, mDrainSequence); 42643b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent writeAckSequence = mWriteAckSequence; 42653b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence &= ~1; 42663b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent drainSequence = mDrainSequence; 42673b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence &= ~1; 4268bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4269bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent { 42704de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote(); 42714de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent if (playbackThread != 0) { 42723b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if (writeAckSequence & 1) { 42734de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent playbackThread->resetWriteBlocked(writeAckSequence >> 1); 4274bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 42753b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if (drainSequence & 1) { 42764de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent playbackThread->resetDraining(drainSequence >> 1); 4277bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4278bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4279bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4280bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4281bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return false; 4282bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4283bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4284bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::AsyncCallbackThread::exit() 4285bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4286bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("AsyncCallbackThread::exit"); 4287bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 4288bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent requestExit(); 4289bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mWaitWorkCV.broadcast(); 4290bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4291bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 42923b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence) 4293bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4294bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 42953b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // bit 0 is cleared 42963b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence = sequence << 1; 42973b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent} 42983b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent 42993b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::AsyncCallbackThread::resetWriteBlocked() 43003b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent{ 43013b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent Mutex::Autolock _l(mLock); 43023b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // ignore unexpected callbacks 43033b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if (mWriteAckSequence & 2) { 43043b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence |= 1; 4305bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mWaitWorkCV.signal(); 4306bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4307bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4308bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 43093b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence) 43103b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent{ 43113b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent Mutex::Autolock _l(mLock); 43123b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // bit 0 is cleared 43133b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence = sequence << 1; 43143b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent} 43153b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent 43163b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::AsyncCallbackThread::resetDraining() 4317bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4318bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 43193b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // ignore unexpected callbacks 43203b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if (mDrainSequence & 2) { 43213b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence |= 1; 4322bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mWaitWorkCV.signal(); 4323bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4324bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4325bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4326bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4327bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// ---------------------------------------------------------------------------- 4328bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger, 4329bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioStreamOut* output, audio_io_handle_t id, uint32_t device) 4330bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent : DirectOutputThread(audioFlinger, output, id, device, OFFLOAD), 4331bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mHwPaused(false), 4332ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent mFlushPending(false), 4333d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent mPausedBytesRemaining(0) 4334bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4335fd4779740ec3e9e865d5514464df26d015354388Eric Laurent //FIXME: mStandby should be set to true by ThreadBase constructor 4336fd4779740ec3e9e865d5514464df26d015354388Eric Laurent mStandby = true; 4337bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4338bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4339bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::OffloadThread::threadLoop_exit() 4340bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4341bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mFlushPending || mHwPaused) { 4342bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // If a flush is pending or track was paused, just discard buffered data 4343bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent flushHw_l(); 4344bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else { 4345bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mMixerStatus = MIXER_DRAIN_ALL; 4346bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent threadLoop_drain(); 4347bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 434856604aa3a56dc8e15532597a0a74b3c7b165e006Uday Gupta if (mUseAsyncWrite) { 434956604aa3a56dc8e15532597a0a74b3c7b165e006Uday Gupta ALOG_ASSERT(mCallbackThread != 0); 435056604aa3a56dc8e15532597a0a74b3c7b165e006Uday Gupta mCallbackThread->exit(); 435156604aa3a56dc8e15532597a0a74b3c7b165e006Uday Gupta } 4352bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent PlaybackThread::threadLoop_exit(); 4353bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4354bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4355bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l( 4356bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Vector< sp<Track> > *tracksToRemove 4357bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent) 4358bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4359bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent size_t count = mActiveTracks.size(); 4360bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4361bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mixer_state mixerStatus = MIXER_IDLE; 4362972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent bool doHwPause = false; 4363972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent bool doHwResume = false; 4364972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent 4365ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent ALOGV("OffloadThread::prepareTracks_l active tracks %d", count); 4366ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent 4367bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // find out which tracks need to be processed 4368bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent for (size_t i = 0; i < count; i++) { 4369bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent sp<Track> t = mActiveTracks[i].promote(); 4370bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // The track died recently 4371bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (t == 0) { 4372bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent continue; 4373bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4374bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Track* const track = t.get(); 4375bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent audio_track_cblk_t* cblk = track->cblk(); 4376fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // Only consider last track started for volume and mixer state control. 4377fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // In theory an older track could underrun and restart after the new one starts 4378fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // but as we only care about the transition phase between two tracks on a 4379fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // direct output, it is not a problem to ignore the underrun case. 4380fd4779740ec3e9e865d5514464df26d015354388Eric Laurent sp<Track> l = mLatestActiveTrack.promote(); 4381fd4779740ec3e9e865d5514464df26d015354388Eric Laurent bool last = l.get() == track; 4382fd4779740ec3e9e865d5514464df26d015354388Eric Laurent 43837844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George if (track->isInvalid()) { 43847844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George ALOGW("An invalidated track shouldn't be in active list"); 43857844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George tracksToRemove->add(track); 43867844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George continue; 43877844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George } 43887844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George 43897844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George if (track->mState == TrackBase::IDLE) { 43907844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George ALOGW("An idle track shouldn't be in active list"); 43917844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George continue; 43927844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George } 43937844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George 4394bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (track->isPausing()) { 4395bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->setPaused(); 4396bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (last) { 4397bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (!mHwPaused) { 4398972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent doHwPause = true; 4399bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mHwPaused = true; 4400bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4401bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // If we were part way through writing the mixbuffer to 4402bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // the HAL we must save this until we resume 4403bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // BUG - this will be wrong if a different track is made active, 4404bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // in that case we want to discard the pending data in the 4405bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // mixbuffer and tell the client to present it again when the 4406bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // track is resumed 4407bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mPausedWriteLength = mCurrentWriteLength; 4408bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mPausedBytesRemaining = mBytesRemaining; 4409bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining = 0; // stop writing 4410bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4411bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent tracksToRemove->add(track); 44127844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George } else if (track->isFlushPending()) { 44137844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George track->flushAck(); 44147844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George if (last) { 44157844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George mFlushPending = true; 44167844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George } 44172d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George } else if (track->isResumePending()){ 44182d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George track->resumeAck(); 44192d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George if (last) { 44202d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George if (mPausedBytesRemaining) { 44212d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George // Need to continue write that was interrupted 44222d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George mCurrentWriteLength = mPausedWriteLength; 44232d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George mBytesRemaining = mPausedBytesRemaining; 44242d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George mPausedBytesRemaining = 0; 44252d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George } 44262d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George if (mHwPaused) { 44272d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George doHwResume = true; 44282d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George mHwPaused = false; 44292d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George // threadLoop_mix() will handle the case that we need to 44302d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George // resume an interrupted write 44312d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George } 44322d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George // enable write to audio HAL 44332d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George sleepTime = 0; 44342d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George 44352d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George // Do not handle new data in this iteration even if track->framesReady() 44362d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George mixerStatus = MIXER_TRACKS_ENABLED; 44372d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George } 44382d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George } else if (track->framesReady() && track->isReady() && 44393b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) { 4440f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten ALOGVV("OffloadThread: track %d s=%08x [OK]", track->name(), cblk->mServer); 4441bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (track->mFillingUpStatus == Track::FS_FILLED) { 4442bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mFillingUpStatus = Track::FS_ACTIVE; 44431abbdb4429479975718421c4fef3f79ce7c820e3Eric Laurent // make sure processVolume_l() will apply new volume even if 0 44441abbdb4429479975718421c4fef3f79ce7c820e3Eric Laurent mLeftVolFloat = mRightVolFloat = -1.0; 4445bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4446bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4447bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (last) { 4448d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent sp<Track> previousTrack = mPreviousTrack.promote(); 4449d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent if (previousTrack != 0) { 4450d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent if (track != previousTrack.get()) { 44519da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // Flush any data still being written from last track 44529da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent mBytesRemaining = 0; 44539da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent if (mPausedBytesRemaining) { 44549da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // Last track was paused so we also need to flush saved 44559da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // mixbuffer state and invalidate track so that it will 44569da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // re-submit that unwritten data when it is next resumed 44579da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent mPausedBytesRemaining = 0; 44589da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // Invalidate is a bit drastic - would be more efficient 44599da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // to have a flag to tell client that some of the 44609da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // previously written data was lost 4461d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent previousTrack->invalidate(); 44629da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent } 44639da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // flush data already sent to the DSP if changing audio session as audio 44649da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // comes from a different source. Also invalidate previous track to force a 44659da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // seek when resuming. 4466d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent if (previousTrack->sessionId() != track->sessionId()) { 4467d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent previousTrack->invalidate(); 44689da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent } 44699da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent } 44709da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent } 44719da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent mPreviousTrack = track; 4472bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // reset retry count 4473bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mRetryCount = kMaxTrackRetriesOffload; 4474bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mActiveTrack = t; 4475bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mixerStatus = MIXER_TRACKS_READY; 4476bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4477bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else { 4478f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten ALOGVV("OffloadThread: track %d s=%08x [NOT READY]", track->name(), cblk->mServer); 4479bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (track->isStopping_1()) { 4480bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Hardware buffer can hold a large amount of audio so we must 4481bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // wait for all current track's data to drain before we say 4482bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // that the track is stopped. 4483bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mBytesRemaining == 0) { 4484bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Only start draining when all data in mixbuffer 4485bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // has been written 4486bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2"); 4487bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mState = TrackBase::STOPPING_2; // so presentation completes after drain 44886a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent // do not drain if no data was ever sent to HAL (mStandby == true) 44896a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent if (last && !mStandby) { 44901b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent // do not modify drain sequence if we are already draining. This happens 44911b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent // when resuming from pause after drain. 44921b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent if ((mDrainSequence & 1) == 0) { 44931b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent sleepTime = 0; 44941b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent standbyTime = systemTime() + standbyDelay; 44951b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent mixerStatus = MIXER_DRAIN_TRACK; 44961b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent mDrainSequence += 2; 44971b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent } 4498bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mHwPaused) { 4499bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // It is possible to move from PAUSED to STOPPING_1 without 4500bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // a resume so we must ensure hardware is running 45011b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent doHwResume = true; 4502bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mHwPaused = false; 4503bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4504bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4505bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4506bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if (track->isStopping_2()) { 45076a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent // Drain has completed or we are in standby, signal presentation complete 45086a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent if (!(mDrainSequence & 1) || !last || mStandby) { 4509bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mState = TrackBase::STOPPED; 4510bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent size_t audioHALFrames = 4511bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; 4512bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent size_t framesWritten = 4513665470b36f202bcc8ee2f7417f68fd2608dd07c1Eric Laurent mBytesWritten / audio_stream_out_frame_size(mOutput->stream); 4514bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->presentationComplete(framesWritten, audioHALFrames); 4515bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->reset(); 4516bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent tracksToRemove->add(track); 4517bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4518bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else { 4519bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // No buffers for this track. Give it a few chances to 4520bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // fill a buffer, then remove it from active list. 4521bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (--(track->mRetryCount) <= 0) { 4522bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("OffloadThread: BUFFER TIMEOUT: remove(%d) from active list", 4523bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->name()); 4524bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent tracksToRemove->add(track); 4525a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent // indicate to client process that the track was disabled because of underrun; 4526a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent // it will then automatically call start() when data is available 4527a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent android_atomic_or(CBLK_DISABLED, &cblk->mFlags); 4528bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if (last){ 4529bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mixerStatus = MIXER_TRACKS_ENABLED; 4530bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4531bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4532bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4533bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // compute volume for this track 4534bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent processVolume_l(track, last); 4535bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 45366bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent 4537ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent // make sure the pause/flush/resume sequence is executed in the right order. 4538ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent // If a flush is pending and a track is active but the HW is not paused, force a HW pause 4539ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent // before flush and then resume HW. This can happen in case of pause/flush/resume 4540ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent // if resume is received before pause is executed. 4541fd4779740ec3e9e865d5514464df26d015354388Eric Laurent if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { 4542972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent mOutput->stream->pause(mOutput->stream); 4543972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent } 45446bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent if (mFlushPending) { 45456bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent flushHw_l(); 45466bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent mFlushPending = false; 45476bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent } 4548fd4779740ec3e9e865d5514464df26d015354388Eric Laurent if (!mStandby && doHwResume) { 4549972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent mOutput->stream->resume(mOutput->stream); 4550972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent } 45516bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent 4552bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // remove all the tracks that need to be... 4553bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent removeTracks_l(*tracksToRemove); 4554bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4555bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return mixerStatus; 4556bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4557bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4558bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// must be called with thread mutex locked 4559bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::OffloadThread::waitingAsyncCallback_l() 4560bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 45613b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d", 45623b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence, mDrainSequence); 45633b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) { 4564bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return true; 4565bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4566bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return false; 4567bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4568bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4569bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// must be called with thread mutex locked 4570bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::OffloadThread::shouldStandby_l() 4571bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4572e6f35b13567d920af19f0b8dc35f9120b7cf1dc9Glenn Kasten bool trackPaused = false; 4573bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4574bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack 4575bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // after a timeout and we will enter standby then. 4576bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mTracks.size() > 0) { 4577e6f35b13567d920af19f0b8dc35f9120b7cf1dc9Glenn Kasten trackPaused = mTracks[mTracks.size() - 1]->isPaused(); 4578bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4579bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4580e6f35b13567d920af19f0b8dc35f9120b7cf1dc9Glenn Kasten return !mStandby && !trackPaused; 4581bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4582bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4583bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4584bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::OffloadThread::waitingAsyncCallback() 4585bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4586bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 4587bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return waitingAsyncCallback_l(); 4588bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4589bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4590bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::OffloadThread::flushHw_l() 4591bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4592bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mOutput->stream->flush(mOutput->stream); 4593bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Flush anything still waiting in the mixbuffer 4594bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mCurrentWriteLength = 0; 4595bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining = 0; 4596bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mPausedWriteLength = 0; 4597bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mPausedBytesRemaining = 0; 45980f02f265123b7ef2fd6ac09ff70cde26eb5559adHaynes Mathew George mHwPaused = false; 45990f02f265123b7ef2fd6ac09ff70cde26eb5559adHaynes Mathew George 4600bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mUseAsyncWrite) { 46013b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // discard any pending drain or write ack by incrementing sequence 46023b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence = (mWriteAckSequence + 2) & ~1; 46033b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence = (mDrainSequence + 2) & ~1; 4604bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 46053b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setWriteBlocked(mWriteAckSequence); 46063b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setDraining(mDrainSequence); 4607bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4608bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4609bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 46104c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew Georgevoid AudioFlinger::OffloadThread::onAddNewTrack_l() 46114c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George{ 46124c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George sp<Track> previousTrack = mPreviousTrack.promote(); 46134c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George sp<Track> latestTrack = mLatestActiveTrack.promote(); 46144c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George 46154c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George if (previousTrack != 0 && latestTrack != 0 && 46164c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George (previousTrack->sessionId() != latestTrack->sessionId())) { 46174c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George mFlushPending = true; 46184c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George } 46194c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George PlaybackThread::onAddNewTrack_l(); 46204c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George} 46214c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George 4622bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// ---------------------------------------------------------------------------- 4623bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 462481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, 462581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioFlinger::MixerThread* mainThread, audio_io_handle_t id) 462681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->outDevice(), 462781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent DUPLICATING), 462881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitTimeMs(UINT_MAX) 462981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 463081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent addOutputTrack(mainThread); 463181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 463281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 463381784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::DuplicatingThread::~DuplicatingThread() 463481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 463581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mOutputTracks.size(); i++) { 463681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutputTracks[i]->destroy(); 463781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 463881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 463981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 464081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::threadLoop_mix() 464181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 464281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mix buffers... 464381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (outputsReady(outputTracks)) { 464481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->process(AudioBufferProvider::kInvalidPTS); 464581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 464625c2dac12114699e90deb1c579cadebce7b91a97Andy Hung memset(mSinkBuffer, 0, mSinkBufferSize); 464781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 464881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = 0; 464981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent writeFrames = mNormalFrameCount; 465025c2dac12114699e90deb1c579cadebce7b91a97Andy Hung mCurrentWriteLength = mSinkBufferSize; 465181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent standbyTime = systemTime() + standbyDelay; 465281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 465381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 465481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::threadLoop_sleepTime() 465581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 465681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sleepTime == 0) { 465781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mMixerStatus == MIXER_TRACKS_ENABLED) { 465881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = activeSleepTime; 465981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 466081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = idleSleepTime; 466181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 466281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (mBytesWritten != 0) { 466381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mMixerStatus == MIXER_TRACKS_ENABLED) { 466481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent writeFrames = mNormalFrameCount; 466525c2dac12114699e90deb1c579cadebce7b91a97Andy Hung memset(mSinkBuffer, 0, mSinkBufferSize); 466681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 466781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // flush remaining overflow buffers in output tracks 466881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent writeFrames = 0; 466981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 467081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = 0; 467181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 467281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 467381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4674bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentssize_t AudioFlinger::DuplicatingThread::threadLoop_write() 467581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 467681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < outputTracks.size(); i++) { 4677010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // We convert the duplicating thread format to AUDIO_FORMAT_PCM_16_BIT 4678010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // for delivery downstream as needed. This in-place conversion is safe as 4679010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // AUDIO_FORMAT_PCM_16_BIT is smaller than any other supported format 4680010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // (AUDIO_FORMAT_PCM_8_BIT is not allowed here). 4681010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung if (mFormat != AUDIO_FORMAT_PCM_16_BIT) { 4682010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung memcpy_by_audio_format(mSinkBuffer, AUDIO_FORMAT_PCM_16_BIT, 4683010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung mSinkBuffer, mFormat, writeFrames * mChannelCount); 4684010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung } 4685010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung outputTracks[i]->write(reinterpret_cast<int16_t*>(mSinkBuffer), writeFrames); 468681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 46872c3740f01acca69c3e0bcc5e01bb0edc51b6777fEric Laurent mStandby = false; 468825c2dac12114699e90deb1c579cadebce7b91a97Andy Hung return (ssize_t)mSinkBufferSize; 468981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 469081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 469181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::threadLoop_standby() 469281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 469381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // DuplicatingThread implements standby by stopping all tracks 469481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < outputTracks.size(); i++) { 469581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent outputTracks[i]->stop(); 469681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 469781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 469881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 469981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::saveOutputTracks() 470081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 470181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent outputTracks = mOutputTracks; 470281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 470381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 470481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::clearOutputTracks() 470581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 470681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent outputTracks.clear(); 470781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 470881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 470981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) 471081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 471181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 471281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME explain this formula 471381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t frameCount = (3 * mNormalFrameCount * mSampleRate) / thread->sampleRate(); 4714010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // OutputTrack is forced to AUDIO_FORMAT_PCM_16_BIT regardless of mFormat 4715010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // due to current usage case and restrictions on the AudioBufferProvider. 4716010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // Actual buffer conversion is done in threadLoop_write(). 4717010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // 4718010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // TODO: This may change in the future, depending on multichannel 4719010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // (and non int16_t*) support on AF::PlaybackThread::OutputTrack 472081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent OutputTrack *outputTrack = new OutputTrack(thread, 472181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent this, 472281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSampleRate, 4723010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung AUDIO_FORMAT_PCM_16_BIT, 472481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mChannelMask, 4725462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen frameCount, 4726462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen IPCThreadState::self()->getCallingUid()); 472781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (outputTrack->cblk() != NULL) { 472881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread->setStreamVolume(AUDIO_STREAM_CNT, 1.0f); 472981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutputTracks.add(outputTrack); 473081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread); 473181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent updateWaitTime_l(); 473281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 473381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 473481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 473581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) 473681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 473781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 473881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mOutputTracks.size(); i++) { 473981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutputTracks[i]->thread() == thread) { 474081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutputTracks[i]->destroy(); 474181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutputTracks.removeAt(i); 474281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent updateWaitTime_l(); 474381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 474481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 474581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 474681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("removeOutputTrack(): unkonwn thread: %p", thread); 474781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 474881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 474981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// caller must hold mLock 475081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::updateWaitTime_l() 475181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 475281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitTimeMs = UINT_MAX; 475381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mOutputTracks.size(); i++) { 475481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); 475581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (strong != 0) { 475681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); 475781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (waitTimeMs < mWaitTimeMs) { 475881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitTimeMs = waitTimeMs; 475981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 476081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 476181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 476281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 476381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 476481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 476581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::DuplicatingThread::outputsReady( 476681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const SortedVector< sp<OutputTrack> > &outputTracks) 476781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 476881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < outputTracks.size(); i++) { 476981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<ThreadBase> thread = outputTracks[i]->thread().promote(); 477081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread == 0) { 477181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", 477281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent outputTracks[i].get()); 477381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 477481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 477581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); 477681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // see note at standby() declaration 477781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (playbackThread->standby() && !playbackThread->isSuspended()) { 477881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), 477981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread.get()); 478081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 478181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 478281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 478381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return true; 478481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 478581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 478681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const 478781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 478881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return (mWaitTimeMs * 1000) / 2; 478981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 479081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 479181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::cacheParameters_l() 479281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 479381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first 479481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent updateWaitTime_l(); 479581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 479681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent MixerThread::cacheParameters_l(); 479781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 479881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 479981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 480081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Record 480181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 480281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 480381784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, 480481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamIn *input, 480581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t id, 4806d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent audio_devices_t outDevice, 480746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten audio_devices_t inDevice 480846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 480946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten , const sp<NBAIO_Sink>& teeSink 481046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 481146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten ) : 4812d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent ThreadBase(audioFlinger, id, outDevice, inDevice, RECORD), 48136dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten mInput(input), mActiveTracksGen(0), mRsmpInBuffer(NULL), 4814deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten // mRsmpInFrames and mRsmpInFramesP2 are set by readInputParameters_l() 48154cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten mRsmpInRear(0) 481646909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 481746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten , mTeeSink(teeSink) 481846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 4819b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize, 4820b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten "RecordThreadRO", MemoryHeapBase::READ_ONLY)) 48216dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // mFastCapture below 48226dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten , mFastCaptureFutex(0) 48236dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // mInputSource 48246dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // mPipeSink 48256dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // mPipeSource 48266dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten , mPipeFramesP2(0) 48276dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // mPipeMemory 48286dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // mFastCaptureNBLogWriter 48296e6704c06d61bc356e30c164081e5bcffb37920cGlenn Kasten , mFastTrackAvail(false) 483081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 483181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent snprintf(mName, kNameLength, "AudioIn_%X", id); 4832481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mName); 483381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4834deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten readInputParameters_l(); 48356dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 48366dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // create an NBAIO source for the HAL input stream, and negotiate 48376dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mInputSource = new AudioStreamInSource(input->stream); 48386dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten size_t numCounterOffers = 0; 48396dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; 48406dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ssize_t index = mInputSource->negotiate(offers, 1, NULL, numCounterOffers); 48416dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOG_ASSERT(index == 0); 48426dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 48436dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // initialize fast capture depending on configuration 48446dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten bool initFastCapture; 48456dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten switch (kUseFastCapture) { 48466dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten case FastCapture_Never: 48476dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten initFastCapture = false; 48486dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten break; 48496dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten case FastCapture_Always: 48506dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten initFastCapture = true; 48516dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten break; 48526dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten case FastCapture_Static: 48536dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten uint32_t primaryOutputSampleRate; 48546dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten { 48556dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten AutoMutex _l(audioFlinger->mHardwareLock); 48566dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten primaryOutputSampleRate = audioFlinger->mPrimaryOutputSampleRate; 48576dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 48586dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten initFastCapture = 48596dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // either capture sample rate is same as (a reasonable) primary output sample rate 48606dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (((primaryOutputSampleRate == 44100 || primaryOutputSampleRate == 48000) && 48616dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (mSampleRate == primaryOutputSampleRate)) || 48626dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // or primary output sample rate is unknown, and capture sample rate is reasonable 48636dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ((primaryOutputSampleRate == 0) && 48646dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ((mSampleRate == 44100 || mSampleRate == 48000)))) && 48659f81de3452dfb2385bd57dc05456a045174a1ab1Glenn Kasten // and the buffer size is < 12 ms 48669f81de3452dfb2385bd57dc05456a045174a1ab1Glenn Kasten (mFrameCount * 1000) / mSampleRate < 12; 48676dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten break; 48686dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // case FastCapture_Dynamic: 48696dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 48706dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 48716dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (initFastCapture) { 48726dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // create a Pipe for FastMixer to write to, and for us and fast tracks to read from 48736dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten NBAIO_Format format = mInputSource->format(); 48746dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten size_t pipeFramesP2 = roundup(mFrameCount * 8); 48756dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten size_t pipeSize = pipeFramesP2 * Format_frameSize(format); 48766dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten void *pipeBuffer; 48776dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten const sp<MemoryDealer> roHeap(readOnlyHeap()); 48786dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sp<IMemory> pipeMemory; 48796dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if ((roHeap == 0) || 48806dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (pipeMemory = roHeap->allocate(pipeSize)) == 0 || 48816dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (pipeBuffer = pipeMemory->pointer()) == NULL) { 48826dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOGE("not enough memory for pipe buffer size=%zu", pipeSize); 48836dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten goto failed; 48846dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 48856dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // pipe will be shared directly with fast clients, so clear to avoid leaking old information 48866dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten memset(pipeBuffer, 0, pipeSize); 48876dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer); 48886dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten const NBAIO_Format offers[1] = {format}; 48896dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten size_t numCounterOffers = 0; 48906dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); 48916dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOG_ASSERT(index == 0); 48926dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mPipeSink = pipe; 48936dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten PipeReader *pipeReader = new PipeReader(*pipe); 48946dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten numCounterOffers = 0; 48956dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); 48966dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOG_ASSERT(index == 0); 48976dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mPipeSource = pipeReader; 48986dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mPipeFramesP2 = pipeFramesP2; 48996dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mPipeMemory = pipeMemory; 49006dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 49016dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // create fast capture 49026dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastCapture = new FastCapture(); 49036dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureStateQueue *sq = mFastCapture->sq(); 49046dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef STATE_QUEUE_DUMP 49056dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // FIXME 49066dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif 49076dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureState *state = sq->begin(); 49086dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mCblk = NULL; 49096dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mInputSource = mInputSource.get(); 49106dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mInputSourceGen++; 49116dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mPipeSink = pipe; 49126dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mPipeSinkGen++; 49136dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mFrameCount = mFrameCount; 49146dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mCommand = FastCaptureState::COLD_IDLE; 49156dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // already done in constructor initialization list 49166dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten //mFastCaptureFutex = 0; 49176dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mColdFutexAddr = &mFastCaptureFutex; 49186dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mColdGen++; 49196dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mDumpState = &mFastCaptureDumpState; 49206dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef TEE_SINK 49216dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // FIXME 49226dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif 49236dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture"); 49246dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mNBLogWriter = mFastCaptureNBLogWriter.get(); 49256dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sq->end(); 49266dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); 49276dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 49286dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // start the fast capture 49296dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO); 49306dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten pid_t tid = mFastCapture->getTid(); 49316dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten int err = requestPriority(getpid_cached, tid, kPriorityFastMixer); 49326dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (err != 0) { 49336dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", 49346dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten kPriorityFastCapture, getpid_cached, tid, err); 49356dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 49366dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 49376dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef AUDIO_WATCHDOG 49386dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // FIXME 49396dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif 49406dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 49416e6704c06d61bc356e30c164081e5bcffb37920cGlenn Kasten mFastTrackAvail = true; 49426dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 49436dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kastenfailed: ; 49446dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 49456dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // FIXME mNormalSource 494681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 494781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 494881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 494981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::RecordThread::~RecordThread() 495081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 49516dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (mFastCapture != 0) { 49526dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureStateQueue *sq = mFastCapture->sq(); 49536dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureState *state = sq->begin(); 49546dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (state->mCommand == FastCaptureState::COLD_IDLE) { 49556dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten int32_t old = android_atomic_inc(&mFastCaptureFutex); 49566dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (old == -1) { 49576dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); 49586dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 49596dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 49606dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mCommand = FastCaptureState::EXIT; 49616dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sq->end(); 49626dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); 49636dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastCapture->join(); 49646dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastCapture.clear(); 49656dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 49666dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter); 4967481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten mAudioFlinger->unregisterWriter(mNBLogWriter); 496881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent delete[] mRsmpInBuffer; 496981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 497081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 497181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::onFirstRef() 497281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 497381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent run(mName, PRIORITY_URGENT_AUDIO); 497481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 497581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 497681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::RecordThread::threadLoop() 497781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 497881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent nsecs_t lastWarning = 0; 497981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 498081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent inputStandBy(); 498181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4982f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kastenreacquire_wakelock: 4983f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten sp<RecordTrack> activeTrack; 49842b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten int activeTracksGen; 4985f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten { 4986f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten Mutex::Autolock _l(mLock); 49872b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten size_t size = mActiveTracks.size(); 49882b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten activeTracksGen = mActiveTracksGen; 49892b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (size > 0) { 49902b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten // FIXME an arbitrary choice 49912b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten activeTrack = mActiveTracks[0]; 49922b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten acquireWakeLock_l(activeTrack->uid()); 49932b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (size > 1) { 49942b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten SortedVector<int> tmp; 49952b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten for (size_t i = 0; i < size; i++) { 49962b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten tmp.add(mActiveTracks[i]->uid()); 49972b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten } 49982b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten updateWakeLockUids_l(tmp); 49992b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten } 50002b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten } else { 50012b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten acquireWakeLock_l(-1); 50022b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten } 5003f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten } 5004f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten 50056dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // used to request a deferred sleep, to be executed later while mutex is unlocked 50066dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten uint32_t sleepUs = 0; 50076dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 50086dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // loop while there is work to do 50094ef0b463a56c19bad9197aa9f90d792090461429Glenn Kasten for (;;) { 5010c527a7c2b1bfd26e8f3086e1b653d56e521379d9Glenn Kasten Vector< sp<EffectChain> > effectChains; 50112cfbf88b89854f30b295e8ae26a031edb8d712f8Glenn Kasten 50125edadd46c76c5ff1c3edabf2ea943c2278e82e1cGlenn Kasten // sleep with mutex unlocked 50136dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (sleepUs > 0) { 50146dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten usleep(sleepUs); 50156dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten sleepUs = 0; 50165edadd46c76c5ff1c3edabf2ea943c2278e82e1cGlenn Kasten } 50175edadd46c76c5ff1c3edabf2ea943c2278e82e1cGlenn Kasten 50186dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // activeTracks accumulates a copy of a subset of mActiveTracks 50196dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten Vector< sp<RecordTrack> > activeTracks; 50206dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 50216dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // reference to the (first and only) fast track 50226dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sp<RecordTrack> fastTrack; 50231035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 502481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // scope for mLock 502581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 5026000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent 5027021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent processConfigEvents_l(); 5028f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten 5029000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent // check exitPending here because checkForNewParameters_l() and 5030000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent // checkForNewParameters_l() can temporarily release mLock 5031000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent if (exitPending()) { 5032000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent break; 5033000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent } 5034000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent 50352b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten // if no active track(s), then standby and release wakelock 50362b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten size_t size = mActiveTracks.size(); 50372b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (size == 0) { 503893e471f620454f7de73d190521568b1e25879767Glenn Kasten standbyIfNotAlreadyInStandby(); 50394ef0b463a56c19bad9197aa9f90d792090461429Glenn Kasten // exitPending() can't become true here 504081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock_l(); 504181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("RecordThread: loop stopping"); 504281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // go to sleep 504381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitWorkCV.wait(mLock); 504481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("RecordThread: loop starting"); 5045f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten goto reacquire_wakelock; 5046f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten } 5047f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten 50482b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (mActiveTracksGen != activeTracksGen) { 50492b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten activeTracksGen = mActiveTracksGen; 5050f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten SortedVector<int> tmp; 50512b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten for (size_t i = 0; i < size; i++) { 50522b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten tmp.add(mActiveTracks[i]->uid()); 50532b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten } 5054f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten updateWakeLockUids_l(tmp); 505581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 50569e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten 50576dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten bool doBroadcast = false; 50586dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten for (size_t i = 0; i < size; ) { 50599e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten 50606dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack = mActiveTracks[i]; 50616dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTrack->isTerminated()) { 50626dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten removeTrack_l(activeTrack); 50632b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracks.remove(activeTrack); 50642b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracksGen++; 50656dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size--; 50669e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten continue; 50679e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten } 50686dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 50696dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten TrackBase::track_state activeTrackState = activeTrack->mState; 50706dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten switch (activeTrackState) { 50716dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 50726dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case TrackBase::PAUSING: 50736dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten mActiveTracks.remove(activeTrack); 50746dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten mActiveTracksGen++; 50756dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten doBroadcast = true; 50766dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size--; 50776dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten continue; 50786dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 50796dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case TrackBase::STARTING_1: 50806dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten sleepUs = 10000; 50816dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten i++; 50826dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten continue; 50836dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 50846dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case TrackBase::STARTING_2: 50856dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten doBroadcast = true; 50866dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten mStandby = false; 50879e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten activeTrack->mState = TrackBase::ACTIVE; 50886dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten break; 50896dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 50906dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case TrackBase::ACTIVE: 50916dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten break; 50926dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 50936dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case TrackBase::IDLE: 50946dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten i++; 50956dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten continue; 50966dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 50976dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten default: 5098adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("Unexpected activeTrackState %d", activeTrackState); 50999e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten } 51002d94426cd3302cb1215c92c5f1c4b90c24ceb72bGlenn Kasten 51016dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTracks.add(activeTrack); 51026dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten i++; 51032d94426cd3302cb1215c92c5f1c4b90c24ceb72bGlenn Kasten 51046dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (activeTrack->isFastTrack()) { 51056dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOG_ASSERT(!mFastTrackAvail); 51066dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOG_ASSERT(fastTrack == 0); 51076dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten fastTrack = activeTrack; 51086dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 51096dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 51106dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (doBroadcast) { 51116dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten mStartStopCond.broadcast(); 51126dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 5113d9fc34fb0fcfcc739f868b116edf50c62af19d5eGlenn Kasten 51146dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // sleep if there are no active tracks to process 51156dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTracks.size() == 0) { 51166dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (sleepUs == 0) { 51176dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten sleepUs = kRecordThreadSleepUs; 51186dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 51196dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten continue; 512081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 51216dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten sleepUs = 0; 51229e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten 512381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lockEffectChains_l(effectChains); 512481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 512581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 51266dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0 51277165268ffa6c7b6b405b6afad82e2a346500e8eeGlenn Kasten 51286dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size_t size = effectChains.size(); 51296dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten for (size_t i = 0; i < size; i++) { 51301ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten // thread mutex is not locked, but effect chain is locked 51311ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten effectChains[i]->process_l(); 51321ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten } 513381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 51346dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // Start the fast capture if it's not already running 51356dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (mFastCapture != 0) { 51366dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureStateQueue *sq = mFastCapture->sq(); 51376dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureState *state = sq->begin(); 51386dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME && 51396dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) { 51406dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (state->mCommand == FastCaptureState::COLD_IDLE) { 51416dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten int32_t old = android_atomic_inc(&mFastCaptureFutex); 51426dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (old == -1) { 51436dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); 51446dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 51456dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 51466dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mCommand = FastCaptureState::READ_WRITE; 51476dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#if 0 // FIXME 51486dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? 51496dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureDumpState::kSamplingNforLowRamDevice : FastMixerDumpState::kSamplingN); 51506dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif 51516dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mCblk = fastTrack != 0 ? fastTrack->cblk() : NULL; 51526dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sq->end(); 51536dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); 51546dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#if 0 51556dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (kUseFastCapture == FastCapture_Dynamic) { 51566dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mNormalSource = mPipeSource; 51576dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 51586dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif 51596dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } else { 51606dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sq->end(false /*didModify*/); 51616dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 51626dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 51636dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 51646dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one. 51656dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // Only the client(s) that are too slow will overrun. But if even the fastest client is too 51666dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // slow, then this RecordThread will overrun by not calling HAL read often enough. 51676dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // If destination is non-contiguous, first read past the nominal end of buffer, then 51686dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // copy to the right place. Permitted because mRsmpInBuffer was over-allocated. 51696dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 51706dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1); 51716dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ssize_t framesRead; 51726dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 51736dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // If an NBAIO source is present, use it to read the normal capture's data 51746dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (mPipeSource != 0) { 51756dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten size_t framesToRead = mBufferSize / mFrameSize; 51766dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten framesRead = mPipeSource->read(&mRsmpInBuffer[rear * mChannelCount], 51776dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten framesToRead, AudioBufferProvider::kInvalidPTS); 51786dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (framesRead == 0) { 51796dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // since pipe is non-blocking, simulate blocking input 51806dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sleepUs = (framesToRead * 1000000LL) / mSampleRate; 51816dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 51826dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // otherwise use the HAL / AudioStreamIn directly 51836dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } else { 51846dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ssize_t bytesRead = mInput->stream->read(mInput->stream, 51856dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten &mRsmpInBuffer[rear * mChannelCount], mBufferSize); 51866dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (bytesRead < 0) { 51876dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten framesRead = bytesRead; 51886dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } else { 51896dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten framesRead = bytesRead / mFrameSize; 51906dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 51916dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 51926dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 51936dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) { 51946dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOGE("read failed: framesRead=%d", framesRead); 51956dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // Force input into standby so that it tries to recover at next read attempt 51966dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten inputStandBy(); 51976dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten sleepUs = kRecordThreadSleepUs; 51986dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 51996dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (framesRead <= 0) { 52003d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten goto unlock; 52016dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 52026dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ALOG_ASSERT(framesRead > 0); 52036dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 52046dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (mTeeSink != 0) { 52056dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten (void) mTeeSink->write(&mRsmpInBuffer[rear * mChannelCount], framesRead); 52066dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 52076dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // If destination is non-contiguous, we now correct for reading past end of buffer. 52083d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten { 52093d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten size_t part1 = mRsmpInFramesP2 - rear; 52103d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten if ((size_t) framesRead > part1) { 52113d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten memcpy(mRsmpInBuffer, &mRsmpInBuffer[mRsmpInFramesP2 * mChannelCount], 52123d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten (framesRead - part1) * mFrameSize); 52133d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten } 52146dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 52156dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten rear = mRsmpInRear += framesRead; 52166dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 52176dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size = activeTracks.size(); 52186dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // loop over each active track 52196dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten for (size_t i = 0; i < size; i++) { 52206dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack = activeTracks[i]; 52216dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 52226dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // skip fast tracks, as those are handled directly by FastCapture 52236dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (activeTrack->isFastTrack()) { 52246dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten continue; 52256dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 52266dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 52276dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten enum { 52286dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten OVERRUN_UNKNOWN, 52296dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten OVERRUN_TRUE, 52306dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten OVERRUN_FALSE 52316dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } overrun = OVERRUN_UNKNOWN; 52326dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 52336dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // loop over getNextBuffer to handle circular sink 52346dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten for (;;) { 52356dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 52366dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mSink.frameCount = ~0; 52376dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten status_t status = activeTrack->getNextBuffer(&activeTrack->mSink); 52386dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size_t framesOut = activeTrack->mSink.frameCount; 52396dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0)); 52406dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 52416dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten int32_t front = activeTrack->mRsmpInFront; 52426dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ssize_t filled = rear - front; 52436dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size_t framesIn; 52446dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 52456dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (filled < 0) { 52466dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // should not happen, but treat like a massive overrun and re-sync 52476dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten framesIn = 0; 52486dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mRsmpInFront = rear; 52496dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten overrun = OVERRUN_TRUE; 5250607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten } else if ((size_t) filled <= mRsmpInFrames) { 52516dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten framesIn = (size_t) filled; 52526dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } else { 52536dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // client is not keeping up with server, but give it latest data 5254607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten framesIn = mRsmpInFrames; 5255607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten activeTrack->mRsmpInFront = front = rear - framesIn; 52566dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten overrun = OVERRUN_TRUE; 52576dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 52586dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 52594cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten if (framesOut == 0 || framesIn == 0) { 52604cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten break; 52614cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten } 52624cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten 52636dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTrack->mResampler == NULL) { 52646dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // no resampling 52656dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (framesIn > framesOut) { 52666dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten framesIn = framesOut; 52676dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } else { 52686dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten framesOut = framesIn; 52696dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 52706dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten int8_t *dst = activeTrack->mSink.i8; 52716dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten while (framesIn > 0) { 52726dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten front &= mRsmpInFramesP2 - 1; 52736dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size_t part1 = mRsmpInFramesP2 - front; 52746dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (part1 > framesIn) { 52756dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten part1 = framesIn; 527681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 52776dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten int8_t *src = (int8_t *)mRsmpInBuffer + (front * mFrameSize); 52784cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten if (mChannelCount == activeTrack->mChannelCount) { 52796dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten memcpy(dst, src, part1 * mFrameSize); 52806dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } else if (mChannelCount == 1) { 5281cd704219d22ec51660103684a680caf2c3a12dadGlenn Kasten upmix_to_stereo_i16_from_mono_i16((int16_t *)dst, (const int16_t *)src, 52826dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten part1); 52831ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten } else { 5284cd704219d22ec51660103684a680caf2c3a12dadGlenn Kasten downmix_to_mono_i16_from_stereo_i16((int16_t *)dst, (const int16_t *)src, 52856dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten part1); 52861ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten } 52876dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten dst += part1 * activeTrack->mFrameSize; 52886dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten front += part1; 52896dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten framesIn -= part1; 52901ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten } 52916dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mRsmpInFront += framesOut; 52926dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 52936dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } else { 52946dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // resampling 52956dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME framesInNeeded should really be part of resampler API, and should 52966dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // depend on the SRC ratio 52976dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // to keep mRsmpInBuffer full so resampler always has sufficient input 52986dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size_t framesInNeeded; 52996dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME only re-calculate when it changes, and optimize for common ratios 53008661aaff9e9a4d4c1b57f6a68cdbcab006354ab2Andy Hung // Do not precompute in/out because floating point is not associative 53018661aaff9e9a4d4c1b57f6a68cdbcab006354ab2Andy Hung // e.g. a*b/c != a*(b/c). 53028661aaff9e9a4d4c1b57f6a68cdbcab006354ab2Andy Hung const double in(mSampleRate); 53038661aaff9e9a4d4c1b57f6a68cdbcab006354ab2Andy Hung const double out(activeTrack->mSampleRate); 53048661aaff9e9a4d4c1b57f6a68cdbcab006354ab2Andy Hung framesInNeeded = ceil(framesOut * in / out) + 1; 5305607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten ALOGV("need %u frames in to produce %u out given in/out ratio of %.4g", 53068661aaff9e9a4d4c1b57f6a68cdbcab006354ab2Andy Hung framesInNeeded, framesOut, in / out); 5307607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten // Although we theoretically have framesIn in circular buffer, some of those are 5308607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten // unreleased frames, and thus must be discounted for purpose of budgeting. 5309607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten size_t unreleased = activeTrack->mRsmpInUnrel; 5310607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten framesIn = framesIn > unreleased ? framesIn - unreleased : 0; 53116dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (framesIn < framesInNeeded) { 5312607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten ALOGV("not enough to resample: have %u frames in but need %u in to " 5313607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten "produce %u out given in/out ratio of %.4g", 53148661aaff9e9a4d4c1b57f6a68cdbcab006354ab2Andy Hung framesIn, framesInNeeded, framesOut, in / out); 53158661aaff9e9a4d4c1b57f6a68cdbcab006354ab2Andy Hung size_t newFramesOut = framesIn > 0 ? floor((framesIn - 1) * out / in) : 0; 5316607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten LOG_ALWAYS_FATAL_IF(newFramesOut >= framesOut); 5317607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten if (newFramesOut == 0) { 5318607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten break; 53194cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten } 53208661aaff9e9a4d4c1b57f6a68cdbcab006354ab2Andy Hung framesInNeeded = ceil(newFramesOut * in / out) + 1; 5321607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten ALOGV("now need %u frames in to produce %u out given out/in ratio of %.4g", 53228661aaff9e9a4d4c1b57f6a68cdbcab006354ab2Andy Hung framesInNeeded, newFramesOut, out / in); 5323607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten LOG_ALWAYS_FATAL_IF(framesIn < framesInNeeded); 5324607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten ALOGV("success 2: have %u frames in and need %u in to produce %u out " 5325607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten "given in/out ratio of %.4g", 53268661aaff9e9a4d4c1b57f6a68cdbcab006354ab2Andy Hung framesIn, framesInNeeded, newFramesOut, in / out); 5327607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten framesOut = newFramesOut; 53284cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten } else { 5329607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten ALOGV("success 1: have %u in and need %u in to produce %u out " 53304cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten "given in/out ratio of %.4g", 53318661aaff9e9a4d4c1b57f6a68cdbcab006354ab2Andy Hung framesIn, framesInNeeded, framesOut, in / out); 53328594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten } 53336dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 53346dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // reallocate mRsmpOutBuffer as needed; we will grow but never shrink 53356dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTrack->mRsmpOutFrameCount < framesOut) { 5336607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten // FIXME why does each track need it's own mRsmpOutBuffer? can't they share? 53376dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten delete[] activeTrack->mRsmpOutBuffer; 53386dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // resampler always outputs stereo 53396dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mRsmpOutBuffer = new int32_t[framesOut * FCC_2]; 53406dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mRsmpOutFrameCount = framesOut; 53418594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten } 53426dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 53436dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // resampler accumulates, but we only have one source track 53446dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten memset(activeTrack->mRsmpOutBuffer, 0, framesOut * FCC_2 * sizeof(int32_t)); 53456dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mResampler->resample(activeTrack->mRsmpOutBuffer, framesOut, 5346607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten // FIXME how about having activeTrack implement this interface itself? 53476dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mResamplerBufferProvider 53486dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten /*this*/ /* AudioBufferProvider* */); 53496dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // ditherAndClamp() works as long as all buffers returned by 53506dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // activeTrack->getNextBuffer() are 32 bit aligned which should be always true. 53514cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten if (activeTrack->mChannelCount == 1) { 535284a0c6e87c48f58a0d3be71961432c086a4d24ccAndy Hung // temporarily type pun mRsmpOutBuffer from Q4.27 to int16_t 53536dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ditherAndClamp(activeTrack->mRsmpOutBuffer, activeTrack->mRsmpOutBuffer, 53546dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten framesOut); 53556dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // the resampler always outputs stereo samples: 53566dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // do post stereo to mono conversion 53576dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten downmix_to_mono_i16_from_stereo_i16(activeTrack->mSink.i16, 5358cd704219d22ec51660103684a680caf2c3a12dadGlenn Kasten (const int16_t *)activeTrack->mRsmpOutBuffer, framesOut); 53596dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } else { 53606dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ditherAndClamp((int32_t *)activeTrack->mSink.raw, 53616dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mRsmpOutBuffer, framesOut); 53628594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten } 53636dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // now done with mRsmpOutBuffer 53648594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten 53658594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten } 53668594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten 53676dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) { 53686dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten overrun = OVERRUN_FALSE; 53691ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten } 537081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 53716dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTrack->mFramesToDrop == 0) { 53726dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (framesOut > 0) { 53736dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mSink.frameCount = framesOut; 53746dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->releaseBuffer(&activeTrack->mSink); 537581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 537681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 53776dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME could do a partial drop of framesOut 53786dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTrack->mFramesToDrop > 0) { 53796dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mFramesToDrop -= framesOut; 53806dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTrack->mFramesToDrop <= 0) { 538125f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten activeTrack->clearSyncStartEvent(); 53826dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 53836dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } else { 53846dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mFramesToDrop += framesOut; 53856dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 || 53866dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mSyncStartEvent->isCancelled()) { 53876dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ALOGW("Synced record %s, session %d, trigger session %d", 53886dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled", 53896dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->sessionId(), 53906dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten (activeTrack->mSyncStartEvent != 0) ? 53916dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mSyncStartEvent->triggerSession() : 0); 539225f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten activeTrack->clearSyncStartEvent(); 53936dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 539481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 539581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 53966dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 53976dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (framesOut == 0) { 53986dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten break; 53996dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 540081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 54016dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 54026dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten switch (overrun) { 54036dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case OVERRUN_TRUE: 54046dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // client isn't retrieving buffers fast enough 54056dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (!activeTrack->setOverflow()) { 54066dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten nsecs_t now = systemTime(); 54076dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME should lastWarning per track? 54086dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if ((now - lastWarning) > kWarningThrottleNs) { 54096dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ALOGW("RecordThread: buffer overflow"); 54106dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten lastWarning = now; 54116dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 541281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 54136dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten break; 54146dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case OVERRUN_FALSE: 54156dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->clearOverflow(); 54166dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten break; 54176dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case OVERRUN_UNKNOWN: 54186dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten break; 541981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 54206dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 542181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 54221ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten 54233d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kastenunlock: 542481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // enable changes in effect chain 542581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent unlockEffectChains(effectChains); 5426c527a7c2b1bfd26e8f3086e1b653d56e521379d9Glenn Kasten // effectChains doesn't need to be cleared, since it is cleared by destructor at scope end 542781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 542881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 542993e471f620454f7de73d190521568b1e25879767Glenn Kasten standbyIfNotAlreadyInStandby(); 543081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 543181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 543281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 54339a54bc27876acd5d8be5b1fc3dc46701fe76fbb3Eric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 54349a54bc27876acd5d8be5b1fc3dc46701fe76fbb3Eric Laurent sp<RecordTrack> track = mTracks[i]; 54359a54bc27876acd5d8be5b1fc3dc46701fe76fbb3Eric Laurent track->invalidate(); 54369a54bc27876acd5d8be5b1fc3dc46701fe76fbb3Eric Laurent } 54372b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracks.clear(); 54382b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracksGen++; 543981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStartStopCond.broadcast(); 544081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 544181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 544281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock(); 544381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 544481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("RecordThread %p exiting", this); 544581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 544681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 544781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 544893e471f620454f7de73d190521568b1e25879767Glenn Kastenvoid AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby() 544981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 545081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!mStandby) { 545181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent inputStandBy(); 545281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStandby = true; 545381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 545481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 545581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 545681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::inputStandBy() 545781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 54586dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // Idle the fast capture if it's currently running 54596dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (mFastCapture != 0) { 54606dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureStateQueue *sq = mFastCapture->sq(); 54616dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureState *state = sq->begin(); 54626dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (!(state->mCommand & FastCaptureState::IDLE)) { 54636dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mCommand = FastCaptureState::COLD_IDLE; 54646dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mColdFutexAddr = &mFastCaptureFutex; 54656dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mColdGen++; 54666dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastCaptureFutex = 0; 54676dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sq->end(); 54686dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now 54696dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED); 54706dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#if 0 54716dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (kUseFastCapture == FastCapture_Dynamic) { 54726dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // FIXME 54736dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 54746dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif 54756dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef AUDIO_WATCHDOG 54766dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // FIXME 54776dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif 54786dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } else { 54796dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sq->end(false /*didModify*/); 54806dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 54816dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 548281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mInput->stream->common.standby(&mInput->stream->common); 548381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 548481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 548505997e21af6c4517f375def6563af4b9ebe95f39Glenn Kasten// RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held 5486e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kastensp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( 548781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<AudioFlinger::Client>& client, 548881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t sampleRate, 548981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_format_t format, 549081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_channel_mask_t channelMask, 549174935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten size_t *pFrameCount, 549281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId, 54937df8c0b799d8f52d6386e03313286dbd7d5cdc7cGlenn Kasten size_t *notificationFrames, 5494462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen int uid, 5495ddb0ccf3fb6fe8da8c71a6deb30561b821f3c0a2Glenn Kasten IAudioFlinger::track_flags_t *flags, 549681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t tid, 549781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t *status) 549881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 549974935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten size_t frameCount = *pFrameCount; 550081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<RecordTrack> track; 550181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t lStatus; 550281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 550390e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten // client expresses a preference for FAST, but we get the final say 550490e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten if (*flags & IAudioFlinger::TRACK_FAST) { 550590e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten if ( 55067410591dad836434c72ddee66680802708b70c10Glenn Kasten // use case: callback handler 55077410591dad836434c72ddee66680802708b70c10Glenn Kasten (tid != -1) && 55087410591dad836434c72ddee66680802708b70c10Glenn Kasten // frame count is not specified, or is exactly the pipe depth 55097410591dad836434c72ddee66680802708b70c10Glenn Kasten ((frameCount == 0) || (frameCount == mPipeFramesP2)) && 55103a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten // PCM data 55113a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten audio_is_linear_pcm(format) && 55126dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // native format 55136dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (format == mFormat) && 55146dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // native channel mask 55156dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (channelMask == mChannelMask) && 55166dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // native hardware sample rate 551790e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten (sampleRate == mSampleRate) && 55183a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten // record thread has an associated fast capture 55196dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten hasFastCapture() && 55206dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // there are sufficient fast track slots available 55216dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastTrackAvail 552290e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten ) { 55237410591dad836434c72ddee66680802708b70c10Glenn Kasten ALOGV("AUDIO_INPUT_FLAG_FAST accepted: frameCount=%u mFrameCount=%u", 552490e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten frameCount, mFrameCount); 552590e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten } else { 55267410591dad836434c72ddee66680802708b70c10Glenn Kasten ALOGV("AUDIO_INPUT_FLAG_FAST denied: frameCount=%u mFrameCount=%u mPipeFramesP2=%u " 55277410591dad836434c72ddee66680802708b70c10Glenn Kasten "format=%#x isLinear=%d channelMask=%#x sampleRate=%u mSampleRate=%u " 55286dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten "hasFastCapture=%d tid=%d mFastTrackAvail=%d", 55297410591dad836434c72ddee66680802708b70c10Glenn Kasten frameCount, mFrameCount, mPipeFramesP2, 55307410591dad836434c72ddee66680802708b70c10Glenn Kasten format, audio_is_linear_pcm(format), channelMask, sampleRate, mSampleRate, 55317410591dad836434c72ddee66680802708b70c10Glenn Kasten hasFastCapture(), tid, mFastTrackAvail); 553290e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten *flags &= ~IAudioFlinger::TRACK_FAST; 55337410591dad836434c72ddee66680802708b70c10Glenn Kasten } 55347410591dad836434c72ddee66680802708b70c10Glenn Kasten } 55357410591dad836434c72ddee66680802708b70c10Glenn Kasten 55367410591dad836434c72ddee66680802708b70c10Glenn Kasten // compute track buffer size in frames, and suggest the notification frame count 55377410591dad836434c72ddee66680802708b70c10Glenn Kasten if (*flags & IAudioFlinger::TRACK_FAST) { 55387410591dad836434c72ddee66680802708b70c10Glenn Kasten // fast track: frame count is exactly the pipe depth 55397410591dad836434c72ddee66680802708b70c10Glenn Kasten frameCount = mPipeFramesP2; 55407410591dad836434c72ddee66680802708b70c10Glenn Kasten // ignore requested notificationFrames, and always notify exactly once every HAL buffer 55417410591dad836434c72ddee66680802708b70c10Glenn Kasten *notificationFrames = mFrameCount; 55427410591dad836434c72ddee66680802708b70c10Glenn Kasten } else { 55437410591dad836434c72ddee66680802708b70c10Glenn Kasten // not fast track: frame count is at least 2 HAL buffers and at least 20 ms 55447410591dad836434c72ddee66680802708b70c10Glenn Kasten size_t minFrameCount = ((int64_t) mFrameCount * 2 * sampleRate + mSampleRate - 1) / 55457410591dad836434c72ddee66680802708b70c10Glenn Kasten mSampleRate; 55467410591dad836434c72ddee66680802708b70c10Glenn Kasten if (frameCount < minFrameCount) { 55477410591dad836434c72ddee66680802708b70c10Glenn Kasten frameCount = minFrameCount; 554890e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten } 55497410591dad836434c72ddee66680802708b70c10Glenn Kasten minFrameCount = (sampleRate * 20 / 1000 + 1) & ~1; 555090e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten if (frameCount < minFrameCount) { 555190e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten frameCount = minFrameCount; 555290e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten } 55537410591dad836434c72ddee66680802708b70c10Glenn Kasten // notification is forced to be at least double-buffering 55547410591dad836434c72ddee66680802708b70c10Glenn Kasten size_t maxNotification = frameCount / 2; 55557410591dad836434c72ddee66680802708b70c10Glenn Kasten if (*notificationFrames == 0 || *notificationFrames > maxNotification) { 55567410591dad836434c72ddee66680802708b70c10Glenn Kasten *notificationFrames = maxNotification; 55577410591dad836434c72ddee66680802708b70c10Glenn Kasten } 555890e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten } 555974935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten *pFrameCount = frameCount; 556090e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten 556115e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten lStatus = initCheck(); 556215e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten if (lStatus != NO_ERROR) { 556315e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten ALOGE("createRecordTrack_l() audio driver not initialized"); 556415e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten goto Exit; 556515e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten } 556681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 556781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // scope for mLock 556881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 556981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 557081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track = new RecordTrack(this, client, sampleRate, 557183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent format, channelMask, frameCount, NULL, sessionId, uid, 557283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent *flags, TrackBase::TYPE_DEFAULT); 557381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5574030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten lStatus = track->initCheck(); 5575030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten if (lStatus != NO_ERROR) { 557635295078ab59c8c5d143a54d5a55557c3ca62c51Glenn Kasten ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus); 557703e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George // track must be cleared from the caller as the caller has the AF lock 557881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 557981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 558081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mTracks.add(track); 558181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 558281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // disable AEC and NS if the device is a BT SCO headset supporting those pre processings 558381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool suspend = audio_is_bluetooth_sco_device(mInDevice) && 558481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioFlinger->btNrecIsOff(); 558581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); 558681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent setEffectSuspended_l(FX_IID_NS, suspend, sessionId); 558790e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten 558890e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { 558990e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten pid_t callingPid = IPCThreadState::self()->getCallingPid(); 559090e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, 559190e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten // so ask activity manager to do this on our behalf 559290e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); 559390e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten } 559481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 559505997e21af6c4517f375def6563af4b9ebe95f39Glenn Kasten 559681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = NO_ERROR; 559781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 559881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit: 55999156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten *status = lStatus; 560081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return track; 560181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 560281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 560381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, 560481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSystem::sync_event_t event, 560581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int triggerSession) 560681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 560781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); 560881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<ThreadBase> strongMe = this; 560981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status = NO_ERROR; 561081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 561181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (event == AudioSystem::SYNC_EVENT_NONE) { 561225f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten recordTrack->clearSyncStartEvent(); 561381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (event != AudioSystem::SYNC_EVENT_SAME) { 56146dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event, 561581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent triggerSession, 561681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent recordTrack->sessionId(), 561781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent syncStartEventCallback, 56186dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack); 561981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Sync event can be cancelled by the trigger session if the track is not in a 562081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // compatible state in which case we start record immediately 56216dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (recordTrack->mSyncStartEvent->isCancelled()) { 562225f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten recordTrack->clearSyncStartEvent(); 562381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 562481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs 56256dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack->mFramesToDrop = - 56264cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000); 562781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 562881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 562981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 563081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 563147c2070b2ce8aedb7300c0aad91caccf3c383841Glenn Kasten // This section is a rendezvous between binder thread executing start() and RecordThread 563281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AutoMutex lock(mLock); 56336dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (mActiveTracks.indexOf(recordTrack) >= 0) { 56346dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (recordTrack->mState == TrackBase::PAUSING) { 56356dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ALOGV("active record track PAUSING -> ACTIVE"); 5636f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten recordTrack->mState = TrackBase::ACTIVE; 56376dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } else { 56386dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ALOGV("active record track state %d", recordTrack->mState); 563981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 564081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 564181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 564281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 56434cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten // TODO consider other ways of handling this, such as changing the state to :STARTING and 56444cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten // adding the track to mActiveTracks after returning from AudioSystem::startInput(), 56454cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten // or using a separate command thread 56466dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack->mState = TrackBase::STARTING_1; 56472b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracks.add(recordTrack); 56482b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracksGen++; 564983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent status_t status = NO_ERROR; 565083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent if (recordTrack->isExternalTrack()) { 565183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent mLock.unlock(); 56524dc680607181e6a76f4e91a39366c4f5dfb7b03eEric Laurent status = AudioSystem::startInput(mId, (audio_session_t)recordTrack->sessionId()); 565383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent mLock.lock(); 565483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent // FIXME should verify that recordTrack is still in mActiveTracks 565583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent if (status != NO_ERROR) { 565683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent mActiveTracks.remove(recordTrack); 565783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent mActiveTracksGen++; 565883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent recordTrack->clearSyncStartEvent(); 565983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent ALOGV("RecordThread::start error %d", status); 566083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent return status; 566183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent } 566281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 56636dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // Catch up with current buffer indices if thread is already running. 56646dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // This is what makes a new client discard all buffered data. If the track's mRsmpInFront 56656dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // was initialized to some value closer to the thread's mRsmpInFront, then the track could 56666dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // see previously buffered data before it called start(), but with greater risk of overrun. 56676dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 56686dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack->mRsmpInFront = mRsmpInRear; 56696dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack->mRsmpInUnrel = 0; 56706dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME why reset? 56716dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (recordTrack->mResampler != NULL) { 56726dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack->mResampler->reset(); 56736dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 56746dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack->mState = TrackBase::STARTING_2; 567581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // signal thread to start 567681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitWorkCV.broadcast(); 56772b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (mActiveTracks.indexOf(recordTrack) < 0) { 567881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("Record failed to start"); 567981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = BAD_VALUE; 568081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto startError; 568181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 568281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 568381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 56847c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten 568581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentstartError: 568683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent if (recordTrack->isExternalTrack()) { 56874dc680607181e6a76f4e91a39366c4f5dfb7b03eEric Laurent AudioSystem::stopInput(mId, (audio_session_t)recordTrack->sessionId()); 568883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent } 568925f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten recordTrack->clearSyncStartEvent(); 56906dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME I wonder why we do not reset the state here? 569181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 569281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 569381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 569481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) 569581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 569681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<SyncEvent> strongEvent = event.promote(); 569781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 569881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (strongEvent != 0) { 56998ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent sp<RefBase> ptr = strongEvent->cookie().promote(); 57008ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent if (ptr != 0) { 57018ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent RecordTrack *recordTrack = (RecordTrack *)ptr.get(); 57028ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent recordTrack->handleSyncStartEvent(strongEvent); 57038ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent } 570481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 570581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 570681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5707a8356f663014e7d4c27869629af83d8bb3441e19Glenn Kastenbool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { 570881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("RecordThread::stop"); 5709a8356f663014e7d4c27869629af83d8bb3441e19Glenn Kasten AutoMutex _l(mLock); 57102b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (mActiveTracks.indexOf(recordTrack) != 0 || recordTrack->mState == TrackBase::PAUSING) { 571181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 571281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 571347c2070b2ce8aedb7300c0aad91caccf3c383841Glenn Kasten // note that threadLoop may still be processing the track at this point [without lock] 571481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent recordTrack->mState = TrackBase::PAUSING; 571581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // do not wait for mStartStopCond if exiting 571681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (exitPending()) { 571781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return true; 571881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 571947c2070b2ce8aedb7300c0aad91caccf3c383841Glenn Kasten // FIXME incorrect usage of wait: no explicit predicate or loop 572081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStartStopCond.wait(mLock); 57212b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten // if we have been restarted, recordTrack is in mActiveTracks here 57222b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (exitPending() || mActiveTracks.indexOf(recordTrack) != 0) { 572381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("Record stopped OK"); 572481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return true; 572581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 572681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 572781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 572881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 57290f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenbool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const 573081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 573181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 573281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 573381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 57340f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenstatus_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused) 573581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 573681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#if 0 // This branch is currently dead code, but is preserved in case it will be needed in future 573781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!isValidSyncEvent(event)) { 573881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 573981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 574081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 574181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int eventSession = event->triggerSession(); 574281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t ret = NAME_NOT_FOUND; 574381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 574481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 574581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 574681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 574781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<RecordTrack> track = mTracks[i]; 574881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (eventSession == track->sessionId()) { 574981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (void) track->setSyncEvent(event); 575081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ret = NO_ERROR; 575181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 575281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 575381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return ret; 575481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#else 575581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 575681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 575781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 575881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 575981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// destroyTrack_l() must be called with ThreadBase::mLock held 576081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track) 576181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 5762bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->terminate(); 5763bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mState = TrackBase::STOPPED; 576481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // active tracks are removed by threadLoop() 57652b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (mActiveTracks.indexOf(track) < 0) { 576681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent removeTrack_l(track); 576781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 576881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 576981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 577081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track) 577181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 577281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mTracks.remove(track); 577381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // need anything related to effects here? 57746dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (track->isFastTrack()) { 57756dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOG_ASSERT(!mFastTrackAvail); 57766dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastTrackAvail = true; 57776dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 577881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 577981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 578081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) 578181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 578281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpInternals(fd, args); 578381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpTracks(fd, args); 578481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpEffectChains(fd, args); 578581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 578681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 578781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args) 578881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 578987cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, "\nInput thread %p:\n", this); 579081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 57912b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (mActiveTracks.size() > 0) { 579287cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Buffer size: %zu bytes\n", mBufferSize); 579381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 579487cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " No active record clients\n"); 579581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 57966e6704c06d61bc356e30c164081e5bcffb37920cGlenn Kasten dprintf(fd, " Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no"); 57976dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten dprintf(fd, " Fast track available: %s\n", mFastTrackAvail ? "yes" : "no"); 579881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 579981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpBase(fd, args); 580081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 580181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 58020f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args __unused) 580381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 580481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const size_t SIZE = 256; 580581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char buffer[SIZE]; 580681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent String8 result; 580781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5808b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numtracks = mTracks.size(); 5809b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numactive = mActiveTracks.size(); 5810b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numactiveseen = 0; 581187cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " %d Tracks", numtracks); 5812b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (numtracks) { 581387cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " of which %d are active\n", numactive); 5814b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen RecordTrack::appendDumpHeader(result); 5815b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen for (size_t i = 0; i < numtracks ; ++i) { 5816b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen sp<RecordTrack> track = mTracks[i]; 5817b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (track != 0) { 5818b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen bool active = mActiveTracks.indexOf(track) >= 0; 5819b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (active) { 5820b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen numactiveseen++; 5821b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 5822b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen track->dump(buffer, SIZE, active); 5823b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append(buffer); 5824b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 582581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 5826b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } else { 582787cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, "\n"); 582881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 582981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5830b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (numactiveseen != numactive) { 5831b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen snprintf(buffer, SIZE, " The following tracks are in the active list but" 5832b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen " not in the track list\n"); 583381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result.append(buffer); 583481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent RecordTrack::appendDumpHeader(result); 5835b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen for (size_t i = 0; i < numactive; ++i) { 58362b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten sp<RecordTrack> track = mActiveTracks[i]; 5837b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mTracks.indexOf(track) < 0) { 5838b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen track->dump(buffer, SIZE, true); 5839b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append(buffer); 5840b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 58412b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten } 584281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 584381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 584481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent write(fd, result.string(), result.size()); 584581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 584681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 584781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// AudioBufferProvider interface 58486dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kastenstatus_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer( 58496dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten AudioBufferProvider::Buffer* buffer, int64_t pts __unused) 585081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 58516dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten RecordTrack *activeTrack = mRecordTrack; 58526dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten sp<ThreadBase> threadBase = activeTrack->mThread.promote(); 58536dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (threadBase == 0) { 58546dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten buffer->frameCount = 0; 5855607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten buffer->raw = NULL; 58566dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten return NOT_ENOUGH_DATA; 58576dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 58586dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten RecordThread *recordThread = (RecordThread *) threadBase.get(); 58596dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten int32_t rear = recordThread->mRsmpInRear; 58606dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten int32_t front = activeTrack->mRsmpInFront; 58618594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten ssize_t filled = rear - front; 58626dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME should not be P2 (don't want to increase latency) 58636dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME if client not keeping up, discard 5864607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames)); 58658594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten // 'filled' may be non-contiguous, so return only the first contiguous chunk 58666dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten front &= recordThread->mRsmpInFramesP2 - 1; 58676dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size_t part1 = recordThread->mRsmpInFramesP2 - front; 58688594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten if (part1 > (size_t) filled) { 58698594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten part1 = filled; 58708594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten } 58718594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten size_t ask = buffer->frameCount; 58728594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten ALOG_ASSERT(ask > 0); 58738594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten if (part1 > ask) { 58748594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten part1 = ask; 58758594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten } 58768594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten if (part1 == 0) { 58778594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten // Higher-level should keep mRsmpInBuffer full, and not call resampler if empty 5878607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten LOG_ALWAYS_FATAL("RecordThread::getNextBuffer() starved"); 58798594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten buffer->raw = NULL; 58808594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten buffer->frameCount = 0; 58816dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mRsmpInUnrel = 0; 58828594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten return NOT_ENOUGH_DATA; 58838594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten } 58848594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten 58856dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten buffer->raw = recordThread->mRsmpInBuffer + front * recordThread->mChannelCount; 58868594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten buffer->frameCount = part1; 58876dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mRsmpInUnrel = part1; 588881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 588981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 589081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 589181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// AudioBufferProvider interface 58926dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kastenvoid AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer( 58936dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten AudioBufferProvider::Buffer* buffer) 589481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 58956dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten RecordTrack *activeTrack = mRecordTrack; 58968594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten size_t stepCount = buffer->frameCount; 58978594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten if (stepCount == 0) { 58988594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten return; 58998594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten } 59006dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ALOG_ASSERT(stepCount <= activeTrack->mRsmpInUnrel); 59016dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mRsmpInUnrel -= stepCount; 59026dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mRsmpInFront += stepCount; 59038594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten buffer->raw = NULL; 590481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent buffer->frameCount = 0; 590581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 590681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 59071035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentbool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair, 59081035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status_t& status) 590981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 591081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool reconfig = false; 591181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 59121035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = NO_ERROR; 59131035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 59141035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent audio_format_t reqFormat = mFormat; 59151035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent uint32_t samplingRate = mSampleRate; 59161035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount); 59171035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 59181035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent AudioParameter param = AudioParameter(keyValuePair); 59191035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent int value; 59201035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // TODO Investigate when this code runs. Check with audio policy when a sample rate and 59211035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // channel count change can be requested. Do we mandate the first client defines the 59221035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // HAL sampling rate and channel count or do we allow changes on the fly? 59231035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { 59241035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent samplingRate = value; 59251035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 59261035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 59271035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { 59281035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if ((audio_format_t) value != AUDIO_FORMAT_PCM_16_BIT) { 59291035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = BAD_VALUE; 59301035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 59311035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reqFormat = (audio_format_t) value; 593281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent reconfig = true; 593381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 59341035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 59351035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { 59361035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent audio_channel_mask_t mask = (audio_channel_mask_t) value; 59371035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (mask != AUDIO_CHANNEL_IN_MONO && mask != AUDIO_CHANNEL_IN_STEREO) { 59381035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = BAD_VALUE; 59391035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 59401035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent channelMask = mask; 59411035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 594281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 59431035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 59441035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { 59451035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // do not accept frame count changes if tracks are open as the track buffer 59461035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // size depends on frame count and correct behavior would not be guaranteed 59471035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // if frame count is changed after track creation 59481035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (mActiveTracks.size() > 0) { 59491035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = INVALID_OPERATION; 59501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 59511035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 595281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 59531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 59541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { 59551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // forward device change to effects that have requested to be 59561035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // aware of attached audio device. 59571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 59581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mEffectChains[i]->setDevice_l(value); 595981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 596081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 59611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // store input device and output device but do not forward output device to audio HAL. 59621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // Note that status is ignored by the caller for output device 59631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // (see AudioFlinger::setParameters() 59641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (audio_is_output_devices(value)) { 59651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mOutDevice = value; 59661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = BAD_VALUE; 59671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 59681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mInDevice = value; 59691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // disable AEC and NS if the device is a BT SCO headset supporting those 59701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // pre processings 59711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (mTracks.size() > 0) { 59721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent bool suspend = audio_is_bluetooth_sco_device(mInDevice) && 59731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mAudioFlinger->btNrecIsOff(); 59741035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 59751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sp<RecordTrack> track = mTracks[i]; 59761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); 59771035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); 597881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 597981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 598081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 59811035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 59821035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && 59831035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mAudioSource != (audio_source_t)value) { 59841035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // forward device change to effects that have requested to be 59851035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // aware of attached audio device. 59861035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 59871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mEffectChains[i]->setAudioSource_l((audio_source_t)value); 598881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 59891035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mAudioSource = (audio_source_t)value; 59901035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 5991e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten 59921035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == NO_ERROR) { 59931035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = mInput->stream->common.set_parameters(&mInput->stream->common, 59941035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent keyValuePair.string()); 59951035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == INVALID_OPERATION) { 59961035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent inputStandBy(); 599781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = mInput->stream->common.set_parameters(&mInput->stream->common, 599881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent keyValuePair.string()); 59991035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 60001035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (reconfig) { 60011035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == BAD_VALUE && 60021035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reqFormat == mInput->stream->common.get_format(&mInput->stream->common) && 60031035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reqFormat == AUDIO_FORMAT_PCM_16_BIT && 60041035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent (mInput->stream->common.get_sample_rate(&mInput->stream->common) 60051035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent <= (2 * samplingRate)) && 6006e541269be94f3a1072932d51537905b120ef4733Andy Hung audio_channel_count_from_in_mask( 6007e541269be94f3a1072932d51537905b120ef4733Andy Hung mInput->stream->common.get_channels(&mInput->stream->common)) <= FCC_2 && 60081035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent (channelMask == AUDIO_CHANNEL_IN_MONO || 60091035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent channelMask == AUDIO_CHANNEL_IN_STEREO)) { 60101035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = NO_ERROR; 601181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 60121035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == NO_ERROR) { 60131035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent readInputParameters_l(); 60141035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sendIoConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED); 601581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 601681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 601781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 60181035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 601981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return reconfig; 602081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 602181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 602281784c37c61b09289654b979567a42bf73cd2b12Eric LaurentString8 AudioFlinger::RecordThread::getParameters(const String8& keys) 602381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 602481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 602581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (initCheck() != NO_ERROR) { 6026d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten return String8(); 602781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 602881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 6029d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten char *s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); 6030d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten const String8 out_s8(s); 603181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent free(s); 603281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return out_s8; 603381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 603481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 6035021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurentvoid AudioFlinger::RecordThread::audioConfigChanged(int event, int param __unused) { 603681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSystem::OutputDescriptor desc; 6037b2737d0b33c17e408d96d6f9eeaa3381479c94c7Glenn Kasten const void *param2 = NULL; 603881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 603981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent switch (event) { 604081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case AudioSystem::INPUT_OPENED: 604181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case AudioSystem::INPUT_CONFIG_CHANGED: 6042fad226abd12435dbcd232f7de396f1a097b2bd5fGlenn Kasten desc.channelMask = mChannelMask; 604381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc.samplingRate = mSampleRate; 604481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc.format = mFormat; 604581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc.frameCount = mFrameCount; 604681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc.latency = 0; 604781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent param2 = &desc; 604881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 604981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 605081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case AudioSystem::INPUT_CLOSED: 605181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent default: 605281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 605381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 6054021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent mAudioFlinger->audioConfigChanged(event, mId, param2); 605581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 605681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 6057deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kastenvoid AudioFlinger::RecordThread::readInputParameters_l() 605881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 605981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); 606081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); 6061e541269be94f3a1072932d51537905b120ef4733Andy Hung mChannelCount = audio_channel_count_from_in_mask(mChannelMask); 6062463be250de73907965faa6a216c00312bf81e049Andy Hung mHALFormat = mInput->stream->common.get_format(&mInput->stream->common); 6063463be250de73907965faa6a216c00312bf81e049Andy Hung mFormat = mHALFormat; 6064291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten if (mFormat != AUDIO_FORMAT_PCM_16_BIT) { 6065cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten ALOGE("HAL format %#x not supported; must be AUDIO_FORMAT_PCM_16_BIT", mFormat); 6066291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten } 6067665470b36f202bcc8ee2f7417f68fd2608dd07c1Eric Laurent mFrameSize = audio_stream_in_frame_size(mInput->stream); 6068548efc94813c1dec6e8cf6c085ae41ccb04827f1Glenn Kasten mBufferSize = mInput->stream->common.get_buffer_size(&mInput->stream->common); 6069548efc94813c1dec6e8cf6c085ae41ccb04827f1Glenn Kasten mFrameCount = mBufferSize / mFrameSize; 60706dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // This is the formula for calculating the temporary buffer size. 6071e842614837e5401adf77e90485300c288b9a7876Glenn Kasten // With 7 HAL buffers, we can guarantee ability to down-sample the input by ratio of 6:1 to 60728594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten // 1 full output buffer, regardless of the alignment of the available input. 6073e842614837e5401adf77e90485300c288b9a7876Glenn Kasten // The value is somewhat arbitrary, and could probably be even larger. 60746dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // A larger value should allow more old data to be read after a track calls start(), 60756dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // without increasing latency. 6076e842614837e5401adf77e90485300c288b9a7876Glenn Kasten mRsmpInFrames = mFrameCount * 7; 60778594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten mRsmpInFramesP2 = roundup(mRsmpInFrames); 60786dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten delete[] mRsmpInBuffer; 60798594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten // Over-allocate beyond mRsmpInFramesP2 to permit a HAL read past end of buffer 60808594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten mRsmpInBuffer = new int16_t[(mRsmpInFramesP2 + mFrameCount - 1) * mChannelCount]; 60816dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 60824cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints. 60834cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten // But if thread's mSampleRate or mChannelCount changes, how will that affect active tracks? 608481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 608581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 60865f972c031d4061f4f037c9fda1ea4bd9b6a756cdGlenn Kastenuint32_t AudioFlinger::RecordThread::getInputFramesLost() 608781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 608881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 608981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (initCheck() != NO_ERROR) { 609081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 609181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 609281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 609381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mInput->stream->get_input_frames_lost(mInput->stream); 609481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 609581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 609681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) const 609781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 609881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 609981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t result = 0; 610081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (getEffectChain_l(sessionId) != 0) { 610181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result = EFFECT_SESSION; 610281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 610381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 610481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 610581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId == mTracks[i]->sessionId()) { 610681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result |= TRACK_SESSION; 610781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 610881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 610981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 611081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 611181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return result; 611281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 611381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 611481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentKeyedVector<int, bool> AudioFlinger::RecordThread::sessionIds() const 611581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 611681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent KeyedVector<int, bool> ids; 611781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 611881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t j = 0; j < mTracks.size(); ++j) { 611981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<RecordThread::RecordTrack> track = mTracks[j]; 612081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId = track->sessionId(); 612181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (ids.indexOfKey(sessionId) < 0) { 612281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ids.add(sessionId, true); 612381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 612481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 612581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return ids; 612681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 612781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 612881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() 612981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 613081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 613181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamIn *input = mInput; 613281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mInput = NULL; 613381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return input; 613481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 613581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 613681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// this method must always be called either with ThreadBase mLock held or inside the thread loop 613781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_stream_t* AudioFlinger::RecordThread::stream() const 613881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 613981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mInput == NULL) { 614081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NULL; 614181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 614281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return &mInput->stream->common; 614381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 614481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 614581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) 614681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 614781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // only one chain per input thread 614881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mEffectChains.size() != 0) { 614981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return INVALID_OPERATION; 615081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 615181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); 615281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 615381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setInBuffer(NULL); 615481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setOutBuffer(NULL); 615581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 615681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent checkSuspendOnAddEffectChain_l(chain); 615781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 615881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains.add(chain); 615981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 616081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 616181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 616281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 616381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsize_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) 616481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 616581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); 616681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW_IF(mEffectChains.size() != 1, 616781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "removeEffectChain_l() %p invalid chain size %d on thread %p", 616881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain.get(), mEffectChains.size(), this); 616981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mEffectChains.size() == 1) { 617081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains.removeAt(0); 617181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 617281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 617381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 617481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 61751c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch, 61761c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_patch_handle_t *handle) 61771c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{ 61781c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status_t status = NO_ERROR; 61791c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { 61801c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent // store new device and send to effects 61811c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent mInDevice = patch->sources[0].ext.device.type; 61821c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 61831c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent mEffectChains[i]->setDevice_l(mInDevice); 61841c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 61851c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 61861c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent // disable AEC and NS if the device is a BT SCO headset supporting those 61871c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent // pre processings 61881c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent if (mTracks.size() > 0) { 61891c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent bool suspend = audio_is_bluetooth_sco_device(mInDevice) && 61901c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent mAudioFlinger->btNrecIsOff(); 61911c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 61921c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent sp<RecordTrack> track = mTracks[i]; 61931c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); 61941c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); 61951c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 61961c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 61971c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 61981c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent // store new source and send to effects 61991c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { 62001c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent mAudioSource = patch->sinks[0].ext.mix.usecase.source; 62011c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 62021c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent mEffectChains[i]->setAudioSource_l(mAudioSource); 62031c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 62041c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 62051c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 62061c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); 62071c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status = hwDevice->create_audio_patch(hwDevice, 62081c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent patch->num_sources, 62091c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent patch->sources, 62101c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent patch->num_sinks, 62111c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent patch->sinks, 62121c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent handle); 62131c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } else { 62141c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent ALOG_ASSERT(false, "createAudioPatch_l() called on a pre 3.0 HAL"); 62151c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 62161c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent return status; 62171c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent} 62181c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 62191c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle) 62201c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{ 62211c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status_t status = NO_ERROR; 62221c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { 62231c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); 62241c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status = hwDevice->release_audio_patch(hwDevice, handle); 62251c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } else { 62261c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent ALOG_ASSERT(false, "releaseAudioPatch_l() called on a pre 3.0 HAL"); 62271c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 62281c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent return status; 62291c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent} 62301c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 623183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::RecordThread::addPatchRecord(const sp<PatchRecord>& record) 623283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{ 623383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent Mutex::Autolock _l(mLock); 623483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent mTracks.add(record); 623583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent} 623683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 623783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::RecordThread::deletePatchRecord(const sp<PatchRecord>& record) 623883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{ 623983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent Mutex::Autolock _l(mLock); 624083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent destroyTrack_l(record); 624183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent} 624283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 624383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::RecordThread::getAudioPortConfig(struct audio_port_config *config) 624483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{ 624583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent ThreadBase::getAudioPortConfig(config); 624683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->role = AUDIO_PORT_ROLE_SINK; 624783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->ext.mix.hw_module = mInput->audioHwDev->handle(); 624883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->ext.mix.usecase.source = mAudioSource; 624983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent} 62501c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 625181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}; // namespace android 6252