Threads.cpp revision c56f3426099a3cf2d07ccff8886050c7fbce140f
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 4181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/AudioStreamOutSink.h> 4281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/MonoPipe.h> 4381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/MonoPipeReader.h> 4481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/Pipe.h> 4581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/PipeReader.h> 4681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/SourceAudioBufferProvider.h> 4781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <powermanager/PowerManager.h> 4981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <common_time/cc_helper.h> 5181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <common_time/local_clock.h> 5281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "AudioFlinger.h" 5481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "AudioMixer.h" 5581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "FastMixer.h" 5681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "ServiceUtilities.h" 5781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "SchedulingPolicyService.h" 5881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef ADD_BATTERY_DATA 6081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/IMediaPlayerService.h> 6181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/IMediaDeathNotifier.h> 6281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 6381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 6481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef DEBUG_CPU_USAGE 6581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <cpustats/CentralTendencyStatistics.h> 6681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <cpustats/ThreadCpuUsage.h> 6781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 6881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 6981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 7081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 7181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Note: the following macro is used for extremely verbose logging message. In 7281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to 7381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// 0; but one side effect of this is to turn all LOGV's as well. Some messages 7481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// are so verbose that we want to suppress them even when we have ALOG_ASSERT 7581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// turned on. Do not uncomment the #def below unless you really know what you 7681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// are doing and want to see all of the extremely verbose messages. 7781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent//#define VERY_VERY_VERBOSE_LOGGING 7881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef VERY_VERY_VERBOSE_LOGGING 7981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#define ALOGVV ALOGV 8081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#else 8181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#define ALOGVV(a...) do { } while(0) 8281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 8381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 8481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentnamespace android { 8581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 8681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// retry counts for buffer fill timeout 8781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// 50 * ~20msecs = 1 second 8881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int8_t kMaxTrackRetries = 50; 8981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int8_t kMaxTrackStartupRetries = 50; 9081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// allow less retry attempts on direct output thread. 9181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// direct outputs can be a scarce resource in audio hardware and should 9281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// be released as quickly as possible. 9381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int8_t kMaxTrackRetriesDirect = 2; 9481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 9581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// don't warn about blocked writes or record buffer overflows more often than this 9681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const nsecs_t kWarningThrottleNs = seconds(5); 9781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 9881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// RecordThread loop sleep time upon application overrun or audio HAL read error 9981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int kRecordThreadSleepUs = 5000; 10081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1011035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// maximum time to wait in sendConfigEvent_l() for a status to be received 1021035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentstatic const nsecs_t kConfigEventTimeoutNs = seconds(2); 10381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 10481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// minimum sleep time for the mixer thread loop when tracks are active but in underrun 10581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const uint32_t kMinThreadSleepTimeUs = 5000; 10681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// maximum divider applied to the active sleep time in the mixer thread loop 10781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const uint32_t kMaxThreadSleepTimeShift = 2; 10881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 10909a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung// minimum normal sink buffer size, expressed in milliseconds rather than frames 11009a5007b17acb49d25cfa386a2e2534d942e8854Andy Hungstatic const uint32_t kMinNormalSinkBufferSizeMs = 20; 11109a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung// maximum normal sink buffer size 11209a5007b17acb49d25cfa386a2e2534d942e8854Andy Hungstatic const uint32_t kMaxNormalSinkBufferSizeMs = 24; 11381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 114972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent// Offloaded output thread standby delay: allows track transition without going to standby 115972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurentstatic const nsecs_t kOffloadStandbyDelayNs = seconds(1); 116972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent 11781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Whether to use fast mixer 11881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const enum { 11981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixer_Never, // never initialize or use: for debugging only 12081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixer_Always, // always initialize and use, even if not needed: for debugging only 12181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // normal mixer multiplier is 1 12281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixer_Static, // initialize if needed, then use all the time if initialized, 12381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // multiplier is calculated based on min & max normal mixer buffer size 12481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load, 12581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // multiplier is calculated based on min & max normal mixer buffer size 12681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME for FastMixer_Dynamic: 12781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Supporting this option will require fixing HALs that can't handle large writes. 12881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // For example, one HAL implementation returns an error from a large write, 12981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // and another HAL implementation corrupts memory, possibly in the sample rate converter. 13081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // We could either fix the HAL implementations, or provide a wrapper that breaks 13181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // up large writes into smaller ones, and the wrapper would need to deal with scheduler. 13281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} kUseFastMixer = FastMixer_Static; 13381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 13481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Priorities for requestPriority 13581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int kPriorityAudioApp = 2; 13681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int kPriorityFastMixer = 3; 13781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 13881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// IAudioFlinger::createTrack() reports back to client the total size of shared memory area 13981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// for the track. The client then sub-divides this into smaller buffers for its use. 140b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten// Currently the client uses N-buffering by default, but doesn't tell us about the value of N. 141b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten// So for now we just assume that client is double-buffered for fast tracks. 142b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten// FIXME It would be better for client to tell AudioFlinger the value of N, 143b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten// so AudioFlinger could allocate the right amount of memory. 14481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// See the client's minBufCount and mNotificationFramesAct calculations for details. 145b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kastenstatic const int kFastTrackMultiplier = 2; 14681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 147b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten// See Thread::readOnlyHeap(). 148b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten// Initially this heap is used to allocate client buffers for "fast" AudioRecord. 149b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten// Eventually it will be the single buffer that FastCapture writes into via HAL read(), 150b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten// and that all "fast" AudioRecord clients read from. In either case, the size can be small. 151b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kastenstatic const size_t kRecordThreadReadOnlyHeapSize = 0x1000; 152b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten 15381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 15481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 15581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef ADD_BATTERY_DATA 15681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// To collect the amplifier usage 15781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic void addBatteryData(uint32_t params) { 15881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); 15981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (service == NULL) { 16081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it already logged 16181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 16281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 16381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 16481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent service->addBatteryData(params); 16581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 16681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 16781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 16881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 16981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 17081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// CPU Stats 17181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 17281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 17381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass CpuStats { 17481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic: 17581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent CpuStats(); 17681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void sample(const String8 &title); 17781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef DEBUG_CPU_USAGE 17881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate: 17981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns 18081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns 18181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 18281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles 18381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 18481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int mCpuNum; // thread's current CPU number 18581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int mCpukHz; // frequency of thread's current CPU in kHz 18681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 18781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}; 18881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 18981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentCpuStats::CpuStats() 19081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef DEBUG_CPU_USAGE 19181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent : mCpuNum(-1), mCpukHz(-1) 19281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 19381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 19481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 19581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1960f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid CpuStats::sample(const String8 &title 1970f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten#ifndef DEBUG_CPU_USAGE 1980f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten __unused 1990f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten#endif 2000f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten ) { 20181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef DEBUG_CPU_USAGE 20281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // get current thread's delta CPU time in wall clock ns 20381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double wcNs; 20481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool valid = mCpuUsage.sampleAndEnable(wcNs); 20581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 20681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // record sample for wall clock statistics 20781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (valid) { 20881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWcStats.sample(wcNs); 20981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 21081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 21181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // get the current CPU number 21281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int cpuNum = sched_getcpu(); 21381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 21481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // get the current CPU frequency in kHz 21581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int cpukHz = mCpuUsage.getCpukHz(cpuNum); 21681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 21781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // check if either CPU number or frequency changed 21881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (cpuNum != mCpuNum || cpukHz != mCpukHz) { 21981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mCpuNum = cpuNum; 22081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mCpukHz = cpukHz; 22181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // ignore sample for purposes of cycles 22281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent valid = false; 22381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 22481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 22581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if no change in CPU number or frequency, then record sample for cycle statistics 22681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (valid && mCpukHz > 0) { 22781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double cycles = wcNs * cpukHz * 0.000001; 22881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHzStats.sample(cycles); 22981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 23081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 23181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent unsigned n = mWcStats.n(); 23281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mCpuUsage.elapsed() is expensive, so don't call it every loop 23381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((n & 127) == 1) { 23481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent long long elapsed = mCpuUsage.elapsed(); 23581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { 23681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double perLoop = elapsed / (double) n; 23781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double perLoop100 = perLoop * 0.01; 23881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double perLoop1k = perLoop * 0.001; 23981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double mean = mWcStats.mean(); 24081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double stddev = mWcStats.stddev(); 24181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double minimum = mWcStats.minimum(); 24281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double maximum = mWcStats.maximum(); 24381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double meanCycles = mHzStats.mean(); 24481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double stddevCycles = mHzStats.stddev(); 24581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double minCycles = mHzStats.minimum(); 24681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double maxCycles = mHzStats.maximum(); 24781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mCpuUsage.resetElapsed(); 24881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWcStats.reset(); 24981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHzStats.reset(); 25081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGD("CPU usage for %s over past %.1f secs\n" 25181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent " (%u mixer loops at %.1f mean ms per loop):\n" 25281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" 25381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" 25481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", 25581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent title.string(), 25681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent elapsed * .000000001, n, perLoop * .000001, 25781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mean * .001, 25881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent stddev * .001, 25981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minimum * .001, 26081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent maximum * .001, 26181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mean / perLoop100, 26281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent stddev / perLoop100, 26381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minimum / perLoop100, 26481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent maximum / perLoop100, 26581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent meanCycles / perLoop1k, 26681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent stddevCycles / perLoop1k, 26781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minCycles / perLoop1k, 26881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent maxCycles / perLoop1k); 26981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 27081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 27181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 27281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 27381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}; 27481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 27581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 27681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ThreadBase 27781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 27881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 27981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, 28081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_devices_t outDevice, audio_devices_t inDevice, type_t type) 28181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent : Thread(false /*canCallJava*/), 28281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mType(type), 2839b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten mAudioFlinger(audioFlinger), 28470949c47fbae3f836d15f040551d7631be3ed7c2Glenn Kasten // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize 285deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten // are set by PlaybackThread::readOutputParameters_l() or 286deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten // RecordThread::readInputParameters_l() 287fd4779740ec3e9e865d5514464df26d015354388Eric Laurent //FIXME: mStandby should be true here. Is this some kind of hack? 28881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStandby(false), mOutDevice(outDevice), mInDevice(inDevice), 28981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id), 29081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mName will be set by concrete (non-virtual) subclass 29181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mDeathRecipient(new PMDeathRecipient(this)) 29281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 29381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 29481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 29581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::ThreadBase::~ThreadBase() 29681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 297c6ae3c8a261794fd4445e4e152d1ada074a3f92fGlenn Kasten // mConfigEvents should be empty, but just in case it isn't, free the memory it owns 298c6ae3c8a261794fd4445e4e152d1ada074a3f92fGlenn Kasten mConfigEvents.clear(); 299c6ae3c8a261794fd4445e4e152d1ada074a3f92fGlenn Kasten 30081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // do not lock the mutex in destructor 30181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock_l(); 30281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mPowerManager != 0) { 30381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<IBinder> binder = mPowerManager->asBinder(); 30481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent binder->unlinkToDeath(mDeathRecipient); 30581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 30681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 30781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 308cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kastenstatus_t AudioFlinger::ThreadBase::readyToRun() 309cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten{ 310cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten status_t status = initCheck(); 311cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten if (status == NO_ERROR) { 312cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten ALOGI("AudioFlinger's thread %p ready to run", this); 313cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten } else { 314cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten ALOGE("No working audio driver found."); 315cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten } 316cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten return status; 317cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten} 318cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten 31981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::exit() 32081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 32181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("ThreadBase::exit"); 32281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // do any cleanup required for exit to succeed 32381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent preExit(); 32481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 32581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // This lock prevents the following race in thread (uniprocessor for illustration): 32681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if (!exitPending()) { 32781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // // context switch from here to exit() 32881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // // exit() calls requestExit(), what exitPending() observes 32981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // // exit() calls signal(), which is dropped since no waiters 33081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // // context switch back from exit() to here 33181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mWaitWorkCV.wait(...); 33281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // // now thread is hung 33381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // } 33481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AutoMutex lock(mLock); 33581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent requestExit(); 33681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitWorkCV.broadcast(); 33781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 33881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // When Thread::requestExitAndWait is made virtual and this method is renamed to 33981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" 34081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent requestExitAndWait(); 34181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 34281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 34381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) 34481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 34581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status; 34681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 34781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); 34881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 34981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent return sendSetParameterConfigEvent_l(keyValuePairs); 3511035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent} 3521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 3531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// sendConfigEvent_l() must be called with ThreadBase::mLock held 3541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// Can temporarily release the lock if waiting for a reply from processConfigEvents_l(). 3551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentstatus_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event) 3561035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent{ 3571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status_t status = NO_ERROR; 3581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 3591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mConfigEvents.add(event); 3601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent ALOGV("sendConfigEvent_l() num events %d event %d", mConfigEvents.size(), event->mType); 36181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitWorkCV.signal(); 3621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mLock.unlock(); 3631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent { 3641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent Mutex::Autolock _l(event->mLock); 3651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent while (event->mWaitStatus) { 3661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) { 3671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent event->mStatus = TIMED_OUT; 3681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent event->mWaitStatus = false; 3691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 3701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 3711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = event->mStatus; 37281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 3731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mLock.lock(); 37481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 37581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 37681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 37781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::sendIoConfigEvent(int event, int param) 37881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 37981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 38081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sendIoConfigEvent_l(event, param); 38181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 38281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 38381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// sendIoConfigEvent_l() must be called with ThreadBase::mLock held 38481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::sendIoConfigEvent_l(int event, int param) 38581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 3861035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, param); 3871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sendConfigEvent_l(configEvent); 38881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 38981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 39081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// sendPrioConfigEvent_l() must be called with ThreadBase::mLock held 39181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio) 39281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 3931035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio); 3941035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sendConfigEvent_l(configEvent); 39581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 39681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3971035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held 3981035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentstatus_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair) 39981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 4001035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sp<ConfigEvent> configEvent = (ConfigEvent *)new SetParameterConfigEvent(keyValuePair); 4011035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent return sendConfigEvent_l(configEvent); 402f777331418a86cd9fd709af898ef24a69967aeb4Glenn Kasten} 403f777331418a86cd9fd709af898ef24a69967aeb4Glenn Kasten 4042cfbf88b89854f30b295e8ae26a031edb8d712f8Glenn Kasten// post condition: mConfigEvents.isEmpty() 405021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurentvoid AudioFlinger::ThreadBase::processConfigEvents_l() 406f777331418a86cd9fd709af898ef24a69967aeb4Glenn Kasten{ 4071035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent bool configChanged = false; 4081035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 40981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent while (!mConfigEvents.isEmpty()) { 4101035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent ALOGV("processConfigEvents_l() remaining events %d", mConfigEvents.size()); 4111035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sp<ConfigEvent> event = mConfigEvents[0]; 41281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mConfigEvents.removeAt(0); 4131035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent switch (event->mType) { 4143468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten case CFG_EVENT_PRIO: { 4151035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get(); 4161035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // FIXME Need to understand why this has to be done asynchronously 4171035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent int err = requestPriority(data->mPid, data->mTid, data->mPrio, 4183468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten true /*asynchronous*/); 4193468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten if (err != 0) { 4203468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", 4211035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent data->mPrio, data->mPid, data->mTid, err); 4223468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten } 4233468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten } break; 4243468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten case CFG_EVENT_IO: { 4251035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent IoConfigEventData *data = (IoConfigEventData *)event->mData.get(); 426021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent audioConfigChanged(data->mEvent, data->mParam); 4271035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } break; 4281035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent case CFG_EVENT_SET_PARAMETER: { 4291035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get(); 4301035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) { 4311035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent configChanged = true; 432d5418eb594435c958d6c37fa9938161a0112adbdGlenn Kasten } 4333468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten } break; 4343468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten default: 4351035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType); 4363468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten break; 43781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 4381035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent { 4391035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent Mutex::Autolock _l(event->mLock); 4401035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (event->mWaitStatus) { 4411035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent event->mWaitStatus = false; 4421035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent event->mCond.signal(); 4431035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 4441035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 4451035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this); 4461035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 4471035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 4481035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (configChanged) { 4491035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent cacheParameters_l(); 45081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 45181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 45281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 453b220884bf3129253cc5bc8d030bc475411ea4911Marco NelissenString8 channelMaskToString(audio_channel_mask_t mask, bool output) { 454b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen String8 s; 455b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (output) { 456b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, "); 457b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, "); 458b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, "); 459b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, "); 460b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, "); 461b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, "); 462b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, "); 463b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, "); 464b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, "); 465b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, "); 466b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, "); 467b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,"); 468b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, "); 469b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, "); 470b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, "); 471b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, "); 472b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " ); 473b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " ); 474b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown, "); 475b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } else { 476b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, "); 477b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, "); 478b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, "); 479b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, "); 480b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, "); 481b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, "); 482b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, "); 483b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, "); 484b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, "); 485b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, "); 486b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, "); 487b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, "); 488b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, "); 489b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, "); 490b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown, "); 491b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 492b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen int len = s.length(); 493b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (s.length() > 2) { 494b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen char *str = s.lockBuffer(len); 495b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen s.unlockBuffer(len - 2); 496b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 497b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen return s; 498b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen} 499b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen 5000f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args __unused) 50181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 50281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const size_t SIZE = 256; 50381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char buffer[SIZE]; 50481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent String8 result; 50581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 50681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool locked = AudioFlinger::dumpTryLock(mLock); 50781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!locked) { 508b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, "thread %p maybe dead locked\n", this); 509b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 510b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen 511b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, " I/O handle: %d\n", mId); 512b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, " TID: %d\n", getTid()); 513b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, " Standby: %s\n", mStandby ? "yes" : "no"); 514b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, " Sample rate: %u\n", mSampleRate); 5151d6fa7af1288b550faabe4ec2cf98684236723dbNarayan Kamath fdprintf(fd, " HAL frame count: %zu\n", mFrameCount); 516b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, " HAL buffer size: %u bytes\n", mBufferSize); 517b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, " Channel Count: %u\n", mChannelCount); 518b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, " Channel Mask: 0x%08x (%s)\n", mChannelMask, 519b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen channelMaskToString(mChannelMask, mType != RECORD).string()); 520b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, " Format: 0x%x (%s)\n", mFormat, formatToString(mFormat)); 5211d6fa7af1288b550faabe4ec2cf98684236723dbNarayan Kamath fdprintf(fd, " Frame size: %zu\n", mFrameSize); 522b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, " Pending config events:"); 523b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numConfig = mConfigEvents.size(); 524b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (numConfig) { 525b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen for (size_t i = 0; i < numConfig; i++) { 526b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen mConfigEvents[i]->dump(buffer, SIZE); 527b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, "\n %s", buffer); 528b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 529b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, "\n"); 530b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } else { 531b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, " none\n"); 53281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 53381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 53481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (locked) { 53581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mLock.unlock(); 53681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 53781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 53881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 53981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) 54081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 54181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const size_t SIZE = 256; 54281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char buffer[SIZE]; 54381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent String8 result; 54481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 545b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numEffectChains = mEffectChains.size(); 5461d6fa7af1288b550faabe4ec2cf98684236723dbNarayan Kamath snprintf(buffer, SIZE, " %zu Effect Chains\n", numEffectChains); 54781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent write(fd, buffer, strlen(buffer)); 54881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 549b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen for (size_t i = 0; i < numEffectChains; ++i) { 55081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = mEffectChains[i]; 55181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 55281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->dump(fd, args); 55381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 55481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 55581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 55681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 557e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissenvoid AudioFlinger::ThreadBase::acquireWakeLock(int uid) 55881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 55981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 560e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen acquireWakeLock_l(uid); 56181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 56281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 563014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan KamathString16 AudioFlinger::ThreadBase::getWakeLockTag() 564014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath{ 565014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath switch (mType) { 566014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath case MIXER: 567014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath return String16("AudioMix"); 568014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath case DIRECT: 569014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath return String16("AudioDirectOut"); 570014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath case DUPLICATING: 571014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath return String16("AudioDup"); 572014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath case RECORD: 573014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath return String16("AudioIn"); 574014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath case OFFLOAD: 575014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath return String16("AudioOffload"); 576014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath default: 577014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath ALOG_ASSERT(false); 578014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath return String16("AudioUnknown"); 579014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath } 580014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath} 581014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath 582e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissenvoid AudioFlinger::ThreadBase::acquireWakeLock_l(int uid) 58381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 584462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen getPowerManager_l(); 58581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mPowerManager != 0) { 58681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<IBinder> binder = new BBinder(); 587e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen status_t status; 588e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen if (uid >= 0) { 589547789d25dc6bd6561553bcf6b384fb0d4fee834Eric Laurent status = mPowerManager->acquireWakeLockWithUid(POWERMANAGER_PARTIAL_WAKE_LOCK, 590e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen binder, 591014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath getWakeLockTag(), 592e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen String16("media"), 593e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen uid); 594e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen } else { 595547789d25dc6bd6561553bcf6b384fb0d4fee834Eric Laurent status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, 596e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen binder, 597014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath getWakeLockTag(), 598e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen String16("media")); 599e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen } 60081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (status == NO_ERROR) { 60181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWakeLockToken = binder; 60281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 60381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("acquireWakeLock_l() %s status %d", mName, status); 60481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 60581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 60681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 60781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::releaseWakeLock() 60881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 60981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 61081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock_l(); 61181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 61281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 61381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::releaseWakeLock_l() 61481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 61581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mWakeLockToken != 0) { 61681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("releaseWakeLock_l() %s", mName); 61781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mPowerManager != 0) { 61881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPowerManager->releaseWakeLock(mWakeLockToken, 0); 61981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 62081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWakeLockToken.clear(); 62181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 62281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 62381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 624462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissenvoid AudioFlinger::ThreadBase::updateWakeLockUids(const SortedVector<int> &uids) { 625462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen Mutex::Autolock _l(mLock); 626462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen updateWakeLockUids_l(uids); 627462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen} 628462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 629462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissenvoid AudioFlinger::ThreadBase::getPowerManager_l() { 630462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 631462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen if (mPowerManager == 0) { 632462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen // use checkService() to avoid blocking if power service is not up yet 633462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen sp<IBinder> binder = 634462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen defaultServiceManager()->checkService(String16("power")); 635462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen if (binder == 0) { 636462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen ALOGW("Thread %s cannot connect to the power manager service", mName); 637462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } else { 638462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mPowerManager = interface_cast<IPowerManager>(binder); 639462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen binder->linkToDeath(mDeathRecipient); 640462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } 641462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } 642462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen} 643462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 644462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissenvoid AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<int> &uids) { 645462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 646462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen getPowerManager_l(); 647462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen if (mWakeLockToken == NULL) { 648462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen ALOGE("no wake lock to update!"); 649462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen return; 650462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } 651462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen if (mPowerManager != 0) { 652462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen sp<IBinder> binder = new BBinder(); 653462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen status_t status; 654462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen status = mPowerManager->updateWakeLockUids(mWakeLockToken, uids.size(), uids.array()); 655462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen ALOGV("acquireWakeLock_l() %s status %d", mName, status); 656462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } 657462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen} 658462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 65981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::clearPowerManager() 66081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 66181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 66281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock_l(); 66381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPowerManager.clear(); 66481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 66581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 6660f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) 66781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 66881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<ThreadBase> thread = mThread.promote(); 66981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread != 0) { 67081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread->clearPowerManager(); 67181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 67281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("power manager service died !!!"); 67381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 67481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 67581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::setEffectSuspended( 67681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const effect_uuid_t *type, bool suspend, int sessionId) 67781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 67881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 67981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent setEffectSuspended_l(type, suspend, sessionId); 68081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 68181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 68281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::setEffectSuspended_l( 68381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const effect_uuid_t *type, bool suspend, int sessionId) 68481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 68581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(sessionId); 68681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 68781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (type != NULL) { 68881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setEffectSuspended_l(type, suspend); 68981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 69081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setEffectSuspendedAll_l(suspend); 69181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 69281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 69381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 69481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent updateSuspendedSessions_l(type, suspend, sessionId); 69581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 69681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 69781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) 69881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 69981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); 70081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (index < 0) { 70181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 70281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 70381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 70481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = 70581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSuspendedSessions.valueAt(index); 70681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 70781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < sessionEffects.size(); i++) { 70881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<SuspendedSessionDesc> desc = sessionEffects.valueAt(i); 70981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (int j = 0; j < desc->mRefCount; j++) { 71081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { 71181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setEffectSuspendedAll_l(true); 71281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 71381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", 71481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc->mType.timeLow); 71581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setEffectSuspended_l(&desc->mType, true); 71681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 71781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 71881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 71981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 72081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 72181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, 72281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool suspend, 72381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId) 72481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 72581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ssize_t index = mSuspendedSessions.indexOfKey(sessionId); 72681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 72781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; 72881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 72981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (suspend) { 73081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (index >= 0) { 73181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionEffects = mSuspendedSessions.valueAt(index); 73281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 73381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSuspendedSessions.add(sessionId, sessionEffects); 73481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 73581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 73681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (index < 0) { 73781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 73881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 73981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionEffects = mSuspendedSessions.valueAt(index); 74081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 74181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 74281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 74381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int key = EffectChain::kKeyForSuspendAll; 74481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (type != NULL) { 74581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent key = type->timeLow; 74681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 74781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent index = sessionEffects.indexOfKey(key); 74881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 74981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<SuspendedSessionDesc> desc; 75081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (suspend) { 75181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (index >= 0) { 75281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc = sessionEffects.valueAt(index); 75381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 75481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc = new SuspendedSessionDesc(); 75581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (type != NULL) { 75681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc->mType = *type; 75781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 75881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionEffects.add(key, desc); 75981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); 76081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 76181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc->mRefCount++; 76281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 76381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (index < 0) { 76481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 76581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 76681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc = sessionEffects.valueAt(index); 76781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (--desc->mRefCount == 0) { 76881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); 76981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionEffects.removeItemsAt(index); 77081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionEffects.isEmpty()) { 77181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("updateSuspendedSessions_l() restore removing session %d", 77281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionId); 77381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSuspendedSessions.removeItem(sessionId); 77481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 77581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 77681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 77781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!sessionEffects.isEmpty()) { 77881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); 77981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 78081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 78181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 78281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, 78381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool enabled, 78481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId) 78581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 78681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 78781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); 78881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 78981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 79081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, 79181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool enabled, 79281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId) 79381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 79481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mType != RECORD) { 79581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on 79681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // another session. This gives the priority to well behaved effect control panels 79781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // and applications not using global effects. 79881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect 79981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // global effects 80081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) { 80181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); 80281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 80381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 80481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 80581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(sessionId); 80681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 80781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->checkSuspendOnEffectEnabled(effect, enabled); 80881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 80981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 81081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 81181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held 81281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( 81381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<AudioFlinger::Client>& client, 81481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<IEffectClient>& effectClient, 81581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int32_t priority, 81681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId, 81781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect_descriptor_t *desc, 81881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int *enabled, 8199156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten status_t *status) 82081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 82181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectModule> effect; 82281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectHandle> handle; 82381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t lStatus; 82481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain; 82581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool chainCreated = false; 82681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool effectCreated = false; 82781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool effectRegistered = false; 82881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 82981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = initCheck(); 83081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR) { 83181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("createEffect_l() Audio driver not initialized."); 83281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 83381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 83481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 83598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Reject any effect on Direct output threads for now, since the format of 83698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo). 83798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mType == DIRECT) { 83898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung ALOGW("createEffect_l() Cannot add effect %s on Direct output type thread %s", 83998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung desc->name, mName); 84098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung lStatus = BAD_VALUE; 84198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung goto Exit; 84298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 84398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung 8445baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent // Allow global effects only on offloaded and mixer threads 8455baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { 8465baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent switch (mType) { 8475baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent case MIXER: 8485baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent case OFFLOAD: 8495baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent break; 8505baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent case DIRECT: 8515baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent case DUPLICATING: 8525baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent case RECORD: 8535baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent default: 8545baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent ALOGW("createEffect_l() Cannot add global effect %s on thread %s", desc->name, mName); 8555baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent lStatus = BAD_VALUE; 8565baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent goto Exit; 8575baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent } 85881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 8595baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent 86081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Only Pre processor effects are allowed on input threads and only on input threads 86181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { 86281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d", 86381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc->name, desc->flags, mType); 86481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 86581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 86681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 86781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 86881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); 86981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 87081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // scope for mLock 87181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 87281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 87381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // check for existing effect chain with the requested audio session 87481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain = getEffectChain_l(sessionId); 87581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain == 0) { 87681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create a new chain for this session 87781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("createEffect_l() new effect chain for session %d", sessionId); 87881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain = new EffectChain(this, sessionId); 87981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent addEffectChain_l(chain); 88081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setStrategy(getStrategyForSession_l(sessionId)); 88181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chainCreated = true; 88281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 88381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect = chain->getEffectFromDesc_l(desc); 88481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 88581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 88681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); 88781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 88881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (effect == 0) { 88981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int id = mAudioFlinger->nextUniqueId(); 89081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Check CPU and memory usage 89181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); 89281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR) { 89381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 89481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 89581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effectRegistered = true; 89681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create a new effect module if none present in the chain 89781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect = new EffectModule(this, chain, desc, id, sessionId); 89881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = effect->status(); 89981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR) { 90081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 90181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 9025baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent effect->setOffloaded(mType == OFFLOAD, mId); 9035baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent 90481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = chain->addEffect_l(effect); 90581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR) { 90681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 90781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 90881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effectCreated = true; 90981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 91081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setDevice(mOutDevice); 91181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setDevice(mInDevice); 91281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setMode(mAudioFlinger->getMode()); 91381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setAudioSource(mAudioSource); 91481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 91581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create effect handle and connect it to effect module 91681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent handle = new EffectHandle(effect, client, effectClient, priority); 917e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten lStatus = handle->initCheck(); 918e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten if (lStatus == OK) { 919e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten lStatus = effect->addHandle(handle.get()); 920e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten } 92181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (enabled != NULL) { 92281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *enabled = (int)effect->isEnabled(); 92381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 92481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 92581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 92681784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit: 92781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { 92881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 92981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (effectCreated) { 93081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->removeEffect_l(effect); 93181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 93281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (effectRegistered) { 93381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSystem::unregisterEffect(effect->id()); 93481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 93581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chainCreated) { 93681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent removeEffectChain_l(chain); 93781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 93881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent handle.clear(); 93981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 94081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 9419156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten *status = lStatus; 94281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return handle; 94381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 94481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 94581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(int sessionId, int effectId) 94681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 94781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 94881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return getEffect_l(sessionId, effectId); 94981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 95081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 95181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId) 95281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 95381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(sessionId); 95481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; 95581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 95681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 95781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and 95881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// PlaybackThread::mLock held 95981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) 96081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 96181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // check for existing effect chain with the requested audio session 96281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId = effect->sessionId(); 96381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(sessionId); 96481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool chainCreated = false; 96581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 9665baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(), 9675baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %x", 9685baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent this, effect->desc().name, effect->desc().flags); 9695baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent 97081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain == 0) { 97181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create a new chain for this session 97281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffect_l() new effect chain for session %d", sessionId); 97381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain = new EffectChain(this, sessionId); 97481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent addEffectChain_l(chain); 97581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setStrategy(getStrategyForSession_l(sessionId)); 97681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chainCreated = true; 97781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 97881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); 97981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 98081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain->getEffectFromId_l(effect->id()) != 0) { 98181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("addEffect_l() %p effect %s already present in chain %p", 98281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent this, effect->desc().name, chain.get()); 98381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 98481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 98581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 9865baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent effect->setOffloaded(mType == OFFLOAD, mId); 9875baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent 98881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status = chain->addEffect_l(effect); 98981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (status != NO_ERROR) { 99081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chainCreated) { 99181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent removeEffectChain_l(chain); 99281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 99381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 99481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 99581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 99681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setDevice(mOutDevice); 99781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setDevice(mInDevice); 99881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setMode(mAudioFlinger->getMode()); 99981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setAudioSource(mAudioSource); 100081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 100181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 100281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 100381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { 100481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 100581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("removeEffect_l() %p effect %p", this, effect.get()); 100681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect_descriptor_t desc = effect->desc(); 100781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 100881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent detachAuxEffect_l(effect->id()); 100981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 101081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 101181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = effect->chain().promote(); 101281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 101381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // remove effect chain if removing last effect 101481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain->removeEffect_l(effect) == 0) { 101581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent removeEffectChain_l(chain); 101681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 101781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 101881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); 101981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 102081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 102181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 102281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::lockEffectChains_l( 102381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<AudioFlinger::EffectChain> >& effectChains) 102481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 102581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effectChains = mEffectChains; 102681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 102781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains[i]->lock(); 102881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 102981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 103081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 103181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::unlockEffectChains( 103281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const Vector< sp<AudioFlinger::EffectChain> >& effectChains) 103381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 103481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < effectChains.size(); i++) { 103581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effectChains[i]->unlock(); 103681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 103781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 103881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 103981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId) 104081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 104181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 104281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return getEffectChain_l(sessionId); 104381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 104481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 104581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) const 104681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 104781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t size = mEffectChains.size(); 104881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < size; i++) { 104981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mEffectChains[i]->sessionId() == sessionId) { 105081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mEffectChains[i]; 105181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 105281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 105381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 105481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 105581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 105681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::setMode(audio_mode_t mode) 105781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 105881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 105981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t size = mEffectChains.size(); 106081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < size; i++) { 106181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains[i]->setMode_l(mode); 106281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 106381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 106481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 106581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::disconnectEffect(const sp<EffectModule>& effect, 106681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent EffectHandle *handle, 106781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool unpinIfLast) { 106881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 106981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 107081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("disconnectEffect() %p effect %p", this, effect.get()); 107181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // delete the effect module if removing last handle on it 107281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (effect->removeHandle(handle) == 0) { 107381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!effect->isPinned() || unpinIfLast) { 107481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent removeEffect_l(effect); 107581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSystem::unregisterEffect(effect->id()); 107681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 107781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 107881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 107981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 108081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 108181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Playback 108281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 108381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 108481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, 108581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamOut* output, 108681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t id, 108781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_devices_t device, 108881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent type_t type) 108981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent : ThreadBase(audioFlinger, id, device, AUDIO_DEVICE_NONE, type), 10902098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung mNormalFrameCount(0), mSinkBuffer(NULL), 109169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferEnabled(false), 109269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBuffer(NULL), 109369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferSize(0), 109469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferFormat(AUDIO_FORMAT_INVALID), 109569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferValid(false), 109698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferEnabled(false), 109798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBuffer(NULL), 109898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferSize(0), 109998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferFormat(AUDIO_FORMAT_INVALID), 110098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferValid(false), 1101c1fac191069774c7bfcb062edbb821ea56e7dbc0Glenn Kasten mSuspended(0), mBytesWritten(0), 1102462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mActiveTracksGeneration(0), 110381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mStreamTypes[] initialized in constructor body 110481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutput(output), 110581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), 110681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMixerStatus(MIXER_IDLE), 110781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMixerStatusIgnoringFastTracks(MIXER_IDLE), 110881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent standbyDelay(AudioFlinger::mStandbyTimeInNsecs), 1109bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining(0), 1110bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mCurrentWriteLength(0), 1111bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mUseAsyncWrite(false), 11123b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence(0), 11133b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence(0), 1114ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent mSignalPending(false), 111581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mScreenState(AudioFlinger::mScreenState), 111681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // index 0 is reserved for normal mixer's submix 1117bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mFastTrackAvailMask(((1 << FastMixerState::kMaxFastTracks) - 1) & ~1), 1118bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten // mLatchD, mLatchQ, 1119bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mLatchDValid(false), mLatchQValid(false) 112081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 112181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent snprintf(mName, kNameLength, "AudioOut_%X", id); 11229e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mName); 112381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 112481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Assumes constructor is called by AudioFlinger with it's mLock held, but 112581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it would be safer to explicitly pass initial masterVolume/masterMute as 112681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // parameter. 112781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // 112881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If the HAL we are using has support for master volume or master mute, 112981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // then do not attenuate or mute during mixing (just leave the volume at 1.0 113081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // and the mute set to false). 113181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterVolume = audioFlinger->masterVolume_l(); 113281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterMute = audioFlinger->masterMute_l(); 113381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutput && mOutput->audioHwDev) { 113481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutput->audioHwDev->canSetMasterVolume()) { 113581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterVolume = 1.0; 113681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 113781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 113881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutput->audioHwDev->canSetMasterMute()) { 113981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterMute = false; 114081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 114181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 114281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1143deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten readOutputParameters_l(); 114481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 114581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mStreamTypes[AUDIO_STREAM_CNT] is initialized by stream_type_t default constructor 114681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // There is no AUDIO_STREAM_MIN, and ++ operator does not compile 114766e4635cb09fadcaccf912f37c387396c428378aGlenn Kasten for (audio_stream_type_t stream = AUDIO_STREAM_MIN; stream < AUDIO_STREAM_CNT; 114881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent stream = (audio_stream_type_t) (stream + 1)) { 114981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream); 115081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); 115181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 115281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mStreamTypes[AUDIO_STREAM_CNT] exists but isn't explicitly initialized here, 115381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // because mAudioFlinger doesn't have one to copy from 115481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 115581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 115681784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread::~PlaybackThread() 115781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 11589e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten mAudioFlinger->unregisterWriter(mNBLogWriter); 1159010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung free(mSinkBuffer); 116069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung free(mMixerBuffer); 116198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung free(mEffectBuffer); 116281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 116381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 116481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) 116581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 116681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpInternals(fd, args); 116781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpTracks(fd, args); 116881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpEffectChains(fd, args); 116981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 117081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 11710f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args __unused) 117281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 117381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const size_t SIZE = 256; 117481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char buffer[SIZE]; 117581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent String8 result; 117681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1177b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.appendFormat(" Stream volumes in dB: "); 117881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { 117981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const stream_type_t *st = &mStreamTypes[i]; 118081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (i > 0) { 118181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result.appendFormat(", "); 118281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 118381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); 118481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (st->mute) { 118581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result.append("M"); 118681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 118781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 118881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result.append("\n"); 118981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent write(fd, result.string(), result.length()); 119081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result.clear(); 119181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1192b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen // These values are "raw"; they will wrap around. See prepareTracks_l() for a better way. 1193b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen FastTrackUnderruns underruns = getFastTrackUnderruns(0); 1194b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, " Normal mixer raw underrun counters: partial=%u empty=%u\n", 1195b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); 1196b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen 1197b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numtracks = mTracks.size(); 1198b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numactive = mActiveTracks.size(); 1199b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, " %d Tracks", numtracks); 1200b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numactiveseen = 0; 1201b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (numtracks) { 1202b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, " of which %d are active\n", numactive); 1203b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen Track::appendDumpHeader(result); 1204b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen for (size_t i = 0; i < numtracks; ++i) { 1205b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen sp<Track> track = mTracks[i]; 1206b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (track != 0) { 1207b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen bool active = mActiveTracks.indexOf(track) >= 0; 1208b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (active) { 1209b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen numactiveseen++; 1210b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 1211b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen track->dump(buffer, SIZE, active); 1212b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append(buffer); 1213b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 121481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1215b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } else { 1216b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append("\n"); 121781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1218b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (numactiveseen != numactive) { 1219b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen // some tracks in the active list were not in the tracks list 1220b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen snprintf(buffer, SIZE, " The following tracks are in the active list but" 1221b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen " not in the track list\n"); 1222b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append(buffer); 1223b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen Track::appendDumpHeader(result); 1224b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen for (size_t i = 0; i < numactive; ++i) { 1225b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen sp<Track> track = mActiveTracks[i].promote(); 1226b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (track != 0 && mTracks.indexOf(track) < 0) { 1227b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen track->dump(buffer, SIZE, true); 1228b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append(buffer); 1229b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 123081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 123181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1232b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen 123381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent write(fd, result.string(), result.size()); 123481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 123581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 123681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 123781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) 123881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 1239b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, "\nOutput thread %p:\n", this); 12401d6fa7af1288b550faabe4ec2cf98684236723dbNarayan Kamath fdprintf(fd, " Normal frame count: %zu\n", mNormalFrameCount); 1241b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, " Last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime)); 1242b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, " Total writes: %d\n", mNumWrites); 1243b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, " Delayed writes: %d\n", mNumDelayedWrites); 1244b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, " Blocked in write: %s\n", mInWrite ? "yes" : "no"); 1245b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, " Suspend count: %d\n", mSuspended); 12462098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung fdprintf(fd, " Sink buffer : %p\n", mSinkBuffer); 124769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung fdprintf(fd, " Mixer buffer: %p\n", mMixerBuffer); 124898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung fdprintf(fd, " Effect buffer: %p\n", mEffectBuffer); 1249b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, " Fast track availMask=%#x\n", mFastTrackAvailMask); 125081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 125181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpBase(fd, args); 125281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 125381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 125481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Thread virtuals 125581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 125681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::onFirstRef() 125781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 125881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent run(mName, ANDROID_PRIORITY_URGENT_AUDIO); 125981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 126081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 126181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ThreadBase virtuals 126281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::preExit() 126381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 126481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV(" preExit()"); 126581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME this is using hard-coded strings but in the future, this functionality will be 126681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // converted to use audio HAL extensions required to support tunneling 126781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutput->stream->common.set_parameters(&mOutput->stream->common, "exiting=1"); 126881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 126981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 127081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held 127181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( 127281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<AudioFlinger::Client>& client, 127381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_stream_type_t streamType, 127481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t sampleRate, 127581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_format_t format, 127681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_channel_mask_t channelMask, 127774935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten size_t *pFrameCount, 127881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<IMemory>& sharedBuffer, 127981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId, 128081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent IAudioFlinger::track_flags_t *flags, 128181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t tid, 1282462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen int uid, 128381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t *status) 128481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 128574935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten size_t frameCount = *pFrameCount; 128681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track; 128781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t lStatus; 128881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 128981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool isTimed = (*flags & IAudioFlinger::TRACK_TIMED) != 0; 129081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 129181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // client expresses a preference for FAST, but we get the final say 129281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (*flags & IAudioFlinger::TRACK_FAST) { 129381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ( 129481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // not timed 129581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (!isTimed) && 129681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // either of these use cases: 129781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ( 129881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // use case 1: shared buffer with any frame count 129981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ( 130081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (sharedBuffer != 0) 130181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ) || 130281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // use case 2: callback handler and frame count is default or at least as large as HAL 130381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ( 130481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (tid != -1) && 130581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ((frameCount == 0) || 1306b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten (frameCount >= mFrameCount)) 130781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ) 130881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ) && 130981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // PCM data 131081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_is_linear_pcm(format) && 131181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mono or stereo 131281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ( (channelMask == AUDIO_CHANNEL_OUT_MONO) || 131381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (channelMask == AUDIO_CHANNEL_OUT_STEREO) ) && 131481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // hardware sample rate 131581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (sampleRate == mSampleRate) && 131681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // normal mixer has an associated fast mixer 131781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent hasFastMixer() && 131881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // there are sufficient fast track slots available 131981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (mFastTrackAvailMask != 0) 132081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME test that MixerThread for this fast track has a capable output HAL 132181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME add a permission test also? 132281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ) { 132381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if frameCount not specified, then it defaults to fast mixer (HAL) frame count 132481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (frameCount == 0) { 132581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent frameCount = mFrameCount * kFastTrackMultiplier; 132681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 132781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%d mFrameCount=%d", 132881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent frameCount, mFrameCount); 132981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 133081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: isTimed=%d sharedBuffer=%p frameCount=%d " 133181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "mFrameCount=%d format=%d isLinear=%d channelMask=%#x sampleRate=%u mSampleRate=%u " 133281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", 133381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent isTimed, sharedBuffer.get(), frameCount, mFrameCount, format, 133481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_is_linear_pcm(format), 133581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); 133681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *flags &= ~IAudioFlinger::TRACK_FAST; 133781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // For compatibility with AudioTrack calculation, buffer depth is forced 133881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // to be at least 2 x the normal mixer frame count and cover audio hardware latency. 133981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // This is probably too conservative, but legacy application code may depend on it. 134081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If you change this calculation, also review the start threshold which is related. 134181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream); 134281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate); 134381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (minBufCount < 2) { 134481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minBufCount = 2; 134581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 134681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t minFrameCount = mNormalFrameCount * minBufCount; 134781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (frameCount < minFrameCount) { 134881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent frameCount = minFrameCount; 134981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 135081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 135181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 135274935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten *pFrameCount = frameCount; 135381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1354c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten switch (mType) { 1355c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten 1356c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten case DIRECT: 1357993fa0603707e94ce259e95e56838a85b5ccbdc5Glenn Kasten if (audio_is_linear_pcm(format)) { 135881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { 1359cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x " 1360cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten "for output %p with format %#x", 136181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sampleRate, format, channelMask, mOutput, mFormat); 136281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 136381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 136481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 136581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1366c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten break; 1367c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten 1368c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten case OFFLOAD: 1369bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { 1370cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \"" 1371cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten "for output %p with format %#x", 1372bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent sampleRate, format, channelMask, mOutput, mFormat); 1373bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent lStatus = BAD_VALUE; 1374bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent goto Exit; 1375bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1376c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten break; 1377c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten 1378c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten default: 1379993fa0603707e94ce259e95e56838a85b5ccbdc5Glenn Kasten if (!audio_is_linear_pcm(format)) { 1380cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten ALOGE("createTrack_l() Bad parameter: format %#x \"" 1381cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten "for output %p with format %#x", 1382bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent format, mOutput, mFormat); 1383bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent lStatus = BAD_VALUE; 1384bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent goto Exit; 1385bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 138681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Resampler implementation limits input sampling rate to 2 x output sampling rate. 138781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sampleRate > mSampleRate*2) { 138881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); 138981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 139081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 139181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1392c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten break; 1393c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten 139481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 139581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 139681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = initCheck(); 139781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR) { 139815e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten ALOGE("createTrack_l() audio driver not initialized"); 139981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 140081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 140181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 140281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // scope for mLock 140381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 140481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 140581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // all tracks in same audio session must share the same routing strategy otherwise 140681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // conflicts will happen when tracks are moved from one output to another by audio policy 140781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // manager 140881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t strategy = AudioSystem::getStrategyForStream(streamType); 140981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 141081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> t = mTracks[i]; 141181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (t != 0 && !t->isOutputTrack()) { 141281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); 141381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId == t->sessionId() && strategy != actual) { 141481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", 141581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent strategy, actual); 141681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 141781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 141881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 141981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 142081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 142181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 142281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!isTimed) { 142381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track = new Track(this, client, streamType, sampleRate, format, 1424462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen channelMask, frameCount, sharedBuffer, sessionId, uid, *flags); 142581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 142681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track = TimedTrack::create(this, client, streamType, sampleRate, format, 1427462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen channelMask, frameCount, sharedBuffer, sessionId, uid); 142881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1429030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten 1430030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten // new Track always returns non-NULL, 1431030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten // but TimedTrack::create() is a factory that could fail by returning NULL 1432030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY; 1433030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten if (lStatus != NO_ERROR) { 14340cde076ddb283c84c3801a2df4cc3df99bd1577fGlenn Kasten ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus); 143503e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George // track must be cleared from the caller as the caller has the AF lock 143681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 143781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 143881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mTracks.add(track); 143981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 144081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(sessionId); 144181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 144281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); 144381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->setMainBuffer(chain->inBuffer()); 144481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); 144581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->incTrackCnt(); 144681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 144781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 144881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { 144981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t callingPid = IPCThreadState::self()->getCallingPid(); 145081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, 145181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // so ask activity manager to do this on our behalf 145281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); 145381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 145481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 145581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 145681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = NO_ERROR; 145781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 145881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit: 14599156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten *status = lStatus; 146081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return track; 146181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 146281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 146381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const 146481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 146581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return latency; 146681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 146781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 146881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::latency() const 146981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 147081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 147181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return latency_l(); 147281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 147381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::latency_l() const 147481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 147581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (initCheck() == NO_ERROR) { 147681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return correctLatency_l(mOutput->stream->get_latency(mOutput->stream)); 147781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 147881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 147981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 148081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 148181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 148281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::setMasterVolume(float value) 148381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 148481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 148581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Don't apply master volume in SW if our HAL can do it for us. 148681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutput && mOutput->audioHwDev && 148781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutput->audioHwDev->canSetMasterVolume()) { 148881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterVolume = 1.0; 148981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 149081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterVolume = value; 149181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 149281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 149381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 149481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::setMasterMute(bool muted) 149581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 149681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 149781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Don't apply master mute in SW if our HAL can do it for us. 149881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutput && mOutput->audioHwDev && 149981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutput->audioHwDev->canSetMasterMute()) { 150081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterMute = false; 150181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 150281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterMute = muted; 150381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 150481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 150581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 150681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) 150781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 150881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 150981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStreamTypes[stream].volume = value; 1510ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent broadcast_l(); 151181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 151281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 151381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) 151481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 151581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 151681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStreamTypes[stream].mute = muted; 1517ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent broadcast_l(); 151881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 151981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 152081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentfloat AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const 152181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 152281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 152381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mStreamTypes[stream].volume; 152481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 152581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 152681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// addTrack_l() must be called with ThreadBase::mLock held 152781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) 152881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 152981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status = ALREADY_EXISTS; 153081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 153181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // set retry count for buffer fill 153281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mRetryCount = kMaxTrackStartupRetries; 153381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mActiveTracks.indexOf(track) < 0) { 153481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // the track is newly added, make sure it fills up all its 153581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // buffers before playing. This is to ensure the client will 153681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // effectively get the latency it requested. 1537bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (!track->isOutputTrack()) { 1538bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent TrackBase::track_state state = track->mState; 1539bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mLock.unlock(); 1540bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent status = AudioSystem::startOutput(mId, track->streamType(), track->sessionId()); 1541bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mLock.lock(); 1542bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // abort track was stopped/paused while we released the lock 1543bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (state != track->mState) { 1544bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (status == NO_ERROR) { 1545bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mLock.unlock(); 1546bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioSystem::stopOutput(mId, track->streamType(), track->sessionId()); 1547bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mLock.lock(); 1548bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1549bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return INVALID_OPERATION; 1550bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1551bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // abort if start is rejected by audio policy manager 1552bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (status != NO_ERROR) { 1553bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return PERMISSION_DENIED; 1554bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1555bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent#ifdef ADD_BATTERY_DATA 1556bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // to track the speaker usage 1557bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); 1558bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent#endif 1559bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1560bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 15619f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten track->mFillingUpStatus = track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING; 156281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mResetDone = false; 156381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mPresentationCompleteFrames = 0; 156481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mActiveTracks.add(track); 1565462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mWakeLockUids.add(track->uid()); 1566462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mActiveTracksGeneration++; 1567fd4779740ec3e9e865d5514464df26d015354388Eric Laurent mLatestActiveTrack = track; 1568d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent sp<EffectChain> chain = getEffectChain_l(track->sessionId()); 1569d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent if (chain != 0) { 1570d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), 1571d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent track->sessionId()); 1572d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent chain->incActiveTrackCnt(); 157381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 157481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 157581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = NO_ERROR; 157681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 157781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 15784c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George onAddNewTrack_l(); 157981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 158081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 158181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1582bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) 158381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 1584bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->terminate(); 158581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // active tracks are removed by threadLoop() 1586bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent bool trackActive = (mActiveTracks.indexOf(track) >= 0); 1587bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mState = TrackBase::STOPPED; 1588bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (!trackActive) { 158981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent removeTrack_l(track); 1590bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if (track->isFastTrack() || track->isOffloaded()) { 1591bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mState = TrackBase::STOPPING_1; 159281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1593bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1594bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return trackActive; 159581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 159681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 159781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) 159881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 159981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); 160081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mTracks.remove(track); 160181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent deleteTrackName_l(track->name()); 160281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // redundant as track is about to be destroyed, for dumpsys only 160381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mName = -1; 160481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isFastTrack()) { 160581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int index = track->mFastIndex; 160681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(0 < index && index < (int)FastMixerState::kMaxFastTracks); 160781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); 160881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastTrackAvailMask |= 1 << index; 160981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // redundant as track is about to be destroyed, for dumpsys only 161081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mFastIndex = -1; 161181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 161281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(track->sessionId()); 161381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 161481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->decTrackCnt(); 161581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 161681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 161781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1618ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurentvoid AudioFlinger::PlaybackThread::broadcast_l() 1619bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 1620bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Thread could be blocked waiting for async 1621bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // so signal it to handle state changes immediately 1622bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // If threadLoop is currently unlocked a signal of mWaitWorkCV will 1623bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // be lost so we also flag to prevent it blocking on mWaitWorkCV 1624bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mSignalPending = true; 1625ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent mWaitWorkCV.broadcast(); 1626bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 1627bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 162881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentString8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) 162981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 163081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 163181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (initCheck() != NO_ERROR) { 1632d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten return String8(); 163381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 163481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1635d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten char *s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); 1636d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten const String8 out_s8(s); 163781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent free(s); 163881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return out_s8; 163981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 164081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1641021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurentvoid AudioFlinger::PlaybackThread::audioConfigChanged(int event, int param) { 164281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSystem::OutputDescriptor desc; 164381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void *param2 = NULL; 164481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1645021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent ALOGV("PlaybackThread::audioConfigChanged, thread %p, event %d, param %d", this, event, 164681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent param); 164781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 164881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent switch (event) { 164981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case AudioSystem::OUTPUT_OPENED: 165081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case AudioSystem::OUTPUT_CONFIG_CHANGED: 1651fad226abd12435dbcd232f7de396f1a097b2bd5fGlenn Kasten desc.channelMask = mChannelMask; 165281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc.samplingRate = mSampleRate; 165381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc.format = mFormat; 165481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc.frameCount = mNormalFrameCount; // FIXME see 165581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // AudioFlinger::frameCount(audio_io_handle_t) 16561035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent desc.latency = latency_l(); 165781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent param2 = &desc; 165881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 165981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 166081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case AudioSystem::STREAM_CONFIG_CHANGED: 166181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent param2 = ¶m; 166281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case AudioSystem::OUTPUT_CLOSED: 166381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent default: 166481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 166581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1666021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent mAudioFlinger->audioConfigChanged(event, mId, param2); 166781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 166881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1669bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::writeCallback() 1670bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 1671bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 16723b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->resetWriteBlocked(); 1673bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 1674bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1675bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::drainCallback() 1676bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 1677bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 16783b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->resetDraining(); 1679bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 1680bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 16813b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence) 1682bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 1683bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 16843b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // reject out of sequence requests 16853b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) { 16863b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence &= ~1; 1687bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mWaitWorkCV.signal(); 1688bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1689bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 1690bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 16913b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence) 1692bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 1693bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 16943b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // reject out of sequence requests 16953b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if ((mDrainSequence & 1) && (sequence == mDrainSequence)) { 16963b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence &= ~1; 1697bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mWaitWorkCV.signal(); 1698bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1699bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 1700bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1701bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// static 1702bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentint AudioFlinger::PlaybackThread::asyncCallback(stream_callback_event_t event, 17030f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten void *param __unused, 1704bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent void *cookie) 1705bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 1706bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioFlinger::PlaybackThread *me = (AudioFlinger::PlaybackThread *)cookie; 1707bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("asyncCallback() event %d", event); 1708bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent switch (event) { 1709bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent case STREAM_CBK_EVENT_WRITE_READY: 1710bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent me->writeCallback(); 1711bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent break; 1712bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent case STREAM_CBK_EVENT_DRAIN_READY: 1713bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent me->drainCallback(); 1714bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent break; 1715bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent default: 1716bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGW("asyncCallback() unknown event %d", event); 1717bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent break; 1718bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1719bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return 0; 1720bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 1721bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1722deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kastenvoid AudioFlinger::PlaybackThread::readOutputParameters_l() 172381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 1724adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten // unfortunately we have no way of recovering from errors here, hence the LOG_ALWAYS_FATAL 172581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common); 172681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common); 17277fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten if (!audio_is_output_channel(mChannelMask)) { 1728adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask); 17297fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten } 17307fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten if ((mType == MIXER || mType == DUPLICATING) && mChannelMask != AUDIO_CHANNEL_OUT_STEREO) { 1731adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output; " 17327fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten "must be AUDIO_CHANNEL_OUT_STEREO", mChannelMask); 17337fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten } 1734f6ed423af92a56ef54bba23eba883b1f21448b54Glenn Kasten mChannelCount = popcount(mChannelMask); 173581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFormat = mOutput->stream->common.get_format(&mOutput->stream->common); 17367fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten if (!audio_is_valid_format(mFormat)) { 1737adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat); 17387fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten } 17397fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten if ((mType == MIXER || mType == DUPLICATING) && mFormat != AUDIO_FORMAT_PCM_16_BIT) { 1740adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("HAL format %#x not supported for mixed output; " 1741adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten "must be AUDIO_FORMAT_PCM_16_BIT", mFormat); 17427fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten } 174381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFrameSize = audio_stream_frame_size(&mOutput->stream->common); 174470949c47fbae3f836d15f040551d7631be3ed7c2Glenn Kasten mBufferSize = mOutput->stream->common.get_buffer_size(&mOutput->stream->common); 174570949c47fbae3f836d15f040551d7631be3ed7c2Glenn Kasten mFrameCount = mBufferSize / mFrameSize; 174681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mFrameCount & 15) { 174781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("HAL output buffer size is %u frames but AudioMixer requires multiples of 16 frames", 174881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFrameCount); 174981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 175081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1751bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if ((mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) && 1752bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent (mOutput->stream->set_callback != NULL)) { 1753bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mOutput->stream->set_callback(mOutput->stream, 1754bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioFlinger::PlaybackThread::asyncCallback, this) == 0) { 1755bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mUseAsyncWrite = true; 17564de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent mCallbackThread = new AudioFlinger::AsyncCallbackThread(this); 1757bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1758bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1759bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 176009a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung // Calculate size of normal sink buffer relative to the HAL output buffer size 176181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double multiplier = 1.0; 176281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mType == MIXER && (kUseFastMixer == FastMixer_Static || 176381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent kUseFastMixer == FastMixer_Dynamic)) { 176409a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000; 176509a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000; 176681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer 176781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minNormalFrameCount = (minNormalFrameCount + 15) & ~15; 176881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent maxNormalFrameCount = maxNormalFrameCount & ~15; 176981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (maxNormalFrameCount < minNormalFrameCount) { 177081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent maxNormalFrameCount = minNormalFrameCount; 177181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 177281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent multiplier = (double) minNormalFrameCount / (double) mFrameCount; 177381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (multiplier <= 1.0) { 177481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent multiplier = 1.0; 177581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (multiplier <= 2.0) { 177681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (2 * mFrameCount <= maxNormalFrameCount) { 177781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent multiplier = 2.0; 177881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 177981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent multiplier = (double) maxNormalFrameCount / (double) mFrameCount; 178081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 178181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 178281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // prefer an even multiplier, for compatibility with doubling of fast tracks due to HAL 178309a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung // SRC (it would be unusual for the normal sink buffer size to not be a multiple of fast 178481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track, but we sometimes have to do this to satisfy the maximum frame count 178581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // constraint) 178681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME this rounding up should not be done if no HAL SRC 178781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t truncMult = (uint32_t) multiplier; 178881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((truncMult & 1)) { 178981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((truncMult + 1) * mFrameCount <= maxNormalFrameCount) { 179081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ++truncMult; 179181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 179281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 179381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent multiplier = (double) truncMult; 179481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 179581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 179681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalFrameCount = multiplier * mFrameCount; 179781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // round up to nearest 16 frames to satisfy AudioMixer 179881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalFrameCount = (mNormalFrameCount + 15) & ~15; 179909a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung ALOGI("HAL output buffer size %u frames, normal sink buffer size %u frames", mFrameCount, 180081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalFrameCount); 180181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1802010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // mSinkBuffer is the sink buffer. Size is always multiple-of-16 frames. 1803010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // Originally this was int16_t[] array, need to remove legacy implications. 1804010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung free(mSinkBuffer); 1805010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung mSinkBuffer = NULL; 18065b10a2037a835e790994b9ebec3c2e55052f1f3bAndy Hung // For sink buffer size, we use the frame size from the downstream sink to avoid problems 18075b10a2037a835e790994b9ebec3c2e55052f1f3bAndy Hung // with non PCM formats for compressed music, e.g. AAC, and Offload threads. 18085b10a2037a835e790994b9ebec3c2e55052f1f3bAndy Hung const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; 1809010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); 181081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 181169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // We resize the mMixerBuffer according to the requirements of the sink buffer which 181269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // drives the output. 181369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung free(mMixerBuffer); 181469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBuffer = NULL; 181569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung if (mMixerBufferEnabled) { 181669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // also valid: AUDIO_FORMAT_PCM_16_BIT. 181769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferSize = mNormalFrameCount * mChannelCount 181869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * audio_bytes_per_sample(mMixerBufferFormat); 181969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize); 182069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung } 182198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung free(mEffectBuffer); 182298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBuffer = NULL; 182398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mEffectBufferEnabled) { 182498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferFormat = AUDIO_FORMAT_PCM_16_BIT; // Note: Effects support 16b only 182598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferSize = mNormalFrameCount * mChannelCount 182698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung * audio_bytes_per_sample(mEffectBufferFormat); 182798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize); 182898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 182969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung 183081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // force reconfiguration of effect chains and engines to take new buffer size and audio 183181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // parameters into account 1832deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten // Note that mLock is not held when readOutputParameters_l() is called from the constructor 183381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // but in this case nothing is done below as no audio sessions have effect yet so it doesn't 183481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // matter. 183581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains 183681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<EffectChain> > effectChains = mEffectChains; 183781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < effectChains.size(); i ++) { 183881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); 183981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 184081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 184181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 184281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1843377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETITstatus_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) 184481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 184581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (halFrames == NULL || dspFrames == NULL) { 184681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 184781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 184881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 184981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (initCheck() != NO_ERROR) { 185081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return INVALID_OPERATION; 185181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 185281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t framesWritten = mBytesWritten / mFrameSize; 185381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *halFrames = framesWritten; 185481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 185581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (isSuspended()) { 185681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // return an estimation of rendered frames when the output is suspended 185781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t latencyFrames = (latency_l() * mSampleRate) / 1000; 185881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *dspFrames = framesWritten >= latencyFrames ? framesWritten - latencyFrames : 0; 185981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 186081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 1861377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT status_t status; 1862377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT uint32_t frames; 1863377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT status = mOutput->stream->get_render_position(mOutput->stream, &frames); 1864377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT *dspFrames = (size_t)frames; 1865377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT return status; 186681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 186781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 186881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 186981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) const 187081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 187181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 187281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t result = 0; 187381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (getEffectChain_l(sessionId) != 0) { 187481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result = EFFECT_SESSION; 187581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 187681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 187781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 187881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mTracks[i]; 18795736c35b841de56ce394b4879389f669b61425e6Glenn Kasten if (sessionId == track->sessionId() && !track->isInvalid()) { 188081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result |= TRACK_SESSION; 188181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 188281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 188381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 188481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 188581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return result; 188681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 188781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 188881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId) 188981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 189081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that 189181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it is moved to correct output by audio policy manager when A2DP is connected or disconnected 189281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { 189381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); 189481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 189581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 189681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mTracks[i]; 18975736c35b841de56ce394b4879389f669b61425e6Glenn Kasten if (sessionId == track->sessionId() && !track->isInvalid()) { 189881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return AudioSystem::getStrategyForStream(track->streamType()); 189981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 190081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 190181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); 190281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 190381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 190481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 190581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const 190681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 190781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 190881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mOutput; 190981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 191081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 191181784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() 191281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 191381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 191481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamOut *output = mOutput; 191581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutput = NULL; 191681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME FastMixer might also have a raw ptr to mOutputSink; 191781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // must push a NULL and wait for ack 191881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutputSink.clear(); 191981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPipeSink.clear(); 192081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink.clear(); 192181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return output; 192281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 192381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 192481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// this method must always be called either with ThreadBase mLock held or inside the thread loop 192581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_stream_t* AudioFlinger::PlaybackThread::stream() const 192681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 192781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutput == NULL) { 192881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NULL; 192981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 193081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return &mOutput->stream->common; 193181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 193281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 193381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const 193481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 193581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); 193681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 193781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 193881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) 193981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 194081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!isValidSyncEvent(event)) { 194181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 194281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 194381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 194481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 194581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 194681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 194781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mTracks[i]; 194881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (event->triggerSession() == track->sessionId()) { 194981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (void) track->setSyncEvent(event); 195081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 195181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 195281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 195381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 195481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NAME_NOT_FOUND; 195581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 195681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 195781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const 195881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 195981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; 196081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 196181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 196281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::threadLoop_removeTracks( 196381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const Vector< sp<Track> >& tracksToRemove) 196481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 196581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t count = tracksToRemove.size(); 196634fca34606b448e6b71c2942f63cb13a0aebd620Glenn Kasten if (count > 0) { 196781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0 ; i < count ; i++) { 196881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<Track>& track = tracksToRemove.itemAt(i); 1969bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (!track->isOutputTrack()) { 197081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSystem::stopOutput(mId, track->streamType(), track->sessionId()); 1971bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent#ifdef ADD_BATTERY_DATA 1972bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // to track the speaker usage 1973bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); 1974bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent#endif 1975bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (track->isTerminated()) { 1976bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioSystem::releaseOutput(mId); 1977bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 197881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 197981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 198081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 198181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 198281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 198381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::checkSilentMode_l() 198481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 198581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!mMasterMute) { 198681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char value[PROPERTY_VALUE_MAX]; 198781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (property_get("ro.audio.silent", value, "0") > 0) { 198881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char *endptr; 198981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent unsigned long ul = strtoul(value, &endptr, 0); 199081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (*endptr == '\0' && ul != 0) { 199181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGD("Silence is golden"); 199281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The setprop command will not allow a property to be changed after 199381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // the first time it is set, so we don't have to worry about un-muting. 199481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent setMasterMute_l(true); 199581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 199681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 199781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 199881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 199981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 200081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// shared by MIXER and DIRECT, overridden by DUPLICATING 2001bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentssize_t AudioFlinger::PlaybackThread::threadLoop_write() 200281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 200381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME rewrite to reduce number of system calls 200481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mLastWriteTime = systemTime(); 200581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mInWrite = true; 2006bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ssize_t bytesWritten; 2007010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung const size_t offset = mCurrentWriteLength - mBytesRemaining; 200881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 200981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If an NBAIO sink is present, use it to write the normal mixer's submix 201081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mNormalSink != 0) { 2011010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung const size_t count = mBytesRemaining / mFrameSize; 2012010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung 20132d590964aa58e137d17a43e095e6443dd0fe2e98Simon Wilson ATRACE_BEGIN("write"); 201481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // update the setpoint when AudioFlinger::mScreenState changes 201581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t screenState = AudioFlinger::mScreenState; 201681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (screenState != mScreenState) { 201781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mScreenState = screenState; 201881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); 201981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (pipe != NULL) { 202081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pipe->setAvgFrames((mScreenState & 1) ? 202181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); 202281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 202381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 2024010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count); 20252d590964aa58e137d17a43e095e6443dd0fe2e98Simon Wilson ATRACE_END(); 202681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (framesWritten > 0) { 2027010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung bytesWritten = framesWritten * mFrameSize; 202881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 202981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bytesWritten = framesWritten; 203081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 2031767094dd98b01baf21de2ad09c27b3c98776cf73Glenn Kasten status_t status = mNormalSink->getTimestamp(mLatchD.mTimestamp); 2032bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten if (status == NO_ERROR) { 2033bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten size_t totalFramesWritten = mNormalSink->framesWritten(); 2034bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten if (totalFramesWritten >= mLatchD.mTimestamp.mPosition) { 2035bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mLatchD.mUnpresentedFrames = totalFramesWritten - mLatchD.mTimestamp.mPosition; 2036bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mLatchDValid = true; 2037bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten } 2038bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten } 203981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // otherwise use the HAL / AudioStreamOut directly 204081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 2041bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Direct output and offload threads 2042010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung 2043bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mUseAsyncWrite) { 20443b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request"); 20453b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence += 2; 20463b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence |= 1; 2047bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 20483b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setWriteBlocked(mWriteAckSequence); 2049bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2050767094dd98b01baf21de2ad09c27b3c98776cf73Glenn Kasten // FIXME We should have an implementation of timestamps for direct output threads. 2051767094dd98b01baf21de2ad09c27b3c98776cf73Glenn Kasten // They are used e.g for multichannel PCM playback over HDMI. 2052bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent bytesWritten = mOutput->stream->write(mOutput->stream, 20532098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung (char *)mSinkBuffer + offset, mBytesRemaining); 2054bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mUseAsyncWrite && 2055bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) { 2056bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // do not wait for async callback in case of error of full write 20573b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence &= ~1; 2058bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 20593b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setWriteBlocked(mWriteAckSequence); 2060bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 206181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 206281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 206381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNumWrites++; 206481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mInWrite = false; 2065fd4779740ec3e9e865d5514464df26d015354388Eric Laurent mStandby = false; 2066bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return bytesWritten; 2067bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 2068bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 2069bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::threadLoop_drain() 2070bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 2071bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mOutput->stream->drain) { 2072bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full"); 2073bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mUseAsyncWrite) { 20743b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request"); 20753b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence |= 1; 2076bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 20773b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setDraining(mDrainSequence); 2078bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2079bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mOutput->stream->drain(mOutput->stream, 2080bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent (mMixerStatus == MIXER_DRAIN_TRACK) ? AUDIO_DRAIN_EARLY_NOTIFY 2081bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent : AUDIO_DRAIN_ALL); 2082bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2083bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 2084bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 2085bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::threadLoop_exit() 2086bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 2087bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Default implementation has nothing to do 208881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 208981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 209081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent/* 209181784c37c61b09289654b979567a42bf73cd2b12Eric LaurentThe derived values that are cached: 209225c2dac12114699e90deb1c579cadebce7b91a97Andy Hung - mSinkBufferSize from frame count * frame size 209381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - activeSleepTime from activeSleepTimeUs() 209481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - idleSleepTime from idleSleepTimeUs() 209581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - standbyDelay from mActiveSleepTimeUs (DIRECT only) 209681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - maxPeriod from frame count and sample rate (MIXER only) 209781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 209881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentThe parameters that affect these derived values are: 209981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - frame count 210081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - frame size 210181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - sample rate 210281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - device type: A2DP or not 210381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - device latency 210481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - format: PCM or not 210581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - active sleep time 210681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - idle sleep time 210781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent*/ 210881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 210981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::cacheParameters_l() 211081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 211125c2dac12114699e90deb1c579cadebce7b91a97Andy Hung mSinkBufferSize = mNormalFrameCount * mFrameSize; 211281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent activeSleepTime = activeSleepTimeUs(); 211381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent idleSleepTime = idleSleepTimeUs(); 211481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 211581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 211681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType) 211781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 21187c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d", 211981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent this, streamType, mTracks.size()); 212081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 212181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 212281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t size = mTracks.size(); 212381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < size; i++) { 212481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> t = mTracks[i]; 212581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (t->streamType() == streamType) { 21265736c35b841de56ce394b4879389f669b61425e6Glenn Kasten t->invalidate(); 212781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 212881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 212981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 213081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 213181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) 213281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 213381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int session = chain->sessionId(); 2134010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung int16_t* buffer = reinterpret_cast<int16_t*>(mEffectBufferEnabled 2135010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung ? mEffectBuffer : mSinkBuffer); 213681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool ownsBuffer = false; 213781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 213881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); 213981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (session > 0) { 214081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Only one effect chain can be present in direct output thread and it uses 21412098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung // the sink buffer as input 214281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mType != DIRECT) { 214381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t numSamples = mNormalFrameCount * mChannelCount; 214481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent buffer = new int16_t[numSamples]; 214581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent memset(buffer, 0, numSamples * sizeof(int16_t)); 214681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); 214781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ownsBuffer = true; 214881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 214981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 215081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Attach all tracks with same session ID to this chain. 215181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 215281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mTracks[i]; 215381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (session == track->sessionId()) { 215481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), 215581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent buffer); 215681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->setMainBuffer(buffer); 215781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->incTrackCnt(); 215881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 215981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 216081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 216181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // indicate all active tracks in the chain 216281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { 216381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mActiveTracks[i].promote(); 216481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track == 0) { 216581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 216681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 216781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (session == track->sessionId()) { 216881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); 216981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->incActiveTrackCnt(); 217081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 217181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 217281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 217381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 217481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setInBuffer(buffer, ownsBuffer); 2175010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung chain->setOutBuffer(reinterpret_cast<int16_t*>(mEffectBufferEnabled 2176010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung ? mEffectBuffer : mSinkBuffer)); 217781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect 217881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // chains list in order to be processed last as it contains output stage effects 217981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before 218081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // session AUDIO_SESSION_OUTPUT_STAGE to be processed 218181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // after track specific effects and before output stage 218281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and 218381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX 218481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Effect chain for other sessions are inserted at beginning of effect 218581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // chains list to be processed before output mix effects. Relative order between other 218681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // sessions is not important 218781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t size = mEffectChains.size(); 218881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t i = 0; 218981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (i = 0; i < size; i++) { 219081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mEffectChains[i]->sessionId() < session) { 219181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 219281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 219381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 219481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains.insertAt(chain, i); 219581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent checkSuspendOnAddEffectChain_l(chain); 219681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 219781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 219881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 219981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 220081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsize_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) 220181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 220281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int session = chain->sessionId(); 220381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 220481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); 220581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 220681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 220781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain == mEffectChains[i]) { 220881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains.removeAt(i); 220981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // detach all active tracks from the chain 221081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { 221181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mActiveTracks[i].promote(); 221281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track == 0) { 221381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 221481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 221581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (session == track->sessionId()) { 221681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", 221781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain.get(), session); 221881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->decActiveTrackCnt(); 221981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 222081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 222181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 222281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // detach all tracks with same session ID from this chain 222381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 222481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mTracks[i]; 222581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (session == track->sessionId()) { 2226010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung track->setMainBuffer(reinterpret_cast<int16_t*>(mSinkBuffer)); 222781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->decTrackCnt(); 222881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 222981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 223081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 223181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 223281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 223381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mEffectChains.size(); 223481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 223581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 223681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::attachAuxEffect( 223781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) 223881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 223981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 224081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return attachAuxEffect_l(track, EffectId); 224181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 224281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 224381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::attachAuxEffect_l( 224481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) 224581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 224681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status = NO_ERROR; 224781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 224881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (EffectId == 0) { 224981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->setAuxBuffer(0, NULL); 225081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 225181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX 225281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); 225381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (effect != 0) { 225481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 225581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); 225681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 225781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = INVALID_OPERATION; 225881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 225981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 226081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = BAD_VALUE; 226181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 226281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 226381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 226481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 226581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 226681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) 226781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 226881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 226981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mTracks[i]; 227081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->auxEffectId() == effectId) { 227181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent attachAuxEffect_l(track, 0); 227281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 227381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 227481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 227581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 227681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::PlaybackThread::threadLoop() 227781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 227881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<Track> > tracksToRemove; 227981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 228081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent standbyTime = systemTime(); 228181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 228281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // MIXER 228381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent nsecs_t lastWarning = 0; 228481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 228581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // DUPLICATING 228681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME could this be made local to while loop? 228781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent writeFrames = 0; 228881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2289462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen int lastGeneration = 0; 2290462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 229181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent cacheParameters_l(); 229281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = idleSleepTime; 229381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 229481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mType == MIXER) { 229581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTimeShift = 0; 229681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 229781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 229881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent CpuStats cpuStats; 229981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); 230081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 230181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent acquireWakeLock(); 230281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 23039e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten // mNBLogWriter->log can only be called while thread mutex mLock is held. 23049e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten // So if you need to log when mutex is unlocked, set logString to a non-NULL string, 23059e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten // and then that string will be logged at the next convenient opportunity. 23069e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten const char *logString = NULL; 23079e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten 2308664539d25180ab8f77e0521533ea2821cf28985fEric Laurent checkSilentMode_l(); 2309664539d25180ab8f77e0521533ea2821cf28985fEric Laurent 231081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent while (!exitPending()) 231181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 231281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent cpuStats.sample(myName); 231381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 231481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<EffectChain> > effectChains; 231581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 231681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // scope for mLock 231781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 231881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 231981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2320021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent processConfigEvents_l(); 23211035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 23229e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten if (logString != NULL) { 23239e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten mNBLogWriter->logTimestamp(); 23249e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten mNBLogWriter->log(logString); 23259e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten logString = NULL; 23269e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten } 23279e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten 2328bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten if (mLatchDValid) { 2329bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mLatchQ = mLatchD; 2330bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mLatchDValid = false; 2331bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mLatchQValid = true; 2332bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten } 2333bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten 233481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent saveOutputTracks(); 2335bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mSignalPending) { 2336bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // A signal was raised while we were unlocked 2337bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mSignalPending = false; 2338bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if (waitingAsyncCallback_l()) { 2339bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (exitPending()) { 2340bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent break; 2341bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2342bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent releaseWakeLock_l(); 2343462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mWakeLockUids.clear(); 2344462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mActiveTracksGeneration++; 2345bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("wait async completion"); 2346bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mWaitWorkCV.wait(mLock); 2347bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("async completion/wake"); 2348bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent acquireWakeLock_l(); 2349972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent standbyTime = systemTime() + standbyDelay; 2350972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent sleepTime = 0; 2351ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent 2352ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent continue; 2353ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent } 2354ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent if ((!mActiveTracks.size() && systemTime() > standbyTime) || 2355bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent isSuspended()) { 2356bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // put audio hardware into standby after short delay 2357bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (shouldStandby_l()) { 235881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 235981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent threadLoop_standby(); 236081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 236181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStandby = true; 236281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 236381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 236481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { 236581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // we're about to wait, flush the binder command buffer 236681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent IPCThreadState::self()->flushCommands(); 236781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 236881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent clearOutputTracks(); 236981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 237081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (exitPending()) { 237181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 237281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 237381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 237481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock_l(); 2375462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mWakeLockUids.clear(); 2376462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mActiveTracksGeneration++; 237781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // wait until we have something to do... 237881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("%s going to sleep", myName.string()); 237981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitWorkCV.wait(mLock); 238081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("%s waking up", myName.string()); 238181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent acquireWakeLock_l(); 238281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 238381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMixerStatus = MIXER_IDLE; 238481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMixerStatusIgnoringFastTracks = MIXER_IDLE; 238581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mBytesWritten = 0; 2386bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining = 0; 238781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent checkSilentMode_l(); 238881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 238981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent standbyTime = systemTime() + standbyDelay; 239081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = idleSleepTime; 239181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mType == MIXER) { 239281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTimeShift = 0; 239381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 239481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 239581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 239681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 239781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 239881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mMixerStatusIgnoringFastTracks is also updated internally 239981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMixerStatus = prepareTracks_l(&tracksToRemove); 240081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2401462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen // compare with previously applied list 2402462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen if (lastGeneration != mActiveTracksGeneration) { 2403462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen // update wakelock 2404462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen updateWakeLockUids_l(mWakeLockUids); 2405462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen lastGeneration = mActiveTracksGeneration; 2406462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } 2407462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 240881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // prevent any changes in effect chain list and in each effect chain 240981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // during mixing and effect process as the audio buffers could be deleted 241081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // or modified if an effect is created or deleted 241181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lockEffectChains_l(effectChains); 2412462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } // mLock scope ends 241381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2414bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mBytesRemaining == 0) { 2415bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mCurrentWriteLength = 0; 2416bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mMixerStatus == MIXER_TRACKS_READY) { 2417bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // threadLoop_mix() sets mCurrentWriteLength 2418bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent threadLoop_mix(); 2419bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if ((mMixerStatus != MIXER_DRAIN_TRACK) 2420bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent && (mMixerStatus != MIXER_DRAIN_ALL)) { 2421bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // threadLoop_sleepTime sets sleepTime to 0 if data 2422bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // must be written to HAL 2423bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent threadLoop_sleepTime(); 2424bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (sleepTime == 0) { 242525c2dac12114699e90deb1c579cadebce7b91a97Andy Hung mCurrentWriteLength = mSinkBufferSize; 2426bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2427bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 242898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Either threadLoop_mix() or threadLoop_sleepTime() should have set 242998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // mMixerBuffer with data if mMixerBufferValid is true and sleepTime == 0. 243098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid) 243198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // or mSinkBuffer (if there are no effects). 243298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // 243398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // This is done pre-effects computation; if effects change to 243498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // support higher precision, this needs to move. 243598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // 243698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // mMixerBufferValid is only set true by MixerThread::prepareTracks_l(). 243798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // TODO use sleepTime == 0 as an additional condition. 243898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mMixerBufferValid) { 243998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer; 244098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat; 244198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung 244298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat, 244398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mNormalFrameCount * mChannelCount); 244498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 244598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung 2446bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining = mCurrentWriteLength; 2447bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (isSuspended()) { 2448bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent sleepTime = suspendSleepTimeUs(); 2449bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // simulate write to HAL when suspended 245025c2dac12114699e90deb1c579cadebce7b91a97Andy Hung mBytesWritten += mSinkBufferSize; 2451bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining = 0; 2452bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 245381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2454bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // only process effects if we're going to write 245559fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent if (sleepTime == 0 && mType != OFFLOAD) { 2456bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent for (size_t i = 0; i < effectChains.size(); i ++) { 2457bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent effectChains[i]->process_l(); 2458bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 245981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 246081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 246159fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent // Process effect chains for offloaded thread even if no audio 246259fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent // was read from audio track: process only updates effect state 246359fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent // and thus does have to be synchronized with audio writes but may have 246459fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent // to be called while waiting for async write callback 246559fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent if (mType == OFFLOAD) { 246659fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent for (size_t i = 0; i < effectChains.size(); i ++) { 246759fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent effectChains[i]->process_l(); 246859fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent } 246959fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent } 247081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 247198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Only if the Effects buffer is enabled and there is data in the 247298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Effects buffer (buffer valid), we need to 247398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // copy into the sink buffer. 247498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // TODO use sleepTime == 0 as an additional condition. 247598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mEffectBufferValid) { 247698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung //ALOGV("writing effect buffer to sink buffer format %#x", mFormat); 247798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat, 247898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mNormalFrameCount * mChannelCount); 247998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 248098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung 248181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // enable changes in effect chain 248281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent unlockEffectChains(effectChains); 248381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2484bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (!waitingAsyncCallback()) { 2485bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // sleepTime == 0 means we must write to audio hardware 2486bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (sleepTime == 0) { 2487bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mBytesRemaining) { 2488bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ssize_t ret = threadLoop_write(); 2489bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (ret < 0) { 2490bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining = 0; 2491bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else { 2492bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesWritten += ret; 2493bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining -= ret; 2494bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2495bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if ((mMixerStatus == MIXER_DRAIN_TRACK) || 2496bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent (mMixerStatus == MIXER_DRAIN_ALL)) { 2497bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent threadLoop_drain(); 2498bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 24994944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten if (mType == MIXER) { 25004944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten // write blocked detection 25014944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten nsecs_t now = systemTime(); 25024944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten nsecs_t delta = now - mLastWriteTime; 25034944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten if (!mStandby && delta > maxPeriod) { 25044944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten mNumDelayedWrites++; 25054944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten if ((now - lastWarning) > kWarningThrottleNs) { 25064944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten ATRACE_NAME("underrun"); 25074944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", 25084944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten ns2ms(delta), mNumDelayedWrites, this); 25094944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten lastWarning = now; 25104944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten } 2511bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 251281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 251381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2514bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else { 2515bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent usleep(sleepTime); 2516bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 251781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 251881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 251981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Finally let go of removed track(s), without the lock held 252081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // since we can't guarantee the destructors won't acquire that 252181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // same lock. This will also mutate and push a new fast mixer state. 252281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent threadLoop_removeTracks(tracksToRemove); 252381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent tracksToRemove.clear(); 252481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 252581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME I don't understand the need for this here; 252681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it was in the original code but maybe the 252781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // assignment in saveOutputTracks() makes this unnecessary? 252881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent clearOutputTracks(); 252981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 253081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Effect chains will be actually deleted here if they were removed from 253181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mEffectChains list during mixing or effects processing 253281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effectChains.clear(); 253381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 253481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME Note that the above .clear() is no longer necessary since effectChains 253581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // is now local to this block, but will keep it for now (at least until merge done). 253681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 253781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2538bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent threadLoop_exit(); 2539bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 254081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // for DuplicatingThread, standby mode is handled by the outputTracks, otherwise ... 2541bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mType == MIXER || mType == DIRECT || mType == OFFLOAD) { 254281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // put output stream into standby mode 254381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!mStandby) { 254481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutput->stream->common.standby(&mOutput->stream->common); 254581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 254681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 254781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 254881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock(); 2549462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mWakeLockUids.clear(); 2550462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mActiveTracksGeneration++; 255181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 255281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("Thread %p type %d exiting", this, mType); 255381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 255481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 255581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2556bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// removeTracks_l() must be called with ThreadBase::mLock held 2557bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove) 2558bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 2559bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent size_t count = tracksToRemove.size(); 256034fca34606b448e6b71c2942f63cb13a0aebd620Glenn Kasten if (count > 0) { 2561bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent for (size_t i=0 ; i<count ; i++) { 2562bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent const sp<Track>& track = tracksToRemove.itemAt(i); 2563bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mActiveTracks.remove(track); 2564462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mWakeLockUids.remove(track->uid()); 2565462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mActiveTracksGeneration++; 2566bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("removeTracks_l removing track on session %d", track->sessionId()); 2567bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent sp<EffectChain> chain = getEffectChain_l(track->sessionId()); 2568bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (chain != 0) { 2569bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("stopping track on chain %p for session Id: %d", chain.get(), 2570bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->sessionId()); 2571bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent chain->decActiveTrackCnt(); 2572bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2573bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (track->isTerminated()) { 2574bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent removeTrack_l(track); 2575bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2576bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2577bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2578bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 2579bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 258081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2581accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurentstatus_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp) 2582accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent{ 2583accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent if (mNormalSink != 0) { 2584accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent return mNormalSink->getTimestamp(timestamp); 2585accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent } 2586accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent if (mType == OFFLOAD && mOutput->stream->get_presentation_position) { 2587accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent uint64_t position64; 2588accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent int ret = mOutput->stream->get_presentation_position( 2589accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent mOutput->stream, &position64, ×tamp.mTime); 2590accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent if (ret == 0) { 2591accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent timestamp.mPosition = (uint32_t)position64; 2592accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent return NO_ERROR; 2593accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent } 2594accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent } 2595accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent return INVALID_OPERATION; 2596accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent} 259781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 259881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 259981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, 260081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t id, audio_devices_t device, type_t type) 260181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent : PlaybackThread(audioFlinger, output, id, device, type), 260281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mAudioMixer below 260381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mFastMixer below 260481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixerFutex(0) 260581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mOutputSink below 260681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mPipeSink below 260781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mNormalSink below 260881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 260981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type); 2610f6ed423af92a56ef54bba23eba883b1f21448b54Glenn Kasten ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%d, mFrameSize=%u, " 261181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "mFrameCount=%d, mNormalFrameCount=%d", 261281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, 261381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalFrameCount); 261481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); 261581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 261681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME - Current mixer implementation only supports stereo output 261781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mChannelCount != FCC_2) { 261881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGE("Invalid audio hardware channel count %d", mChannelCount); 261981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 262081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 262181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create an NBAIO sink for the HAL output stream, and negotiate 262281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutputSink = new AudioStreamOutSink(output->stream); 262381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t numCounterOffers = 0; 2624f69f9869514730aebe5724c461768507084dfff7Glenn Kasten const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; 262581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ssize_t index = mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); 262681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(index == 0); 262781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 262881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // initialize fast mixer depending on configuration 262981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool initFastMixer; 263081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent switch (kUseFastMixer) { 263181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Never: 263281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent initFastMixer = false; 263381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 263481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Always: 263581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent initFastMixer = true; 263681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 263781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Static: 263881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Dynamic: 263981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent initFastMixer = mFrameCount < mNormalFrameCount; 264081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 264181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 264281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (initFastMixer) { 264381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 264481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create a MonoPipe to connect our submix to FastMixer 264581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent NBAIO_Format format = mOutputSink->format(); 264681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // This pipe depth compensates for scheduling latency of the normal mixer thread. 264781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // When it wakes up after a maximum latency, it runs a few cycles quickly before 264881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // finally blocking. Note the pipe implementation rounds up the request to a power of 2. 264981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); 265081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const NBAIO_Format offers[1] = {format}; 265181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t numCounterOffers = 0; 265281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ssize_t index = monoPipe->negotiate(offers, 1, NULL, numCounterOffers); 265381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(index == 0); 265481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent monoPipe->setAvgFrames((mScreenState & 1) ? 265581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); 265681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPipeSink = monoPipe; 265781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 265846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 2659da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (mTeeSinkOutputEnabled) { 2660da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten // create a Pipe to archive a copy of FastMixer's output for dumpsys 2661da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten Pipe *teeSink = new Pipe(mTeeSinkOutputFrames, format); 2662da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten numCounterOffers = 0; 2663da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten index = teeSink->negotiate(offers, 1, NULL, numCounterOffers); 2664da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten ALOG_ASSERT(index == 0); 2665da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten mTeeSink = teeSink; 2666da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten PipeReader *teeSource = new PipeReader(*teeSink); 2667da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten numCounterOffers = 0; 2668da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten index = teeSource->negotiate(offers, 1, NULL, numCounterOffers); 2669da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten ALOG_ASSERT(index == 0); 2670da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten mTeeSource = teeSource; 2671da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 267246909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 267381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 267481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create fast mixer and configure it initially with just one fast track for our submix 267581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixer = new FastMixer(); 267681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 267781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef STATE_QUEUE_DUMP 267881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->setObserverDump(&mStateQueueObserverDump); 267981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->setMutatorDump(&mStateQueueMutatorDump); 268081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 268181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerState *state = sq->begin(); 268281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastTrack *fastTrack = &state->mFastTracks[0]; 268381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // wrap the source side of the MonoPipe to make it an AudioBufferProvider 268481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); 268581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mVolumeProvider = NULL; 268681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mGeneration++; 268781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mFastTracksGen++; 268881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mTrackMask = 1; 268981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // fast mixer will use the HAL output sink 269081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mOutputSink = mOutputSink.get(); 269181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mOutputSinkGen++; 269281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mFrameCount = mFrameCount; 269381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand = FastMixerState::COLD_IDLE; 269481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // already done in constructor initialization list 269581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent //mFastMixerFutex = 0; 269681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mColdFutexAddr = &mFastMixerFutex; 269781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mColdGen++; 269881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mDumpState = &mFastMixerDumpState; 269946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 270081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mTeeSink = mTeeSink.get(); 270146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 27029e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer"); 27039e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten state->mNBLogWriter = mFastMixerNBLogWriter.get(); 270481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(); 270581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); 270681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 270781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // start the fast mixer 270881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); 270981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t tid = mFastMixer->getTid(); 271081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int err = requestPriority(getpid_cached, tid, kPriorityFastMixer); 271181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (err != 0) { 271281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", 271381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent kPriorityFastMixer, getpid_cached, tid, err); 271481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 271581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 271681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG 271781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create and start the watchdog 271881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog = new AudioWatchdog(); 271981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->setDump(&mAudioWatchdogDump); 272081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); 272181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent tid = mAudioWatchdog->getTid(); 272281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent err = requestPriority(getpid_cached, tid, kPriorityFastMixer); 272381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (err != 0) { 272481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", 272581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent kPriorityFastMixer, getpid_cached, tid, err); 272681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 272781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 272881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 272981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 273081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixer = NULL; 273181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 273281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 273381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent switch (kUseFastMixer) { 273481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Never: 273581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Dynamic: 273681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink = mOutputSink; 273781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 273881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Always: 273981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink = mPipeSink; 274081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 274181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Static: 274281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink = initFastMixer ? mPipeSink : mOutputSink; 274381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 274481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 274581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 274681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 274781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::MixerThread::~MixerThread() 274881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 274981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mFastMixer != NULL) { 275081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 275181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerState *state = sq->begin(); 275281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (state->mCommand == FastMixerState::COLD_IDLE) { 275381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int32_t old = android_atomic_inc(&mFastMixerFutex); 275481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (old == -1) { 2755110135b9610fee8bfa5bac2d9ef4fc4c24991c36Glenn Kasten (void) __futex_syscall3(&mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); 275681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 275781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 275881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand = FastMixerState::EXIT; 275981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(); 276081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); 276181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixer->join(); 276281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Though the fast mixer thread has exited, it's state queue is still valid. 276381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // We'll use that extract the final state which contains one remaining fast track 276481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // corresponding to our sub-mix. 276581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state = sq->begin(); 276681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(state->mTrackMask == 1); 276781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastTrack *fastTrack = &state->mFastTracks[0]; 276881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(fastTrack->mBufferProvider != NULL); 276981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent delete fastTrack->mBufferProvider; 277081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(false /*didModify*/); 277181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent delete mFastMixer; 277281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG 277381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mAudioWatchdog != 0) { 277481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->requestExit(); 277581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->requestExitAndWait(); 277681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog.clear(); 277781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 277881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 277981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 27809e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter); 278181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent delete mAudioMixer; 278281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 278381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 278481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 278581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const 278681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 278781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mFastMixer != NULL) { 278881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); 278981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent latency += (pipe->getAvgFrames() * 1000) / mSampleRate; 279081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 279181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return latency; 279281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 279381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 279481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 279581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) 279681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 279781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread::threadLoop_removeTracks(tracksToRemove); 279881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 279981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2800bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentssize_t AudioFlinger::MixerThread::threadLoop_write() 280181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 280281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME we should only do one push per cycle; confirm this is true 280381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Start the fast mixer if it's not already running 280481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mFastMixer != NULL) { 280581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 280681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerState *state = sq->begin(); 280781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (state->mCommand != FastMixerState::MIX_WRITE && 280881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { 280981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (state->mCommand == FastMixerState::COLD_IDLE) { 281081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int32_t old = android_atomic_inc(&mFastMixerFutex); 281181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (old == -1) { 2812110135b9610fee8bfa5bac2d9ef4fc4c24991c36Glenn Kasten (void) __futex_syscall3(&mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); 281381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 281481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG 281581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mAudioWatchdog != 0) { 281681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->resume(); 281781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 281881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 281981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 282081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand = FastMixerState::MIX_WRITE; 28214182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? 28224182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten FastMixerDumpState::kSamplingNforLowRamDevice : FastMixerDumpState::kSamplingN); 282381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(); 282481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); 282581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (kUseFastMixer == FastMixer_Dynamic) { 282681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink = mPipeSink; 282781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 282881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 282981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(false /*didModify*/); 283081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 283181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 2832bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return PlaybackThread::threadLoop_write(); 283381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 283481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 283581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::threadLoop_standby() 283681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 283781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Idle the fast mixer if it's currently running 283881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mFastMixer != NULL) { 283981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 284081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerState *state = sq->begin(); 284181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!(state->mCommand & FastMixerState::IDLE)) { 284281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand = FastMixerState::COLD_IDLE; 284381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mColdFutexAddr = &mFastMixerFutex; 284481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mColdGen++; 284581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixerFutex = 0; 284681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(); 284781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now 284881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); 284981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (kUseFastMixer == FastMixer_Dynamic) { 285081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink = mOutputSink; 285181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 285281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG 285381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mAudioWatchdog != 0) { 285481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->pause(); 285581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 285681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 285781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 285881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(false /*didModify*/); 285981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 286081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 286181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread::threadLoop_standby(); 286281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 286381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2864bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::PlaybackThread::waitingAsyncCallback_l() 2865bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 2866bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return false; 2867bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 2868bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 2869bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::PlaybackThread::shouldStandby_l() 2870bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 2871bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return !mStandby; 2872bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 2873bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 2874bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::PlaybackThread::waitingAsyncCallback() 2875bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 2876bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 2877bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return waitingAsyncCallback_l(); 2878bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 2879bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 288081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// shared by MIXER and DIRECT, overridden by DUPLICATING 288181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::threadLoop_standby() 288281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 288381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended); 288481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutput->stream->common.standby(&mOutput->stream->common); 2885bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mUseAsyncWrite != 0) { 28863b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // discard any pending drain or write ack by incrementing sequence 28873b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence = (mWriteAckSequence + 2) & ~1; 28883b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence = (mDrainSequence + 2) & ~1; 2889bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 28903b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setWriteBlocked(mWriteAckSequence); 28913b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setDraining(mDrainSequence); 2892bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 289381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 289481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 28954c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew Georgevoid AudioFlinger::PlaybackThread::onAddNewTrack_l() 28964c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George{ 28974c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George ALOGV("signal playback thread"); 28984c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George broadcast_l(); 28994c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George} 29004c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George 290181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::threadLoop_mix() 290281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 290381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // obtain the presentation timestamp of the next output buffer 290481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int64_t pts; 290581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status = INVALID_OPERATION; 290681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 290781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mNormalSink != 0) { 290881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = mNormalSink->getNextWriteTimestamp(&pts); 290981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 291081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = mOutputSink->getNextWriteTimestamp(&pts); 291181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 291281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 291381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (status != NO_ERROR) { 291481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pts = AudioBufferProvider::kInvalidPTS; 291581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 291681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 291781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mix buffers... 291881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->process(pts); 291925c2dac12114699e90deb1c579cadebce7b91a97Andy Hung mCurrentWriteLength = mSinkBufferSize; 292081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // increase sleep time progressively when application underrun condition clears. 292181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Only increase sleep time if the mixer is ready for two consecutive times to avoid 292281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // that a steady state of alternating ready/not ready conditions keeps the sleep time 292381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // such that we would underrun the audio HAL. 292481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((sleepTime == 0) && (sleepTimeShift > 0)) { 292581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTimeShift--; 292681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 292781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = 0; 292881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent standbyTime = systemTime() + standbyDelay; 292981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent //TODO: delay standby when effects have a tail 293081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 293181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 293281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::threadLoop_sleepTime() 293381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 293481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If no tracks are ready, sleep once for the duration of an output 293581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // buffer size, then write 0s to the output 293681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sleepTime == 0) { 293781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mMixerStatus == MIXER_TRACKS_ENABLED) { 293881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = activeSleepTime >> sleepTimeShift; 293981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sleepTime < kMinThreadSleepTimeUs) { 294081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = kMinThreadSleepTimeUs; 294181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 294281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // reduce sleep time in case of consecutive application underruns to avoid 294381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer 294481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // duration we would end up writing less data than needed by the audio HAL if 294581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // the condition persists. 294681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sleepTimeShift < kMaxThreadSleepTimeShift) { 294781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTimeShift++; 294881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 294981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 295081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = idleSleepTime; 295181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 295281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { 295398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared 295498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // before effects processing or output. 295598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mMixerBufferValid) { 295698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung memset(mMixerBuffer, 0, mMixerBufferSize); 295798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } else { 295898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung memset(mSinkBuffer, 0, mSinkBufferSize); 295998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 296081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = 0; 296181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), 296281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "anticipated start"); 296381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 296481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // TODO add standby time extension fct of effect tail 296581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 296681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 296781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// prepareTracks_l() must be called with ThreadBase::mLock held 296881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( 296981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<Track> > *tracksToRemove) 297081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 297181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 297281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixer_state mixerStatus = MIXER_IDLE; 297381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // find out which tracks need to be processed 297481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t count = mActiveTracks.size(); 297581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t mixedTracks = 0; 297681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t tracksWithEffect = 0; 297781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // counts only _active_ fast tracks 297881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t fastTracks = 0; 297981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset 298081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 298181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent float masterVolume = mMasterVolume; 298281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool masterMute = mMasterMute; 298381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 298481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (masterMute) { 298581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent masterVolume = 0; 298681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 298781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Delegate master volume control to effect in output mix effect chain if needed 298881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); 298981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 299081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t v = (uint32_t)(masterVolume * (1 << 24)); 299181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setVolume_l(&v, &v); 299281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent masterVolume = (float)((v + (1 << 23)) >> 24); 299381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain.clear(); 299481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 299581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 299681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // prepare a new state to push 299781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue *sq = NULL; 299881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerState *state = NULL; 299981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool didModify = false; 300081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; 300181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mFastMixer != NULL) { 300281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq = mFastMixer->sq(); 300381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state = sq->begin(); 300481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 300581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 300669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferValid = false; // mMixerBuffer has no valid data until appropriate tracks found. 300798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found. 300869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung 300981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i=0 ; i<count ; i++) { 30109fdcb0a9497ca290bcf364b10868587b6bde3a34Glenn Kasten const sp<Track> t = mActiveTracks[i].promote(); 301181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (t == 0) { 301281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 301381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 301481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 301581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // this const just means the local variable doesn't change 301681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Track* const track = t.get(); 301781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 301881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // process fast tracks 301981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isFastTrack()) { 302081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 302181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // It's theoretically possible (though unlikely) for a fast track to be created 302281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // and then removed within the same normal mix cycle. This is not a problem, as 302381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // the track never becomes active so it's fast mixer slot is never touched. 302481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The converse, of removing an (active) track and then creating a new track 302581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // at the identical fast mixer slot within the same normal mix cycle, 302681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // is impossible because the slot isn't marked available until the end of each cycle. 302781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int j = track->mFastIndex; 302881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(0 < j && j < (int)FastMixerState::kMaxFastTracks); 302981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); 303081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastTrack *fastTrack = &state->mFastTracks[j]; 303181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 303281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Determine whether the track is currently in underrun condition, 303381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // and whether it had a recent underrun. 303481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; 303581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastTrackUnderruns underruns = ftDump->mUnderruns; 303681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t recentFull = (underruns.mBitFields.mFull - 303781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK; 303881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t recentPartial = (underruns.mBitFields.mPartial - 303981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK; 304081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t recentEmpty = (underruns.mBitFields.mEmpty - 304181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK; 304281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t recentUnderruns = recentPartial + recentEmpty; 304381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mObservedUnderruns = underruns; 304481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // don't count underruns that occur while stopping or pausing 304581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // or stopped which can occur when flush() is called while active 304682aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten if (!(track->isStopping() || track->isPausing() || track->isStopped()) && 304782aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten recentUnderruns > 0) { 304882aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun 304982aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten track->mAudioTrackServerProxy->tallyUnderrunFrames(recentUnderruns * mFrameCount); 305081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 305181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 305281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // This is similar to the state machine for normal tracks, 305381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // with a few modifications for fast tracks. 305481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool isActive = true; 305581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent switch (track->mState) { 305681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::STOPPING_1: 305781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track stays active in STOPPING_1 state until first underrun 3058bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (recentUnderruns > 0 || track->isTerminated()) { 305981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mState = TrackBase::STOPPING_2; 306081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 306181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 306281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::PAUSING: 306381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // ramp down is not yet implemented 306481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->setPaused(); 306581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 306681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::RESUMING: 306781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // ramp up is not yet implemented 306881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mState = TrackBase::ACTIVE; 306981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 307081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::ACTIVE: 307181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (recentFull > 0 || recentPartial > 0) { 307281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track has provided at least some frames recently: reset retry count 307381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mRetryCount = kMaxTrackRetries; 307481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 307581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (recentUnderruns == 0) { 307681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // no recent underruns: stay active 307781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 307881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 307981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // there has recently been an underrun of some kind 308081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->sharedBuffer() == 0) { 308181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // were any of the recent underruns "empty" (no frames available)? 308281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (recentEmpty == 0) { 308381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // no, then ignore the partial underruns as they are allowed indefinitely 308481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 308581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 308681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // there has recently been an "empty" underrun: decrement the retry counter 308781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (--(track->mRetryCount) > 0) { 308881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 308981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 309081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // indicate to client process that the track was disabled because of underrun; 309181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it will then automatically call start() when data is available 309296f60d8f04432a1ed503b3e24d5736d28c63c9a2Glenn Kasten android_atomic_or(CBLK_DISABLED, &track->mCblk->mFlags); 309381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // remove from active list, but state remains ACTIVE [confusing but true] 309481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent isActive = false; 309581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 309681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 309781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // fall through 309881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::STOPPING_2: 309981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::PAUSED: 310081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::STOPPED: 310181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::FLUSHED: // flush() while active 310281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Check for presentation complete if track is inactive 310381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // We have consumed all the buffers of this track. 310481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // This would be incomplete if we auto-paused on underrun 310581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 310681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t audioHALFrames = 310781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; 310881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t framesWritten = mBytesWritten / mFrameSize; 310981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { 311081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track stays in active list until presentation is complete 311181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 311281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 311381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 311481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isStopping_2()) { 311581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mState = TrackBase::STOPPED; 311681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 311781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isStopped()) { 311881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Can't reset directly, as fast mixer is still polling this track 311981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track->reset(); 312081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // So instead mark this track as needing to be reset after push with ack 312181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent resetMask |= 1 << i; 312281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 312381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent isActive = false; 312481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 312581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::IDLE: 312681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent default: 3127adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("unexpected track state %d", track->mState); 312881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 312981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 313081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (isActive) { 313181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // was it previously inactive? 313281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!(state->mTrackMask & (1 << j))) { 313381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ExtendedAudioBufferProvider *eabp = track; 313481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent VolumeProvider *vp = track; 313581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mBufferProvider = eabp; 313681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mVolumeProvider = vp; 313781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mChannelMask = track->mChannelMask; 313881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mGeneration++; 313981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mTrackMask |= 1 << j; 314081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent didModify = true; 314181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // no acknowledgement required for newly active tracks 314281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 314381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // cache the combined master volume and stream type volume for fast mixer; this 314481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // lacks any synchronization or barrier so VolumeProvider may read a stale value 3145e4756fe3a387615acb63c6a05788c8db9b5786cbGlenn Kasten track->mCachedVolume = masterVolume * mStreamTypes[track->streamType()].volume; 314681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ++fastTracks; 314781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 314881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // was it previously active? 314981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (state->mTrackMask & (1 << j)) { 315081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mBufferProvider = NULL; 315181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mGeneration++; 315281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mTrackMask &= ~(1 << j); 315381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent didModify = true; 315481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If any fast tracks were removed, we must wait for acknowledgement 315581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // because we're about to decrement the last sp<> on those tracks. 315681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; 315781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 3158adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("fast track %d should have been active", j); 315981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 316081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent tracksToRemove->add(track); 316181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Avoids a misleading display in dumpsys 316281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL; 316381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 316481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 316581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 316681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 316781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // local variable scope to avoid goto warning 316881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 316981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_track_cblk_t* cblk = track->cblk(); 317081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 317181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The first time a track is added we wait 317281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // for all its buffers to be filled before processing it 317381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int name = track->name(); 317481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // make sure that we have enough frames to mix one full buffer. 317581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // enforce this condition only once to enable draining the buffer in case the client 317681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // app does not call stop() and relies on underrun to stop: 317781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed 317881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // during last round 31799f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten size_t desiredFrames; 31809fdcb0a9497ca290bcf364b10868587b6bde3a34Glenn Kasten uint32_t sr = track->sampleRate(); 31819fdcb0a9497ca290bcf364b10868587b6bde3a34Glenn Kasten if (sr == mSampleRate) { 31829f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten desiredFrames = mNormalFrameCount; 31839f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } else { 31849f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // +1 for rounding and +1 for additional sample needed for interpolation 31859fdcb0a9497ca290bcf364b10868587b6bde3a34Glenn Kasten desiredFrames = (mNormalFrameCount * sr) / mSampleRate + 1 + 1; 31869f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // add frames already consumed but not yet released by the resampler 31872fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten // because mAudioTrackServerProxy->framesReady() will include these frames 31889f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten desiredFrames += mAudioMixer->getUnreleasedFrames(track->name()); 318974935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten#if 0 31909f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // the minimum track buffer size is normally twice the number of frames necessary 31919f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // to fill one buffer and the resampler should not leave more than one buffer worth 31929f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // of unreleased frames after each pass, but just in case... 31939f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten ALOG_ASSERT(desiredFrames <= cblk->frameCount_); 319474935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten#endif 31959f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 319681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t minFrames = 1; 319781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && 319881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { 31999f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten minFrames = desiredFrames; 32009f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 320113e4c960ea3db03a43e084fbd85d52aa77f7b871Eric Laurent 320213e4c960ea3db03a43e084fbd85d52aa77f7b871Eric Laurent size_t framesReady = track->framesReady(); 32039f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if ((framesReady >= minFrames) && track->isReady() && 320481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent !track->isPaused() && !track->isTerminated()) 320581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 3206f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten ALOGVV("track %d s=%08x [OK] on thread %p", name, cblk->mServer, this); 320781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 320881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixedTracks++; 320981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 321069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // track->mainBuffer() != mSinkBuffer or mMixerBuffer means 321169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // there is an effect chain connected to the track 321281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain.clear(); 321369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung if (track->mainBuffer() != mSinkBuffer && 321469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung track->mainBuffer() != mMixerBuffer) { 321598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mEffectBufferEnabled) { 321698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferValid = true; // Later can set directly. 321798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 321881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain = getEffectChain_l(track->sessionId()); 321981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Delegate volume control to effect in track effect chain if needed 322081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 322181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent tracksWithEffect++; 322281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 322381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on " 322481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "session %d", 322581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent name, track->sessionId()); 322681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 322781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 322881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 322981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 323081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int param = AudioMixer::VOLUME; 323181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->mFillingUpStatus == Track::FS_FILLED) { 323281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // no ramp for the first volume setting 323381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mFillingUpStatus = Track::FS_ACTIVE; 323481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->mState == TrackBase::RESUMING) { 323581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mState = TrackBase::ACTIVE; 323681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent param = AudioMixer::RAMP_VOLUME; 323781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 323881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); 3239f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten // FIXME should not make a decision based on mServer 3240f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten } else if (cblk->mServer != 0) { 324181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If the track is stopped before the first frame was mixed, 324281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // do not apply ramp 324381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent param = AudioMixer::RAMP_VOLUME; 324481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 324581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 324681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // compute volume for this track 324781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t vl, vr, va; 3248e4756fe3a387615acb63c6a05788c8db9b5786cbGlenn Kasten if (track->isPausing() || mStreamTypes[track->streamType()].mute) { 324981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent vl = vr = va = 0; 325081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isPausing()) { 325181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->setPaused(); 325281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 325381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 325481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 325581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // read original volumes with volume control 325681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent float typeVolume = mStreamTypes[track->streamType()].volume; 325781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent float v = masterVolume * typeVolume; 32589f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; 3259c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten gain_minifloat_packed_t vlr = proxy->getVolumeLR(); 3260c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten float vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); 3261c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten float vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); 326281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track volumes come from shared memory, so can't be trusted and must be clamped 3263c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten if (vlf > GAIN_FLOAT_UNITY) { 3264c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten ALOGV("Track left volume out of range: %.3g", vlf); 3265c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten vlf = GAIN_FLOAT_UNITY; 326681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 3267c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten if (vrf > GAIN_FLOAT_UNITY) { 3268c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten ALOGV("Track right volume out of range: %.3g", vrf); 3269c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten vrf = GAIN_FLOAT_UNITY; 327081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 327181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // now apply the master volume and stream type volume 3272c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten // FIXME we're losing the wonderful dynamic range in the minifloat representation 3273c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten float v8_24 = v * (MAX_GAIN_INT * MAX_GAIN_INT); 3274c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten vl = (uint32_t) (v8_24 * vlf); 3275c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten vr = (uint32_t) (v8_24 * vrf); 327681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // assuming master volume and stream type volume each go up to 1.0, 327781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // vl and vr are now in 8.24 format 327881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3279e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten uint16_t sendLevel = proxy->getSendLevel_U4_12(); 328081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // send level comes from shared memory and so may be corrupt 328181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sendLevel > MAX_GAIN_INT) { 328281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("Track send level out of range: %04X", sendLevel); 328381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sendLevel = MAX_GAIN_INT; 328481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 328581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent va = (uint32_t)(v * sendLevel); 328681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 3287bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 328881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Delegate volume control to effect in track effect chain if needed 328981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0 && chain->setVolume_l(&vl, &vr)) { 329081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Do not ramp volume if volume is controlled by effect 329181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent param = AudioMixer::VOLUME; 329281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mHasVolumeController = true; 329381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 329481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // force no volume ramp when volume controller was just disabled or removed 329581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // from effect chain to avoid volume spike 329681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->mHasVolumeController) { 329781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent param = AudioMixer::VOLUME; 329881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 329981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mHasVolumeController = false; 330081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 330181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3302c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten // FIXME Use float 330381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Convert volumes from 8.24 to 4.12 format 330481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // This additional clamping is needed in case chain->setVolume_l() overshot 330581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent vl = (vl + (1 << 11)) >> 12; 330681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (vl > MAX_GAIN_INT) { 330781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent vl = MAX_GAIN_INT; 330881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 330981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent vr = (vr + (1 << 11)) >> 12; 331081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (vr > MAX_GAIN_INT) { 331181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent vr = MAX_GAIN_INT; 331281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 331381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 331481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (va > MAX_GAIN_INT) { 331581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent va = MAX_GAIN_INT; // va is uint32_t, so no need to check for - 331681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 331781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 331881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // XXX: these things DON'T need to be done each time 331981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->setBufferProvider(name, track); 332081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->enable(name); 332181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3322377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, (void *)(uintptr_t)vl); 3323377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, (void *)(uintptr_t)vr); 3324377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, (void *)(uintptr_t)va); 332581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->setParameter( 332681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent name, 332781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::TRACK, 332881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::FORMAT, (void *)track->format()); 332981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->setParameter( 333081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent name, 333181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::TRACK, 3332377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask()); 3333e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten // limit track sample rate to 2 x output sample rate, which changes at re-configuration 3334e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten uint32_t maxSampleRate = mSampleRate * 2; 33359f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten uint32_t reqSampleRate = track->mAudioTrackServerProxy->getSampleRate(); 3336e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten if (reqSampleRate == 0) { 3337e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten reqSampleRate = mSampleRate; 3338e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten } else if (reqSampleRate > maxSampleRate) { 3339e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten reqSampleRate = maxSampleRate; 3340e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten } 334181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->setParameter( 334281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent name, 334381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::RESAMPLE, 334481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::SAMPLE_RATE, 3345377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT (void *)(uintptr_t)reqSampleRate); 334669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung /* 334769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * Select the appropriate output buffer for the track. 334869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * 334998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung * Tracks with effects go into their own effects chain buffer 335098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung * and from there into either mEffectBuffer or mSinkBuffer. 335169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * 335269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * Other tracks can use mMixerBuffer for higher precision 335369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * channel accumulation. If this buffer is enabled 335469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * (mMixerBufferEnabled true), then selected tracks will accumulate 335569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * into it. 335669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * 335769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung */ 335869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung if (mMixerBufferEnabled 335969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung && (track->mainBuffer() == mSinkBuffer 336069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung || track->mainBuffer() == mMixerBuffer)) { 336169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mAudioMixer->setParameter( 336269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung name, 336369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung AudioMixer::TRACK, 3364788207057ed4b8df4719ed8089f376ef52de9ca1Andy Hung AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat); 336569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mAudioMixer->setParameter( 336669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung name, 336769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung AudioMixer::TRACK, 336869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer); 336969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // TODO: override track->mainBuffer()? 337069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferValid = true; 337169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung } else { 337269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mAudioMixer->setParameter( 337369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung name, 337469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung AudioMixer::TRACK, 3375788207057ed4b8df4719ed8089f376ef52de9ca1Andy Hung AudioMixer::MIXER_FORMAT, (void *)AUDIO_FORMAT_PCM_16_BIT); 337669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mAudioMixer->setParameter( 337769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung name, 337869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung AudioMixer::TRACK, 337969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); 338069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung } 338181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->setParameter( 338281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent name, 338381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::TRACK, 338481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); 338581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 338681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // reset retry count 338781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mRetryCount = kMaxTrackRetries; 338881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 338981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If one track is ready, set the mixer ready if: 339081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // - the mixer was not ready during previous round OR 339181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // - no other track is not ready 339281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || 339381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixerStatus != MIXER_TRACKS_ENABLED) { 339481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixerStatus = MIXER_TRACKS_READY; 339581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 339681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 33979f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { 339882aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten track->mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames); 33999f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 340081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // clear effect chain input buffer if an active track underruns to avoid sending 340181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // previous audio buffer again to effects 340281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain = getEffectChain_l(track->sessionId()); 340381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 340481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->clearInputBuffer(); 340581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 340681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3407f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten ALOGVV("track %d s=%08x [NOT READY] on thread %p", name, cblk->mServer, this); 340881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((track->sharedBuffer() != 0) || track->isTerminated() || 340981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->isStopped() || track->isPaused()) { 341081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // We have consumed all the buffers of this track. 341181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Remove it from the list of active tracks. 341281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // TODO: use actual buffer filling status instead of latency when available from 341381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // audio HAL 341481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; 341581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t framesWritten = mBytesWritten / mFrameSize; 341681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { 341781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isStopped()) { 341881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->reset(); 341981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 342081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent tracksToRemove->add(track); 342181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 342281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 342381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // No buffers for this track. Give it a few chances to 342481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // fill a buffer, then remove it from active list. 342581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (--(track->mRetryCount) <= 0) { 3426c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten ALOGI("BUFFER TIMEOUT: remove(%d) from active list on thread %p", name, this); 342781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent tracksToRemove->add(track); 342881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // indicate to client process that the track was disabled because of underrun; 342981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it will then automatically call start() when data is available 343096f60d8f04432a1ed503b3e24d5736d28c63c9a2Glenn Kasten android_atomic_or(CBLK_DISABLED, &cblk->mFlags); 343181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If one track is not ready, mark the mixer also not ready if: 343281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // - the mixer was ready during previous round OR 343381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // - no other track is ready 343481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || 343581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixerStatus != MIXER_TRACKS_READY) { 343681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixerStatus = MIXER_TRACKS_ENABLED; 343781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 343881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 343981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->disable(name); 344081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 344181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 344281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } // local variable scope to avoid goto warning 344381784c37c61b09289654b979567a42bf73cd2b12Eric Laurenttrack_is_ready: ; 344481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 344581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 344681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 344781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Push the new FastMixer state if necessary 344881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool pauseAudioWatchdog = false; 344981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (didModify) { 345081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mFastTracksGen++; 345181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle 345281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (kUseFastMixer == FastMixer_Dynamic && 345381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { 345481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand = FastMixerState::COLD_IDLE; 345581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mColdFutexAddr = &mFastMixerFutex; 345681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mColdGen++; 345781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixerFutex = 0; 345881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (kUseFastMixer == FastMixer_Dynamic) { 345981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink = mOutputSink; 346081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 346181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If we go into cold idle, need to wait for acknowledgement 346281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // so that fast mixer stops doing I/O. 346381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; 346481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pauseAudioWatchdog = true; 346581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 346681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 346781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sq != NULL) { 346881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(didModify); 346981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->push(block); 347081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 347181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG 347281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (pauseAudioWatchdog && mAudioWatchdog != 0) { 347381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->pause(); 347481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 347581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 347681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 347781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Now perform the deferred reset on fast tracks that have stopped 347881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent while (resetMask != 0) { 347981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t i = __builtin_ctz(resetMask); 348081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(i < count); 348181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent resetMask &= ~(1 << i); 348281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> t = mActiveTracks[i].promote(); 348381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (t == 0) { 348481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 348581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 348681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Track* track = t.get(); 348781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(track->isFastTrack() && track->isStopped()); 348881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->reset(); 348981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 349081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 349181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // remove all the tracks that need to be... 3492bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent removeTracks_l(*tracksToRemove); 349381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 349469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // sink or mix buffer must be cleared if all tracks are connected to an 349569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // effect chain as in this case the mixer will not write to the sink or mix buffer 349669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // and track effects will accumulate into it 3497bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || 3498bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent (mixedTracks == 0 && fastTracks > 0))) { 349981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME as a performance optimization, should remember previous zero status 350069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung if (mMixerBufferValid) { 350169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung memset(mMixerBuffer, 0, mMixerBufferSize); 350269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // TODO: In testing, mSinkBuffer below need not be cleared because 350369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer 350469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // after mixing. 350569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // 350669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // To enforce this guarantee: 350769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || 350869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // (mixedTracks == 0 && fastTracks > 0)) 350969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // must imply MIXER_TRACKS_READY. 351069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // Later, we may clear buffers regardless, and skip much of this logic. 351169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung } 351298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // TODO - either mEffectBuffer or mSinkBuffer needs to be cleared. 351398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mEffectBufferValid) { 351498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung memset(mEffectBuffer, 0, mEffectBufferSize); 351598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 351698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // FIXME as a performance optimization, should remember previous zero status 35172098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung memset(mSinkBuffer, 0, mNormalFrameCount * mChannelCount * sizeof(int16_t)); 351881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 351981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 352081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if any fast tracks, then status is ready 352181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMixerStatusIgnoringFastTracks = mixerStatus; 352281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (fastTracks > 0) { 352381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixerStatus = MIXER_TRACKS_READY; 352481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 352581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mixerStatus; 352681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 352781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 352881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// getTrackName_l() must be called with ThreadBase::mLock held 352981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentint AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask, int sessionId) 353081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 353181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mAudioMixer->getTrackName(channelMask, sessionId); 353281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 353381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 353481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// deleteTrackName_l() must be called with ThreadBase::mLock held 353581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::deleteTrackName_l(int name) 353681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 353781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("remove track (%d) and delete from mixer", name); 353881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->deleteTrackName(name); 353981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 354081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 35411035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// checkForNewParameter_l() must be called with ThreadBase::mLock held 35421035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentbool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair, 35431035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status_t& status) 354481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 354581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool reconfig = false; 354681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 35471035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = NO_ERROR; 354881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 35491035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // if !&IDLE, holds the FastMixer state to restore after new parameters processed 35501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; 35511035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (mFastMixer != NULL) { 35521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 35531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent FastMixerState *state = sq->begin(); 35541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (!(state->mCommand & FastMixerState::IDLE)) { 35551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent previousCommand = state->mCommand; 35561035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent state->mCommand = FastMixerState::HOT_IDLE; 35571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sq->end(); 35581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); 35591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 35601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sq->end(false /*didModify*/); 356181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 35621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 356381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 35641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent AudioParameter param = AudioParameter(keyValuePair); 35651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent int value; 35661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { 35671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 35681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 35691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { 35701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if ((audio_format_t) value != AUDIO_FORMAT_PCM_16_BIT) { 35711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = BAD_VALUE; 35721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 35731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // no need to save value, since it's constant 357481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent reconfig = true; 357581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 35761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 35771035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { 35781035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if ((audio_channel_mask_t) value != AUDIO_CHANNEL_OUT_STEREO) { 35791035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = BAD_VALUE; 35801035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 35811035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // no need to save value, since it's constant 35821035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 358381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 35841035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 35851035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { 35861035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // do not accept frame count changes if tracks are open as the track buffer 35871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // size depends on frame count and correct behavior would not be guaranteed 35881035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // if frame count is changed after track creation 35891035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (!mTracks.isEmpty()) { 35901035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = INVALID_OPERATION; 35911035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 35921035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 359381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 35941035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 35951035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { 359681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef ADD_BATTERY_DATA 35971035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // when changing the audio output device, call addBatteryData to notify 35981035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // the change 35991035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (mOutDevice != value) { 36001035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent uint32_t params = 0; 36011035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // check whether speaker is on 36021035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (value & AUDIO_DEVICE_OUT_SPEAKER) { 36031035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent params |= IMediaPlayerService::kBatteryDataSpeakerOn; 36041035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 360581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 36061035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent audio_devices_t deviceWithoutSpeaker 36071035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; 36081035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // check if any other device (except speaker) is on 36091035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (value & deviceWithoutSpeaker ) { 36101035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; 36111035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 361281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 36131035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (params != 0) { 36141035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent addBatteryData(params); 361581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 36161035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 361781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 361881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 36191035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // forward device change to effects that have requested to be 36201035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // aware of attached audio device. 36211035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (value != AUDIO_DEVICE_NONE) { 36221035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mOutDevice = value; 36231035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 36241035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mEffectChains[i]->setDevice_l(mOutDevice); 362581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 362681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 36271035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 362881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 36291035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == NO_ERROR) { 36301035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = mOutput->stream->common.set_parameters(&mOutput->stream->common, 36311035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent keyValuePair.string()); 36321035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (!mStandby && status == INVALID_OPERATION) { 36331035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mOutput->stream->common.standby(&mOutput->stream->common); 36341035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mStandby = true; 36351035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mBytesWritten = 0; 363681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = mOutput->stream->common.set_parameters(&mOutput->stream->common, 36371035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent keyValuePair.string()); 36381035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 36391035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == NO_ERROR && reconfig) { 36401035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent readOutputParameters_l(); 36411035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent delete mAudioMixer; 36421035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); 36431035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent for (size_t i = 0; i < mTracks.size() ; i++) { 36441035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent int name = getTrackName_l(mTracks[i]->mChannelMask, mTracks[i]->mSessionId); 36451035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (name < 0) { 36461035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent break; 364781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 36481035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mTracks[i]->mName = name; 364981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 36501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sendIoConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); 365181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 365281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 365381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 365481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!(previousCommand & FastMixerState::IDLE)) { 365581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(mFastMixer != NULL); 365681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 365781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerState *state = sq->begin(); 365881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); 365981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand = previousCommand; 366081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(); 366181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); 366281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 366381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 366481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return reconfig; 366581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 366681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 366781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 366881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) 366981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 367081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const size_t SIZE = 256; 367181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char buffer[SIZE]; 367281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent String8 result; 367381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 367481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread::dumpInternals(fd, args); 367581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3676b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, " AudioMixer tracks: 0x%08x\n", mAudioMixer->trackNames()); 367781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 367881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Make a non-atomic copy of fast mixer dump state so it won't change underneath us 36794182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten const FastMixerDumpState copy(mFastMixerDumpState); 368081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent copy.dump(fd); 368181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 368281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef STATE_QUEUE_DUMP 368381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Similar for state queue 368481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent StateQueueObserverDump observerCopy = mStateQueueObserverDump; 368581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent observerCopy.dump(fd); 368681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; 368781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mutatorCopy.dump(fd); 368881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 368981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 369046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 369181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Write the tee output to a .wav file 369281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpTee(fd, mTeeSource, mId); 369346909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 369481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 369581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG 369681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mAudioWatchdog != 0) { 369781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Make a non-atomic copy of audio watchdog dump so it won't change underneath us 369881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioWatchdogDump wdCopy = mAudioWatchdogDump; 369981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent wdCopy.dump(fd); 370081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 370181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 370281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 370381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 370481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const 370581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 370681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; 370781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 370881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 370981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const 371081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 371181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); 371281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 371381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 371481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::cacheParameters_l() 371581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 371681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread::cacheParameters_l(); 371781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 371881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME: Relaxed timing because of a certain device that can't meet latency 371981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Should be reduced to 2x after the vendor fixes the driver issue 372081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // increase threshold again due to low power audio mode. The way this warning 372181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // threshold is calculated and its usefulness should be reconsidered anyway. 372281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; 372381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 372481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 372581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 372681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 372781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, 372881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device) 372981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent : PlaybackThread(audioFlinger, output, id, device, DIRECT) 373081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mLeftVolFloat, mRightVolFloat 373181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 373281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 373381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3734bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, 3735bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioStreamOut* output, audio_io_handle_t id, uint32_t device, 3736bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ThreadBase::type_t type) 3737bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent : PlaybackThread(audioFlinger, output, id, device, type) 3738bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // mLeftVolFloat, mRightVolFloat 3739bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 3740bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 3741bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 374281784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::DirectOutputThread::~DirectOutputThread() 374381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 374481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 374581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3746bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack) 3747bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 3748bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent audio_track_cblk_t* cblk = track->cblk(); 3749bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent float left, right; 3750bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 3751bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mMasterMute || mStreamTypes[track->streamType()].mute) { 3752bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent left = right = 0; 3753bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else { 3754bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent float typeVolume = mStreamTypes[track->streamType()].volume; 3755bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent float v = mMasterVolume * typeVolume; 3756bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; 3757c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten gain_minifloat_packed_t vlr = proxy->getVolumeLR(); 3758c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten left = float_from_gain(gain_minifloat_unpack_left(vlr)); 3759c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten if (left > GAIN_FLOAT_UNITY) { 3760c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten left = GAIN_FLOAT_UNITY; 3761c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten } 3762c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten left *= v; 3763c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten right = float_from_gain(gain_minifloat_unpack_right(vlr)); 3764c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten if (right > GAIN_FLOAT_UNITY) { 3765c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten right = GAIN_FLOAT_UNITY; 3766c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten } 3767c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten right *= v; 3768bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 3769bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 3770bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (lastTrack) { 3771bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (left != mLeftVolFloat || right != mRightVolFloat) { 3772bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mLeftVolFloat = left; 3773bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mRightVolFloat = right; 3774bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 3775bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Convert volumes from float to 8.24 3776bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent uint32_t vl = (uint32_t)(left * (1 << 24)); 3777bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent uint32_t vr = (uint32_t)(right * (1 << 24)); 3778bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 3779bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Delegate volume control to effect in track effect chain if needed 3780bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // only one effect chain can be present on DirectOutputThread, so if 3781bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // there is one, the track is connected to it 3782bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (!mEffectChains.isEmpty()) { 3783bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mEffectChains[0]->setVolume_l(&vl, &vr); 3784bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent left = (float)vl / (1 << 24); 3785bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent right = (float)vr / (1 << 24); 3786bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 3787bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mOutput->stream->set_volume) { 3788bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mOutput->stream->set_volume(mOutput->stream, left, right); 3789bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 3790bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 3791bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 3792bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 3793bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 3794bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 379581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( 379681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<Track> > *tracksToRemove 379781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent) 379881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 3799d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent size_t count = mActiveTracks.size(); 380081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixer_state mixerStatus = MIXER_IDLE; 380181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 380281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // find out which tracks need to be processed 3803d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent for (size_t i = 0; i < count; i++) { 3804d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent sp<Track> t = mActiveTracks[i].promote(); 380581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The track died recently 380681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (t == 0) { 3807d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent continue; 380881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 380981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 381081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Track* const track = t.get(); 381181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_track_cblk_t* cblk = track->cblk(); 3812fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // Only consider last track started for volume and mixer state control. 3813fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // In theory an older track could underrun and restart after the new one starts 3814fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // but as we only care about the transition phase between two tracks on a 3815fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // direct output, it is not a problem to ignore the underrun case. 3816fd4779740ec3e9e865d5514464df26d015354388Eric Laurent sp<Track> l = mLatestActiveTrack.promote(); 3817fd4779740ec3e9e865d5514464df26d015354388Eric Laurent bool last = l.get() == track; 381881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 381981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The first time a track is added we wait 382081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // for all its buffers to be filled before processing it 382181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t minFrames; 382281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing()) { 382381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minFrames = mNormalFrameCount; 382481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 382581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minFrames = 1; 382681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 3827bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 382881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((track->framesReady() >= minFrames) && track->isReady() && 382981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent !track->isPaused() && !track->isTerminated()) 383081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 3831f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten ALOGVV("track %d s=%08x [OK]", track->name(), cblk->mServer); 383281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 383381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->mFillingUpStatus == Track::FS_FILLED) { 383481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mFillingUpStatus = Track::FS_ACTIVE; 38351abbdb4429479975718421c4fef3f79ce7c820e3Eric Laurent // make sure processVolume_l() will apply new volume even if 0 38361abbdb4429479975718421c4fef3f79ce7c820e3Eric Laurent mLeftVolFloat = mRightVolFloat = -1.0; 383781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->mState == TrackBase::RESUMING) { 383881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mState = TrackBase::ACTIVE; 383981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 384081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 384181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 384281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // compute volume for this track 3843bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent processVolume_l(track, last); 3844bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (last) { 3845d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent // reset retry count 3846d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent track->mRetryCount = kMaxTrackRetriesDirect; 3847d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent mActiveTrack = t; 3848d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent mixerStatus = MIXER_TRACKS_READY; 3849d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent } 385081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 3851d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent // clear effect chain input buffer if the last active track started underruns 3852d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent // to avoid sending previous audio buffer again to effects 3853fd4779740ec3e9e865d5514464df26d015354388Eric Laurent if (!mEffectChains.isEmpty() && last) { 385481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains[0]->clearInputBuffer(); 385581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 385681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3857f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten ALOGVV("track %d s=%08x [NOT READY]", track->name(), cblk->mServer); 385881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((track->sharedBuffer() != 0) || track->isTerminated() || 385981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->isStopped() || track->isPaused()) { 386081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // We have consumed all the buffers of this track. 386181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Remove it from the list of active tracks. 386281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // TODO: implement behavior for compressed audio 386381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; 386481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t framesWritten = mBytesWritten / mFrameSize; 3865fd4779740ec3e9e865d5514464df26d015354388Eric Laurent if (mStandby || !last || 3866fd4779740ec3e9e865d5514464df26d015354388Eric Laurent track->presentationComplete(framesWritten, audioHALFrames)) { 386781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isStopped()) { 386881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->reset(); 386981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 3870d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent tracksToRemove->add(track); 387181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 387281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 387381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // No buffers for this track. Give it a few chances to 387481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // fill a buffer, then remove it from active list. 3875d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent // Only consider last track started for mixer state control 387681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (--(track->mRetryCount) <= 0) { 387781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); 3878d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent tracksToRemove->add(track); 3879a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent // indicate to client process that the track was disabled because of underrun; 3880a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent // it will then automatically call start() when data is available 3881a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent android_atomic_or(CBLK_DISABLED, &cblk->mFlags); 3882bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if (last) { 388381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixerStatus = MIXER_TRACKS_ENABLED; 388481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 388581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 388681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 388781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 388881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 388981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // remove all the tracks that need to be... 3890bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent removeTracks_l(*tracksToRemove); 389181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 389281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mixerStatus; 389381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 389481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 389581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DirectOutputThread::threadLoop_mix() 389681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 389781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t frameCount = mFrameCount; 38982098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung int8_t *curBuf = (int8_t *)mSinkBuffer; 389981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // output audio to hardware 390081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent while (frameCount) { 390134542acfa25c6413c87a94b6f7cc315a0c496277Glenn Kasten AudioBufferProvider::Buffer buffer; 390281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent buffer.frameCount = frameCount; 390381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mActiveTrack->getNextBuffer(&buffer); 3904fa319e6d918b84f93fb5457af5d1cca6421ac517Glenn Kasten if (buffer.raw == NULL) { 390581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent memset(curBuf, 0, frameCount * mFrameSize); 390681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 390781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 390881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); 390981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent frameCount -= buffer.frameCount; 391081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent curBuf += buffer.frameCount * mFrameSize; 391181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mActiveTrack->releaseBuffer(&buffer); 391281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 39132098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer; 391481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = 0; 391581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent standbyTime = systemTime() + standbyDelay; 391681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mActiveTrack.clear(); 391781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 391881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 391981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DirectOutputThread::threadLoop_sleepTime() 392081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 392181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sleepTime == 0) { 392281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mMixerStatus == MIXER_TRACKS_ENABLED) { 392381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = activeSleepTime; 392481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 392581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = idleSleepTime; 392681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 392781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) { 39282098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung memset(mSinkBuffer, 0, mFrameCount * mFrameSize); 392981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = 0; 393081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 393181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 393281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 393381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// getTrackName_l() must be called with ThreadBase::mLock held 39340f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenint AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask __unused, 39350f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten int sessionId __unused) 393681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 393781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 393881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 393981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 394081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// deleteTrackName_l() must be called with ThreadBase::mLock held 39410f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::DirectOutputThread::deleteTrackName_l(int name __unused) 394281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 394381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 394481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 39451035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// checkForNewParameter_l() must be called with ThreadBase::mLock held 39461035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentbool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair, 39471035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status_t& status) 394881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 394981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool reconfig = false; 395081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 39511035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = NO_ERROR; 39521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 39531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent AudioParameter param = AudioParameter(keyValuePair); 39541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent int value; 39551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { 39561035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // forward device change to effects that have requested to be 39571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // aware of attached audio device. 39581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (value != AUDIO_DEVICE_NONE) { 39591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mOutDevice = value; 39601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 39611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mEffectChains[i]->setDevice_l(mOutDevice); 3962c125f38cd0ae35409a01b98a99e483550daa1313Glenn Kasten } 3963c125f38cd0ae35409a01b98a99e483550daa1313Glenn Kasten } 39641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 39651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { 39661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // do not accept frame count changes if tracks are open as the track buffer 39671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // size depends on frame count and correct behavior would not be garantied 39681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // if frame count is changed after track creation 39691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (!mTracks.isEmpty()) { 39701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = INVALID_OPERATION; 39711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 39721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 397381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 39741035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 39751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == NO_ERROR) { 39761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = mOutput->stream->common.set_parameters(&mOutput->stream->common, 39771035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent keyValuePair.string()); 39781035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (!mStandby && status == INVALID_OPERATION) { 39791035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mOutput->stream->common.standby(&mOutput->stream->common); 39801035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mStandby = true; 39811035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mBytesWritten = 0; 398281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = mOutput->stream->common.set_parameters(&mOutput->stream->common, 39831035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent keyValuePair.string()); 39841035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 39851035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == NO_ERROR && reconfig) { 39861035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent readOutputParameters_l(); 39871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sendIoConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); 398881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 398981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 39901035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 399181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return reconfig; 399281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 399381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 399481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const 399581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 399681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t time; 399781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (audio_is_linear_pcm(mFormat)) { 399881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent time = PlaybackThread::activeSleepTimeUs(); 399981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 400081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent time = 10000; 400181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 400281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return time; 400381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 400481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 400581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const 400681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 400781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t time; 400881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (audio_is_linear_pcm(mFormat)) { 400981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; 401081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 401181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent time = 10000; 401281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 401381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return time; 401481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 401581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 401681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const 401781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 401881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t time; 401981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (audio_is_linear_pcm(mFormat)) { 402081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); 402181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 402281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent time = 10000; 402381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 402481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return time; 402581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 402681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 402781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DirectOutputThread::cacheParameters_l() 402881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 402981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread::cacheParameters_l(); 403081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 403181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // use shorter standby delay as on normal output to release 403281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // hardware resources as soon as possible 4033972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent if (audio_is_linear_pcm(mFormat)) { 4034972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent standbyDelay = microseconds(activeSleepTime*2); 4035972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent } else { 4036972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent standbyDelay = kOffloadStandbyDelayNs; 4037972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent } 403881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 403981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 404081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 404181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4042bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::AsyncCallbackThread::AsyncCallbackThread( 40434de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent const wp<AudioFlinger::PlaybackThread>& playbackThread) 4044bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent : Thread(false /*canCallJava*/), 40454de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent mPlaybackThread(playbackThread), 40463b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence(0), 40473b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence(0) 4048bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4049bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4050bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4051bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::AsyncCallbackThread::~AsyncCallbackThread() 4052bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4053bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4054bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4055bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::AsyncCallbackThread::onFirstRef() 4056bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4057bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO); 4058bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4059bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4060bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::AsyncCallbackThread::threadLoop() 4061bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4062bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent while (!exitPending()) { 40633b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent uint32_t writeAckSequence; 40643b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent uint32_t drainSequence; 4065bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4066bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent { 4067bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 406824a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George while (!((mWriteAckSequence & 1) || 406924a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George (mDrainSequence & 1) || 407024a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George exitPending())) { 407124a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George mWaitWorkCV.wait(mLock); 407224a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George } 407324a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George 4074bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (exitPending()) { 4075bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent break; 4076bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 40773b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d", 40783b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence, mDrainSequence); 40793b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent writeAckSequence = mWriteAckSequence; 40803b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence &= ~1; 40813b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent drainSequence = mDrainSequence; 40823b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence &= ~1; 4083bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4084bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent { 40854de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote(); 40864de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent if (playbackThread != 0) { 40873b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if (writeAckSequence & 1) { 40884de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent playbackThread->resetWriteBlocked(writeAckSequence >> 1); 4089bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 40903b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if (drainSequence & 1) { 40914de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent playbackThread->resetDraining(drainSequence >> 1); 4092bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4093bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4094bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4095bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4096bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return false; 4097bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4098bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4099bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::AsyncCallbackThread::exit() 4100bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4101bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("AsyncCallbackThread::exit"); 4102bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 4103bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent requestExit(); 4104bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mWaitWorkCV.broadcast(); 4105bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4106bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 41073b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence) 4108bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4109bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 41103b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // bit 0 is cleared 41113b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence = sequence << 1; 41123b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent} 41133b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent 41143b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::AsyncCallbackThread::resetWriteBlocked() 41153b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent{ 41163b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent Mutex::Autolock _l(mLock); 41173b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // ignore unexpected callbacks 41183b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if (mWriteAckSequence & 2) { 41193b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence |= 1; 4120bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mWaitWorkCV.signal(); 4121bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4122bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4123bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 41243b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence) 41253b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent{ 41263b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent Mutex::Autolock _l(mLock); 41273b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // bit 0 is cleared 41283b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence = sequence << 1; 41293b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent} 41303b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent 41313b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::AsyncCallbackThread::resetDraining() 4132bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4133bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 41343b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // ignore unexpected callbacks 41353b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if (mDrainSequence & 2) { 41363b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence |= 1; 4137bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mWaitWorkCV.signal(); 4138bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4139bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4140bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4141bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4142bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// ---------------------------------------------------------------------------- 4143bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger, 4144bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioStreamOut* output, audio_io_handle_t id, uint32_t device) 4145bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent : DirectOutputThread(audioFlinger, output, id, device, OFFLOAD), 4146bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mHwPaused(false), 4147ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent mFlushPending(false), 4148d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent mPausedBytesRemaining(0) 4149bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4150fd4779740ec3e9e865d5514464df26d015354388Eric Laurent //FIXME: mStandby should be set to true by ThreadBase constructor 4151fd4779740ec3e9e865d5514464df26d015354388Eric Laurent mStandby = true; 4152bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4153bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4154bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::OffloadThread::threadLoop_exit() 4155bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4156bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mFlushPending || mHwPaused) { 4157bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // If a flush is pending or track was paused, just discard buffered data 4158bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent flushHw_l(); 4159bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else { 4160bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mMixerStatus = MIXER_DRAIN_ALL; 4161bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent threadLoop_drain(); 4162bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4163bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mCallbackThread->exit(); 4164bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent PlaybackThread::threadLoop_exit(); 4165bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4166bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4167bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l( 4168bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Vector< sp<Track> > *tracksToRemove 4169bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent) 4170bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4171bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent size_t count = mActiveTracks.size(); 4172bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4173bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mixer_state mixerStatus = MIXER_IDLE; 4174972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent bool doHwPause = false; 4175972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent bool doHwResume = false; 4176972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent 4177ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent ALOGV("OffloadThread::prepareTracks_l active tracks %d", count); 4178ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent 4179bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // find out which tracks need to be processed 4180bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent for (size_t i = 0; i < count; i++) { 4181bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent sp<Track> t = mActiveTracks[i].promote(); 4182bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // The track died recently 4183bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (t == 0) { 4184bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent continue; 4185bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4186bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Track* const track = t.get(); 4187bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent audio_track_cblk_t* cblk = track->cblk(); 4188fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // Only consider last track started for volume and mixer state control. 4189fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // In theory an older track could underrun and restart after the new one starts 4190fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // but as we only care about the transition phase between two tracks on a 4191fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // direct output, it is not a problem to ignore the underrun case. 4192fd4779740ec3e9e865d5514464df26d015354388Eric Laurent sp<Track> l = mLatestActiveTrack.promote(); 4193fd4779740ec3e9e865d5514464df26d015354388Eric Laurent bool last = l.get() == track; 4194fd4779740ec3e9e865d5514464df26d015354388Eric Laurent 41957844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George if (track->isInvalid()) { 41967844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George ALOGW("An invalidated track shouldn't be in active list"); 41977844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George tracksToRemove->add(track); 41987844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George continue; 41997844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George } 42007844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George 42017844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George if (track->mState == TrackBase::IDLE) { 42027844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George ALOGW("An idle track shouldn't be in active list"); 42037844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George continue; 42047844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George } 42057844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George 4206bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (track->isPausing()) { 4207bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->setPaused(); 4208bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (last) { 4209bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (!mHwPaused) { 4210972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent doHwPause = true; 4211bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mHwPaused = true; 4212bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4213bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // If we were part way through writing the mixbuffer to 4214bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // the HAL we must save this until we resume 4215bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // BUG - this will be wrong if a different track is made active, 4216bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // in that case we want to discard the pending data in the 4217bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // mixbuffer and tell the client to present it again when the 4218bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // track is resumed 4219bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mPausedWriteLength = mCurrentWriteLength; 4220bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mPausedBytesRemaining = mBytesRemaining; 4221bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining = 0; // stop writing 4222bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4223bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent tracksToRemove->add(track); 42247844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George } else if (track->isFlushPending()) { 42257844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George track->flushAck(); 42267844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George if (last) { 42277844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George mFlushPending = true; 42287844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George } 42292d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George } else if (track->isResumePending()){ 42302d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George track->resumeAck(); 42312d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George if (last) { 42322d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George if (mPausedBytesRemaining) { 42332d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George // Need to continue write that was interrupted 42342d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George mCurrentWriteLength = mPausedWriteLength; 42352d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George mBytesRemaining = mPausedBytesRemaining; 42362d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George mPausedBytesRemaining = 0; 42372d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George } 42382d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George if (mHwPaused) { 42392d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George doHwResume = true; 42402d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George mHwPaused = false; 42412d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George // threadLoop_mix() will handle the case that we need to 42422d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George // resume an interrupted write 42432d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George } 42442d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George // enable write to audio HAL 42452d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George sleepTime = 0; 42462d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George 42472d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George // Do not handle new data in this iteration even if track->framesReady() 42482d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George mixerStatus = MIXER_TRACKS_ENABLED; 42492d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George } 42502d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George } else if (track->framesReady() && track->isReady() && 42513b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) { 4252f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten ALOGVV("OffloadThread: track %d s=%08x [OK]", track->name(), cblk->mServer); 4253bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (track->mFillingUpStatus == Track::FS_FILLED) { 4254bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mFillingUpStatus = Track::FS_ACTIVE; 42551abbdb4429479975718421c4fef3f79ce7c820e3Eric Laurent // make sure processVolume_l() will apply new volume even if 0 42561abbdb4429479975718421c4fef3f79ce7c820e3Eric Laurent mLeftVolFloat = mRightVolFloat = -1.0; 4257bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4258bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4259bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (last) { 4260d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent sp<Track> previousTrack = mPreviousTrack.promote(); 4261d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent if (previousTrack != 0) { 4262d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent if (track != previousTrack.get()) { 42639da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // Flush any data still being written from last track 42649da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent mBytesRemaining = 0; 42659da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent if (mPausedBytesRemaining) { 42669da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // Last track was paused so we also need to flush saved 42679da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // mixbuffer state and invalidate track so that it will 42689da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // re-submit that unwritten data when it is next resumed 42699da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent mPausedBytesRemaining = 0; 42709da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // Invalidate is a bit drastic - would be more efficient 42719da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // to have a flag to tell client that some of the 42729da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // previously written data was lost 4273d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent previousTrack->invalidate(); 42749da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent } 42759da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // flush data already sent to the DSP if changing audio session as audio 42769da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // comes from a different source. Also invalidate previous track to force a 42779da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // seek when resuming. 4278d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent if (previousTrack->sessionId() != track->sessionId()) { 4279d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent previousTrack->invalidate(); 42809da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent } 42819da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent } 42829da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent } 42839da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent mPreviousTrack = track; 4284bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // reset retry count 4285bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mRetryCount = kMaxTrackRetriesOffload; 4286bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mActiveTrack = t; 4287bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mixerStatus = MIXER_TRACKS_READY; 4288bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4289bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else { 4290f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten ALOGVV("OffloadThread: track %d s=%08x [NOT READY]", track->name(), cblk->mServer); 4291bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (track->isStopping_1()) { 4292bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Hardware buffer can hold a large amount of audio so we must 4293bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // wait for all current track's data to drain before we say 4294bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // that the track is stopped. 4295bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mBytesRemaining == 0) { 4296bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Only start draining when all data in mixbuffer 4297bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // has been written 4298bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2"); 4299bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mState = TrackBase::STOPPING_2; // so presentation completes after drain 43006a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent // do not drain if no data was ever sent to HAL (mStandby == true) 43016a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent if (last && !mStandby) { 43021b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent // do not modify drain sequence if we are already draining. This happens 43031b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent // when resuming from pause after drain. 43041b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent if ((mDrainSequence & 1) == 0) { 43051b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent sleepTime = 0; 43061b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent standbyTime = systemTime() + standbyDelay; 43071b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent mixerStatus = MIXER_DRAIN_TRACK; 43081b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent mDrainSequence += 2; 43091b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent } 4310bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mHwPaused) { 4311bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // It is possible to move from PAUSED to STOPPING_1 without 4312bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // a resume so we must ensure hardware is running 43131b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent doHwResume = true; 4314bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mHwPaused = false; 4315bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4316bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4317bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4318bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if (track->isStopping_2()) { 43196a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent // Drain has completed or we are in standby, signal presentation complete 43206a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent if (!(mDrainSequence & 1) || !last || mStandby) { 4321bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mState = TrackBase::STOPPED; 4322bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent size_t audioHALFrames = 4323bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; 4324bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent size_t framesWritten = 4325bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesWritten / audio_stream_frame_size(&mOutput->stream->common); 4326bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->presentationComplete(framesWritten, audioHALFrames); 4327bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->reset(); 4328bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent tracksToRemove->add(track); 4329bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4330bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else { 4331bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // No buffers for this track. Give it a few chances to 4332bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // fill a buffer, then remove it from active list. 4333bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (--(track->mRetryCount) <= 0) { 4334bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("OffloadThread: BUFFER TIMEOUT: remove(%d) from active list", 4335bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->name()); 4336bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent tracksToRemove->add(track); 4337a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent // indicate to client process that the track was disabled because of underrun; 4338a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent // it will then automatically call start() when data is available 4339a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent android_atomic_or(CBLK_DISABLED, &cblk->mFlags); 4340bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if (last){ 4341bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mixerStatus = MIXER_TRACKS_ENABLED; 4342bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4343bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4344bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4345bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // compute volume for this track 4346bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent processVolume_l(track, last); 4347bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 43486bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent 4349ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent // make sure the pause/flush/resume sequence is executed in the right order. 4350ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent // If a flush is pending and a track is active but the HW is not paused, force a HW pause 4351ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent // before flush and then resume HW. This can happen in case of pause/flush/resume 4352ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent // if resume is received before pause is executed. 4353fd4779740ec3e9e865d5514464df26d015354388Eric Laurent if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { 4354972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent mOutput->stream->pause(mOutput->stream); 4355972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent } 43566bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent if (mFlushPending) { 43576bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent flushHw_l(); 43586bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent mFlushPending = false; 43596bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent } 4360fd4779740ec3e9e865d5514464df26d015354388Eric Laurent if (!mStandby && doHwResume) { 4361972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent mOutput->stream->resume(mOutput->stream); 4362972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent } 43636bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent 4364bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // remove all the tracks that need to be... 4365bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent removeTracks_l(*tracksToRemove); 4366bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4367bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return mixerStatus; 4368bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4369bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4370bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// must be called with thread mutex locked 4371bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::OffloadThread::waitingAsyncCallback_l() 4372bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 43733b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d", 43743b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence, mDrainSequence); 43753b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) { 4376bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return true; 4377bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4378bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return false; 4379bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4380bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4381bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// must be called with thread mutex locked 4382bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::OffloadThread::shouldStandby_l() 4383bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4384e6f35b13567d920af19f0b8dc35f9120b7cf1dc9Glenn Kasten bool trackPaused = false; 4385bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4386bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack 4387bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // after a timeout and we will enter standby then. 4388bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mTracks.size() > 0) { 4389e6f35b13567d920af19f0b8dc35f9120b7cf1dc9Glenn Kasten trackPaused = mTracks[mTracks.size() - 1]->isPaused(); 4390bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4391bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4392e6f35b13567d920af19f0b8dc35f9120b7cf1dc9Glenn Kasten return !mStandby && !trackPaused; 4393bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4394bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4395bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4396bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::OffloadThread::waitingAsyncCallback() 4397bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4398bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 4399bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return waitingAsyncCallback_l(); 4400bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4401bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4402bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::OffloadThread::flushHw_l() 4403bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4404bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mOutput->stream->flush(mOutput->stream); 4405bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Flush anything still waiting in the mixbuffer 4406bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mCurrentWriteLength = 0; 4407bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining = 0; 4408bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mPausedWriteLength = 0; 4409bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mPausedBytesRemaining = 0; 44100f02f265123b7ef2fd6ac09ff70cde26eb5559adHaynes Mathew George mHwPaused = false; 44110f02f265123b7ef2fd6ac09ff70cde26eb5559adHaynes Mathew George 4412bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mUseAsyncWrite) { 44133b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // discard any pending drain or write ack by incrementing sequence 44143b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence = (mWriteAckSequence + 2) & ~1; 44153b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence = (mDrainSequence + 2) & ~1; 4416bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 44173b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setWriteBlocked(mWriteAckSequence); 44183b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setDraining(mDrainSequence); 4419bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4420bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4421bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 44224c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew Georgevoid AudioFlinger::OffloadThread::onAddNewTrack_l() 44234c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George{ 44244c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George sp<Track> previousTrack = mPreviousTrack.promote(); 44254c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George sp<Track> latestTrack = mLatestActiveTrack.promote(); 44264c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George 44274c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George if (previousTrack != 0 && latestTrack != 0 && 44284c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George (previousTrack->sessionId() != latestTrack->sessionId())) { 44294c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George mFlushPending = true; 44304c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George } 44314c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George PlaybackThread::onAddNewTrack_l(); 44324c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George} 44334c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George 4434bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// ---------------------------------------------------------------------------- 4435bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 443681784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, 443781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioFlinger::MixerThread* mainThread, audio_io_handle_t id) 443881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->outDevice(), 443981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent DUPLICATING), 444081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitTimeMs(UINT_MAX) 444181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 444281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent addOutputTrack(mainThread); 444381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 444481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 444581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::DuplicatingThread::~DuplicatingThread() 444681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 444781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mOutputTracks.size(); i++) { 444881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutputTracks[i]->destroy(); 444981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 445081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 445181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 445281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::threadLoop_mix() 445381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 445481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mix buffers... 445581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (outputsReady(outputTracks)) { 445681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->process(AudioBufferProvider::kInvalidPTS); 445781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 445825c2dac12114699e90deb1c579cadebce7b91a97Andy Hung memset(mSinkBuffer, 0, mSinkBufferSize); 445981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 446081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = 0; 446181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent writeFrames = mNormalFrameCount; 446225c2dac12114699e90deb1c579cadebce7b91a97Andy Hung mCurrentWriteLength = mSinkBufferSize; 446381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent standbyTime = systemTime() + standbyDelay; 446481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 446581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 446681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::threadLoop_sleepTime() 446781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 446881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sleepTime == 0) { 446981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mMixerStatus == MIXER_TRACKS_ENABLED) { 447081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = activeSleepTime; 447181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 447281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = idleSleepTime; 447381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 447481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (mBytesWritten != 0) { 447581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mMixerStatus == MIXER_TRACKS_ENABLED) { 447681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent writeFrames = mNormalFrameCount; 447725c2dac12114699e90deb1c579cadebce7b91a97Andy Hung memset(mSinkBuffer, 0, mSinkBufferSize); 447881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 447981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // flush remaining overflow buffers in output tracks 448081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent writeFrames = 0; 448181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 448281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = 0; 448381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 448481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 448581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4486bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentssize_t AudioFlinger::DuplicatingThread::threadLoop_write() 448781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 448881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < outputTracks.size(); i++) { 4489010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // We convert the duplicating thread format to AUDIO_FORMAT_PCM_16_BIT 4490010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // for delivery downstream as needed. This in-place conversion is safe as 4491010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // AUDIO_FORMAT_PCM_16_BIT is smaller than any other supported format 4492010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // (AUDIO_FORMAT_PCM_8_BIT is not allowed here). 4493010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung if (mFormat != AUDIO_FORMAT_PCM_16_BIT) { 4494010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung memcpy_by_audio_format(mSinkBuffer, AUDIO_FORMAT_PCM_16_BIT, 4495010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung mSinkBuffer, mFormat, writeFrames * mChannelCount); 4496010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung } 4497010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung outputTracks[i]->write(reinterpret_cast<int16_t*>(mSinkBuffer), writeFrames); 449881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 44992c3740f01acca69c3e0bcc5e01bb0edc51b6777fEric Laurent mStandby = false; 450025c2dac12114699e90deb1c579cadebce7b91a97Andy Hung return (ssize_t)mSinkBufferSize; 450181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 450281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 450381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::threadLoop_standby() 450481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 450581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // DuplicatingThread implements standby by stopping all tracks 450681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < outputTracks.size(); i++) { 450781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent outputTracks[i]->stop(); 450881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 450981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 451081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 451181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::saveOutputTracks() 451281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 451381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent outputTracks = mOutputTracks; 451481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 451581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 451681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::clearOutputTracks() 451781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 451881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent outputTracks.clear(); 451981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 452081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 452181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) 452281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 452381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 452481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME explain this formula 452581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t frameCount = (3 * mNormalFrameCount * mSampleRate) / thread->sampleRate(); 4526010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // OutputTrack is forced to AUDIO_FORMAT_PCM_16_BIT regardless of mFormat 4527010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // due to current usage case and restrictions on the AudioBufferProvider. 4528010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // Actual buffer conversion is done in threadLoop_write(). 4529010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // 4530010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // TODO: This may change in the future, depending on multichannel 4531010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // (and non int16_t*) support on AF::PlaybackThread::OutputTrack 453281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent OutputTrack *outputTrack = new OutputTrack(thread, 453381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent this, 453481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSampleRate, 4535010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung AUDIO_FORMAT_PCM_16_BIT, 453681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mChannelMask, 4537462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen frameCount, 4538462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen IPCThreadState::self()->getCallingUid()); 453981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (outputTrack->cblk() != NULL) { 454081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread->setStreamVolume(AUDIO_STREAM_CNT, 1.0f); 454181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutputTracks.add(outputTrack); 454281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread); 454381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent updateWaitTime_l(); 454481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 454581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 454681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 454781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) 454881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 454981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 455081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mOutputTracks.size(); i++) { 455181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutputTracks[i]->thread() == thread) { 455281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutputTracks[i]->destroy(); 455381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutputTracks.removeAt(i); 455481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent updateWaitTime_l(); 455581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 455681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 455781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 455881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("removeOutputTrack(): unkonwn thread: %p", thread); 455981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 456081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 456181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// caller must hold mLock 456281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::updateWaitTime_l() 456381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 456481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitTimeMs = UINT_MAX; 456581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mOutputTracks.size(); i++) { 456681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); 456781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (strong != 0) { 456881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); 456981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (waitTimeMs < mWaitTimeMs) { 457081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitTimeMs = waitTimeMs; 457181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 457281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 457381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 457481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 457581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 457681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 457781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::DuplicatingThread::outputsReady( 457881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const SortedVector< sp<OutputTrack> > &outputTracks) 457981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 458081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < outputTracks.size(); i++) { 458181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<ThreadBase> thread = outputTracks[i]->thread().promote(); 458281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread == 0) { 458381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", 458481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent outputTracks[i].get()); 458581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 458681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 458781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); 458881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // see note at standby() declaration 458981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (playbackThread->standby() && !playbackThread->isSuspended()) { 459081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), 459181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread.get()); 459281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 459381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 459481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 459581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return true; 459681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 459781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 459881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const 459981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 460081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return (mWaitTimeMs * 1000) / 2; 460181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 460281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 460381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::cacheParameters_l() 460481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 460581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first 460681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent updateWaitTime_l(); 460781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 460881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent MixerThread::cacheParameters_l(); 460981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 461081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 461181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 461281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Record 461381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 461481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 461581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, 461681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamIn *input, 461781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t id, 4618d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent audio_devices_t outDevice, 461946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten audio_devices_t inDevice 462046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 462146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten , const sp<NBAIO_Sink>& teeSink 462246909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 462346909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten ) : 4624d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent ThreadBase(audioFlinger, id, outDevice, inDevice, RECORD), 46256dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten mInput(input), mActiveTracksGen(0), mRsmpInBuffer(NULL), 4626deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten // mRsmpInFrames and mRsmpInFramesP2 are set by readInputParameters_l() 46274cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten mRsmpInRear(0) 462846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 462946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten , mTeeSink(teeSink) 463046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 4631b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize, 4632b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten "RecordThreadRO", MemoryHeapBase::READ_ONLY)) 463381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 463481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent snprintf(mName, kNameLength, "AudioIn_%X", id); 4635481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mName); 463681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4637deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten readInputParameters_l(); 463881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 463981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 464081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 464181784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::RecordThread::~RecordThread() 464281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 4643481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten mAudioFlinger->unregisterWriter(mNBLogWriter); 464481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent delete[] mRsmpInBuffer; 464581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 464681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 464781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::onFirstRef() 464881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 464981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent run(mName, PRIORITY_URGENT_AUDIO); 465081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 465181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 465281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::RecordThread::threadLoop() 465381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 465481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent nsecs_t lastWarning = 0; 465581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 465681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent inputStandBy(); 465781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4658f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kastenreacquire_wakelock: 4659f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten sp<RecordTrack> activeTrack; 46602b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten int activeTracksGen; 4661f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten { 4662f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten Mutex::Autolock _l(mLock); 46632b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten size_t size = mActiveTracks.size(); 46642b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten activeTracksGen = mActiveTracksGen; 46652b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (size > 0) { 46662b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten // FIXME an arbitrary choice 46672b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten activeTrack = mActiveTracks[0]; 46682b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten acquireWakeLock_l(activeTrack->uid()); 46692b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (size > 1) { 46702b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten SortedVector<int> tmp; 46712b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten for (size_t i = 0; i < size; i++) { 46722b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten tmp.add(mActiveTracks[i]->uid()); 46732b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten } 46742b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten updateWakeLockUids_l(tmp); 46752b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten } 46762b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten } else { 46772b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten acquireWakeLock_l(-1); 46782b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten } 4679f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten } 4680f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten 46816dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // used to request a deferred sleep, to be executed later while mutex is unlocked 46826dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten uint32_t sleepUs = 0; 46836dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 46846dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // loop while there is work to do 46854ef0b463a56c19bad9197aa9f90d792090461429Glenn Kasten for (;;) { 4686c527a7c2b1bfd26e8f3086e1b653d56e521379d9Glenn Kasten Vector< sp<EffectChain> > effectChains; 46872cfbf88b89854f30b295e8ae26a031edb8d712f8Glenn Kasten 46885edadd46c76c5ff1c3edabf2ea943c2278e82e1cGlenn Kasten // sleep with mutex unlocked 46896dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (sleepUs > 0) { 46906dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten usleep(sleepUs); 46916dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten sleepUs = 0; 46925edadd46c76c5ff1c3edabf2ea943c2278e82e1cGlenn Kasten } 46935edadd46c76c5ff1c3edabf2ea943c2278e82e1cGlenn Kasten 46946dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // activeTracks accumulates a copy of a subset of mActiveTracks 46956dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten Vector< sp<RecordTrack> > activeTracks; 46966dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 46971035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 469881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // scope for mLock 469981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 4700000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent 4701021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent processConfigEvents_l(); 4702f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten 4703000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent // check exitPending here because checkForNewParameters_l() and 4704000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent // checkForNewParameters_l() can temporarily release mLock 4705000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent if (exitPending()) { 4706000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent break; 4707000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent } 4708000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent 47092b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten // if no active track(s), then standby and release wakelock 47102b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten size_t size = mActiveTracks.size(); 47112b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (size == 0) { 471293e471f620454f7de73d190521568b1e25879767Glenn Kasten standbyIfNotAlreadyInStandby(); 47134ef0b463a56c19bad9197aa9f90d792090461429Glenn Kasten // exitPending() can't become true here 471481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock_l(); 471581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("RecordThread: loop stopping"); 471681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // go to sleep 471781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitWorkCV.wait(mLock); 471881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("RecordThread: loop starting"); 4719f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten goto reacquire_wakelock; 4720f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten } 4721f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten 47222b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (mActiveTracksGen != activeTracksGen) { 47232b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten activeTracksGen = mActiveTracksGen; 4724f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten SortedVector<int> tmp; 47252b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten for (size_t i = 0; i < size; i++) { 47262b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten tmp.add(mActiveTracks[i]->uid()); 47272b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten } 4728f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten updateWakeLockUids_l(tmp); 472981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 47309e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten 47316dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten bool doBroadcast = false; 47326dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten for (size_t i = 0; i < size; ) { 47339e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten 47346dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack = mActiveTracks[i]; 47356dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTrack->isTerminated()) { 47366dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten removeTrack_l(activeTrack); 47372b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracks.remove(activeTrack); 47382b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracksGen++; 47396dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size--; 47409e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten continue; 47419e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten } 47426dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 47436dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten TrackBase::track_state activeTrackState = activeTrack->mState; 47446dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten switch (activeTrackState) { 47456dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 47466dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case TrackBase::PAUSING: 47476dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten mActiveTracks.remove(activeTrack); 47486dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten mActiveTracksGen++; 47496dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten doBroadcast = true; 47506dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size--; 47516dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten continue; 47526dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 47536dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case TrackBase::STARTING_1: 47546dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten sleepUs = 10000; 47556dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten i++; 47566dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten continue; 47576dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 47586dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case TrackBase::STARTING_2: 47596dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten doBroadcast = true; 47606dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten mStandby = false; 47619e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten activeTrack->mState = TrackBase::ACTIVE; 47626dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten break; 47636dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 47646dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case TrackBase::ACTIVE: 47656dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten break; 47666dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 47676dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case TrackBase::IDLE: 47686dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten i++; 47696dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten continue; 47706dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 47716dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten default: 4772adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("Unexpected activeTrackState %d", activeTrackState); 47739e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten } 47742d94426cd3302cb1215c92c5f1c4b90c24ceb72bGlenn Kasten 47756dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTracks.add(activeTrack); 47766dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten i++; 47772d94426cd3302cb1215c92c5f1c4b90c24ceb72bGlenn Kasten 47786dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 47796dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (doBroadcast) { 47806dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten mStartStopCond.broadcast(); 47816dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 4782d9fc34fb0fcfcc739f868b116edf50c62af19d5eGlenn Kasten 47836dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // sleep if there are no active tracks to process 47846dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTracks.size() == 0) { 47856dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (sleepUs == 0) { 47866dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten sleepUs = kRecordThreadSleepUs; 47876dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 47886dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten continue; 478981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 47906dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten sleepUs = 0; 47919e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten 479281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lockEffectChains_l(effectChains); 479381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 479481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 47956dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0 47967165268ffa6c7b6b405b6afad82e2a346500e8eeGlenn Kasten 47976dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size_t size = effectChains.size(); 47986dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten for (size_t i = 0; i < size; i++) { 47991ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten // thread mutex is not locked, but effect chain is locked 48001ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten effectChains[i]->process_l(); 48011ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten } 480281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 48036dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one. 48046dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // Only the client(s) that are too slow will overrun. But if even the fastest client is too 48056dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // slow, then this RecordThread will overrun by not calling HAL read often enough. 48066dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // If destination is non-contiguous, first read past the nominal end of buffer, then 48076dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // copy to the right place. Permitted because mRsmpInBuffer was over-allocated. 48086dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 48096dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1); 48106dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ssize_t bytesRead = mInput->stream->read(mInput->stream, 48116dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten &mRsmpInBuffer[rear * mChannelCount], mBufferSize); 48126dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (bytesRead <= 0) { 48136dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ALOGE("read failed: bytesRead=%d < %u", bytesRead, mBufferSize); 48146dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // Force input into standby so that it tries to recover at next read attempt 48156dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten inputStandBy(); 48166dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten sleepUs = kRecordThreadSleepUs; 48176dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten continue; 48186dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 48196dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ALOG_ASSERT((size_t) bytesRead <= mBufferSize); 48206dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size_t framesRead = bytesRead / mFrameSize; 48216dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ALOG_ASSERT(framesRead > 0); 48226dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (mTeeSink != 0) { 48236dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten (void) mTeeSink->write(&mRsmpInBuffer[rear * mChannelCount], framesRead); 48246dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 48256dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // If destination is non-contiguous, we now correct for reading past end of buffer. 48266dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size_t part1 = mRsmpInFramesP2 - rear; 48276dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (framesRead > part1) { 48286dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten memcpy(mRsmpInBuffer, &mRsmpInBuffer[mRsmpInFramesP2 * mChannelCount], 48296dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten (framesRead - part1) * mFrameSize); 48306dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 48316dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten rear = mRsmpInRear += framesRead; 48326dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 48336dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size = activeTracks.size(); 48346dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // loop over each active track 48356dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten for (size_t i = 0; i < size; i++) { 48366dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack = activeTracks[i]; 48376dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 48386dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten enum { 48396dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten OVERRUN_UNKNOWN, 48406dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten OVERRUN_TRUE, 48416dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten OVERRUN_FALSE 48426dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } overrun = OVERRUN_UNKNOWN; 48436dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 48446dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // loop over getNextBuffer to handle circular sink 48456dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten for (;;) { 48466dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 48476dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mSink.frameCount = ~0; 48486dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten status_t status = activeTrack->getNextBuffer(&activeTrack->mSink); 48496dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size_t framesOut = activeTrack->mSink.frameCount; 48506dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0)); 48516dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 48526dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten int32_t front = activeTrack->mRsmpInFront; 48536dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ssize_t filled = rear - front; 48546dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size_t framesIn; 48556dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 48566dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (filled < 0) { 48576dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // should not happen, but treat like a massive overrun and re-sync 48586dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten framesIn = 0; 48596dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mRsmpInFront = rear; 48606dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten overrun = OVERRUN_TRUE; 4861607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten } else if ((size_t) filled <= mRsmpInFrames) { 48626dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten framesIn = (size_t) filled; 48636dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } else { 48646dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // client is not keeping up with server, but give it latest data 4865607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten framesIn = mRsmpInFrames; 4866607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten activeTrack->mRsmpInFront = front = rear - framesIn; 48676dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten overrun = OVERRUN_TRUE; 48686dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 48696dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 48704cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten if (framesOut == 0 || framesIn == 0) { 48714cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten break; 48724cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten } 48734cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten 48746dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTrack->mResampler == NULL) { 48756dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // no resampling 48766dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (framesIn > framesOut) { 48776dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten framesIn = framesOut; 48786dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } else { 48796dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten framesOut = framesIn; 48806dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 48816dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten int8_t *dst = activeTrack->mSink.i8; 48826dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten while (framesIn > 0) { 48836dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten front &= mRsmpInFramesP2 - 1; 48846dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size_t part1 = mRsmpInFramesP2 - front; 48856dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (part1 > framesIn) { 48866dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten part1 = framesIn; 488781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 48886dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten int8_t *src = (int8_t *)mRsmpInBuffer + (front * mFrameSize); 48894cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten if (mChannelCount == activeTrack->mChannelCount) { 48906dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten memcpy(dst, src, part1 * mFrameSize); 48916dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } else if (mChannelCount == 1) { 48926dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten upmix_to_stereo_i16_from_mono_i16((int16_t *)dst, (int16_t *)src, 48936dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten part1); 48941ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten } else { 48956dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten downmix_to_mono_i16_from_stereo_i16((int16_t *)dst, (int16_t *)src, 48966dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten part1); 48971ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten } 48986dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten dst += part1 * activeTrack->mFrameSize; 48996dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten front += part1; 49006dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten framesIn -= part1; 49011ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten } 49026dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mRsmpInFront += framesOut; 49036dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 49046dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } else { 49056dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // resampling 49066dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME framesInNeeded should really be part of resampler API, and should 49076dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // depend on the SRC ratio 49086dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // to keep mRsmpInBuffer full so resampler always has sufficient input 49096dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size_t framesInNeeded; 49106dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME only re-calculate when it changes, and optimize for common ratios 49116dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten double inOverOut = (double) mSampleRate / activeTrack->mSampleRate; 49126dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten double outOverIn = (double) activeTrack->mSampleRate / mSampleRate; 49134cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten framesInNeeded = ceil(framesOut * inOverOut) + 1; 4914607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten ALOGV("need %u frames in to produce %u out given in/out ratio of %.4g", 4915607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten framesInNeeded, framesOut, inOverOut); 4916607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten // Although we theoretically have framesIn in circular buffer, some of those are 4917607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten // unreleased frames, and thus must be discounted for purpose of budgeting. 4918607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten size_t unreleased = activeTrack->mRsmpInUnrel; 4919607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten framesIn = framesIn > unreleased ? framesIn - unreleased : 0; 49206dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (framesIn < framesInNeeded) { 4921607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten ALOGV("not enough to resample: have %u frames in but need %u in to " 4922607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten "produce %u out given in/out ratio of %.4g", 49234cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten framesIn, framesInNeeded, framesOut, inOverOut); 49244cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten size_t newFramesOut = framesIn > 0 ? floor((framesIn - 1) * outOverIn) : 0; 4925607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten LOG_ALWAYS_FATAL_IF(newFramesOut >= framesOut); 4926607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten if (newFramesOut == 0) { 4927607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten break; 49284cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten } 4929607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten framesInNeeded = ceil(newFramesOut * inOverOut) + 1; 4930607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten ALOGV("now need %u frames in to produce %u out given out/in ratio of %.4g", 4931607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten framesInNeeded, newFramesOut, outOverIn); 4932607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten LOG_ALWAYS_FATAL_IF(framesIn < framesInNeeded); 4933607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten ALOGV("success 2: have %u frames in and need %u in to produce %u out " 4934607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten "given in/out ratio of %.4g", 4935607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten framesIn, framesInNeeded, newFramesOut, inOverOut); 4936607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten framesOut = newFramesOut; 49374cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten } else { 4938607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten ALOGV("success 1: have %u in and need %u in to produce %u out " 49394cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten "given in/out ratio of %.4g", 49404cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten framesIn, framesInNeeded, framesOut, inOverOut); 49418594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten } 49426dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 49436dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // reallocate mRsmpOutBuffer as needed; we will grow but never shrink 49446dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTrack->mRsmpOutFrameCount < framesOut) { 4945607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten // FIXME why does each track need it's own mRsmpOutBuffer? can't they share? 49466dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten delete[] activeTrack->mRsmpOutBuffer; 49476dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // resampler always outputs stereo 49486dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mRsmpOutBuffer = new int32_t[framesOut * FCC_2]; 49496dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mRsmpOutFrameCount = framesOut; 49508594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten } 49516dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 49526dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // resampler accumulates, but we only have one source track 49536dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten memset(activeTrack->mRsmpOutBuffer, 0, framesOut * FCC_2 * sizeof(int32_t)); 49546dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mResampler->resample(activeTrack->mRsmpOutBuffer, framesOut, 4955607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten // FIXME how about having activeTrack implement this interface itself? 49566dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mResamplerBufferProvider 49576dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten /*this*/ /* AudioBufferProvider* */); 49586dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // ditherAndClamp() works as long as all buffers returned by 49596dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // activeTrack->getNextBuffer() are 32 bit aligned which should be always true. 49604cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten if (activeTrack->mChannelCount == 1) { 496184a0c6e87c48f58a0d3be71961432c086a4d24ccAndy Hung // temporarily type pun mRsmpOutBuffer from Q4.27 to int16_t 49626dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ditherAndClamp(activeTrack->mRsmpOutBuffer, activeTrack->mRsmpOutBuffer, 49636dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten framesOut); 49646dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // the resampler always outputs stereo samples: 49656dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // do post stereo to mono conversion 49666dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten downmix_to_mono_i16_from_stereo_i16(activeTrack->mSink.i16, 49676dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten (int16_t *)activeTrack->mRsmpOutBuffer, framesOut); 49686dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } else { 49696dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ditherAndClamp((int32_t *)activeTrack->mSink.raw, 49706dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mRsmpOutBuffer, framesOut); 49718594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten } 49726dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // now done with mRsmpOutBuffer 49738594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten 49748594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten } 49758594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten 49766dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) { 49776dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten overrun = OVERRUN_FALSE; 49781ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten } 497981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 49806dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTrack->mFramesToDrop == 0) { 49816dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (framesOut > 0) { 49826dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mSink.frameCount = framesOut; 49836dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->releaseBuffer(&activeTrack->mSink); 498481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 498581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 49866dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME could do a partial drop of framesOut 49876dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTrack->mFramesToDrop > 0) { 49886dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mFramesToDrop -= framesOut; 49896dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTrack->mFramesToDrop <= 0) { 499025f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten activeTrack->clearSyncStartEvent(); 49916dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 49926dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } else { 49936dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mFramesToDrop += framesOut; 49946dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 || 49956dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mSyncStartEvent->isCancelled()) { 49966dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ALOGW("Synced record %s, session %d, trigger session %d", 49976dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled", 49986dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->sessionId(), 49996dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten (activeTrack->mSyncStartEvent != 0) ? 50006dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mSyncStartEvent->triggerSession() : 0); 500125f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten activeTrack->clearSyncStartEvent(); 50026dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 500381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 500481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 50056dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 50066dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (framesOut == 0) { 50076dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten break; 50086dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 500981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 50106dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 50116dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten switch (overrun) { 50126dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case OVERRUN_TRUE: 50136dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // client isn't retrieving buffers fast enough 50146dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (!activeTrack->setOverflow()) { 50156dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten nsecs_t now = systemTime(); 50166dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME should lastWarning per track? 50176dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if ((now - lastWarning) > kWarningThrottleNs) { 50186dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ALOGW("RecordThread: buffer overflow"); 50196dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten lastWarning = now; 50206dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 502181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 50226dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten break; 50236dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case OVERRUN_FALSE: 50246dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->clearOverflow(); 50256dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten break; 50266dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case OVERRUN_UNKNOWN: 50276dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten break; 502881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 50296dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 503081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 50311ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten 503281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // enable changes in effect chain 503381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent unlockEffectChains(effectChains); 5034c527a7c2b1bfd26e8f3086e1b653d56e521379d9Glenn Kasten // effectChains doesn't need to be cleared, since it is cleared by destructor at scope end 503581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 503681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 503793e471f620454f7de73d190521568b1e25879767Glenn Kasten standbyIfNotAlreadyInStandby(); 503881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 503981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 504081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 50419a54bc27876acd5d8be5b1fc3dc46701fe76fbb3Eric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 50429a54bc27876acd5d8be5b1fc3dc46701fe76fbb3Eric Laurent sp<RecordTrack> track = mTracks[i]; 50439a54bc27876acd5d8be5b1fc3dc46701fe76fbb3Eric Laurent track->invalidate(); 50449a54bc27876acd5d8be5b1fc3dc46701fe76fbb3Eric Laurent } 50452b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracks.clear(); 50462b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracksGen++; 504781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStartStopCond.broadcast(); 504881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 504981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 505081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock(); 505181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 505281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("RecordThread %p exiting", this); 505381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 505481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 505581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 505693e471f620454f7de73d190521568b1e25879767Glenn Kastenvoid AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby() 505781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 505881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!mStandby) { 505981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent inputStandBy(); 506081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStandby = true; 506181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 506281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 506381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 506481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::inputStandBy() 506581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 506681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mInput->stream->common.standby(&mInput->stream->common); 506781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 506881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 506905997e21af6c4517f375def6563af4b9ebe95f39Glenn Kasten// RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held 5070e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kastensp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( 507181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<AudioFlinger::Client>& client, 507281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t sampleRate, 507381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_format_t format, 507481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_channel_mask_t channelMask, 507574935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten size_t *pFrameCount, 507681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId, 5077462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen int uid, 5078ddb0ccf3fb6fe8da8c71a6deb30561b821f3c0a2Glenn Kasten IAudioFlinger::track_flags_t *flags, 507981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t tid, 508081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t *status) 508181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 508274935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten size_t frameCount = *pFrameCount; 508381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<RecordTrack> track; 508481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t lStatus; 508581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 508690e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten // client expresses a preference for FAST, but we get the final say 508790e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten if (*flags & IAudioFlinger::TRACK_FAST) { 508890e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten if ( 508990e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten // use case: callback handler and frame count is default or at least as large as HAL 509090e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten ( 509190e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten (tid != -1) && 509290e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten ((frameCount == 0) || 50933a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten // FIXME not necessarily true, should be native frame count for native SR! 5094b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten (frameCount >= mFrameCount)) 509590e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten ) && 50963a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten // PCM data 50973a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten audio_is_linear_pcm(format) && 509890e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten // mono or stereo 5099828f883a43f66f77d776a75d0ea2b87c7c826071Glenn Kasten ( (channelMask == AUDIO_CHANNEL_IN_MONO) || 5100828f883a43f66f77d776a75d0ea2b87c7c826071Glenn Kasten (channelMask == AUDIO_CHANNEL_IN_STEREO) ) && 510190e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten // hardware sample rate 51023a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten // FIXME actually the native hardware sample rate 510390e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten (sampleRate == mSampleRate) && 51043a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten // record thread has an associated fast capture 51053a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten hasFastCapture() 51063a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten // fast capture does not require slots 510790e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten ) { 51083a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten // if frameCount not specified, then it defaults to fast capture (HAL) frame count 510990e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten if (frameCount == 0) { 51103a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten // FIXME wrong mFrameCount 511190e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten frameCount = mFrameCount * kFastTrackMultiplier; 511290e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten } 511390e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten ALOGV("AUDIO_INPUT_FLAG_FAST accepted: frameCount=%d mFrameCount=%d", 511490e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten frameCount, mFrameCount); 511590e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten } else { 511690e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten ALOGV("AUDIO_INPUT_FLAG_FAST denied: frameCount=%d " 511790e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten "mFrameCount=%d format=%d isLinear=%d channelMask=%#x sampleRate=%u mSampleRate=%u " 51183a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten "hasFastCapture=%d tid=%d", 511990e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten frameCount, mFrameCount, format, 512090e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten audio_is_linear_pcm(format), 51213a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten channelMask, sampleRate, mSampleRate, hasFastCapture(), tid); 512290e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten *flags &= ~IAudioFlinger::TRACK_FAST; 51233a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten // FIXME It's not clear that we need to enforce this any more, since we have a pipe. 512490e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten // For compatibility with AudioRecord calculation, buffer depth is forced 512590e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten // to be at least 2 x the record thread frame count and cover audio hardware latency. 512690e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten // This is probably too conservative, but legacy application code may depend on it. 512790e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten // If you change this calculation, also review the start threshold which is related. 512890e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten uint32_t latencyMs = 50; // FIXME mInput->stream->get_latency(mInput->stream); 512990e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten size_t mNormalFrameCount = 2048; // FIXME 513090e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate); 513190e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten if (minBufCount < 2) { 513290e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten minBufCount = 2; 513390e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten } 513490e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten size_t minFrameCount = mNormalFrameCount * minBufCount; 513590e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten if (frameCount < minFrameCount) { 513690e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten frameCount = minFrameCount; 513790e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten } 513890e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten } 513990e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten } 514074935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten *pFrameCount = frameCount; 514190e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten 514215e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten lStatus = initCheck(); 514315e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten if (lStatus != NO_ERROR) { 514415e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten ALOGE("createRecordTrack_l() audio driver not initialized"); 514515e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten goto Exit; 514615e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten } 514781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 514881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // scope for mLock 514981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 515081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 515181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track = new RecordTrack(this, client, sampleRate, 5152d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten format, channelMask, frameCount, sessionId, uid, 5153755b0a611f539dfa49e88aac592a938427c7e1b8Glenn Kasten *flags); 515481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5155030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten lStatus = track->initCheck(); 5156030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten if (lStatus != NO_ERROR) { 515735295078ab59c8c5d143a54d5a55557c3ca62c51Glenn Kasten ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus); 515803e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George // track must be cleared from the caller as the caller has the AF lock 515981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 516081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 516181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mTracks.add(track); 516281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 516381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // disable AEC and NS if the device is a BT SCO headset supporting those pre processings 516481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool suspend = audio_is_bluetooth_sco_device(mInDevice) && 516581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioFlinger->btNrecIsOff(); 516681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); 516781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent setEffectSuspended_l(FX_IID_NS, suspend, sessionId); 516890e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten 516990e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { 517090e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten pid_t callingPid = IPCThreadState::self()->getCallingPid(); 517190e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, 517290e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten // so ask activity manager to do this on our behalf 517390e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); 517490e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten } 517581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 517605997e21af6c4517f375def6563af4b9ebe95f39Glenn Kasten 517781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = NO_ERROR; 517881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 517981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit: 51809156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten *status = lStatus; 518181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return track; 518281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 518381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 518481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, 518581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSystem::sync_event_t event, 518681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int triggerSession) 518781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 518881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); 518981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<ThreadBase> strongMe = this; 519081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status = NO_ERROR; 519181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 519281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (event == AudioSystem::SYNC_EVENT_NONE) { 519325f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten recordTrack->clearSyncStartEvent(); 519481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (event != AudioSystem::SYNC_EVENT_SAME) { 51956dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event, 519681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent triggerSession, 519781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent recordTrack->sessionId(), 519881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent syncStartEventCallback, 51996dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack); 520081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Sync event can be cancelled by the trigger session if the track is not in a 520181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // compatible state in which case we start record immediately 52026dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (recordTrack->mSyncStartEvent->isCancelled()) { 520325f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten recordTrack->clearSyncStartEvent(); 520481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 520581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs 52066dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack->mFramesToDrop = - 52074cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000); 520881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 520981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 521081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 521181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 521247c2070b2ce8aedb7300c0aad91caccf3c383841Glenn Kasten // This section is a rendezvous between binder thread executing start() and RecordThread 521381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AutoMutex lock(mLock); 52146dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (mActiveTracks.indexOf(recordTrack) >= 0) { 52156dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (recordTrack->mState == TrackBase::PAUSING) { 52166dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ALOGV("active record track PAUSING -> ACTIVE"); 5217f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten recordTrack->mState = TrackBase::ACTIVE; 52186dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } else { 52196dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ALOGV("active record track state %d", recordTrack->mState); 522081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 522181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 522281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 522381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 52244cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten // TODO consider other ways of handling this, such as changing the state to :STARTING and 52254cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten // adding the track to mActiveTracks after returning from AudioSystem::startInput(), 52264cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten // or using a separate command thread 52276dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack->mState = TrackBase::STARTING_1; 52282b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracks.add(recordTrack); 52292b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracksGen++; 523081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mLock.unlock(); 523181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status = AudioSystem::startInput(mId); 523281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mLock.lock(); 52336dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME should verify that recordTrack is still in mActiveTracks 523481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (status != NO_ERROR) { 52352b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracks.remove(recordTrack); 52362b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracksGen++; 523725f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten recordTrack->clearSyncStartEvent(); 523881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 523981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 52406dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // Catch up with current buffer indices if thread is already running. 52416dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // This is what makes a new client discard all buffered data. If the track's mRsmpInFront 52426dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // was initialized to some value closer to the thread's mRsmpInFront, then the track could 52436dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // see previously buffered data before it called start(), but with greater risk of overrun. 52446dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 52456dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack->mRsmpInFront = mRsmpInRear; 52466dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack->mRsmpInUnrel = 0; 52476dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME why reset? 52486dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (recordTrack->mResampler != NULL) { 52496dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack->mResampler->reset(); 52506dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 52516dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack->mState = TrackBase::STARTING_2; 525281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // signal thread to start 525381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitWorkCV.broadcast(); 52542b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (mActiveTracks.indexOf(recordTrack) < 0) { 525581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("Record failed to start"); 525681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = BAD_VALUE; 525781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto startError; 525881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 525981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 526081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 52617c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten 526281784c37c61b09289654b979567a42bf73cd2b12Eric LaurentstartError: 526381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSystem::stopInput(mId); 526425f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten recordTrack->clearSyncStartEvent(); 52656dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME I wonder why we do not reset the state here? 526681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 526781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 526881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 526981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) 527081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 527181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<SyncEvent> strongEvent = event.promote(); 527281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 527381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (strongEvent != 0) { 52748ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent sp<RefBase> ptr = strongEvent->cookie().promote(); 52758ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent if (ptr != 0) { 52768ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent RecordTrack *recordTrack = (RecordTrack *)ptr.get(); 52778ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent recordTrack->handleSyncStartEvent(strongEvent); 52788ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent } 527981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 528081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 528181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5282a8356f663014e7d4c27869629af83d8bb3441e19Glenn Kastenbool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { 528381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("RecordThread::stop"); 5284a8356f663014e7d4c27869629af83d8bb3441e19Glenn Kasten AutoMutex _l(mLock); 52852b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (mActiveTracks.indexOf(recordTrack) != 0 || recordTrack->mState == TrackBase::PAUSING) { 528681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 528781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 528847c2070b2ce8aedb7300c0aad91caccf3c383841Glenn Kasten // note that threadLoop may still be processing the track at this point [without lock] 528981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent recordTrack->mState = TrackBase::PAUSING; 529081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // do not wait for mStartStopCond if exiting 529181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (exitPending()) { 529281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return true; 529381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 529447c2070b2ce8aedb7300c0aad91caccf3c383841Glenn Kasten // FIXME incorrect usage of wait: no explicit predicate or loop 529581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStartStopCond.wait(mLock); 52962b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten // if we have been restarted, recordTrack is in mActiveTracks here 52972b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (exitPending() || mActiveTracks.indexOf(recordTrack) != 0) { 529881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("Record stopped OK"); 529981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return true; 530081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 530181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 530281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 530381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 53040f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenbool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const 530581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 530681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 530781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 530881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 53090f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenstatus_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused) 531081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 531181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#if 0 // This branch is currently dead code, but is preserved in case it will be needed in future 531281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!isValidSyncEvent(event)) { 531381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 531481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 531581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 531681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int eventSession = event->triggerSession(); 531781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t ret = NAME_NOT_FOUND; 531881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 531981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 532081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 532181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 532281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<RecordTrack> track = mTracks[i]; 532381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (eventSession == track->sessionId()) { 532481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (void) track->setSyncEvent(event); 532581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ret = NO_ERROR; 532681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 532781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 532881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return ret; 532981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#else 533081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 533181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 533281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 533381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 533481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// destroyTrack_l() must be called with ThreadBase::mLock held 533581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track) 533681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 5337bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->terminate(); 5338bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mState = TrackBase::STOPPED; 533981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // active tracks are removed by threadLoop() 53402b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (mActiveTracks.indexOf(track) < 0) { 534181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent removeTrack_l(track); 534281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 534381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 534481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 534581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track) 534681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 534781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mTracks.remove(track); 534881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // need anything related to effects here? 534981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 535081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 535181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) 535281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 535381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpInternals(fd, args); 535481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpTracks(fd, args); 535581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpEffectChains(fd, args); 535681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 535781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 535881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args) 535981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 5360b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, "\nInput thread %p:\n", this); 536181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 53622b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (mActiveTracks.size() > 0) { 53631d6fa7af1288b550faabe4ec2cf98684236723dbNarayan Kamath fdprintf(fd, " Buffer size: %zu bytes\n", mBufferSize); 536481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 53656dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten fdprintf(fd, " No active record clients\n"); 536681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 536781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 536881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpBase(fd, args); 536981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 537081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 53710f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args __unused) 537281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 537381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const size_t SIZE = 256; 537481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char buffer[SIZE]; 537581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent String8 result; 537681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5377b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numtracks = mTracks.size(); 5378b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numactive = mActiveTracks.size(); 5379b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numactiveseen = 0; 5380b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, " %d Tracks", numtracks); 5381b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (numtracks) { 5382b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, " of which %d are active\n", numactive); 5383b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen RecordTrack::appendDumpHeader(result); 5384b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen for (size_t i = 0; i < numtracks ; ++i) { 5385b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen sp<RecordTrack> track = mTracks[i]; 5386b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (track != 0) { 5387b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen bool active = mActiveTracks.indexOf(track) >= 0; 5388b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (active) { 5389b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen numactiveseen++; 5390b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 5391b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen track->dump(buffer, SIZE, active); 5392b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append(buffer); 5393b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 539481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 5395b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } else { 5396b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen fdprintf(fd, "\n"); 539781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 539881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5399b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (numactiveseen != numactive) { 5400b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen snprintf(buffer, SIZE, " The following tracks are in the active list but" 5401b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen " not in the track list\n"); 540281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result.append(buffer); 540381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent RecordTrack::appendDumpHeader(result); 5404b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen for (size_t i = 0; i < numactive; ++i) { 54052b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten sp<RecordTrack> track = mActiveTracks[i]; 5406b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mTracks.indexOf(track) < 0) { 5407b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen track->dump(buffer, SIZE, true); 5408b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append(buffer); 5409b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 54102b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten } 541181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 541281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 541381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent write(fd, result.string(), result.size()); 541481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 541581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 541681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// AudioBufferProvider interface 54176dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kastenstatus_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer( 54186dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten AudioBufferProvider::Buffer* buffer, int64_t pts __unused) 541981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 54206dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten RecordTrack *activeTrack = mRecordTrack; 54216dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten sp<ThreadBase> threadBase = activeTrack->mThread.promote(); 54226dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (threadBase == 0) { 54236dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten buffer->frameCount = 0; 5424607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten buffer->raw = NULL; 54256dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten return NOT_ENOUGH_DATA; 54266dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 54276dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten RecordThread *recordThread = (RecordThread *) threadBase.get(); 54286dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten int32_t rear = recordThread->mRsmpInRear; 54296dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten int32_t front = activeTrack->mRsmpInFront; 54308594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten ssize_t filled = rear - front; 54316dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME should not be P2 (don't want to increase latency) 54326dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME if client not keeping up, discard 5433607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames)); 54348594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten // 'filled' may be non-contiguous, so return only the first contiguous chunk 54356dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten front &= recordThread->mRsmpInFramesP2 - 1; 54366dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size_t part1 = recordThread->mRsmpInFramesP2 - front; 54378594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten if (part1 > (size_t) filled) { 54388594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten part1 = filled; 54398594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten } 54408594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten size_t ask = buffer->frameCount; 54418594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten ALOG_ASSERT(ask > 0); 54428594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten if (part1 > ask) { 54438594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten part1 = ask; 54448594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten } 54458594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten if (part1 == 0) { 54468594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten // Higher-level should keep mRsmpInBuffer full, and not call resampler if empty 5447607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten LOG_ALWAYS_FATAL("RecordThread::getNextBuffer() starved"); 54488594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten buffer->raw = NULL; 54498594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten buffer->frameCount = 0; 54506dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mRsmpInUnrel = 0; 54518594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten return NOT_ENOUGH_DATA; 54528594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten } 54538594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten 54546dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten buffer->raw = recordThread->mRsmpInBuffer + front * recordThread->mChannelCount; 54558594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten buffer->frameCount = part1; 54566dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mRsmpInUnrel = part1; 545781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 545881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 545981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 546081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// AudioBufferProvider interface 54616dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kastenvoid AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer( 54626dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten AudioBufferProvider::Buffer* buffer) 546381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 54646dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten RecordTrack *activeTrack = mRecordTrack; 54658594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten size_t stepCount = buffer->frameCount; 54668594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten if (stepCount == 0) { 54678594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten return; 54688594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten } 54696dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ALOG_ASSERT(stepCount <= activeTrack->mRsmpInUnrel); 54706dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mRsmpInUnrel -= stepCount; 54716dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mRsmpInFront += stepCount; 54728594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten buffer->raw = NULL; 547381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent buffer->frameCount = 0; 547481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 547581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 54761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentbool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair, 54771035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status_t& status) 547881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 547981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool reconfig = false; 548081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 54811035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = NO_ERROR; 54821035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 54831035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent audio_format_t reqFormat = mFormat; 54841035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent uint32_t samplingRate = mSampleRate; 54851035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount); 54861035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 54871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent AudioParameter param = AudioParameter(keyValuePair); 54881035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent int value; 54891035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // TODO Investigate when this code runs. Check with audio policy when a sample rate and 54901035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // channel count change can be requested. Do we mandate the first client defines the 54911035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // HAL sampling rate and channel count or do we allow changes on the fly? 54921035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { 54931035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent samplingRate = value; 54941035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 54951035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 54961035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { 54971035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if ((audio_format_t) value != AUDIO_FORMAT_PCM_16_BIT) { 54981035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = BAD_VALUE; 54991035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 55001035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reqFormat = (audio_format_t) value; 550181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent reconfig = true; 550281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 55031035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 55041035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { 55051035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent audio_channel_mask_t mask = (audio_channel_mask_t) value; 55061035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (mask != AUDIO_CHANNEL_IN_MONO && mask != AUDIO_CHANNEL_IN_STEREO) { 55071035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = BAD_VALUE; 55081035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 55091035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent channelMask = mask; 55101035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 551181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 55121035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 55131035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { 55141035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // do not accept frame count changes if tracks are open as the track buffer 55151035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // size depends on frame count and correct behavior would not be guaranteed 55161035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // if frame count is changed after track creation 55171035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (mActiveTracks.size() > 0) { 55181035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = INVALID_OPERATION; 55191035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 55201035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 552181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 55221035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 55231035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { 55241035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // forward device change to effects that have requested to be 55251035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // aware of attached audio device. 55261035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 55271035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mEffectChains[i]->setDevice_l(value); 552881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 552981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 55301035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // store input device and output device but do not forward output device to audio HAL. 55311035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // Note that status is ignored by the caller for output device 55321035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // (see AudioFlinger::setParameters() 55331035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (audio_is_output_devices(value)) { 55341035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mOutDevice = value; 55351035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = BAD_VALUE; 55361035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 55371035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mInDevice = value; 55381035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // disable AEC and NS if the device is a BT SCO headset supporting those 55391035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // pre processings 55401035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (mTracks.size() > 0) { 55411035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent bool suspend = audio_is_bluetooth_sco_device(mInDevice) && 55421035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mAudioFlinger->btNrecIsOff(); 55431035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 55441035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sp<RecordTrack> track = mTracks[i]; 55451035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); 55461035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); 554781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 554881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 554981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 55501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 55511035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && 55521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mAudioSource != (audio_source_t)value) { 55531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // forward device change to effects that have requested to be 55541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // aware of attached audio device. 55551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 55561035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mEffectChains[i]->setAudioSource_l((audio_source_t)value); 555781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 55581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mAudioSource = (audio_source_t)value; 55591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 5560e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten 55611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == NO_ERROR) { 55621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = mInput->stream->common.set_parameters(&mInput->stream->common, 55631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent keyValuePair.string()); 55641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == INVALID_OPERATION) { 55651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent inputStandBy(); 556681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = mInput->stream->common.set_parameters(&mInput->stream->common, 556781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent keyValuePair.string()); 55681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 55691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (reconfig) { 55701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == BAD_VALUE && 55711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reqFormat == mInput->stream->common.get_format(&mInput->stream->common) && 55721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reqFormat == AUDIO_FORMAT_PCM_16_BIT && 55731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent (mInput->stream->common.get_sample_rate(&mInput->stream->common) 55741035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent <= (2 * samplingRate)) && 55751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent popcount(mInput->stream->common.get_channels(&mInput->stream->common)) 55761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent <= FCC_2 && 55771035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent (channelMask == AUDIO_CHANNEL_IN_MONO || 55781035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent channelMask == AUDIO_CHANNEL_IN_STEREO)) { 55791035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = NO_ERROR; 558081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 55811035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == NO_ERROR) { 55821035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent readInputParameters_l(); 55831035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sendIoConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED); 558481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 558581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 558681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 55871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 558881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return reconfig; 558981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 559081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 559181784c37c61b09289654b979567a42bf73cd2b12Eric LaurentString8 AudioFlinger::RecordThread::getParameters(const String8& keys) 559281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 559381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 559481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (initCheck() != NO_ERROR) { 5595d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten return String8(); 559681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 559781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5598d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten char *s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); 5599d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten const String8 out_s8(s); 560081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent free(s); 560181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return out_s8; 560281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 560381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5604021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurentvoid AudioFlinger::RecordThread::audioConfigChanged(int event, int param __unused) { 560581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSystem::OutputDescriptor desc; 5606b2737d0b33c17e408d96d6f9eeaa3381479c94c7Glenn Kasten const void *param2 = NULL; 560781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 560881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent switch (event) { 560981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case AudioSystem::INPUT_OPENED: 561081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case AudioSystem::INPUT_CONFIG_CHANGED: 5611fad226abd12435dbcd232f7de396f1a097b2bd5fGlenn Kasten desc.channelMask = mChannelMask; 561281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc.samplingRate = mSampleRate; 561381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc.format = mFormat; 561481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc.frameCount = mFrameCount; 561581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc.latency = 0; 561681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent param2 = &desc; 561781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 561881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 561981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case AudioSystem::INPUT_CLOSED: 562081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent default: 562181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 562281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 5623021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent mAudioFlinger->audioConfigChanged(event, mId, param2); 562481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 562581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5626deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kastenvoid AudioFlinger::RecordThread::readInputParameters_l() 562781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 562881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); 562981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); 5630f6ed423af92a56ef54bba23eba883b1f21448b54Glenn Kasten mChannelCount = popcount(mChannelMask); 563181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFormat = mInput->stream->common.get_format(&mInput->stream->common); 5632291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten if (mFormat != AUDIO_FORMAT_PCM_16_BIT) { 5633cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten ALOGE("HAL format %#x not supported; must be AUDIO_FORMAT_PCM_16_BIT", mFormat); 5634291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten } 563581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFrameSize = audio_stream_frame_size(&mInput->stream->common); 5636548efc94813c1dec6e8cf6c085ae41ccb04827f1Glenn Kasten mBufferSize = mInput->stream->common.get_buffer_size(&mInput->stream->common); 5637548efc94813c1dec6e8cf6c085ae41ccb04827f1Glenn Kasten mFrameCount = mBufferSize / mFrameSize; 56386dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // This is the formula for calculating the temporary buffer size. 5639e842614837e5401adf77e90485300c288b9a7876Glenn Kasten // With 7 HAL buffers, we can guarantee ability to down-sample the input by ratio of 6:1 to 56408594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten // 1 full output buffer, regardless of the alignment of the available input. 5641e842614837e5401adf77e90485300c288b9a7876Glenn Kasten // The value is somewhat arbitrary, and could probably be even larger. 56426dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // A larger value should allow more old data to be read after a track calls start(), 56436dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // without increasing latency. 5644e842614837e5401adf77e90485300c288b9a7876Glenn Kasten mRsmpInFrames = mFrameCount * 7; 56458594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten mRsmpInFramesP2 = roundup(mRsmpInFrames); 56466dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten delete[] mRsmpInBuffer; 56478594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten // Over-allocate beyond mRsmpInFramesP2 to permit a HAL read past end of buffer 56488594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten mRsmpInBuffer = new int16_t[(mRsmpInFramesP2 + mFrameCount - 1) * mChannelCount]; 56496dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 56504cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints. 56514cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten // But if thread's mSampleRate or mChannelCount changes, how will that affect active tracks? 565281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 565381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 56545f972c031d4061f4f037c9fda1ea4bd9b6a756cdGlenn Kastenuint32_t AudioFlinger::RecordThread::getInputFramesLost() 565581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 565681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 565781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (initCheck() != NO_ERROR) { 565881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 565981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 566081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 566181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mInput->stream->get_input_frames_lost(mInput->stream); 566281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 566381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 566481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) const 566581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 566681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 566781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t result = 0; 566881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (getEffectChain_l(sessionId) != 0) { 566981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result = EFFECT_SESSION; 567081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 567181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 567281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 567381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId == mTracks[i]->sessionId()) { 567481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result |= TRACK_SESSION; 567581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 567681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 567781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 567881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 567981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return result; 568081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 568181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 568281784c37c61b09289654b979567a42bf73cd2b12Eric LaurentKeyedVector<int, bool> AudioFlinger::RecordThread::sessionIds() const 568381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 568481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent KeyedVector<int, bool> ids; 568581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 568681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t j = 0; j < mTracks.size(); ++j) { 568781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<RecordThread::RecordTrack> track = mTracks[j]; 568881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId = track->sessionId(); 568981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (ids.indexOfKey(sessionId) < 0) { 569081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ids.add(sessionId, true); 569181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 569281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 569381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return ids; 569481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 569581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 569681784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() 569781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 569881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 569981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamIn *input = mInput; 570081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mInput = NULL; 570181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return input; 570281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 570381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 570481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// this method must always be called either with ThreadBase mLock held or inside the thread loop 570581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_stream_t* AudioFlinger::RecordThread::stream() const 570681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 570781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mInput == NULL) { 570881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NULL; 570981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 571081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return &mInput->stream->common; 571181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 571281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 571381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) 571481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 571581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // only one chain per input thread 571681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mEffectChains.size() != 0) { 571781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return INVALID_OPERATION; 571881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 571981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); 572081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 572181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setInBuffer(NULL); 572281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setOutBuffer(NULL); 572381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 572481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent checkSuspendOnAddEffectChain_l(chain); 572581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 572681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains.add(chain); 572781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 572881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 572981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 573081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 573181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsize_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) 573281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 573381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); 573481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW_IF(mEffectChains.size() != 1, 573581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "removeEffectChain_l() %p invalid chain size %d on thread %p", 573681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain.get(), mEffectChains.size(), this); 573781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mEffectChains.size() == 1) { 573881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains.removeAt(0); 573981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 574081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 574181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 574281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 574381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}; // namespace android 5744