Threads.cpp revision 43b4dcc660e6da96285e4672ae371070ab845401
181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent/* 281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** 381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** Copyright 2012, The Android Open Source Project 481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** 581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** Licensed under the Apache License, Version 2.0 (the "License"); 681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** you may not use this file except in compliance with the License. 781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** You may obtain a copy of the License at 881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** 981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** http://www.apache.org/licenses/LICENSE-2.0 1081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** 1181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** Unless required by applicable law or agreed to in writing, software 1281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** distributed under the License is distributed on an "AS IS" BASIS, 1381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** See the License for the specific language governing permissions and 1581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** limitations under the License. 1681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent*/ 1781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#define LOG_TAG "AudioFlinger" 2081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent//#define LOG_NDEBUG 0 21371eb9756c32109ea572b91216b19bb623f6d3fdAlex Ray#define ATRACE_TAG ATRACE_TAG_AUDIO 2281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 23153b9fe667e6e78e0218ff0159353097428c7657Glenn Kasten#include "Configuration.h" 2481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <math.h> 2581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <fcntl.h> 26ad8510a339ffab330c2c46e5c247dd1cf9e15c22Glenn Kasten#include <linux/futex.h> 2781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <sys/stat.h> 28ad8510a339ffab330c2c46e5c247dd1cf9e15c22Glenn Kasten#include <sys/syscall.h> 2981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <cutils/properties.h> 301ab85ec401801ef9a9184650d0f5a1639b45eeb9Glenn Kasten#include <media/AudioParameter.h> 31cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung#include <media/AudioResamplerPublic.h> 3281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <utils/Log.h> 33371eb9756c32109ea572b91216b19bb623f6d3fdAlex Ray#include <utils/Trace.h> 3481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <private/media/AudioTrackShared.h> 3681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <hardware/audio.h> 3781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <audio_effects/effect_ns.h> 3881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <audio_effects/effect_aec.h> 3981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <audio_utils/primitives.h> 4098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung#include <audio_utils/format.h> 41c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten#include <audio_utils/minifloat.h> 4281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// NBAIO implementations 446dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#include <media/nbaio/AudioStreamInSource.h> 4581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/AudioStreamOutSink.h> 4681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/MonoPipe.h> 4781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/MonoPipeReader.h> 4881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/Pipe.h> 4981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/PipeReader.h> 5081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/SourceAudioBufferProvider.h> 5181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <powermanager/PowerManager.h> 5381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <common_time/cc_helper.h> 5581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <common_time/local_clock.h> 5681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "AudioFlinger.h" 5881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "AudioMixer.h" 59d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung#include "BufferProviders.h" 6081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "FastMixer.h" 616dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#include "FastCapture.h" 6281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "ServiceUtilities.h" 6381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "SchedulingPolicyService.h" 6481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 6581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef ADD_BATTERY_DATA 6681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/IMediaPlayerService.h> 6781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/IMediaDeathNotifier.h> 6881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 6981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 7081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef DEBUG_CPU_USAGE 7181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <cpustats/CentralTendencyStatistics.h> 7281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <cpustats/ThreadCpuUsage.h> 7381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 7481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 7581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 7681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 7781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Note: the following macro is used for extremely verbose logging message. In 7881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to 7981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// 0; but one side effect of this is to turn all LOGV's as well. Some messages 8081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// are so verbose that we want to suppress them even when we have ALOG_ASSERT 8181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// turned on. Do not uncomment the #def below unless you really know what you 8281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// are doing and want to see all of the extremely verbose messages. 8381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent//#define VERY_VERY_VERBOSE_LOGGING 8481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef VERY_VERY_VERBOSE_LOGGING 8581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#define ALOGVV ALOGV 8681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#else 8781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#define ALOGVV(a...) do { } while(0) 8881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 8981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 906770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung// TODO: Move these macro/inlines to a header file. 9149d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten#define max(a, b) ((a) > (b) ? (a) : (b)) 926770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hungtemplate <typename T> 936770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hungstatic inline T min(const T& a, const T& b) 946770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung{ 956770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung return a < b ? a : b; 966770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung} 9749d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten 98d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung#ifndef ARRAY_SIZE 99d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) 100d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung#endif 101d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung 10281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentnamespace android { 10381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 10481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// retry counts for buffer fill timeout 10581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// 50 * ~20msecs = 1 second 10681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int8_t kMaxTrackRetries = 50; 10781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int8_t kMaxTrackStartupRetries = 50; 10881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// allow less retry attempts on direct output thread. 10981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// direct outputs can be a scarce resource in audio hardware and should 11081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// be released as quickly as possible. 11181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int8_t kMaxTrackRetriesDirect = 2; 11281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 11381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// don't warn about blocked writes or record buffer overflows more often than this 11481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const nsecs_t kWarningThrottleNs = seconds(5); 11581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 11681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// RecordThread loop sleep time upon application overrun or audio HAL read error 11781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int kRecordThreadSleepUs = 5000; 11881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1191035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// maximum time to wait in sendConfigEvent_l() for a status to be received 1201035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentstatic const nsecs_t kConfigEventTimeoutNs = seconds(2); 12181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 12281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// minimum sleep time for the mixer thread loop when tracks are active but in underrun 12381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const uint32_t kMinThreadSleepTimeUs = 5000; 12481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// maximum divider applied to the active sleep time in the mixer thread loop 12581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const uint32_t kMaxThreadSleepTimeShift = 2; 12681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 12709a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung// minimum normal sink buffer size, expressed in milliseconds rather than frames 12809a5007b17acb49d25cfa386a2e2534d942e8854Andy Hungstatic const uint32_t kMinNormalSinkBufferSizeMs = 20; 12909a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung// maximum normal sink buffer size 13009a5007b17acb49d25cfa386a2e2534d942e8854Andy Hungstatic const uint32_t kMaxNormalSinkBufferSizeMs = 24; 13181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 132972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent// Offloaded output thread standby delay: allows track transition without going to standby 133972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurentstatic const nsecs_t kOffloadStandbyDelayNs = seconds(1); 134972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent 13581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Whether to use fast mixer 13681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const enum { 13781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixer_Never, // never initialize or use: for debugging only 13881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixer_Always, // always initialize and use, even if not needed: for debugging only 13981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // normal mixer multiplier is 1 14081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixer_Static, // initialize if needed, then use all the time if initialized, 14181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // multiplier is calculated based on min & max normal mixer buffer size 14281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load, 14381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // multiplier is calculated based on min & max normal mixer buffer size 14481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME for FastMixer_Dynamic: 14581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Supporting this option will require fixing HALs that can't handle large writes. 14681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // For example, one HAL implementation returns an error from a large write, 14781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // and another HAL implementation corrupts memory, possibly in the sample rate converter. 14881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // We could either fix the HAL implementations, or provide a wrapper that breaks 14981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // up large writes into smaller ones, and the wrapper would need to deal with scheduler. 15081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} kUseFastMixer = FastMixer_Static; 15181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1526dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten// Whether to use fast capture 1536dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kastenstatic const enum { 1546dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCapture_Never, // never initialize or use: for debugging only 1556dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCapture_Always, // always initialize and use, even if not needed: for debugging only 1566dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCapture_Static, // initialize if needed, then use all the time if initialized 1576dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten} kUseFastCapture = FastCapture_Static; 1586dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 15981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Priorities for requestPriority 16081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int kPriorityAudioApp = 2; 16181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int kPriorityFastMixer = 3; 1626dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kastenstatic const int kPriorityFastCapture = 3; 16381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 16481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// IAudioFlinger::createTrack() reports back to client the total size of shared memory area 16581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// for the track. The client then sub-divides this into smaller buffers for its use. 166b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten// Currently the client uses N-buffering by default, but doesn't tell us about the value of N. 167b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten// So for now we just assume that client is double-buffered for fast tracks. 168b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten// FIXME It would be better for client to tell AudioFlinger the value of N, 169b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten// so AudioFlinger could allocate the right amount of memory. 17081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// See the client's minBufCount and mNotificationFramesAct calculations for details. 1710349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten 1720349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten// This is the default value, if not specified by property. 173b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kastenstatic const int kFastTrackMultiplier = 2; 17481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1750349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten// The minimum and maximum allowed values 1760349009fd19f89f8414c428f6b71b369f7546085Glenn Kastenstatic const int kFastTrackMultiplierMin = 1; 1770349009fd19f89f8414c428f6b71b369f7546085Glenn Kastenstatic const int kFastTrackMultiplierMax = 2; 1780349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten 1790349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten// The actual value to use, which can be specified per-device via property af.fast_track_multiplier. 1800349009fd19f89f8414c428f6b71b369f7546085Glenn Kastenstatic int sFastTrackMultiplier = kFastTrackMultiplier; 1810349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten 182b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten// See Thread::readOnlyHeap(). 183b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten// Initially this heap is used to allocate client buffers for "fast" AudioRecord. 184b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten// Eventually it will be the single buffer that FastCapture writes into via HAL read(), 185b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten// and that all "fast" AudioRecord clients read from. In either case, the size can be small. 1869f81de3452dfb2385bd57dc05456a045174a1ab1Glenn Kastenstatic const size_t kRecordThreadReadOnlyHeapSize = 0x2000; 187b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten 18881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 18981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1900349009fd19f89f8414c428f6b71b369f7546085Glenn Kastenstatic pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT; 1910349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten 1920349009fd19f89f8414c428f6b71b369f7546085Glenn Kastenstatic void sFastTrackMultiplierInit() 1930349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten{ 1940349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten char value[PROPERTY_VALUE_MAX]; 1950349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten if (property_get("af.fast_track_multiplier", value, NULL) > 0) { 1960349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten char *endptr; 1970349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten unsigned long ul = strtoul(value, &endptr, 0); 1980349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) { 1990349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten sFastTrackMultiplier = (int) ul; 2000349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten } 2010349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten } 2020349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten} 2030349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten 2040349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten// ---------------------------------------------------------------------------- 2050349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten 20681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef ADD_BATTERY_DATA 20781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// To collect the amplifier usage 20881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic void addBatteryData(uint32_t params) { 20981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); 21081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (service == NULL) { 21181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it already logged 21281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 21381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 21481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 21581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent service->addBatteryData(params); 21681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 21781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 21881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 21981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 22081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 22181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// CPU Stats 22281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 22381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 22481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass CpuStats { 22581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic: 22681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent CpuStats(); 22781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void sample(const String8 &title); 22881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef DEBUG_CPU_USAGE 22981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate: 23081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns 23181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns 23281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 23381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles 23481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 23581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int mCpuNum; // thread's current CPU number 23681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int mCpukHz; // frequency of thread's current CPU in kHz 23781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 23881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}; 23981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 24081784c37c61b09289654b979567a42bf73cd2b12Eric LaurentCpuStats::CpuStats() 24181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef DEBUG_CPU_USAGE 24281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent : mCpuNum(-1), mCpukHz(-1) 24381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 24481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 24581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 24681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2470f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid CpuStats::sample(const String8 &title 2480f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten#ifndef DEBUG_CPU_USAGE 2490f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten __unused 2500f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten#endif 2510f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten ) { 25281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef DEBUG_CPU_USAGE 25381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // get current thread's delta CPU time in wall clock ns 25481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double wcNs; 25581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool valid = mCpuUsage.sampleAndEnable(wcNs); 25681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 25781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // record sample for wall clock statistics 25881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (valid) { 25981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWcStats.sample(wcNs); 26081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 26181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 26281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // get the current CPU number 26381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int cpuNum = sched_getcpu(); 26481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 26581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // get the current CPU frequency in kHz 26681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int cpukHz = mCpuUsage.getCpukHz(cpuNum); 26781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 26881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // check if either CPU number or frequency changed 26981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (cpuNum != mCpuNum || cpukHz != mCpukHz) { 27081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mCpuNum = cpuNum; 27181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mCpukHz = cpukHz; 27281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // ignore sample for purposes of cycles 27381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent valid = false; 27481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 27581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 27681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if no change in CPU number or frequency, then record sample for cycle statistics 27781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (valid && mCpukHz > 0) { 27881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double cycles = wcNs * cpukHz * 0.000001; 27981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHzStats.sample(cycles); 28081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 28181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 28281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent unsigned n = mWcStats.n(); 28381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mCpuUsage.elapsed() is expensive, so don't call it every loop 28481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((n & 127) == 1) { 28581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent long long elapsed = mCpuUsage.elapsed(); 28681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { 28781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double perLoop = elapsed / (double) n; 28881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double perLoop100 = perLoop * 0.01; 28981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double perLoop1k = perLoop * 0.001; 29081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double mean = mWcStats.mean(); 29181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double stddev = mWcStats.stddev(); 29281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double minimum = mWcStats.minimum(); 29381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double maximum = mWcStats.maximum(); 29481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double meanCycles = mHzStats.mean(); 29581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double stddevCycles = mHzStats.stddev(); 29681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double minCycles = mHzStats.minimum(); 29781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double maxCycles = mHzStats.maximum(); 29881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mCpuUsage.resetElapsed(); 29981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWcStats.reset(); 30081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHzStats.reset(); 30181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGD("CPU usage for %s over past %.1f secs\n" 30281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent " (%u mixer loops at %.1f mean ms per loop):\n" 30381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" 30481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" 30581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", 30681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent title.string(), 30781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent elapsed * .000000001, n, perLoop * .000001, 30881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mean * .001, 30981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent stddev * .001, 31081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minimum * .001, 31181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent maximum * .001, 31281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mean / perLoop100, 31381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent stddev / perLoop100, 31481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minimum / perLoop100, 31581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent maximum / perLoop100, 31681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent meanCycles / perLoop1k, 31781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent stddevCycles / perLoop1k, 31881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minCycles / perLoop1k, 31981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent maxCycles / perLoop1k); 32081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 32181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 32281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 32381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 32481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}; 32581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 32681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 32781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ThreadBase 32881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 32981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 33097b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten// static 33197b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kastenconst char *AudioFlinger::ThreadBase::threadTypeToString(AudioFlinger::ThreadBase::type_t type) 33297b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten{ 33397b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten switch (type) { 33497b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten case MIXER: 33597b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten return "MIXER"; 33697b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten case DIRECT: 33797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten return "DIRECT"; 33897b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten case DUPLICATING: 33997b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten return "DUPLICATING"; 34097b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten case RECORD: 34197b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten return "RECORD"; 34297b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten case OFFLOAD: 34397b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten return "OFFLOAD"; 34497b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten default: 34597b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten return "unknown"; 34697b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten } 34797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten} 34897b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten 3490f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn KastenString8 devicesToString(audio_devices_t devices) 3500f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten{ 3510f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten static const struct mapping { 3520f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten audio_devices_t mDevices; 3530f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten const char * mString; 3540f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } mappingsOut[] = { 3550f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten AUDIO_DEVICE_OUT_EARPIECE, "EARPIECE", 3560f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten AUDIO_DEVICE_OUT_SPEAKER, "SPEAKER", 3570f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten AUDIO_DEVICE_OUT_WIRED_HEADSET, "WIRED_HEADSET", 3580f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten AUDIO_DEVICE_OUT_WIRED_HEADPHONE, "WIRED_HEADPHONE", 35984d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_OUT_BLUETOOTH_SCO, "BLUETOOTH_SCO", 36084d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET, "BLUETOOTH_SCO_HEADSET", 36184d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, "BLUETOOTH_SCO_CARKIT", 36284d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, "BLUETOOTH_A2DP", 36384d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, "BLUETOOTH_A2DP_HEADPHONES", 36484d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER, "BLUETOOTH_A2DP_SPEAKER", 36584d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_OUT_AUX_DIGITAL, "AUX_DIGITAL", 36684d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_OUT_HDMI, "HDMI", 36784d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET, "ANLG_DOCK_HEADSET", 36884d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, "DGTL_DOCK_HEADSET", 36984d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_OUT_USB_ACCESSORY, "USB_ACCESSORY", 37084d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_OUT_USB_DEVICE, "USB_DEVICE", 3710f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten AUDIO_DEVICE_OUT_TELEPHONY_TX, "TELEPHONY_TX", 37284d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_OUT_LINE, "LINE", 37384d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_OUT_HDMI_ARC, "HDMI_ARC", 37484d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_OUT_SPDIF, "SPDIF", 37584d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_OUT_FM, "FM", 37684d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_OUT_AUX_LINE, "AUX_LINE", 37784d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_OUT_SPEAKER_SAFE, "SPEAKER_SAFE", 3780f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten AUDIO_DEVICE_NONE, "NONE", // must be last 3790f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten }, mappingsIn[] = { 38084d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_IN_COMMUNICATION, "COMMUNICATION", 38184d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_IN_AMBIENT, "AMBIENT", 3820f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten AUDIO_DEVICE_IN_BUILTIN_MIC, "BUILTIN_MIC", 38384d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, "BLUETOOTH_SCO_HEADSET", 3840f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten AUDIO_DEVICE_IN_WIRED_HEADSET, "WIRED_HEADSET", 38584d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_IN_AUX_DIGITAL, "AUX_DIGITAL", 3860f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten AUDIO_DEVICE_IN_VOICE_CALL, "VOICE_CALL", 38784d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_IN_TELEPHONY_RX, "TELEPHONY_RX", 38884d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_IN_BACK_MIC, "BACK_MIC", 3890f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten AUDIO_DEVICE_IN_REMOTE_SUBMIX, "REMOTE_SUBMIX", 39084d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET, "ANLG_DOCK_HEADSET", 39184d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET, "DGTL_DOCK_HEADSET", 39284d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_IN_USB_ACCESSORY, "USB_ACCESSORY", 39384d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_IN_USB_DEVICE, "USB_DEVICE", 39484d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_IN_FM_TUNER, "FM_TUNER", 39584d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_IN_TV_TUNER, "TV_TUNER", 39684d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_IN_LINE, "LINE", 39784d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_IN_SPDIF, "SPDIF", 39884d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_IN_BLUETOOTH_A2DP, "BLUETOOTH_A2DP", 39984d61caa6040e346df68dbe6f8e4fe339906de08Glenn Kasten AUDIO_DEVICE_IN_LOOPBACK, "LOOPBACK", 4000f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten AUDIO_DEVICE_NONE, "NONE", // must be last 4010f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten }; 4020f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten String8 result; 4030f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten audio_devices_t allDevices = AUDIO_DEVICE_NONE; 4040f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten const mapping *entry; 4050f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten if (devices & AUDIO_DEVICE_BIT_IN) { 4060f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten devices &= ~AUDIO_DEVICE_BIT_IN; 4070f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten entry = mappingsIn; 4080f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } else { 4090f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten entry = mappingsOut; 4100f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 4110f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten for ( ; entry->mDevices != AUDIO_DEVICE_NONE; entry++) { 4120f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten allDevices = (audio_devices_t) (allDevices | entry->mDevices); 4130f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten if (devices & entry->mDevices) { 4140f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten if (!result.isEmpty()) { 4150f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten result.append("|"); 4160f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 4170f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten result.append(entry->mString); 4180f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 4190f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 4200f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten if (devices & ~allDevices) { 4210f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten if (!result.isEmpty()) { 4220f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten result.append("|"); 4230f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 4240f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten result.appendFormat("0x%X", devices & ~allDevices); 4250f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 4260f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten if (result.isEmpty()) { 4270f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten result.append(entry->mString); 4280f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 4290f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten return result; 4300f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten} 4310f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten 4320f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn KastenString8 inputFlagsToString(audio_input_flags_t flags) 4330f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten{ 4340f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten static const struct mapping { 4350f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten audio_input_flags_t mFlag; 4360f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten const char * mString; 4370f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } mappings[] = { 4380f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten AUDIO_INPUT_FLAG_FAST, "FAST", 4390f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten AUDIO_INPUT_FLAG_HW_HOTWORD, "HW_HOTWORD", 4400f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten AUDIO_INPUT_FLAG_NONE, "NONE", // must be last 4410f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten }; 4420f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten String8 result; 4430f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten audio_input_flags_t allFlags = AUDIO_INPUT_FLAG_NONE; 4440f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten const mapping *entry; 4450f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten for (entry = mappings; entry->mFlag != AUDIO_INPUT_FLAG_NONE; entry++) { 4460f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten allFlags = (audio_input_flags_t) (allFlags | entry->mFlag); 4470f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten if (flags & entry->mFlag) { 4480f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten if (!result.isEmpty()) { 4490f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten result.append("|"); 4500f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 4510f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten result.append(entry->mString); 4520f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 4530f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 4540f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten if (flags & ~allFlags) { 4550f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten if (!result.isEmpty()) { 4560f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten result.append("|"); 4570f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 4580f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten result.appendFormat("0x%X", flags & ~allFlags); 4590f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 4600f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten if (result.isEmpty()) { 4610f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten result.append(entry->mString); 4620f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 4630f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten return result; 4640f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten} 4650f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten 4660f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn KastenString8 outputFlagsToString(audio_output_flags_t flags) 46797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten{ 46897b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten static const struct mapping { 46997b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten audio_output_flags_t mFlag; 47097b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten const char * mString; 47197b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten } mappings[] = { 47297b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten AUDIO_OUTPUT_FLAG_DIRECT, "DIRECT", 47397b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten AUDIO_OUTPUT_FLAG_PRIMARY, "PRIMARY", 47497b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten AUDIO_OUTPUT_FLAG_FAST, "FAST", 47597b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten AUDIO_OUTPUT_FLAG_DEEP_BUFFER, "DEEP_BUFFER", 476dfb0e115d827887e2f56a8877fe41b256d24360eGlenn Kasten AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD, "COMPRESS_OFFLOAD", 47797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten AUDIO_OUTPUT_FLAG_NON_BLOCKING, "NON_BLOCKING", 47897b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten AUDIO_OUTPUT_FLAG_HW_AV_SYNC, "HW_AV_SYNC", 47997b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten AUDIO_OUTPUT_FLAG_NONE, "NONE", // must be last 48097b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten }; 48197b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten String8 result; 48297b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten audio_output_flags_t allFlags = AUDIO_OUTPUT_FLAG_NONE; 48397b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten const mapping *entry; 48497b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten for (entry = mappings; entry->mFlag != AUDIO_OUTPUT_FLAG_NONE; entry++) { 48597b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten allFlags = (audio_output_flags_t) (allFlags | entry->mFlag); 48697b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten if (flags & entry->mFlag) { 48797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten if (!result.isEmpty()) { 48897b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten result.append("|"); 48997b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten } 49097b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten result.append(entry->mString); 49197b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten } 49297b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten } 49397b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten if (flags & ~allFlags) { 49497b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten if (!result.isEmpty()) { 49597b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten result.append("|"); 49697b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten } 49797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten result.appendFormat("0x%X", flags & ~allFlags); 49897b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten } 49997b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten if (result.isEmpty()) { 50097b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten result.append(entry->mString); 50197b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten } 50297b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten return result; 50397b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten} 50497b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten 5050f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kastenconst char *sourceToString(audio_source_t source) 5060f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten{ 5070f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten switch (source) { 5080f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten case AUDIO_SOURCE_DEFAULT: return "default"; 5090f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten case AUDIO_SOURCE_MIC: return "mic"; 5100f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten case AUDIO_SOURCE_VOICE_UPLINK: return "voice uplink"; 5110f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten case AUDIO_SOURCE_VOICE_DOWNLINK: return "voice downlink"; 5120f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten case AUDIO_SOURCE_VOICE_CALL: return "voice call"; 5130f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten case AUDIO_SOURCE_CAMCORDER: return "camcorder"; 5140f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten case AUDIO_SOURCE_VOICE_RECOGNITION: return "voice recognition"; 5150f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten case AUDIO_SOURCE_VOICE_COMMUNICATION: return "voice communication"; 5160f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten case AUDIO_SOURCE_REMOTE_SUBMIX: return "remote submix"; 5170f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten case AUDIO_SOURCE_FM_TUNER: return "FM tuner"; 5180f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten case AUDIO_SOURCE_HOTWORD: return "hotword"; 5190f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten default: return "unknown"; 5200f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 5210f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten} 5220f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten 52381784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, 52472e3f39146fce4686bd96f11057c051bea376dfbEric Laurent audio_devices_t outDevice, audio_devices_t inDevice, type_t type, bool systemReady) 52581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent : Thread(false /*canCallJava*/), 52681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mType(type), 5279b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten mAudioFlinger(audioFlinger), 52870949c47fbae3f836d15f040551d7631be3ed7c2Glenn Kasten // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize 529deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten // are set by PlaybackThread::readOutputParameters_l() or 530deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten // RecordThread::readInputParameters_l() 531fd4779740ec3e9e865d5514464df26d015354388Eric Laurent //FIXME: mStandby should be true here. Is this some kind of hack? 53281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStandby(false), mOutDevice(outDevice), mInDevice(inDevice), 53381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id), 53481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mName will be set by concrete (non-virtual) subclass 53572e3f39146fce4686bd96f11057c051bea376dfbEric Laurent mDeathRecipient(new PMDeathRecipient(this)), 53672e3f39146fce4686bd96f11057c051bea376dfbEric Laurent mSystemReady(systemReady) 53781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 538296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent memset(&mPatch, 0, sizeof(struct audio_patch)); 53981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 54081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 54181784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::ThreadBase::~ThreadBase() 54281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 543c6ae3c8a261794fd4445e4e152d1ada074a3f92fGlenn Kasten // mConfigEvents should be empty, but just in case it isn't, free the memory it owns 544c6ae3c8a261794fd4445e4e152d1ada074a3f92fGlenn Kasten mConfigEvents.clear(); 545c6ae3c8a261794fd4445e4e152d1ada074a3f92fGlenn Kasten 54681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // do not lock the mutex in destructor 54781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock_l(); 54881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mPowerManager != 0) { 54906b46062d2f8bc82ca3061a23d197734ae51918bMarco Nelissen sp<IBinder> binder = IInterface::asBinder(mPowerManager); 55081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent binder->unlinkToDeath(mDeathRecipient); 55181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 55281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 55381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 554cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kastenstatus_t AudioFlinger::ThreadBase::readyToRun() 555cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten{ 556cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten status_t status = initCheck(); 557cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten if (status == NO_ERROR) { 558cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten ALOGI("AudioFlinger's thread %p ready to run", this); 559cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten } else { 560cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten ALOGE("No working audio driver found."); 561cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten } 562cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten return status; 563cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten} 564cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten 56581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::exit() 56681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 56781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("ThreadBase::exit"); 56881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // do any cleanup required for exit to succeed 56981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent preExit(); 57081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 57181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // This lock prevents the following race in thread (uniprocessor for illustration): 57281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if (!exitPending()) { 57381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // // context switch from here to exit() 57481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // // exit() calls requestExit(), what exitPending() observes 57581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // // exit() calls signal(), which is dropped since no waiters 57681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // // context switch back from exit() to here 57781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mWaitWorkCV.wait(...); 57881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // // now thread is hung 57981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // } 58081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AutoMutex lock(mLock); 58181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent requestExit(); 58281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitWorkCV.broadcast(); 58381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 58481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // When Thread::requestExitAndWait is made virtual and this method is renamed to 58581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" 58681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent requestExitAndWait(); 58781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 58881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 58981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) 59081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 59181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status; 59281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 59381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); 59481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 59581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5961035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent return sendSetParameterConfigEvent_l(keyValuePairs); 5971035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent} 5981035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 5991035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// sendConfigEvent_l() must be called with ThreadBase::mLock held 6001035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// Can temporarily release the lock if waiting for a reply from processConfigEvents_l(). 6011035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentstatus_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event) 6021035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent{ 6031035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status_t status = NO_ERROR; 6041035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 60572e3f39146fce4686bd96f11057c051bea376dfbEric Laurent if (event->mRequiresSystemReady && !mSystemReady) { 60672e3f39146fce4686bd96f11057c051bea376dfbEric Laurent event->mWaitStatus = false; 60772e3f39146fce4686bd96f11057c051bea376dfbEric Laurent mPendingConfigEvents.add(event); 60872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent return status; 60972e3f39146fce4686bd96f11057c051bea376dfbEric Laurent } 6101035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mConfigEvents.add(event); 6111035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent ALOGV("sendConfigEvent_l() num events %d event %d", mConfigEvents.size(), event->mType); 61281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitWorkCV.signal(); 6131035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mLock.unlock(); 6141035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent { 6151035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent Mutex::Autolock _l(event->mLock); 6161035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent while (event->mWaitStatus) { 6171035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) { 6181035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent event->mStatus = TIMED_OUT; 6191035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent event->mWaitStatus = false; 6201035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 6211035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 6221035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = event->mStatus; 62381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 6241035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mLock.lock(); 62581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 62681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 62781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 62873e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurentvoid AudioFlinger::ThreadBase::sendIoConfigEvent(audio_io_config_event event) 62981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 63081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 63173e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent sendIoConfigEvent_l(event); 63281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 63381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 63481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// sendIoConfigEvent_l() must be called with ThreadBase::mLock held 63573e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurentvoid AudioFlinger::ThreadBase::sendIoConfigEvent_l(audio_io_config_event event) 63681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 63773e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event); 6381035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sendConfigEvent_l(configEvent); 63981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 64081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 64172e3f39146fce4686bd96f11057c051bea376dfbEric Laurentvoid AudioFlinger::ThreadBase::sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio) 64272e3f39146fce4686bd96f11057c051bea376dfbEric Laurent{ 64372e3f39146fce4686bd96f11057c051bea376dfbEric Laurent Mutex::Autolock _l(mLock); 64472e3f39146fce4686bd96f11057c051bea376dfbEric Laurent sendPrioConfigEvent_l(pid, tid, prio); 64572e3f39146fce4686bd96f11057c051bea376dfbEric Laurent} 64672e3f39146fce4686bd96f11057c051bea376dfbEric Laurent 64781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// sendPrioConfigEvent_l() must be called with ThreadBase::mLock held 64881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio) 64981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 6501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio); 6511035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sendConfigEvent_l(configEvent); 65281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 65381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 6541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held 6551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentstatus_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair) 65681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 6571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sp<ConfigEvent> configEvent = (ConfigEvent *)new SetParameterConfigEvent(keyValuePair); 6581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent return sendConfigEvent_l(configEvent); 659f777331418a86cd9fd709af898ef24a69967aeb4Glenn Kasten} 660f777331418a86cd9fd709af898ef24a69967aeb4Glenn Kasten 6611c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::ThreadBase::sendCreateAudioPatchConfigEvent( 6621c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent const struct audio_patch *patch, 6631c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_patch_handle_t *handle) 6641c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{ 6651c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent Mutex::Autolock _l(mLock); 6661c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle); 6671c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status_t status = sendConfigEvent_l(configEvent); 6681c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent if (status == NO_ERROR) { 6691c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent CreateAudioPatchConfigEventData *data = 6701c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent (CreateAudioPatchConfigEventData *)configEvent->mData.get(); 6711c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent *handle = data->mHandle; 6721c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 6731c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent return status; 6741c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent} 6751c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 6761c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::ThreadBase::sendReleaseAudioPatchConfigEvent( 6771c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent const audio_patch_handle_t handle) 6781c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{ 6791c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent Mutex::Autolock _l(mLock); 6801c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle); 6811c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent return sendConfigEvent_l(configEvent); 6821c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent} 6831c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 6841c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 6852cfbf88b89854f30b295e8ae26a031edb8d712f8Glenn Kasten// post condition: mConfigEvents.isEmpty() 686021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurentvoid AudioFlinger::ThreadBase::processConfigEvents_l() 687f777331418a86cd9fd709af898ef24a69967aeb4Glenn Kasten{ 6881035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent bool configChanged = false; 6891035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 69081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent while (!mConfigEvents.isEmpty()) { 6911035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent ALOGV("processConfigEvents_l() remaining events %d", mConfigEvents.size()); 6921035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sp<ConfigEvent> event = mConfigEvents[0]; 69381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mConfigEvents.removeAt(0); 6941035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent switch (event->mType) { 6953468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten case CFG_EVENT_PRIO: { 6961035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get(); 6971035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // FIXME Need to understand why this has to be done asynchronously 6981035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent int err = requestPriority(data->mPid, data->mTid, data->mPrio, 6993468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten true /*asynchronous*/); 7003468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten if (err != 0) { 7013468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", 7021035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent data->mPrio, data->mPid, data->mTid, err); 7033468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten } 7043468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten } break; 7053468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten case CFG_EVENT_IO: { 7061035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent IoConfigEventData *data = (IoConfigEventData *)event->mData.get(); 70773e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent ioConfigChanged(data->mEvent); 7081035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } break; 7091035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent case CFG_EVENT_SET_PARAMETER: { 7101035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get(); 7111035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) { 7121035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent configChanged = true; 713d5418eb594435c958d6c37fa9938161a0112adbdGlenn Kasten } 7143468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten } break; 7151c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent case CFG_EVENT_CREATE_AUDIO_PATCH: { 7161c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent CreateAudioPatchConfigEventData *data = 7171c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent (CreateAudioPatchConfigEventData *)event->mData.get(); 7181c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle); 7191c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } break; 7201c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent case CFG_EVENT_RELEASE_AUDIO_PATCH: { 7211c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent ReleaseAudioPatchConfigEventData *data = 7221c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent (ReleaseAudioPatchConfigEventData *)event->mData.get(); 7231c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent event->mStatus = releaseAudioPatch_l(data->mHandle); 7241c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } break; 7253468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten default: 7261035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType); 7273468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten break; 72881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 7291035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent { 7301035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent Mutex::Autolock _l(event->mLock); 7311035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (event->mWaitStatus) { 7321035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent event->mWaitStatus = false; 7331035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent event->mCond.signal(); 7341035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 7351035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 7361035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this); 7371035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 7381035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 7391035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (configChanged) { 7401035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent cacheParameters_l(); 74181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 74281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 74381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 744b220884bf3129253cc5bc8d030bc475411ea4911Marco NelissenString8 channelMaskToString(audio_channel_mask_t mask, bool output) { 745b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen String8 s; 746f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung const audio_channel_representation_t representation = audio_channel_mask_get_representation(mask); 747f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung 748f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung switch (representation) { 749f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung case AUDIO_CHANNEL_REPRESENTATION_POSITION: { 750f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (output) { 751f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, "); 752f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, "); 753f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, "); 754f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, "); 755f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, "); 756f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, "); 757f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, "); 758f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, "); 759f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, "); 760f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, "); 761f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, "); 762f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,"); 763f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, "); 764f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, "); 765f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, "); 766f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, "); 767f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " ); 768f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " ); 769f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown, "); 770f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung } else { 771f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, "); 772f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, "); 773f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, "); 774f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, "); 775f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, "); 776f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, "); 777f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, "); 778f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, "); 779f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, "); 780f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, "); 781f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, "); 782f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, "); 783f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, "); 784f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, "); 785f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown, "); 786f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung } 787f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung const int len = s.length(); 788f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (len > 2) { 789f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung char *str = s.lockBuffer(len); // needed? 790f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung s.unlockBuffer(len - 2); // remove trailing ", " 791f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung } 792f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung return s; 793b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 794f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung case AUDIO_CHANNEL_REPRESENTATION_INDEX: 795f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung s.appendFormat("index mask, bits:%#x", audio_channel_mask_get_bits(mask)); 796f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung return s; 797f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung default: 798f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung s.appendFormat("unknown mask, representation:%d bits:%#x", 799f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung representation, audio_channel_mask_get_bits(mask)); 800f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung return s; 801b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 802b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen} 803b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen 8040f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args __unused) 80581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 80681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const size_t SIZE = 256; 80781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char buffer[SIZE]; 80881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent String8 result; 80981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 81081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool locked = AudioFlinger::dumpTryLock(mLock); 81181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!locked) { 81297b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten dprintf(fd, "thread %p may be deadlocked\n", this); 813b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 814b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen 8150b89bc0d285b8fd4798df1ff0ba9f93851a3bd48Glenn Kasten dprintf(fd, " Thread name: %s\n", mThreadName); 81687cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " I/O handle: %d\n", mId); 81787cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " TID: %d\n", getTid()); 81887cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Standby: %s\n", mStandby ? "yes" : "no"); 81997b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten dprintf(fd, " Sample rate: %u Hz\n", mSampleRate); 82087cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " HAL frame count: %zu\n", mFrameCount); 82197b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten dprintf(fd, " HAL format: 0x%x (%s)\n", mHALFormat, formatToString(mHALFormat)); 82287cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " HAL buffer size: %u bytes\n", mBufferSize); 82397b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten dprintf(fd, " Channel count: %u\n", mChannelCount); 82497b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten dprintf(fd, " Channel mask: 0x%08x (%s)\n", mChannelMask, 825b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen channelMaskToString(mChannelMask, mType != RECORD).string()); 82697b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten dprintf(fd, " Format: 0x%x (%s)\n", mFormat, formatToString(mFormat)); 82797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten dprintf(fd, " Frame size: %zu bytes\n", mFrameSize); 82887cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Pending config events:"); 829b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numConfig = mConfigEvents.size(); 830b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (numConfig) { 831b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen for (size_t i = 0; i < numConfig; i++) { 832b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen mConfigEvents[i]->dump(buffer, SIZE); 83387cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, "\n %s", buffer); 834b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 83587cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, "\n"); 836b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } else { 83787cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " none\n"); 83881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 8390b89bc0d285b8fd4798df1ff0ba9f93851a3bd48Glenn Kasten dprintf(fd, " Output device: %#x (%s)\n", mOutDevice, devicesToString(mOutDevice).string()); 8400b89bc0d285b8fd4798df1ff0ba9f93851a3bd48Glenn Kasten dprintf(fd, " Input device: %#x (%s)\n", mInDevice, devicesToString(mInDevice).string()); 8410b89bc0d285b8fd4798df1ff0ba9f93851a3bd48Glenn Kasten dprintf(fd, " Audio source: %d (%s)\n", mAudioSource, sourceToString(mAudioSource)); 84281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 84381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (locked) { 84481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mLock.unlock(); 84581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 84681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 84781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 84881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) 84981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 85081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const size_t SIZE = 256; 85181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char buffer[SIZE]; 85281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent String8 result; 85381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 854b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numEffectChains = mEffectChains.size(); 8551d6fa7af1288b550faabe4ec2cf98684236723dbNarayan Kamath snprintf(buffer, SIZE, " %zu Effect Chains\n", numEffectChains); 85681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent write(fd, buffer, strlen(buffer)); 85781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 858b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen for (size_t i = 0; i < numEffectChains; ++i) { 85981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = mEffectChains[i]; 86081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 86181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->dump(fd, args); 86281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 86381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 86481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 86581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 866e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissenvoid AudioFlinger::ThreadBase::acquireWakeLock(int uid) 86781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 86881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 869e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen acquireWakeLock_l(uid); 87081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 87181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 872014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan KamathString16 AudioFlinger::ThreadBase::getWakeLockTag() 873014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath{ 874014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath switch (mType) { 875bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten case MIXER: 876bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten return String16("AudioMix"); 877bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten case DIRECT: 878bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten return String16("AudioDirectOut"); 879bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten case DUPLICATING: 880bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten return String16("AudioDup"); 881bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten case RECORD: 882bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten return String16("AudioIn"); 883bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten case OFFLOAD: 884bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten return String16("AudioOffload"); 885bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten default: 886bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten ALOG_ASSERT(false); 887bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten return String16("AudioUnknown"); 888014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath } 889014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath} 890014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath 891e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissenvoid AudioFlinger::ThreadBase::acquireWakeLock_l(int uid) 89281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 893462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen getPowerManager_l(); 89481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mPowerManager != 0) { 89581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<IBinder> binder = new BBinder(); 896e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen status_t status; 897e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen if (uid >= 0) { 898547789d25dc6bd6561553bcf6b384fb0d4fee834Eric Laurent status = mPowerManager->acquireWakeLockWithUid(POWERMANAGER_PARTIAL_WAKE_LOCK, 899e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen binder, 900014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath getWakeLockTag(), 901e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen String16("media"), 9023abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten uid, 9033abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten true /* FIXME force oneway contrary to .aidl */); 904e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen } else { 905547789d25dc6bd6561553bcf6b384fb0d4fee834Eric Laurent status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, 906e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen binder, 907014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath getWakeLockTag(), 9083abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten String16("media"), 9093abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten true /* FIXME force oneway contrary to .aidl */); 910e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen } 91181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (status == NO_ERROR) { 91281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWakeLockToken = binder; 91381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 914d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status); 91581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 91681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 91781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 91881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::releaseWakeLock() 91981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 92081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 92181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock_l(); 92281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 92381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 92481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::releaseWakeLock_l() 92581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 92681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mWakeLockToken != 0) { 927d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten ALOGV("releaseWakeLock_l() %s", mThreadName); 92881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mPowerManager != 0) { 9293abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten mPowerManager->releaseWakeLock(mWakeLockToken, 0, 9303abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten true /* FIXME force oneway contrary to .aidl */); 93181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 93281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWakeLockToken.clear(); 93381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 93481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 93581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 936462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissenvoid AudioFlinger::ThreadBase::updateWakeLockUids(const SortedVector<int> &uids) { 937462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen Mutex::Autolock _l(mLock); 938462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen updateWakeLockUids_l(uids); 939462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen} 940462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 941462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissenvoid AudioFlinger::ThreadBase::getPowerManager_l() { 94272e3f39146fce4686bd96f11057c051bea376dfbEric Laurent if (mSystemReady && mPowerManager == 0) { 943462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen // use checkService() to avoid blocking if power service is not up yet 944462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen sp<IBinder> binder = 945462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen defaultServiceManager()->checkService(String16("power")); 946462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen if (binder == 0) { 947d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten ALOGW("Thread %s cannot connect to the power manager service", mThreadName); 948462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } else { 949462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mPowerManager = interface_cast<IPowerManager>(binder); 950462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen binder->linkToDeath(mDeathRecipient); 951462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } 952462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } 953462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen} 954462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 955462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissenvoid AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<int> &uids) { 956462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen getPowerManager_l(); 957462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen if (mWakeLockToken == NULL) { 958462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen ALOGE("no wake lock to update!"); 959462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen return; 960462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } 961462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen if (mPowerManager != 0) { 962462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen sp<IBinder> binder = new BBinder(); 963462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen status_t status; 9643abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten status = mPowerManager->updateWakeLockUids(mWakeLockToken, uids.size(), uids.array(), 9653abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten true /* FIXME force oneway contrary to .aidl */); 966d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status); 967462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } 968462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen} 969462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 97081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::clearPowerManager() 97181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 97281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 97381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock_l(); 97481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPowerManager.clear(); 97581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 97681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 9770f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) 97881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 97981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<ThreadBase> thread = mThread.promote(); 98081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread != 0) { 98181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread->clearPowerManager(); 98281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 98381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("power manager service died !!!"); 98481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 98581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 98681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::setEffectSuspended( 98781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const effect_uuid_t *type, bool suspend, int sessionId) 98881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 98981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 99081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent setEffectSuspended_l(type, suspend, sessionId); 99181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 99281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 99381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::setEffectSuspended_l( 99481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const effect_uuid_t *type, bool suspend, int sessionId) 99581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 99681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(sessionId); 99781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 99881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (type != NULL) { 99981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setEffectSuspended_l(type, suspend); 100081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 100181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setEffectSuspendedAll_l(suspend); 100281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 100381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 100481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 100581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent updateSuspendedSessions_l(type, suspend, sessionId); 100681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 100781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 100881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) 100981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 101081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); 101181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (index < 0) { 101281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 101381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 101481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 101581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = 101681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSuspendedSessions.valueAt(index); 101781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 101881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < sessionEffects.size(); i++) { 101981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<SuspendedSessionDesc> desc = sessionEffects.valueAt(i); 102081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (int j = 0; j < desc->mRefCount; j++) { 102181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { 102281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setEffectSuspendedAll_l(true); 102381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 102481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", 102581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc->mType.timeLow); 102681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setEffectSuspended_l(&desc->mType, true); 102781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 102881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 102981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 103081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 103181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 103281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, 103381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool suspend, 103481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId) 103581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 103681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ssize_t index = mSuspendedSessions.indexOfKey(sessionId); 103781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 103881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; 103981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 104081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (suspend) { 104181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (index >= 0) { 104281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionEffects = mSuspendedSessions.valueAt(index); 104381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 104481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSuspendedSessions.add(sessionId, sessionEffects); 104581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 104681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 104781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (index < 0) { 104881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 104981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 105081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionEffects = mSuspendedSessions.valueAt(index); 105181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 105281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 105381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 105481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int key = EffectChain::kKeyForSuspendAll; 105581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (type != NULL) { 105681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent key = type->timeLow; 105781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 105881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent index = sessionEffects.indexOfKey(key); 105981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 106081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<SuspendedSessionDesc> desc; 106181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (suspend) { 106281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (index >= 0) { 106381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc = sessionEffects.valueAt(index); 106481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 106581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc = new SuspendedSessionDesc(); 106681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (type != NULL) { 106781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc->mType = *type; 106881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 106981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionEffects.add(key, desc); 107081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); 107181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 107281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc->mRefCount++; 107381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 107481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (index < 0) { 107581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 107681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 107781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc = sessionEffects.valueAt(index); 107881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (--desc->mRefCount == 0) { 107981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); 108081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionEffects.removeItemsAt(index); 108181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionEffects.isEmpty()) { 108281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("updateSuspendedSessions_l() restore removing session %d", 108381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionId); 108481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSuspendedSessions.removeItem(sessionId); 108581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 108681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 108781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 108881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!sessionEffects.isEmpty()) { 108981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); 109081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 109181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 109281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 109381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, 109481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool enabled, 109581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId) 109681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 109781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 109881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); 109981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 110081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 110181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, 110281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool enabled, 110381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId) 110481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 110581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mType != RECORD) { 110681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on 110781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // another session. This gives the priority to well behaved effect control panels 110881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // and applications not using global effects. 110981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect 111081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // global effects 111181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) { 111281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); 111381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 111481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 111581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 111681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(sessionId); 111781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 111881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->checkSuspendOnEffectEnabled(effect, enabled); 111981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 112081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 112181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 112281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held 112381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( 112481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<AudioFlinger::Client>& client, 112581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<IEffectClient>& effectClient, 112681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int32_t priority, 112781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId, 112881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect_descriptor_t *desc, 112981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int *enabled, 11309156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten status_t *status) 113181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 113281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectModule> effect; 113381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectHandle> handle; 113481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t lStatus; 113581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain; 113681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool chainCreated = false; 113781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool effectCreated = false; 113881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool effectRegistered = false; 113981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 114081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = initCheck(); 114181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR) { 114281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("createEffect_l() Audio driver not initialized."); 114381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 114481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 114581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 114698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Reject any effect on Direct output threads for now, since the format of 114798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo). 114898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mType == DIRECT) { 114998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung ALOGW("createEffect_l() Cannot add effect %s on Direct output type thread %s", 1150d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten desc->name, mThreadName); 115198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung lStatus = BAD_VALUE; 115298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung goto Exit; 115398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 115498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung 1155389cfdbb9a92a438a0d7710321c2964c7ad55ecaAndy Hung // Reject any effect on mixer or duplicating multichannel sinks. 11569a59276fb465e492138e0576523b54079671e8f4Andy Hung // TODO: fix both format and multichannel issues with effects. 1157389cfdbb9a92a438a0d7710321c2964c7ad55ecaAndy Hung if ((mType == MIXER || mType == DUPLICATING) && mChannelCount != FCC_2) { 1158389cfdbb9a92a438a0d7710321c2964c7ad55ecaAndy Hung ALOGW("createEffect_l() Cannot add effect %s for multichannel(%d) %s threads", 1159389cfdbb9a92a438a0d7710321c2964c7ad55ecaAndy Hung desc->name, mChannelCount, mType == MIXER ? "MIXER" : "DUPLICATING"); 11609a59276fb465e492138e0576523b54079671e8f4Andy Hung lStatus = BAD_VALUE; 11619a59276fb465e492138e0576523b54079671e8f4Andy Hung goto Exit; 11629a59276fb465e492138e0576523b54079671e8f4Andy Hung } 11639a59276fb465e492138e0576523b54079671e8f4Andy Hung 11645baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent // Allow global effects only on offloaded and mixer threads 11655baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { 11665baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent switch (mType) { 11675baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent case MIXER: 11685baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent case OFFLOAD: 11695baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent break; 11705baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent case DIRECT: 11715baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent case DUPLICATING: 11725baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent case RECORD: 11735baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent default: 1174d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten ALOGW("createEffect_l() Cannot add global effect %s on thread %s", 1175d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten desc->name, mThreadName); 11765baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent lStatus = BAD_VALUE; 11775baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent goto Exit; 11785baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent } 117981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 11805baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent 118181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Only Pre processor effects are allowed on input threads and only on input threads 118281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { 118381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d", 118481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc->name, desc->flags, mType); 118581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 118681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 118781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 118881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 118981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); 119081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 119181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // scope for mLock 119281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 119381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 119481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // check for existing effect chain with the requested audio session 119581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain = getEffectChain_l(sessionId); 119681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain == 0) { 119781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create a new chain for this session 119881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("createEffect_l() new effect chain for session %d", sessionId); 119981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain = new EffectChain(this, sessionId); 120081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent addEffectChain_l(chain); 120181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setStrategy(getStrategyForSession_l(sessionId)); 120281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chainCreated = true; 120381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 120481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect = chain->getEffectFromDesc_l(desc); 120581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 120681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 120781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); 120881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 120981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (effect == 0) { 121081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int id = mAudioFlinger->nextUniqueId(); 121181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Check CPU and memory usage 121281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); 121381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR) { 121481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 121581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 121681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effectRegistered = true; 121781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create a new effect module if none present in the chain 121881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect = new EffectModule(this, chain, desc, id, sessionId); 121981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = effect->status(); 122081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR) { 122181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 122281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 12235baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent effect->setOffloaded(mType == OFFLOAD, mId); 12245baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent 122581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = chain->addEffect_l(effect); 122681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR) { 122781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 122881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 122981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effectCreated = true; 123081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 123181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setDevice(mOutDevice); 123281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setDevice(mInDevice); 123381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setMode(mAudioFlinger->getMode()); 123481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setAudioSource(mAudioSource); 123581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 123681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create effect handle and connect it to effect module 123781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent handle = new EffectHandle(effect, client, effectClient, priority); 1238e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten lStatus = handle->initCheck(); 1239e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten if (lStatus == OK) { 1240e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten lStatus = effect->addHandle(handle.get()); 1241e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten } 124281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (enabled != NULL) { 124381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *enabled = (int)effect->isEnabled(); 124481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 124581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 124681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 124781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit: 124881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { 124981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 125081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (effectCreated) { 125181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->removeEffect_l(effect); 125281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 125381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (effectRegistered) { 125481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSystem::unregisterEffect(effect->id()); 125581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 125681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chainCreated) { 125781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent removeEffectChain_l(chain); 125881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 125981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent handle.clear(); 126081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 126181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 12629156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten *status = lStatus; 126381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return handle; 126481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 126581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 126681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(int sessionId, int effectId) 126781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 126881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 126981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return getEffect_l(sessionId, effectId); 127081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 127181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 127281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId) 127381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 127481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(sessionId); 127581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; 127681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 127781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 127881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and 127981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// PlaybackThread::mLock held 128081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) 128181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 128281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // check for existing effect chain with the requested audio session 128381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId = effect->sessionId(); 128481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(sessionId); 128581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool chainCreated = false; 128681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 12875baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(), 12885baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %x", 12895baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent this, effect->desc().name, effect->desc().flags); 12905baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent 129181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain == 0) { 129281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create a new chain for this session 129381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffect_l() new effect chain for session %d", sessionId); 129481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain = new EffectChain(this, sessionId); 129581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent addEffectChain_l(chain); 129681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setStrategy(getStrategyForSession_l(sessionId)); 129781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chainCreated = true; 129881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 129981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); 130081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 130181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain->getEffectFromId_l(effect->id()) != 0) { 130281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("addEffect_l() %p effect %s already present in chain %p", 130381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent this, effect->desc().name, chain.get()); 130481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 130581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 130681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 13075baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent effect->setOffloaded(mType == OFFLOAD, mId); 13085baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent 130981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status = chain->addEffect_l(effect); 131081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (status != NO_ERROR) { 131181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chainCreated) { 131281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent removeEffectChain_l(chain); 131381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 131481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 131581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 131681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 131781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setDevice(mOutDevice); 131881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setDevice(mInDevice); 131981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setMode(mAudioFlinger->getMode()); 132081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setAudioSource(mAudioSource); 132181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 132281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 132381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 132481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { 132581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 132681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("removeEffect_l() %p effect %p", this, effect.get()); 132781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect_descriptor_t desc = effect->desc(); 132881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 132981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent detachAuxEffect_l(effect->id()); 133081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 133181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 133281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = effect->chain().promote(); 133381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 133481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // remove effect chain if removing last effect 133581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain->removeEffect_l(effect) == 0) { 133681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent removeEffectChain_l(chain); 133781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 133881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 133981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); 134081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 134181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 134281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 134381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::lockEffectChains_l( 134481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<AudioFlinger::EffectChain> >& effectChains) 134581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 134681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effectChains = mEffectChains; 134781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 134881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains[i]->lock(); 134981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 135081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 135181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 135281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::unlockEffectChains( 135381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const Vector< sp<AudioFlinger::EffectChain> >& effectChains) 135481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 135581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < effectChains.size(); i++) { 135681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effectChains[i]->unlock(); 135781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 135881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 135981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 136081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId) 136181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 136281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 136381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return getEffectChain_l(sessionId); 136481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 136581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 136681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) const 136781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 136881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t size = mEffectChains.size(); 136981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < size; i++) { 137081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mEffectChains[i]->sessionId() == sessionId) { 137181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mEffectChains[i]; 137281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 137381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 137481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 137581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 137681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 137781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::setMode(audio_mode_t mode) 137881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 137981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 138081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t size = mEffectChains.size(); 138181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < size; i++) { 138281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains[i]->setMode_l(mode); 138381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 138481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 138581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 138683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::ThreadBase::getAudioPortConfig(struct audio_port_config *config) 138783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{ 138883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->type = AUDIO_PORT_TYPE_MIX; 138983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->ext.mix.handle = mId; 139083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->sample_rate = mSampleRate; 139183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->format = mFormat; 139283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->channel_mask = mChannelMask; 139383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| 139483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent AUDIO_PORT_CONFIG_FORMAT; 139583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent} 139683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 139772e3f39146fce4686bd96f11057c051bea376dfbEric Laurentvoid AudioFlinger::ThreadBase::systemReady() 139872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent{ 139972e3f39146fce4686bd96f11057c051bea376dfbEric Laurent Mutex::Autolock _l(mLock); 140072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent if (mSystemReady) { 140172e3f39146fce4686bd96f11057c051bea376dfbEric Laurent return; 140272e3f39146fce4686bd96f11057c051bea376dfbEric Laurent } 140372e3f39146fce4686bd96f11057c051bea376dfbEric Laurent mSystemReady = true; 140472e3f39146fce4686bd96f11057c051bea376dfbEric Laurent 140572e3f39146fce4686bd96f11057c051bea376dfbEric Laurent for (size_t i = 0; i < mPendingConfigEvents.size(); i++) { 140672e3f39146fce4686bd96f11057c051bea376dfbEric Laurent sendConfigEvent_l(mPendingConfigEvents.editItemAt(i)); 140772e3f39146fce4686bd96f11057c051bea376dfbEric Laurent } 140872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent mPendingConfigEvents.clear(); 140972e3f39146fce4686bd96f11057c051bea376dfbEric Laurent} 141072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent 141183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 141281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 141381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Playback 141481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 141581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 141681784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, 141781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamOut* output, 141881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t id, 141981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_devices_t device, 142072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent type_t type, 142172e3f39146fce4686bd96f11057c051bea376dfbEric Laurent bool systemReady) 142272e3f39146fce4686bd96f11057c051bea376dfbEric Laurent : ThreadBase(audioFlinger, id, device, AUDIO_DEVICE_NONE, type, systemReady), 14232098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung mNormalFrameCount(0), mSinkBuffer(NULL), 14246146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision), 142569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBuffer(NULL), 142669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferSize(0), 142769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferFormat(AUDIO_FORMAT_INVALID), 142869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferValid(false), 14296146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision), 143098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBuffer(NULL), 143198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferSize(0), 143298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferFormat(AUDIO_FORMAT_INVALID), 143398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferValid(false), 1434c1fac191069774c7bfcb062edbb821ea56e7dbc0Glenn Kasten mSuspended(0), mBytesWritten(0), 1435462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mActiveTracksGeneration(0), 143681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mStreamTypes[] initialized in constructor body 143781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutput(output), 143881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), 143981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMixerStatus(MIXER_IDLE), 144081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMixerStatusIgnoringFastTracks(MIXER_IDLE), 1441ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mStandbyDelayNs(AudioFlinger::mStandbyTimeInNsecs), 1442bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining(0), 1443bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mCurrentWriteLength(0), 1444bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mUseAsyncWrite(false), 14453b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence(0), 14463b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence(0), 1447ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent mSignalPending(false), 144881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mScreenState(AudioFlinger::mScreenState), 144981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // index 0 is reserved for normal mixer's submix 1450bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mFastTrackAvailMask(((1 << FastMixerState::kMaxFastTracks) - 1) & ~1), 1451d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent mHwSupportsPause(false), mHwPaused(false), mFlushPending(false), 1452bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten // mLatchD, mLatchQ, 1453bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mLatchDValid(false), mLatchQValid(false) 145481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 1455d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id); 1456d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); 145781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 145881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Assumes constructor is called by AudioFlinger with it's mLock held, but 145981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it would be safer to explicitly pass initial masterVolume/masterMute as 146081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // parameter. 146181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // 146281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If the HAL we are using has support for master volume or master mute, 146381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // then do not attenuate or mute during mixing (just leave the volume at 1.0 146481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // and the mute set to false). 146581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterVolume = audioFlinger->masterVolume_l(); 146681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterMute = audioFlinger->masterMute_l(); 146781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutput && mOutput->audioHwDev) { 146881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutput->audioHwDev->canSetMasterVolume()) { 146981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterVolume = 1.0; 147081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 147181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 147281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutput->audioHwDev->canSetMasterMute()) { 147381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterMute = false; 147481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 147581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 147681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1477deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten readOutputParameters_l(); 147881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1479223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent // ++ operator does not compile 148066e4635cb09fadcaccf912f37c387396c428378aGlenn Kasten for (audio_stream_type_t stream = AUDIO_STREAM_MIN; stream < AUDIO_STREAM_CNT; 148181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent stream = (audio_stream_type_t) (stream + 1)) { 148281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream); 148381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); 148481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 148581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 148681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 148781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread::~PlaybackThread() 148881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 14899e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten mAudioFlinger->unregisterWriter(mNBLogWriter); 1490010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung free(mSinkBuffer); 149169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung free(mMixerBuffer); 149298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung free(mEffectBuffer); 149381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 149481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 149581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) 149681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 149781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpInternals(fd, args); 149881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpTracks(fd, args); 149981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpEffectChains(fd, args); 150081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 150181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 15020f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args __unused) 150381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 150481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const size_t SIZE = 256; 150581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char buffer[SIZE]; 150681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent String8 result; 150781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1508b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.appendFormat(" Stream volumes in dB: "); 150981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { 151081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const stream_type_t *st = &mStreamTypes[i]; 151181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (i > 0) { 151281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result.appendFormat(", "); 151381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 151481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); 151581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (st->mute) { 151681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result.append("M"); 151781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 151881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 151981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result.append("\n"); 152081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent write(fd, result.string(), result.length()); 152181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result.clear(); 152281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1523b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen // These values are "raw"; they will wrap around. See prepareTracks_l() for a better way. 1524b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen FastTrackUnderruns underruns = getFastTrackUnderruns(0); 152587cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Normal mixer raw underrun counters: partial=%u empty=%u\n", 1526b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); 1527b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen 1528b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numtracks = mTracks.size(); 1529b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numactive = mActiveTracks.size(); 153087cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " %d Tracks", numtracks); 1531b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numactiveseen = 0; 1532b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (numtracks) { 153387cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " of which %d are active\n", numactive); 1534b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen Track::appendDumpHeader(result); 1535b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen for (size_t i = 0; i < numtracks; ++i) { 1536b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen sp<Track> track = mTracks[i]; 1537b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (track != 0) { 1538b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen bool active = mActiveTracks.indexOf(track) >= 0; 1539b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (active) { 1540b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen numactiveseen++; 1541b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 1542b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen track->dump(buffer, SIZE, active); 1543b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append(buffer); 1544b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 154581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1546b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } else { 1547b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append("\n"); 154881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1549b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (numactiveseen != numactive) { 1550b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen // some tracks in the active list were not in the tracks list 1551b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen snprintf(buffer, SIZE, " The following tracks are in the active list but" 1552b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen " not in the track list\n"); 1553b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append(buffer); 1554b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen Track::appendDumpHeader(result); 1555b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen for (size_t i = 0; i < numactive; ++i) { 1556b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen sp<Track> track = mActiveTracks[i].promote(); 1557b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (track != 0 && mTracks.indexOf(track) < 0) { 1558b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen track->dump(buffer, SIZE, true); 1559b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append(buffer); 1560b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 156181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 156281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1563b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen 156481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent write(fd, result.string(), result.size()); 156581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 156681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 156781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) 156881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 156997b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten dprintf(fd, "\nOutput thread %p type %d (%s):\n", this, type(), threadTypeToString(type())); 157044182c206f7c5584ef2cf504da6be98fab665dbfGlenn Kasten 157144182c206f7c5584ef2cf504da6be98fab665dbfGlenn Kasten dumpBase(fd, args); 157244182c206f7c5584ef2cf504da6be98fab665dbfGlenn Kasten 157387cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Normal frame count: %zu\n", mNormalFrameCount); 157487cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime)); 157587cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Total writes: %d\n", mNumWrites); 157687cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Delayed writes: %d\n", mNumDelayedWrites); 157787cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Blocked in write: %s\n", mInWrite ? "yes" : "no"); 157887cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Suspend count: %d\n", mSuspended); 157987cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Sink buffer : %p\n", mSinkBuffer); 158087cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Mixer buffer: %p\n", mMixerBuffer); 158187cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Effect buffer: %p\n", mEffectBuffer); 158287cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Fast track availMask=%#x\n", mFastTrackAvailMask); 158397b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten AudioStreamOut *output = mOutput; 158497b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten audio_output_flags_t flags = output != NULL ? output->flags : AUDIO_OUTPUT_FLAG_NONE; 158597b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten String8 flagsAsString = outputFlagsToString(flags); 158697b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten dprintf(fd, " AudioStreamOut: %p flags %#x (%s)\n", output, flags, flagsAsString.string()); 158781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 158881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 158981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Thread virtuals 159081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 159181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::onFirstRef() 159281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 1593d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); 159481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 159581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 159681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ThreadBase virtuals 159781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::preExit() 159881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 159981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV(" preExit()"); 160081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME this is using hard-coded strings but in the future, this functionality will be 160181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // converted to use audio HAL extensions required to support tunneling 160281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutput->stream->common.set_parameters(&mOutput->stream->common, "exiting=1"); 160381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 160481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 160581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held 160681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( 160781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<AudioFlinger::Client>& client, 160881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_stream_type_t streamType, 160981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t sampleRate, 161081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_format_t format, 161181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_channel_mask_t channelMask, 161274935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten size_t *pFrameCount, 161381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<IMemory>& sharedBuffer, 161481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId, 161581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent IAudioFlinger::track_flags_t *flags, 161681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t tid, 1617462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen int uid, 161881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t *status) 161981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 162074935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten size_t frameCount = *pFrameCount; 162181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track; 162281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t lStatus; 162381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 162481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool isTimed = (*flags & IAudioFlinger::TRACK_TIMED) != 0; 162581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 162681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // client expresses a preference for FAST, but we get the final say 162781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (*flags & IAudioFlinger::TRACK_FAST) { 162881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ( 162981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // not timed 163081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (!isTimed) && 163181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // either of these use cases: 163281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ( 163381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // use case 1: shared buffer with any frame count 163481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ( 163581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (sharedBuffer != 0) 163681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ) || 16371dfe2f9c2d03fc8d0ed0cdfe0b9fb894bc0bcc11Glenn Kasten // use case 2: frame count is default or at least as large as HAL 163881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ( 16391dfe2f9c2d03fc8d0ed0cdfe0b9fb894bc0bcc11Glenn Kasten // we formerly checked for a callback handler (non-0 tid), 16401dfe2f9c2d03fc8d0ed0cdfe0b9fb894bc0bcc11Glenn Kasten // but that is no longer required for TRANSFER_OBTAIN mode 164181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ((frameCount == 0) || 1642b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten (frameCount >= mFrameCount)) 164381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ) 164481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ) && 164581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // PCM data 164681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_is_linear_pcm(format) && 16471f439e1cf16a29347288ba9ddd06c0b6d086a145Andy Hung // TODO: extract as a data library function that checks that a computationally 16481f439e1cf16a29347288ba9ddd06c0b6d086a145Andy Hung // expensive downmixer is not required: isFastOutputChannelConversion() 16499a59276fb465e492138e0576523b54079671e8f4Andy Hung (channelMask == mChannelMask || 16501f439e1cf16a29347288ba9ddd06c0b6d086a145Andy Hung mChannelMask != AUDIO_CHANNEL_OUT_STEREO || 16511f439e1cf16a29347288ba9ddd06c0b6d086a145Andy Hung (channelMask == AUDIO_CHANNEL_OUT_MONO 16521f439e1cf16a29347288ba9ddd06c0b6d086a145Andy Hung /* && mChannelMask == AUDIO_CHANNEL_OUT_STEREO */)) && 165381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // hardware sample rate 165481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (sampleRate == mSampleRate) && 165581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // normal mixer has an associated fast mixer 165681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent hasFastMixer() && 165781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // there are sufficient fast track slots available 165881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (mFastTrackAvailMask != 0) 165981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME test that MixerThread for this fast track has a capable output HAL 166081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME add a permission test also? 166181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ) { 166281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if frameCount not specified, then it defaults to fast mixer (HAL) frame count 166381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (frameCount == 0) { 16640349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten // read the fast track multiplier property the first time it is needed 16650349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit); 16660349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten if (ok != 0) { 16670349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten ALOGE("%s pthread_once failed: %d", __func__, ok); 16680349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten } 16690349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten frameCount = mFrameCount * sFastTrackMultiplier; 167081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 167181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%d mFrameCount=%d", 167281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent frameCount, mFrameCount); 167381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 167481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: isTimed=%d sharedBuffer=%p frameCount=%d " 16756146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung "mFrameCount=%d format=%#x mFormat=%#x isLinear=%d channelMask=%#x " 16766146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung "sampleRate=%u mSampleRate=%u " 167781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", 16786146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung isTimed, sharedBuffer.get(), frameCount, mFrameCount, format, mFormat, 167981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_is_linear_pcm(format), 168081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); 168181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *flags &= ~IAudioFlinger::TRACK_FAST; 16820e48d25606c82def035ad10a5b3923767a765cddAndy Hung } 16830e48d25606c82def035ad10a5b3923767a765cddAndy Hung } 16840e48d25606c82def035ad10a5b3923767a765cddAndy Hung // For normal PCM streaming tracks, update minimum frame count. 16850e48d25606c82def035ad10a5b3923767a765cddAndy Hung // For compatibility with AudioTrack calculation, buffer depth is forced 16860e48d25606c82def035ad10a5b3923767a765cddAndy Hung // to be at least 2 x the normal mixer frame count and cover audio hardware latency. 16870e48d25606c82def035ad10a5b3923767a765cddAndy Hung // This is probably too conservative, but legacy application code may depend on it. 16880e48d25606c82def035ad10a5b3923767a765cddAndy Hung // If you change this calculation, also review the start threshold which is related. 16890e48d25606c82def035ad10a5b3923767a765cddAndy Hung if (!(*flags & IAudioFlinger::TRACK_FAST) 16900e48d25606c82def035ad10a5b3923767a765cddAndy Hung && audio_is_linear_pcm(format) && sharedBuffer == 0) { 16918edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung // this must match AudioTrack.cpp calculateMinFrameCount(). 16928edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung // TODO: Move to a common library 169381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream); 169481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate); 169581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (minBufCount < 2) { 169681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minBufCount = 2; 169781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 16988edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung // For normal mixing tracks, if speed is > 1.0f (normal), AudioTrack 16998edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung // or the client should compute and pass in a larger buffer request. 17000e48d25606c82def035ad10a5b3923767a765cddAndy Hung size_t minFrameCount = 17018edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung minBufCount * sourceFramesNeededWithTimestretch( 17028edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung sampleRate, mNormalFrameCount, 17038edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung mSampleRate, AUDIO_TIMESTRETCH_SPEED_NORMAL /*speed*/); 17040e48d25606c82def035ad10a5b3923767a765cddAndy Hung if (frameCount < minFrameCount) { // including frameCount == 0 170581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent frameCount = minFrameCount; 170681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 170781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 170874935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten *pFrameCount = frameCount; 170981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1710c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten switch (mType) { 1711c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten 1712c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten case DIRECT: 1713993fa0603707e94ce259e95e56838a85b5ccbdc5Glenn Kasten if (audio_is_linear_pcm(format)) { 171481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { 1715cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x " 1716cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten "for output %p with format %#x", 171781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sampleRate, format, channelMask, mOutput, mFormat); 171881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 171981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 172081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 172181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1722c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten break; 1723c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten 1724c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten case OFFLOAD: 1725bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { 1726cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \"" 1727cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten "for output %p with format %#x", 1728bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent sampleRate, format, channelMask, mOutput, mFormat); 1729bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent lStatus = BAD_VALUE; 1730bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent goto Exit; 1731bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1732c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten break; 1733c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten 1734c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten default: 1735993fa0603707e94ce259e95e56838a85b5ccbdc5Glenn Kasten if (!audio_is_linear_pcm(format)) { 1736cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten ALOGE("createTrack_l() Bad parameter: format %#x \"" 1737cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten "for output %p with format %#x", 1738bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent format, mOutput, mFormat); 1739bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent lStatus = BAD_VALUE; 1740bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent goto Exit; 1741bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1742cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) { 174381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); 174481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 174581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 174681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1747c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten break; 1748c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten 174981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 175081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 175181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = initCheck(); 175281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR) { 175315e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten ALOGE("createTrack_l() audio driver not initialized"); 175481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 175581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 175681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 175781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // scope for mLock 175881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 175981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 176081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // all tracks in same audio session must share the same routing strategy otherwise 176181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // conflicts will happen when tracks are moved from one output to another by audio policy 176281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // manager 176381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t strategy = AudioSystem::getStrategyForStream(streamType); 176481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 176581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> t = mTracks[i]; 176683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent if (t != 0 && t->isExternalTrack()) { 176781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); 176881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId == t->sessionId() && strategy != actual) { 176981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", 177081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent strategy, actual); 177181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 177281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 177381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 177481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 177581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 177681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 177781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!isTimed) { 177881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track = new Track(this, client, streamType, sampleRate, format, 177983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent channelMask, frameCount, NULL, sharedBuffer, 178083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent sessionId, uid, *flags, TrackBase::TYPE_DEFAULT); 178181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 178281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track = TimedTrack::create(this, client, streamType, sampleRate, format, 1783462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen channelMask, frameCount, sharedBuffer, sessionId, uid); 178481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1785030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten 1786030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten // new Track always returns non-NULL, 1787030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten // but TimedTrack::create() is a factory that could fail by returning NULL 1788030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY; 1789030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten if (lStatus != NO_ERROR) { 17900cde076ddb283c84c3801a2df4cc3df99bd1577fGlenn Kasten ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus); 179103e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George // track must be cleared from the caller as the caller has the AF lock 179281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 179381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 179481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mTracks.add(track); 179581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 179681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(sessionId); 179781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 179881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); 179981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->setMainBuffer(chain->inBuffer()); 180081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); 180181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->incTrackCnt(); 180281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 180381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 180481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { 180581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t callingPid = IPCThreadState::self()->getCallingPid(); 180681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, 180781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // so ask activity manager to do this on our behalf 180881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); 180981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 181081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 181181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 181281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = NO_ERROR; 181381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 181481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit: 18159156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten *status = lStatus; 181681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return track; 181781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 181881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 181981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const 182081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 182181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return latency; 182281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 182381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 182481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::latency() const 182581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 182681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 182781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return latency_l(); 182881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 182981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::latency_l() const 183081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 183181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (initCheck() == NO_ERROR) { 183281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return correctLatency_l(mOutput->stream->get_latency(mOutput->stream)); 183381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 183481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 183581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 183681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 183781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 183881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::setMasterVolume(float value) 183981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 184081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 184181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Don't apply master volume in SW if our HAL can do it for us. 184281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutput && mOutput->audioHwDev && 184381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutput->audioHwDev->canSetMasterVolume()) { 184481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterVolume = 1.0; 184581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 184681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterVolume = value; 184781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 184881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 184981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 185081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::setMasterMute(bool muted) 185181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 185281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 185381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Don't apply master mute in SW if our HAL can do it for us. 185481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutput && mOutput->audioHwDev && 185581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutput->audioHwDev->canSetMasterMute()) { 185681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterMute = false; 185781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 185881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterMute = muted; 185981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 186081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 186181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 186281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) 186381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 186481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 186581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStreamTypes[stream].volume = value; 1866ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent broadcast_l(); 186781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 186881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 186981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) 187081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 187181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 187281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStreamTypes[stream].mute = muted; 1873ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent broadcast_l(); 187481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 187581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 187681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentfloat AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const 187781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 187881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 187981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mStreamTypes[stream].volume; 188081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 188181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 188281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// addTrack_l() must be called with ThreadBase::mLock held 188381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) 188481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 188581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status = ALREADY_EXISTS; 188681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 188781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // set retry count for buffer fill 188881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mRetryCount = kMaxTrackStartupRetries; 188981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mActiveTracks.indexOf(track) < 0) { 189081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // the track is newly added, make sure it fills up all its 189181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // buffers before playing. This is to ensure the client will 189281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // effectively get the latency it requested. 189383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent if (track->isExternalTrack()) { 1894bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent TrackBase::track_state state = track->mState; 1895bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mLock.unlock(); 1896e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent status = AudioSystem::startOutput(mId, track->streamType(), 1897e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent (audio_session_t)track->sessionId()); 1898bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mLock.lock(); 1899bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // abort track was stopped/paused while we released the lock 1900bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (state != track->mState) { 1901bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (status == NO_ERROR) { 1902bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mLock.unlock(); 1903e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent AudioSystem::stopOutput(mId, track->streamType(), 1904e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent (audio_session_t)track->sessionId()); 1905bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mLock.lock(); 1906bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1907bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return INVALID_OPERATION; 1908bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1909bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // abort if start is rejected by audio policy manager 1910bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (status != NO_ERROR) { 1911bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return PERMISSION_DENIED; 1912bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1913bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent#ifdef ADD_BATTERY_DATA 1914bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // to track the speaker usage 1915bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); 1916bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent#endif 1917bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1918bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 19199f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten track->mFillingUpStatus = track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING; 192081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mResetDone = false; 192181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mPresentationCompleteFrames = 0; 192281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mActiveTracks.add(track); 1923462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mWakeLockUids.add(track->uid()); 1924462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mActiveTracksGeneration++; 1925fd4779740ec3e9e865d5514464df26d015354388Eric Laurent mLatestActiveTrack = track; 1926d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent sp<EffectChain> chain = getEffectChain_l(track->sessionId()); 1927d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent if (chain != 0) { 1928d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), 1929d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent track->sessionId()); 1930d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent chain->incActiveTrackCnt(); 193181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 193281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 193381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = NO_ERROR; 193481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 193581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 19364c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George onAddNewTrack_l(); 193781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 193881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 193981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1940bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) 194181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 1942bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->terminate(); 194381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // active tracks are removed by threadLoop() 1944bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent bool trackActive = (mActiveTracks.indexOf(track) >= 0); 1945bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mState = TrackBase::STOPPED; 1946bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (!trackActive) { 194781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent removeTrack_l(track); 1948ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) { 1949bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mState = TrackBase::STOPPING_1; 195081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1951bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1952bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return trackActive; 195381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 195481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 195581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) 195681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 195781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); 195881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mTracks.remove(track); 195981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent deleteTrackName_l(track->name()); 196081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // redundant as track is about to be destroyed, for dumpsys only 196181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mName = -1; 196281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isFastTrack()) { 196381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int index = track->mFastIndex; 196481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(0 < index && index < (int)FastMixerState::kMaxFastTracks); 196581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); 196681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastTrackAvailMask |= 1 << index; 196781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // redundant as track is about to be destroyed, for dumpsys only 196881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mFastIndex = -1; 196981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 197081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(track->sessionId()); 197181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 197281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->decTrackCnt(); 197381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 197481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 197581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1976ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurentvoid AudioFlinger::PlaybackThread::broadcast_l() 1977bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 1978bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Thread could be blocked waiting for async 1979bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // so signal it to handle state changes immediately 1980bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // If threadLoop is currently unlocked a signal of mWaitWorkCV will 1981bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // be lost so we also flag to prevent it blocking on mWaitWorkCV 1982bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mSignalPending = true; 1983ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent mWaitWorkCV.broadcast(); 1984bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 1985bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 198681784c37c61b09289654b979567a42bf73cd2b12Eric LaurentString8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) 198781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 198881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 198981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (initCheck() != NO_ERROR) { 1990d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten return String8(); 199181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 199281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1993d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten char *s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); 1994d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten const String8 out_s8(s); 199581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent free(s); 199681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return out_s8; 199781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 199881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 199973e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurentvoid AudioFlinger::PlaybackThread::ioConfigChanged(audio_io_config_event event) { 200073e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); 200173e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent ALOGV("PlaybackThread::ioConfigChanged, thread %p, event %d", this, event); 200281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 200373e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent desc->mIoHandle = mId; 200481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 200581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent switch (event) { 200673e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent case AUDIO_OUTPUT_OPENED: 200773e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent case AUDIO_OUTPUT_CONFIG_CHANGED: 2008296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent desc->mPatch = mPatch; 200973e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent desc->mChannelMask = mChannelMask; 201073e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent desc->mSamplingRate = mSampleRate; 201173e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent desc->mFormat = mFormat; 201273e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent desc->mFrameCount = mNormalFrameCount; // FIXME see 201381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // AudioFlinger::frameCount(audio_io_handle_t) 201473e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent desc->mLatency = latency_l(); 201581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 201681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 201773e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent case AUDIO_OUTPUT_CLOSED: 201881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent default: 201981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 202081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 202173e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent mAudioFlinger->ioConfigChanged(event, desc); 202281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 202381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2024bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::writeCallback() 2025bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 2026bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 20273b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->resetWriteBlocked(); 2028bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 2029bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 2030bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::drainCallback() 2031bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 2032bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 20333b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->resetDraining(); 2034bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 2035bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 20363b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence) 2037bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 2038bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 20393b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // reject out of sequence requests 20403b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) { 20413b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence &= ~1; 2042bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mWaitWorkCV.signal(); 2043bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2044bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 2045bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 20463b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence) 2047bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 2048bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 20493b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // reject out of sequence requests 20503b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if ((mDrainSequence & 1) && (sequence == mDrainSequence)) { 20513b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence &= ~1; 2052bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mWaitWorkCV.signal(); 2053bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2054bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 2055bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 2056bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// static 2057bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentint AudioFlinger::PlaybackThread::asyncCallback(stream_callback_event_t event, 20580f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten void *param __unused, 2059bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent void *cookie) 2060bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 2061bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioFlinger::PlaybackThread *me = (AudioFlinger::PlaybackThread *)cookie; 2062bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("asyncCallback() event %d", event); 2063bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent switch (event) { 2064bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent case STREAM_CBK_EVENT_WRITE_READY: 2065bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent me->writeCallback(); 2066bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent break; 2067bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent case STREAM_CBK_EVENT_DRAIN_READY: 2068bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent me->drainCallback(); 2069bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent break; 2070bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent default: 2071bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGW("asyncCallback() unknown event %d", event); 2072bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent break; 2073bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2074bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return 0; 2075bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 2076bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 2077deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kastenvoid AudioFlinger::PlaybackThread::readOutputParameters_l() 207881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 2079adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten // unfortunately we have no way of recovering from errors here, hence the LOG_ALWAYS_FATAL 208081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common); 208181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common); 20827fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten if (!audio_is_output_channel(mChannelMask)) { 2083adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask); 20847fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten } 20859a59276fb465e492138e0576523b54079671e8f4Andy Hung if ((mType == MIXER || mType == DUPLICATING) 20869a59276fb465e492138e0576523b54079671e8f4Andy Hung && !isValidPcmSinkChannelMask(mChannelMask)) { 20879a59276fb465e492138e0576523b54079671e8f4Andy Hung LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output", 20889a59276fb465e492138e0576523b54079671e8f4Andy Hung mChannelMask); 20897fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten } 2090e541269be94f3a1072932d51537905b120ef4733Andy Hung mChannelCount = audio_channel_count_from_out_mask(mChannelMask); 2091463be250de73907965faa6a216c00312bf81e049Andy Hung mHALFormat = mOutput->stream->common.get_format(&mOutput->stream->common); 2092463be250de73907965faa6a216c00312bf81e049Andy Hung mFormat = mHALFormat; 20937fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten if (!audio_is_valid_format(mFormat)) { 2094adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat); 20957fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten } 20966146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung if ((mType == MIXER || mType == DUPLICATING) 20976146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung && !isValidPcmSinkFormat(mFormat)) { 20986146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung LOG_FATAL("HAL format %#x not supported for mixed output", 20996146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung mFormat); 21007fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten } 2101062e67a26e0553dd142be622821f493df541f0c6Phil Burk mFrameSize = mOutput->getFrameSize(); 210270949c47fbae3f836d15f040551d7631be3ed7c2Glenn Kasten mBufferSize = mOutput->stream->common.get_buffer_size(&mOutput->stream->common); 210370949c47fbae3f836d15f040551d7631be3ed7c2Glenn Kasten mFrameCount = mBufferSize / mFrameSize; 210481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mFrameCount & 15) { 210581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("HAL output buffer size is %u frames but AudioMixer requires multiples of 16 frames", 210681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFrameCount); 210781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 210881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2109bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if ((mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) && 2110bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent (mOutput->stream->set_callback != NULL)) { 2111bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mOutput->stream->set_callback(mOutput->stream, 2112bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioFlinger::PlaybackThread::asyncCallback, this) == 0) { 2113bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mUseAsyncWrite = true; 21144de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent mCallbackThread = new AudioFlinger::AsyncCallbackThread(this); 2115bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2116bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2117bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 2118d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent mHwSupportsPause = false; 2119d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) { 2120d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent if (mOutput->stream->pause != NULL) { 2121d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent if (mOutput->stream->resume != NULL) { 2122d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent mHwSupportsPause = true; 2123d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } else { 2124d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent ALOGW("direct output implements pause but not resume"); 2125d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 2126d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } else if (mOutput->stream->resume != NULL) { 2127d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent ALOGW("direct output implements resume but not pause"); 2128d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 2129d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 21306fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk if (!mHwSupportsPause && mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) { 21316fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk LOG_ALWAYS_FATAL("HW_AV_SYNC requested but HAL does not implement pause and resume"); 21326fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk } 2133d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent 2134fbfc3959f4aac839445edc7075532067fef497c2Andy Hung if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) { 2135fbfc3959f4aac839445edc7075532067fef497c2Andy Hung // For best precision, we use float instead of the associated output 2136fbfc3959f4aac839445edc7075532067fef497c2Andy Hung // device format (typically PCM 16 bit). 2137fbfc3959f4aac839445edc7075532067fef497c2Andy Hung 2138fbfc3959f4aac839445edc7075532067fef497c2Andy Hung mFormat = AUDIO_FORMAT_PCM_FLOAT; 2139fbfc3959f4aac839445edc7075532067fef497c2Andy Hung mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); 2140fbfc3959f4aac839445edc7075532067fef497c2Andy Hung mBufferSize = mFrameSize * mFrameCount; 2141fbfc3959f4aac839445edc7075532067fef497c2Andy Hung 2142fbfc3959f4aac839445edc7075532067fef497c2Andy Hung // TODO: We currently use the associated output device channel mask and sample rate. 2143fbfc3959f4aac839445edc7075532067fef497c2Andy Hung // (1) Perhaps use the ORed channel mask of all downstream MixerThreads 2144fbfc3959f4aac839445edc7075532067fef497c2Andy Hung // (if a valid mask) to avoid premature downmix. 2145fbfc3959f4aac839445edc7075532067fef497c2Andy Hung // (2) Perhaps use the maximum sample rate of all downstream MixerThreads 2146fbfc3959f4aac839445edc7075532067fef497c2Andy Hung // instead of the output device sample rate to avoid loss of high frequency information. 2147fbfc3959f4aac839445edc7075532067fef497c2Andy Hung // This may need to be updated as MixerThread/OutputTracks are added and not here. 2148fbfc3959f4aac839445edc7075532067fef497c2Andy Hung } 2149fbfc3959f4aac839445edc7075532067fef497c2Andy Hung 215009a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung // Calculate size of normal sink buffer relative to the HAL output buffer size 215181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double multiplier = 1.0; 215281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mType == MIXER && (kUseFastMixer == FastMixer_Static || 215381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent kUseFastMixer == FastMixer_Dynamic)) { 215409a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000; 215509a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000; 215681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer 215781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minNormalFrameCount = (minNormalFrameCount + 15) & ~15; 215881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent maxNormalFrameCount = maxNormalFrameCount & ~15; 215981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (maxNormalFrameCount < minNormalFrameCount) { 216081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent maxNormalFrameCount = minNormalFrameCount; 216181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 216281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent multiplier = (double) minNormalFrameCount / (double) mFrameCount; 216381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (multiplier <= 1.0) { 216481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent multiplier = 1.0; 216581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (multiplier <= 2.0) { 216681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (2 * mFrameCount <= maxNormalFrameCount) { 216781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent multiplier = 2.0; 216881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 216981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent multiplier = (double) maxNormalFrameCount / (double) mFrameCount; 217081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 217181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 217281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // prefer an even multiplier, for compatibility with doubling of fast tracks due to HAL 217309a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung // SRC (it would be unusual for the normal sink buffer size to not be a multiple of fast 217481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track, but we sometimes have to do this to satisfy the maximum frame count 217581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // constraint) 217681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME this rounding up should not be done if no HAL SRC 217781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t truncMult = (uint32_t) multiplier; 217881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((truncMult & 1)) { 217981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((truncMult + 1) * mFrameCount <= maxNormalFrameCount) { 218081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ++truncMult; 218181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 218281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 218381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent multiplier = (double) truncMult; 218481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 218581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 218681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalFrameCount = multiplier * mFrameCount; 218781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // round up to nearest 16 frames to satisfy AudioMixer 2188ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent if (mType == MIXER || mType == DUPLICATING) { 2189ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent mNormalFrameCount = (mNormalFrameCount + 15) & ~15; 2190ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent } 219109a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung ALOGI("HAL output buffer size %u frames, normal sink buffer size %u frames", mFrameCount, 219281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalFrameCount); 219381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 219408fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung // Check if we want to throttle the processing to no more than 2x normal rate 219508fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung mThreadThrottle = property_get_bool("af.thread.throttle", true /* default_value */); 219608fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung mHalfBufferMs = mNormalFrameCount * 1000 / (2 * mSampleRate); 219708fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung 2198010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // mSinkBuffer is the sink buffer. Size is always multiple-of-16 frames. 2199010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // Originally this was int16_t[] array, need to remove legacy implications. 2200010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung free(mSinkBuffer); 2201010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung mSinkBuffer = NULL; 22025b10a2037a835e790994b9ebec3c2e55052f1f3bAndy Hung // For sink buffer size, we use the frame size from the downstream sink to avoid problems 22035b10a2037a835e790994b9ebec3c2e55052f1f3bAndy Hung // with non PCM formats for compressed music, e.g. AAC, and Offload threads. 22045b10a2037a835e790994b9ebec3c2e55052f1f3bAndy Hung const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; 2205010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); 220681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 220769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // We resize the mMixerBuffer according to the requirements of the sink buffer which 220869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // drives the output. 220969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung free(mMixerBuffer); 221069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBuffer = NULL; 221169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung if (mMixerBufferEnabled) { 221269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // also valid: AUDIO_FORMAT_PCM_16_BIT. 221369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferSize = mNormalFrameCount * mChannelCount 221469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * audio_bytes_per_sample(mMixerBufferFormat); 221569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize); 221669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung } 221798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung free(mEffectBuffer); 221898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBuffer = NULL; 221998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mEffectBufferEnabled) { 222098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferFormat = AUDIO_FORMAT_PCM_16_BIT; // Note: Effects support 16b only 222198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferSize = mNormalFrameCount * mChannelCount 222298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung * audio_bytes_per_sample(mEffectBufferFormat); 222398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize); 222498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 222569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung 222681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // force reconfiguration of effect chains and engines to take new buffer size and audio 222781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // parameters into account 2228deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten // Note that mLock is not held when readOutputParameters_l() is called from the constructor 222981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // but in this case nothing is done below as no audio sessions have effect yet so it doesn't 223081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // matter. 223181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains 223281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<EffectChain> > effectChains = mEffectChains; 223381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < effectChains.size(); i ++) { 223481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); 223581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 223681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 223781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 223881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2239377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETITstatus_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) 224081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 224181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (halFrames == NULL || dspFrames == NULL) { 224281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 224381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 224481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 224581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (initCheck() != NO_ERROR) { 224681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return INVALID_OPERATION; 224781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 224881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t framesWritten = mBytesWritten / mFrameSize; 224981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *halFrames = framesWritten; 225081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 225181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (isSuspended()) { 225281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // return an estimation of rendered frames when the output is suspended 225381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t latencyFrames = (latency_l() * mSampleRate) / 1000; 225481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *dspFrames = framesWritten >= latencyFrames ? framesWritten - latencyFrames : 0; 225581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 225681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 2257377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT status_t status; 2258377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT uint32_t frames; 2259062e67a26e0553dd142be622821f493df541f0c6Phil Burk status = mOutput->getRenderPosition(&frames); 2260377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT *dspFrames = (size_t)frames; 2261377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT return status; 226281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 226381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 226481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 226581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) const 226681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 226781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 226881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t result = 0; 226981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (getEffectChain_l(sessionId) != 0) { 227081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result = EFFECT_SESSION; 227181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 227281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 227381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 227481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mTracks[i]; 22755736c35b841de56ce394b4879389f669b61425e6Glenn Kasten if (sessionId == track->sessionId() && !track->isInvalid()) { 227681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result |= TRACK_SESSION; 227781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 227881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 227981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 228081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 228181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return result; 228281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 228381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 228481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId) 228581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 228681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that 228781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it is moved to correct output by audio policy manager when A2DP is connected or disconnected 228881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { 228981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); 229081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 229181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 229281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mTracks[i]; 22935736c35b841de56ce394b4879389f669b61425e6Glenn Kasten if (sessionId == track->sessionId() && !track->isInvalid()) { 229481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return AudioSystem::getStrategyForStream(track->streamType()); 229581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 229681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 229781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); 229881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 229981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 230081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2301062e67a26e0553dd142be622821f493df541f0c6Phil BurkAudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const 230281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 230381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 230481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mOutput; 230581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 230681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2307062e67a26e0553dd142be622821f493df541f0c6Phil BurkAudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() 230881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 230981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 231081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamOut *output = mOutput; 231181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutput = NULL; 231281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME FastMixer might also have a raw ptr to mOutputSink; 231381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // must push a NULL and wait for ack 231481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutputSink.clear(); 231581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPipeSink.clear(); 231681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink.clear(); 231781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return output; 231881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 231981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 232081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// this method must always be called either with ThreadBase mLock held or inside the thread loop 232181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_stream_t* AudioFlinger::PlaybackThread::stream() const 232281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 232381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutput == NULL) { 232481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NULL; 232581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 232681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return &mOutput->stream->common; 232781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 232881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 232981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const 233081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 233181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); 233281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 233381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 233481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) 233581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 233681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!isValidSyncEvent(event)) { 233781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 233881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 233981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 234081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 234181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 234281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 234381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mTracks[i]; 234481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (event->triggerSession() == track->sessionId()) { 234581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (void) track->setSyncEvent(event); 234681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 234781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 234881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 234981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 235081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NAME_NOT_FOUND; 235181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 235281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 235381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const 235481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 235581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; 235681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 235781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 235881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::threadLoop_removeTracks( 235981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const Vector< sp<Track> >& tracksToRemove) 236081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 236181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t count = tracksToRemove.size(); 236234fca34606b448e6b71c2942f63cb13a0aebd620Glenn Kasten if (count > 0) { 236381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0 ; i < count ; i++) { 236481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<Track>& track = tracksToRemove.itemAt(i); 236583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent if (track->isExternalTrack()) { 2366e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent AudioSystem::stopOutput(mId, track->streamType(), 2367e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent (audio_session_t)track->sessionId()); 2368bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent#ifdef ADD_BATTERY_DATA 2369bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // to track the speaker usage 2370bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); 2371bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent#endif 2372bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (track->isTerminated()) { 2373e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent AudioSystem::releaseOutput(mId, track->streamType(), 2374e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent (audio_session_t)track->sessionId()); 2375bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 237681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 237781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 237881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 237981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 238081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 238181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::checkSilentMode_l() 238281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 238381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!mMasterMute) { 238481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char value[PROPERTY_VALUE_MAX]; 238581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (property_get("ro.audio.silent", value, "0") > 0) { 238681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char *endptr; 238781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent unsigned long ul = strtoul(value, &endptr, 0); 238881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (*endptr == '\0' && ul != 0) { 238981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGD("Silence is golden"); 239081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The setprop command will not allow a property to be changed after 239181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // the first time it is set, so we don't have to worry about un-muting. 239281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent setMasterMute_l(true); 239381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 239481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 239581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 239681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 239781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 239881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// shared by MIXER and DIRECT, overridden by DUPLICATING 2399bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentssize_t AudioFlinger::PlaybackThread::threadLoop_write() 240081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 240181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME rewrite to reduce number of system calls 240281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mLastWriteTime = systemTime(); 240381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mInWrite = true; 2404bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ssize_t bytesWritten; 2405010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung const size_t offset = mCurrentWriteLength - mBytesRemaining; 240681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 240781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If an NBAIO sink is present, use it to write the normal mixer's submix 240881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mNormalSink != 0) { 24094c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten 2410010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung const size_t count = mBytesRemaining / mFrameSize; 2411010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung 24122d590964aa58e137d17a43e095e6443dd0fe2e98Simon Wilson ATRACE_BEGIN("write"); 241381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // update the setpoint when AudioFlinger::mScreenState changes 241481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t screenState = AudioFlinger::mScreenState; 241581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (screenState != mScreenState) { 241681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mScreenState = screenState; 241781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); 241881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (pipe != NULL) { 241981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pipe->setAvgFrames((mScreenState & 1) ? 242081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); 242181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 242281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 2423010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count); 24242d590964aa58e137d17a43e095e6443dd0fe2e98Simon Wilson ATRACE_END(); 242581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (framesWritten > 0) { 2426010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung bytesWritten = framesWritten * mFrameSize; 242781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 242881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bytesWritten = framesWritten; 242981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 2430efaa7ab620b2ee936efa4fb74dc4c670ed757a46Glenn Kasten mLatchDValid = false; 2431767094dd98b01baf21de2ad09c27b3c98776cf73Glenn Kasten status_t status = mNormalSink->getTimestamp(mLatchD.mTimestamp); 2432bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten if (status == NO_ERROR) { 2433bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten size_t totalFramesWritten = mNormalSink->framesWritten(); 2434bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten if (totalFramesWritten >= mLatchD.mTimestamp.mPosition) { 2435bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mLatchD.mUnpresentedFrames = totalFramesWritten - mLatchD.mTimestamp.mPosition; 24364c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten // mLatchD.mFramesReleased is set immediately before D is clocked into Q 2437bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mLatchDValid = true; 2438bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten } 2439bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten } 244081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // otherwise use the HAL / AudioStreamOut directly 244181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 2442bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Direct output and offload threads 2443010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung 2444bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mUseAsyncWrite) { 24453b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request"); 24463b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence += 2; 24473b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence |= 1; 2448bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 24493b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setWriteBlocked(mWriteAckSequence); 2450bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2451767094dd98b01baf21de2ad09c27b3c98776cf73Glenn Kasten // FIXME We should have an implementation of timestamps for direct output threads. 2452767094dd98b01baf21de2ad09c27b3c98776cf73Glenn Kasten // They are used e.g for multichannel PCM playback over HDMI. 2453062e67a26e0553dd142be622821f493df541f0c6Phil Burk bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining); 2454bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mUseAsyncWrite && 2455bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) { 2456bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // do not wait for async callback in case of error of full write 24573b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence &= ~1; 2458bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 24593b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setWriteBlocked(mWriteAckSequence); 2460bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 246181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 246281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 246381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNumWrites++; 246481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mInWrite = false; 2465fd4779740ec3e9e865d5514464df26d015354388Eric Laurent mStandby = false; 2466bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return bytesWritten; 2467bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 2468bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 2469bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::threadLoop_drain() 2470bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 2471bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mOutput->stream->drain) { 2472bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full"); 2473bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mUseAsyncWrite) { 24743b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request"); 24753b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence |= 1; 2476bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 24773b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setDraining(mDrainSequence); 2478bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2479bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mOutput->stream->drain(mOutput->stream, 2480bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent (mMixerStatus == MIXER_DRAIN_TRACK) ? AUDIO_DRAIN_EARLY_NOTIFY 2481bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent : AUDIO_DRAIN_ALL); 2482bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2483bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 2484bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 2485bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::threadLoop_exit() 2486bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 2487275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent { 2488275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent Mutex::Autolock _l(mLock); 2489275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 2490275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent sp<Track> track = mTracks[i]; 2491275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent track->invalidate(); 2492275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent } 2493275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent } 249481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 249581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 249681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent/* 249781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentThe derived values that are cached: 249825c2dac12114699e90deb1c579cadebce7b91a97Andy Hung - mSinkBufferSize from frame count * frame size 2499ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent - mActiveSleepTimeUs from activeSleepTimeUs() 2500ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent - mIdleSleepTimeUs from idleSleepTimeUs() 2501ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent - mStandbyDelayNs from mActiveSleepTimeUs (DIRECT only) 250281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - maxPeriod from frame count and sample rate (MIXER only) 250381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 250481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentThe parameters that affect these derived values are: 250581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - frame count 250681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - frame size 250781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - sample rate 250881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - device type: A2DP or not 250981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - device latency 251081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - format: PCM or not 251181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - active sleep time 251281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - idle sleep time 251381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent*/ 251481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 251581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::cacheParameters_l() 251681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 251725c2dac12114699e90deb1c579cadebce7b91a97Andy Hung mSinkBufferSize = mNormalFrameCount * mFrameSize; 2518ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mActiveSleepTimeUs = activeSleepTimeUs(); 2519ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mIdleSleepTimeUs = idleSleepTimeUs(); 252081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 252181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 252281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType) 252381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 25247c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d", 252581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent this, streamType, mTracks.size()); 252681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 252781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 252881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t size = mTracks.size(); 252981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < size; i++) { 253081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> t = mTracks[i]; 253181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (t->streamType() == streamType) { 25325736c35b841de56ce394b4879389f669b61425e6Glenn Kasten t->invalidate(); 253381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 253481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 253581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 253681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 253781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) 253881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 253981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int session = chain->sessionId(); 2540010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung int16_t* buffer = reinterpret_cast<int16_t*>(mEffectBufferEnabled 2541010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung ? mEffectBuffer : mSinkBuffer); 254281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool ownsBuffer = false; 254381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 254481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); 254581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (session > 0) { 254681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Only one effect chain can be present in direct output thread and it uses 25472098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung // the sink buffer as input 254881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mType != DIRECT) { 254981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t numSamples = mNormalFrameCount * mChannelCount; 255081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent buffer = new int16_t[numSamples]; 255181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent memset(buffer, 0, numSamples * sizeof(int16_t)); 255281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); 255381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ownsBuffer = true; 255481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 255581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 255681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Attach all tracks with same session ID to this chain. 255781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 255881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mTracks[i]; 255981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (session == track->sessionId()) { 256081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), 256181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent buffer); 256281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->setMainBuffer(buffer); 256381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->incTrackCnt(); 256481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 256581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 256681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 256781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // indicate all active tracks in the chain 256881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { 256981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mActiveTracks[i].promote(); 257081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track == 0) { 257181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 257281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 257381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (session == track->sessionId()) { 257481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); 257581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->incActiveTrackCnt(); 257681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 257781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 257881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 2579aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent chain->setThread(this); 258081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setInBuffer(buffer, ownsBuffer); 2581010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung chain->setOutBuffer(reinterpret_cast<int16_t*>(mEffectBufferEnabled 2582010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung ? mEffectBuffer : mSinkBuffer)); 258381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect 258481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // chains list in order to be processed last as it contains output stage effects 258581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before 258681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // session AUDIO_SESSION_OUTPUT_STAGE to be processed 258781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // after track specific effects and before output stage 258881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and 258981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX 259081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Effect chain for other sessions are inserted at beginning of effect 259181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // chains list to be processed before output mix effects. Relative order between other 259281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // sessions is not important 259381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t size = mEffectChains.size(); 259481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t i = 0; 259581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (i = 0; i < size; i++) { 259681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mEffectChains[i]->sessionId() < session) { 259781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 259881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 259981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 260081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains.insertAt(chain, i); 260181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent checkSuspendOnAddEffectChain_l(chain); 260281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 260381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 260481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 260581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 260681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsize_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) 260781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 260881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int session = chain->sessionId(); 260981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 261081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); 261181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 261281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 261381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain == mEffectChains[i]) { 261481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains.removeAt(i); 261581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // detach all active tracks from the chain 261681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { 261781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mActiveTracks[i].promote(); 261881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track == 0) { 261981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 262081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 262181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (session == track->sessionId()) { 262281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", 262381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain.get(), session); 262481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->decActiveTrackCnt(); 262581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 262681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 262781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 262881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // detach all tracks with same session ID from this chain 262981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 263081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mTracks[i]; 263181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (session == track->sessionId()) { 2632010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung track->setMainBuffer(reinterpret_cast<int16_t*>(mSinkBuffer)); 263381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->decTrackCnt(); 263481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 263581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 263681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 263781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 263881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 263981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mEffectChains.size(); 264081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 264181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 264281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::attachAuxEffect( 264381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) 264481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 264581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 264681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return attachAuxEffect_l(track, EffectId); 264781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 264881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 264981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::attachAuxEffect_l( 265081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) 265181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 265281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status = NO_ERROR; 265381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 265481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (EffectId == 0) { 265581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->setAuxBuffer(0, NULL); 265681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 265781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX 265881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); 265981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (effect != 0) { 266081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 266181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); 266281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 266381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = INVALID_OPERATION; 266481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 266581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 266681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = BAD_VALUE; 266781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 266881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 266981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 267081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 267181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 267281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) 267381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 267481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 267581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mTracks[i]; 267681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->auxEffectId() == effectId) { 267781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent attachAuxEffect_l(track, 0); 267881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 267981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 268081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 268181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 268281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::PlaybackThread::threadLoop() 268381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 268481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<Track> > tracksToRemove; 268581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2686ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mStandbyTimeNs = systemTime(); 268781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 268881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // MIXER 268981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent nsecs_t lastWarning = 0; 269081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 269181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // DUPLICATING 269281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME could this be made local to while loop? 269381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent writeFrames = 0; 269481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2695462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen int lastGeneration = 0; 2696462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 269781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent cacheParameters_l(); 2698ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mSleepTimeUs = mIdleSleepTimeUs; 269981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 270081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mType == MIXER) { 270181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTimeShift = 0; 270281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 270381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 270481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent CpuStats cpuStats; 270581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); 270681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 270781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent acquireWakeLock(); 270881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 27099e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten // mNBLogWriter->log can only be called while thread mutex mLock is held. 27109e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten // So if you need to log when mutex is unlocked, set logString to a non-NULL string, 27119e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten // and then that string will be logged at the next convenient opportunity. 27129e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten const char *logString = NULL; 27139e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten 2714664539d25180ab8f77e0521533ea2821cf28985fEric Laurent checkSilentMode_l(); 2715664539d25180ab8f77e0521533ea2821cf28985fEric Laurent 271681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent while (!exitPending()) 271781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 271881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent cpuStats.sample(myName); 271981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 272081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<EffectChain> > effectChains; 272181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 272281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // scope for mLock 272381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 272481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 272581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2726021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent processConfigEvents_l(); 27271035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 27289e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten if (logString != NULL) { 27299e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten mNBLogWriter->logTimestamp(); 27309e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten mNBLogWriter->log(logString); 27319e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten logString = NULL; 27329e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten } 27339e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten 27344c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten // Gather the framesReleased counters for all active tracks, 27354c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten // and latch them atomically with the timestamp. 27364c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten // FIXME We're using raw pointers as indices. A unique track ID would be a better index. 27374c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten mLatchD.mFramesReleased.clear(); 27384c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten size_t size = mActiveTracks.size(); 27394c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten for (size_t i = 0; i < size; i++) { 27404c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten sp<Track> t = mActiveTracks[i].promote(); 27414c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten if (t != 0) { 27424c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten mLatchD.mFramesReleased.add(t.get(), 27434c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten t->mAudioTrackServerProxy->framesReleased()); 27444c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten } 27454c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten } 2746bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten if (mLatchDValid) { 2747bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mLatchQ = mLatchD; 2748bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mLatchDValid = false; 2749bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mLatchQValid = true; 2750bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten } 2751bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten 275281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent saveOutputTracks(); 2753bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mSignalPending) { 2754bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // A signal was raised while we were unlocked 2755bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mSignalPending = false; 2756bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if (waitingAsyncCallback_l()) { 2757bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (exitPending()) { 2758bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent break; 2759bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2760078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen bool released = false; 2761078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen // The following works around a bug in the offload driver. Ideally we would release 2762078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen // the wake lock every time, but that causes the last offload buffer(s) to be 2763078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen // dropped while the device is on battery, so we need to hold a wake lock during 2764078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen // the drain phase. 2765078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen if (mBytesRemaining && !(mDrainSequence & 1)) { 2766078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen releaseWakeLock_l(); 2767078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen released = true; 2768078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen } 2769462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mWakeLockUids.clear(); 2770462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mActiveTracksGeneration++; 2771bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("wait async completion"); 2772bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mWaitWorkCV.wait(mLock); 2773bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("async completion/wake"); 2774078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen if (released) { 2775078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen acquireWakeLock_l(); 2776078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen } 2777ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mStandbyTimeNs = systemTime() + mStandbyDelayNs; 2778ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mSleepTimeUs = 0; 2779ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent 2780ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent continue; 2781ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent } 2782ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent if ((!mActiveTracks.size() && systemTime() > mStandbyTimeNs) || 2783bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent isSuspended()) { 2784bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // put audio hardware into standby after short delay 2785bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (shouldStandby_l()) { 278681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 278781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent threadLoop_standby(); 278881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 278981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStandby = true; 279081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 279181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 279281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { 279381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // we're about to wait, flush the binder command buffer 279481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent IPCThreadState::self()->flushCommands(); 279581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 279681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent clearOutputTracks(); 279781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 279881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (exitPending()) { 279981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 280081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 280181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 280281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock_l(); 2803462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mWakeLockUids.clear(); 2804462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mActiveTracksGeneration++; 280581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // wait until we have something to do... 280681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("%s going to sleep", myName.string()); 280781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitWorkCV.wait(mLock); 280881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("%s waking up", myName.string()); 280981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent acquireWakeLock_l(); 281081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 281181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMixerStatus = MIXER_IDLE; 281281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMixerStatusIgnoringFastTracks = MIXER_IDLE; 281381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mBytesWritten = 0; 2814bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining = 0; 281581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent checkSilentMode_l(); 281681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2817ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mStandbyTimeNs = systemTime() + mStandbyDelayNs; 2818ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mSleepTimeUs = mIdleSleepTimeUs; 281981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mType == MIXER) { 282081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTimeShift = 0; 282181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 282281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 282381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 282481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 282581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 282681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mMixerStatusIgnoringFastTracks is also updated internally 282781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMixerStatus = prepareTracks_l(&tracksToRemove); 282881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2829462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen // compare with previously applied list 2830462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen if (lastGeneration != mActiveTracksGeneration) { 2831462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen // update wakelock 2832462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen updateWakeLockUids_l(mWakeLockUids); 2833462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen lastGeneration = mActiveTracksGeneration; 2834462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } 2835462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 283681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // prevent any changes in effect chain list and in each effect chain 283781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // during mixing and effect process as the audio buffers could be deleted 283881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // or modified if an effect is created or deleted 283981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lockEffectChains_l(effectChains); 2840462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } // mLock scope ends 284181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2842bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mBytesRemaining == 0) { 2843bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mCurrentWriteLength = 0; 2844bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mMixerStatus == MIXER_TRACKS_READY) { 2845bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // threadLoop_mix() sets mCurrentWriteLength 2846bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent threadLoop_mix(); 2847bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if ((mMixerStatus != MIXER_DRAIN_TRACK) 2848bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent && (mMixerStatus != MIXER_DRAIN_ALL)) { 2849ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent // threadLoop_sleepTime sets mSleepTimeUs to 0 if data 2850bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // must be written to HAL 2851bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent threadLoop_sleepTime(); 2852ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent if (mSleepTimeUs == 0) { 285325c2dac12114699e90deb1c579cadebce7b91a97Andy Hung mCurrentWriteLength = mSinkBufferSize; 2854bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2855bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 285698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Either threadLoop_mix() or threadLoop_sleepTime() should have set 2857ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent // mMixerBuffer with data if mMixerBufferValid is true and mSleepTimeUs == 0. 285898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid) 285998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // or mSinkBuffer (if there are no effects). 286098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // 286198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // This is done pre-effects computation; if effects change to 286298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // support higher precision, this needs to move. 286398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // 286498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // mMixerBufferValid is only set true by MixerThread::prepareTracks_l(). 2865ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent // TODO use mSleepTimeUs == 0 as an additional condition. 286698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mMixerBufferValid) { 286798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer; 286898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat; 286998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung 287098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat, 287198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mNormalFrameCount * mChannelCount); 287298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 287398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung 2874bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining = mCurrentWriteLength; 2875bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (isSuspended()) { 2876ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mSleepTimeUs = suspendSleepTimeUs(); 2877bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // simulate write to HAL when suspended 287825c2dac12114699e90deb1c579cadebce7b91a97Andy Hung mBytesWritten += mSinkBufferSize; 2879bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining = 0; 2880bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 288181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2882bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // only process effects if we're going to write 2883ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent if (mSleepTimeUs == 0 && mType != OFFLOAD) { 2884bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent for (size_t i = 0; i < effectChains.size(); i ++) { 2885bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent effectChains[i]->process_l(); 2886bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 288781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 288881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 288959fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent // Process effect chains for offloaded thread even if no audio 289059fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent // was read from audio track: process only updates effect state 289159fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent // and thus does have to be synchronized with audio writes but may have 289259fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent // to be called while waiting for async write callback 289359fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent if (mType == OFFLOAD) { 289459fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent for (size_t i = 0; i < effectChains.size(); i ++) { 289559fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent effectChains[i]->process_l(); 289659fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent } 289759fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent } 289881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 289998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Only if the Effects buffer is enabled and there is data in the 290098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Effects buffer (buffer valid), we need to 290198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // copy into the sink buffer. 2902ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent // TODO use mSleepTimeUs == 0 as an additional condition. 290398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mEffectBufferValid) { 290498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung //ALOGV("writing effect buffer to sink buffer format %#x", mFormat); 290598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat, 290698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mNormalFrameCount * mChannelCount); 290798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 290898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung 290981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // enable changes in effect chain 291081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent unlockEffectChains(effectChains); 291181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2912bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (!waitingAsyncCallback()) { 2913ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent // mSleepTimeUs == 0 means we must write to audio hardware 2914ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent if (mSleepTimeUs == 0) { 291508fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung ssize_t ret = 0; 2916bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mBytesRemaining) { 291708fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung ret = threadLoop_write(); 2918bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (ret < 0) { 2919bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining = 0; 2920bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else { 2921bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesWritten += ret; 2922bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining -= ret; 2923bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2924bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if ((mMixerStatus == MIXER_DRAIN_TRACK) || 2925bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent (mMixerStatus == MIXER_DRAIN_ALL)) { 2926bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent threadLoop_drain(); 2927bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 292808fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung if (mType == MIXER && !mStandby) { 29294944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten // write blocked detection 29304944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten nsecs_t now = systemTime(); 29314944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten nsecs_t delta = now - mLastWriteTime; 293208fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung if (delta > maxPeriod) { 29334944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten mNumDelayedWrites++; 29344944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten if ((now - lastWarning) > kWarningThrottleNs) { 29354944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten ATRACE_NAME("underrun"); 29364944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", 29374944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten ns2ms(delta), mNumDelayedWrites, this); 29384944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten lastWarning = now; 29394944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten } 2940bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 294108fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung 294208fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung if (mThreadThrottle 294308fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung && mMixerStatus == MIXER_TRACKS_READY // we are mixing (active tracks) 294408fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung && ret > 0) { // we wrote something 294508fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung // Limit MixerThread data processing to no more than twice the 294608fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung // expected processing rate. 294708fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung // 294808fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung // This helps prevent underruns with NuPlayer and other applications 294908fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung // which may set up buffers that are close to the minimum size, or use 295008fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung // deep buffers, and rely on a double-buffering sleep strategy to fill. 295108fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung // 295208fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung // The throttle smooths out sudden large data drains from the device, 295308fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung // e.g. when it comes out of standby, which often causes problems with 295408fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung // (1) mixer threads without a fast mixer (which has its own warm-up) 295508fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung // (2) minimum buffer sized tracks (even if the track is full, 295608fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung // the app won't fill fast enough to handle the sudden draw). 295708fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung 295808fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung const int32_t deltaMs = delta / 1000000; 295908fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung const int32_t throttleMs = mHalfBufferMs - deltaMs; 296008fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung if ((signed)mHalfBufferMs >= throttleMs && throttleMs > 0) { 296108fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung usleep(throttleMs * 1000); 296208fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung ALOGD("mixer(%p) throttle: ret(%zd) deltaMs(%d) requires sleep %d ms", 296308fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung this, ret, deltaMs, throttleMs); 296408fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung } 296508fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung } 296681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 296781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2968bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else { 2969e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten ATRACE_BEGIN("sleep"); 2970ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent usleep(mSleepTimeUs); 2971e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten ATRACE_END(); 2972bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 297381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 297481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 297581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Finally let go of removed track(s), without the lock held 297681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // since we can't guarantee the destructors won't acquire that 297781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // same lock. This will also mutate and push a new fast mixer state. 297881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent threadLoop_removeTracks(tracksToRemove); 297981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent tracksToRemove.clear(); 298081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 298181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME I don't understand the need for this here; 298281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it was in the original code but maybe the 298381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // assignment in saveOutputTracks() makes this unnecessary? 298481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent clearOutputTracks(); 298581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 298681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Effect chains will be actually deleted here if they were removed from 298781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mEffectChains list during mixing or effects processing 298881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effectChains.clear(); 298981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 299081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME Note that the above .clear() is no longer necessary since effectChains 299181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // is now local to this block, but will keep it for now (at least until merge done). 299281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 299381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2994bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent threadLoop_exit(); 2995bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 2996cf817a2330936947df94c11859f48771f5596a59Eric Laurent if (!mStandby) { 2997cf817a2330936947df94c11859f48771f5596a59Eric Laurent threadLoop_standby(); 2998cf817a2330936947df94c11859f48771f5596a59Eric Laurent mStandby = true; 299981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 300081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 300181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock(); 3002462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mWakeLockUids.clear(); 3003462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mActiveTracksGeneration++; 300481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 300581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("Thread %p type %d exiting", this, mType); 300681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 300781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 300881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3009bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// removeTracks_l() must be called with ThreadBase::mLock held 3010bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove) 3011bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 3012bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent size_t count = tracksToRemove.size(); 301334fca34606b448e6b71c2942f63cb13a0aebd620Glenn Kasten if (count > 0) { 3014bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent for (size_t i=0 ; i<count ; i++) { 3015bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent const sp<Track>& track = tracksToRemove.itemAt(i); 3016bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mActiveTracks.remove(track); 3017462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mWakeLockUids.remove(track->uid()); 3018462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mActiveTracksGeneration++; 3019bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("removeTracks_l removing track on session %d", track->sessionId()); 3020bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent sp<EffectChain> chain = getEffectChain_l(track->sessionId()); 3021bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (chain != 0) { 3022bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("stopping track on chain %p for session Id: %d", chain.get(), 3023bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->sessionId()); 3024bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent chain->decActiveTrackCnt(); 3025bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 3026bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (track->isTerminated()) { 3027bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent removeTrack_l(track); 3028bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 3029bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 3030bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 3031bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 3032bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 303381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3034accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurentstatus_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp) 3035accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent{ 3036accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent if (mNormalSink != 0) { 3037accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent return mNormalSink->getTimestamp(timestamp); 3038accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent } 30399a1c8892f6835325db6931529dc74bf98cf0aee8Andy Hung if ((mType == OFFLOAD || mType == DIRECT) 30409a1c8892f6835325db6931529dc74bf98cf0aee8Andy Hung && mOutput != NULL && mOutput->stream->get_presentation_position) { 3041accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent uint64_t position64; 3042062e67a26e0553dd142be622821f493df541f0c6Phil Burk int ret = mOutput->getPresentationPosition(&position64, ×tamp.mTime); 3043accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent if (ret == 0) { 3044accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent timestamp.mPosition = (uint32_t)position64; 3045accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent return NO_ERROR; 3046accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent } 3047accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent } 3048accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent return INVALID_OPERATION; 3049accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent} 30501c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 3051054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurentstatus_t AudioFlinger::MixerThread::createAudioPatch_l(const struct audio_patch *patch, 3052054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent audio_patch_handle_t *handle) 3053054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent{ 3054054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent // if !&IDLE, holds the FastMixer state to restore after new parameters processed 3055054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; 3056054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (mFastMixer != 0) { 3057054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 3058054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent FastMixerState *state = sq->begin(); 3059054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (!(state->mCommand & FastMixerState::IDLE)) { 3060054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent previousCommand = state->mCommand; 3061054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent state->mCommand = FastMixerState::HOT_IDLE; 3062054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent sq->end(); 3063054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); 3064054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } else { 3065054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent sq->end(false /*didModify*/); 3066054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 3067054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 3068054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent status_t status = PlaybackThread::createAudioPatch_l(patch, handle); 3069054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 3070054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (!(previousCommand & FastMixerState::IDLE)) { 3071054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent ALOG_ASSERT(mFastMixer != 0); 3072054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 3073054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent FastMixerState *state = sq->begin(); 3074054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); 3075054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent state->mCommand = previousCommand; 3076054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent sq->end(); 3077054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); 3078054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 3079054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 3080054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent return status; 3081054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent} 3082054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 30831c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch, 30841c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_patch_handle_t *handle) 30851c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{ 30861c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status_t status = NO_ERROR; 3087054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 3088054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent // store new device and send to effects 3089054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent audio_devices_t type = AUDIO_DEVICE_NONE; 3090054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent for (unsigned int i = 0; i < patch->num_sinks; i++) { 3091054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent type |= patch->sinks[i].ext.device.type; 3092054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 3093054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 3094054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent#ifdef ADD_BATTERY_DATA 3095054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent // when changing the audio output device, call addBatteryData to notify 3096054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent // the change 3097054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (mOutDevice != type) { 3098054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent uint32_t params = 0; 3099054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent // check whether speaker is on 3100054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (type & AUDIO_DEVICE_OUT_SPEAKER) { 3101054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent params |= IMediaPlayerService::kBatteryDataSpeakerOn; 31021c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 3103054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 3104054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent audio_devices_t deviceWithoutSpeaker 3105054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; 3106054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent // check if any other device (except speaker) is on 3107054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (type & deviceWithoutSpeaker) { 3108054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; 3109054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 3110054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 3111054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (params != 0) { 3112054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent addBatteryData(params); 31131c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 3114054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 3115054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent#endif 31161c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 3117054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 3118054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent mEffectChains[i]->setDevice_l(type); 3119054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 3120054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent mOutDevice = type; 3121296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent mPatch = *patch; 3122054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 3123054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { 31241c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); 31251c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status = hwDevice->create_audio_patch(hwDevice, 31261c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent patch->num_sources, 31271c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent patch->sources, 31281c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent patch->num_sinks, 31291c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent patch->sinks, 31301c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent handle); 31311c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } else { 3132054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent char *address; 3133054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (strcmp(patch->sinks[0].ext.device.address, "") != 0) { 3134054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent //FIXME: we only support address on first sink with HAL version < 3.0 3135054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent address = audio_device_address_to_parameter( 3136054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent patch->sinks[0].ext.device.type, 3137054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent patch->sinks[0].ext.device.address); 3138054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } else { 3139054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent address = (char *)calloc(1, 1); 3140054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 3141054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent AudioParameter param = AudioParameter(String8(address)); 3142054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent free(address); 3143054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), (int)type); 3144054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent status = mOutput->stream->common.set_parameters(&mOutput->stream->common, 3145054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent param.toString().string()); 3146054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent *handle = AUDIO_PATCH_HANDLE_NONE; 3147054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 3148296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); 3149054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent return status; 3150054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent} 3151054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 3152054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurentstatus_t AudioFlinger::MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle) 3153054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent{ 3154054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent // if !&IDLE, holds the FastMixer state to restore after new parameters processed 3155054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; 3156054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (mFastMixer != 0) { 3157054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 3158054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent FastMixerState *state = sq->begin(); 3159054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (!(state->mCommand & FastMixerState::IDLE)) { 3160054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent previousCommand = state->mCommand; 3161054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent state->mCommand = FastMixerState::HOT_IDLE; 3162054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent sq->end(); 3163054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); 3164054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } else { 3165054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent sq->end(false /*didModify*/); 3166054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 31671c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 3168054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 3169054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent status_t status = PlaybackThread::releaseAudioPatch_l(handle); 3170054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 3171054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (!(previousCommand & FastMixerState::IDLE)) { 3172054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent ALOG_ASSERT(mFastMixer != 0); 3173054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 3174054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent FastMixerState *state = sq->begin(); 3175054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); 3176054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent state->mCommand = previousCommand; 3177054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent sq->end(); 3178054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); 3179054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 3180054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 31811c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent return status; 31821c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent} 31831c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 31841c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle) 31851c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{ 31861c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status_t status = NO_ERROR; 3187054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 3188054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent mOutDevice = AUDIO_DEVICE_NONE; 3189054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 31901c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { 31911c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); 31921c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status = hwDevice->release_audio_patch(hwDevice, handle); 31931c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } else { 3194054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent AudioParameter param; 3195054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0); 3196054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent status = mOutput->stream->common.set_parameters(&mOutput->stream->common, 3197054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent param.toString().string()); 31981c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 31991c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent return status; 32001c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent} 32011c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 320283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track) 320383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{ 320483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent Mutex::Autolock _l(mLock); 320583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent mTracks.add(track); 320683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent} 320783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 320883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track) 320983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{ 321083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent Mutex::Autolock _l(mLock); 321183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent destroyTrack_l(track); 321283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent} 321383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 321483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::PlaybackThread::getAudioPortConfig(struct audio_port_config *config) 321583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{ 321683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent ThreadBase::getAudioPortConfig(config); 321783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->role = AUDIO_PORT_ROLE_SOURCE; 321883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->ext.mix.hw_module = mOutput->audioHwDev->handle(); 321983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; 322083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent} 322183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 322281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 322381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 322481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, 322572e3f39146fce4686bd96f11057c051bea376dfbEric Laurent audio_io_handle_t id, audio_devices_t device, bool systemReady, type_t type) 322672e3f39146fce4686bd96f11057c051bea376dfbEric Laurent : PlaybackThread(audioFlinger, output, id, device, type, systemReady), 322781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mAudioMixer below 322881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mFastMixer below 322981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixerFutex(0) 323081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mOutputSink below 323181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mPipeSink below 323281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mNormalSink below 323381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 323481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type); 3235f6ed423af92a56ef54bba23eba883b1f21448b54Glenn Kasten ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%d, mFrameSize=%u, " 323681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "mFrameCount=%d, mNormalFrameCount=%d", 323781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, 323881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalFrameCount); 323981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); 324081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3241fbfc3959f4aac839445edc7075532067fef497c2Andy Hung if (type == DUPLICATING) { 3242fbfc3959f4aac839445edc7075532067fef497c2Andy Hung // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks 3243fbfc3959f4aac839445edc7075532067fef497c2Andy Hung // (downstream MixerThreads) in DuplicatingThread::threadLoop_write(). 3244fbfc3959f4aac839445edc7075532067fef497c2Andy Hung // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink. 3245fbfc3959f4aac839445edc7075532067fef497c2Andy Hung return; 3246fbfc3959f4aac839445edc7075532067fef497c2Andy Hung } 324781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create an NBAIO sink for the HAL output stream, and negotiate 324881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutputSink = new AudioStreamOutSink(output->stream); 324981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t numCounterOffers = 0; 3250f69f9869514730aebe5724c461768507084dfff7Glenn Kasten const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; 325181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ssize_t index = mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); 325281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(index == 0); 325381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 325481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // initialize fast mixer depending on configuration 325581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool initFastMixer; 325681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent switch (kUseFastMixer) { 325781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Never: 325881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent initFastMixer = false; 325981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 326081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Always: 326181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent initFastMixer = true; 326281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 326381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Static: 326481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Dynamic: 326581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent initFastMixer = mFrameCount < mNormalFrameCount; 326681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 326781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 326881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (initFastMixer) { 32691258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung audio_format_t fastMixerFormat; 32701258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung if (mMixerBufferEnabled && mEffectBufferEnabled) { 32711258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT; 32721258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung } else { 32731258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT; 32741258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung } 32751258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung if (mFormat != fastMixerFormat) { 32761258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung // change our Sink format to accept our intermediate precision 32771258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung mFormat = fastMixerFormat; 32781258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung free(mSinkBuffer); 32791258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); 32801258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; 32811258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); 32821258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung } 328381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 328481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create a MonoPipe to connect our submix to FastMixer 328581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent NBAIO_Format format = mOutputSink->format(); 3286ba0b34c18da93681c0813ecdab19b0e215b6d261Glenn Kasten NBAIO_Format origformat = format; 32871258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung // adjust format to match that of the Fast Mixer 328897b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten ALOGV("format changed from %d to %d", format.mFormat, fastMixerFormat); 32891258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung format.mFormat = fastMixerFormat; 32901258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount; 32911258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung 329281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // This pipe depth compensates for scheduling latency of the normal mixer thread. 329381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // When it wakes up after a maximum latency, it runs a few cycles quickly before 329481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // finally blocking. Note the pipe implementation rounds up the request to a power of 2. 329581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); 329681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const NBAIO_Format offers[1] = {format}; 329781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t numCounterOffers = 0; 329881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ssize_t index = monoPipe->negotiate(offers, 1, NULL, numCounterOffers); 329981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(index == 0); 330081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent monoPipe->setAvgFrames((mScreenState & 1) ? 330181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); 330281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPipeSink = monoPipe; 330381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 330446909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 3305da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (mTeeSinkOutputEnabled) { 3306da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten // create a Pipe to archive a copy of FastMixer's output for dumpsys 3307ba0b34c18da93681c0813ecdab19b0e215b6d261Glenn Kasten Pipe *teeSink = new Pipe(mTeeSinkOutputFrames, origformat); 3308ba0b34c18da93681c0813ecdab19b0e215b6d261Glenn Kasten const NBAIO_Format offers2[1] = {origformat}; 3309da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten numCounterOffers = 0; 3310ba0b34c18da93681c0813ecdab19b0e215b6d261Glenn Kasten index = teeSink->negotiate(offers2, 1, NULL, numCounterOffers); 3311da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten ALOG_ASSERT(index == 0); 3312da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten mTeeSink = teeSink; 3313da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten PipeReader *teeSource = new PipeReader(*teeSink); 3314da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten numCounterOffers = 0; 3315ba0b34c18da93681c0813ecdab19b0e215b6d261Glenn Kasten index = teeSource->negotiate(offers2, 1, NULL, numCounterOffers); 3316da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten ALOG_ASSERT(index == 0); 3317da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten mTeeSource = teeSource; 3318da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 331946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 332081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 332181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create fast mixer and configure it initially with just one fast track for our submix 332281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixer = new FastMixer(); 332381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 332481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef STATE_QUEUE_DUMP 332581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->setObserverDump(&mStateQueueObserverDump); 332681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->setMutatorDump(&mStateQueueMutatorDump); 332781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 332881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerState *state = sq->begin(); 332981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastTrack *fastTrack = &state->mFastTracks[0]; 333081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // wrap the source side of the MonoPipe to make it an AudioBufferProvider 333181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); 333281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mVolumeProvider = NULL; 3333e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung fastTrack->mChannelMask = mChannelMask; // mPipeSink channel mask for audio to FastMixer 3334e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer 333581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mGeneration++; 333681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mFastTracksGen++; 333781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mTrackMask = 1; 333881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // fast mixer will use the HAL output sink 333981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mOutputSink = mOutputSink.get(); 334081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mOutputSinkGen++; 334181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mFrameCount = mFrameCount; 334281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand = FastMixerState::COLD_IDLE; 334381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // already done in constructor initialization list 334481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent //mFastMixerFutex = 0; 334581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mColdFutexAddr = &mFastMixerFutex; 334681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mColdGen++; 334781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mDumpState = &mFastMixerDumpState; 334846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 334981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mTeeSink = mTeeSink.get(); 335046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 33519e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer"); 33529e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten state->mNBLogWriter = mFastMixerNBLogWriter.get(); 335381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(); 335481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); 335581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 335681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // start the fast mixer 335781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); 335881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t tid = mFastMixer->getTid(); 335972e3f39146fce4686bd96f11057c051bea376dfbEric Laurent sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer); 336081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 336181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG 336281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create and start the watchdog 336381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog = new AudioWatchdog(); 336481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->setDump(&mAudioWatchdogDump); 336581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); 336681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent tid = mAudioWatchdog->getTid(); 336772e3f39146fce4686bd96f11057c051bea376dfbEric Laurent sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer); 336881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 336981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 337081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 337181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 337281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent switch (kUseFastMixer) { 337381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Never: 337481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Dynamic: 337581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink = mOutputSink; 337681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 337781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Always: 337881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink = mPipeSink; 337981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 338081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Static: 338181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink = initFastMixer ? mPipeSink : mOutputSink; 338281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 338381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 338481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 338581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 338681784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::MixerThread::~MixerThread() 338781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 33884d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten if (mFastMixer != 0) { 338981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 339081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerState *state = sq->begin(); 339181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (state->mCommand == FastMixerState::COLD_IDLE) { 339281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int32_t old = android_atomic_inc(&mFastMixerFutex); 339381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (old == -1) { 3394ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); 339581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 339681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 339781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand = FastMixerState::EXIT; 339881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(); 339981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); 340081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixer->join(); 340181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Though the fast mixer thread has exited, it's state queue is still valid. 340281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // We'll use that extract the final state which contains one remaining fast track 340381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // corresponding to our sub-mix. 340481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state = sq->begin(); 340581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(state->mTrackMask == 1); 340681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastTrack *fastTrack = &state->mFastTracks[0]; 340781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(fastTrack->mBufferProvider != NULL); 340881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent delete fastTrack->mBufferProvider; 340981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(false /*didModify*/); 34104d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten mFastMixer.clear(); 341181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG 341281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mAudioWatchdog != 0) { 341381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->requestExit(); 341481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->requestExitAndWait(); 341581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog.clear(); 341681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 341781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 341881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 34199e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter); 342081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent delete mAudioMixer; 342181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 342281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 342381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 342481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const 342581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 34264d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten if (mFastMixer != 0) { 342781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); 342881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent latency += (pipe->getAvgFrames() * 1000) / mSampleRate; 342981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 343081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return latency; 343181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 343281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 343381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 343481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) 343581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 343681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread::threadLoop_removeTracks(tracksToRemove); 343781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 343881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3439bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentssize_t AudioFlinger::MixerThread::threadLoop_write() 344081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 344181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME we should only do one push per cycle; confirm this is true 344281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Start the fast mixer if it's not already running 34434d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten if (mFastMixer != 0) { 344481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 344581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerState *state = sq->begin(); 344681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (state->mCommand != FastMixerState::MIX_WRITE && 344781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { 344881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (state->mCommand == FastMixerState::COLD_IDLE) { 344981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int32_t old = android_atomic_inc(&mFastMixerFutex); 345081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (old == -1) { 3451ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); 345281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 345381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG 345481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mAudioWatchdog != 0) { 345581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->resume(); 345681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 345781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 345881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 345981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand = FastMixerState::MIX_WRITE; 3460d797a9d5daad3051f9ac1d348abc2f434ea3ddcfGlenn Kasten#ifdef FAST_THREAD_STATISTICS 34614182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? 3462fbdb2aceab7317aa44bc8f301a93eb49e17b2bceGlenn Kasten FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN); 3463d797a9d5daad3051f9ac1d348abc2f434ea3ddcfGlenn Kasten#endif 346481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(); 346581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); 346681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (kUseFastMixer == FastMixer_Dynamic) { 346781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink = mPipeSink; 346881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 346981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 347081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(false /*didModify*/); 347181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 347281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 3473bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return PlaybackThread::threadLoop_write(); 347481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 347581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 347681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::threadLoop_standby() 347781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 347881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Idle the fast mixer if it's currently running 34794d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten if (mFastMixer != 0) { 348081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 348181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerState *state = sq->begin(); 348281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!(state->mCommand & FastMixerState::IDLE)) { 348381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand = FastMixerState::COLD_IDLE; 348481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mColdFutexAddr = &mFastMixerFutex; 348581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mColdGen++; 348681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixerFutex = 0; 348781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(); 348881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now 348981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); 349081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (kUseFastMixer == FastMixer_Dynamic) { 349181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink = mOutputSink; 349281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 349381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG 349481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mAudioWatchdog != 0) { 349581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->pause(); 349681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 349781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 349881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 349981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(false /*didModify*/); 350081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 350181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 350281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread::threadLoop_standby(); 350381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 350481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3505bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::PlaybackThread::waitingAsyncCallback_l() 3506bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 3507bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return false; 3508bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 3509bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 3510bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::PlaybackThread::shouldStandby_l() 3511bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 3512bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return !mStandby; 3513bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 3514bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 3515bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::PlaybackThread::waitingAsyncCallback() 3516bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 3517bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 3518bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return waitingAsyncCallback_l(); 3519bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 3520bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 352181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// shared by MIXER and DIRECT, overridden by DUPLICATING 352281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::threadLoop_standby() 352381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 352481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended); 3525062e67a26e0553dd142be622821f493df541f0c6Phil Burk mOutput->standby(); 3526bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mUseAsyncWrite != 0) { 35273b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // discard any pending drain or write ack by incrementing sequence 35283b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence = (mWriteAckSequence + 2) & ~1; 35293b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence = (mDrainSequence + 2) & ~1; 3530bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 35313b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setWriteBlocked(mWriteAckSequence); 35323b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setDraining(mDrainSequence); 3533bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 3534d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent mHwPaused = false; 353581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 353681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 35374c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew Georgevoid AudioFlinger::PlaybackThread::onAddNewTrack_l() 35384c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George{ 35394c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George ALOGV("signal playback thread"); 35404c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George broadcast_l(); 35414c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George} 35424c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George 354381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::threadLoop_mix() 354481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 354581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // obtain the presentation timestamp of the next output buffer 354681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int64_t pts; 354781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status = INVALID_OPERATION; 354881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 354981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mNormalSink != 0) { 355081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = mNormalSink->getNextWriteTimestamp(&pts); 355181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 355281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = mOutputSink->getNextWriteTimestamp(&pts); 355381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 355481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 355581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (status != NO_ERROR) { 355681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pts = AudioBufferProvider::kInvalidPTS; 355781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 355881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 355981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mix buffers... 356081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->process(pts); 356125c2dac12114699e90deb1c579cadebce7b91a97Andy Hung mCurrentWriteLength = mSinkBufferSize; 356281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // increase sleep time progressively when application underrun condition clears. 356381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Only increase sleep time if the mixer is ready for two consecutive times to avoid 356481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // that a steady state of alternating ready/not ready conditions keeps the sleep time 356581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // such that we would underrun the audio HAL. 3566ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent if ((mSleepTimeUs == 0) && (sleepTimeShift > 0)) { 356781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTimeShift--; 356881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 3569ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mSleepTimeUs = 0; 3570ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mStandbyTimeNs = systemTime() + mStandbyDelayNs; 357181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent //TODO: delay standby when effects have a tail 35724c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten 357381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 357481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 357581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::threadLoop_sleepTime() 357681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 357781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If no tracks are ready, sleep once for the duration of an output 357881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // buffer size, then write 0s to the output 3579ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent if (mSleepTimeUs == 0) { 358081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mMixerStatus == MIXER_TRACKS_ENABLED) { 3581ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mSleepTimeUs = mActiveSleepTimeUs >> sleepTimeShift; 3582ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent if (mSleepTimeUs < kMinThreadSleepTimeUs) { 3583ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mSleepTimeUs = kMinThreadSleepTimeUs; 358481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 358581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // reduce sleep time in case of consecutive application underruns to avoid 358681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer 358781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // duration we would end up writing less data than needed by the audio HAL if 358881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // the condition persists. 358981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sleepTimeShift < kMaxThreadSleepTimeShift) { 359081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTimeShift++; 359181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 359281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 3593ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mSleepTimeUs = mIdleSleepTimeUs; 359481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 359581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { 359698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared 359798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // before effects processing or output. 359898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mMixerBufferValid) { 359998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung memset(mMixerBuffer, 0, mMixerBufferSize); 360098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } else { 360198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung memset(mSinkBuffer, 0, mSinkBufferSize); 360298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 3603ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mSleepTimeUs = 0; 360481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), 360581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "anticipated start"); 360681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 360781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // TODO add standby time extension fct of effect tail 360881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 360981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 361081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// prepareTracks_l() must be called with ThreadBase::mLock held 361181784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( 361281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<Track> > *tracksToRemove) 361381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 361481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 361581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixer_state mixerStatus = MIXER_IDLE; 361681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // find out which tracks need to be processed 361781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t count = mActiveTracks.size(); 361881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t mixedTracks = 0; 361981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t tracksWithEffect = 0; 362081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // counts only _active_ fast tracks 362181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t fastTracks = 0; 362281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset 362381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 362481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent float masterVolume = mMasterVolume; 362581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool masterMute = mMasterMute; 362681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 362781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (masterMute) { 362881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent masterVolume = 0; 362981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 363081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Delegate master volume control to effect in output mix effect chain if needed 363181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); 363281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 363381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t v = (uint32_t)(masterVolume * (1 << 24)); 363481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setVolume_l(&v, &v); 363581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent masterVolume = (float)((v + (1 << 23)) >> 24); 363681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain.clear(); 363781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 363881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 363981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // prepare a new state to push 364081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue *sq = NULL; 364181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerState *state = NULL; 364281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool didModify = false; 364381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; 36444d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten if (mFastMixer != 0) { 364581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq = mFastMixer->sq(); 364681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state = sq->begin(); 364781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 364881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 364969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferValid = false; // mMixerBuffer has no valid data until appropriate tracks found. 365098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found. 365169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung 365281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i=0 ; i<count ; i++) { 36539fdcb0a9497ca290bcf364b10868587b6bde3a34Glenn Kasten const sp<Track> t = mActiveTracks[i].promote(); 365481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (t == 0) { 365581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 365681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 365781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 365881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // this const just means the local variable doesn't change 365981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Track* const track = t.get(); 366081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 366181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // process fast tracks 366281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isFastTrack()) { 366381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 366481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // It's theoretically possible (though unlikely) for a fast track to be created 366581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // and then removed within the same normal mix cycle. This is not a problem, as 366681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // the track never becomes active so it's fast mixer slot is never touched. 366781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The converse, of removing an (active) track and then creating a new track 366881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // at the identical fast mixer slot within the same normal mix cycle, 366981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // is impossible because the slot isn't marked available until the end of each cycle. 367081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int j = track->mFastIndex; 367181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(0 < j && j < (int)FastMixerState::kMaxFastTracks); 367281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); 367381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastTrack *fastTrack = &state->mFastTracks[j]; 367481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 367581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Determine whether the track is currently in underrun condition, 367681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // and whether it had a recent underrun. 367781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; 367881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastTrackUnderruns underruns = ftDump->mUnderruns; 367981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t recentFull = (underruns.mBitFields.mFull - 368081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK; 368181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t recentPartial = (underruns.mBitFields.mPartial - 368281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK; 368381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t recentEmpty = (underruns.mBitFields.mEmpty - 368481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK; 368581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t recentUnderruns = recentPartial + recentEmpty; 368681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mObservedUnderruns = underruns; 368781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // don't count underruns that occur while stopping or pausing 368881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // or stopped which can occur when flush() is called while active 368982aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten if (!(track->isStopping() || track->isPausing() || track->isStopped()) && 369082aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten recentUnderruns > 0) { 369182aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun 369282aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten track->mAudioTrackServerProxy->tallyUnderrunFrames(recentUnderruns * mFrameCount); 369381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 369481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 369581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // This is similar to the state machine for normal tracks, 369681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // with a few modifications for fast tracks. 369781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool isActive = true; 369881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent switch (track->mState) { 369981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::STOPPING_1: 370081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track stays active in STOPPING_1 state until first underrun 3701bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (recentUnderruns > 0 || track->isTerminated()) { 370281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mState = TrackBase::STOPPING_2; 370381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 370481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 370581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::PAUSING: 370681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // ramp down is not yet implemented 370781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->setPaused(); 370881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 370981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::RESUMING: 371081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // ramp up is not yet implemented 371181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mState = TrackBase::ACTIVE; 371281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 371381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::ACTIVE: 371481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (recentFull > 0 || recentPartial > 0) { 371581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track has provided at least some frames recently: reset retry count 371681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mRetryCount = kMaxTrackRetries; 371781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 371881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (recentUnderruns == 0) { 371981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // no recent underruns: stay active 372081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 372181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 372281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // there has recently been an underrun of some kind 372381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->sharedBuffer() == 0) { 372481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // were any of the recent underruns "empty" (no frames available)? 372581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (recentEmpty == 0) { 372681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // no, then ignore the partial underruns as they are allowed indefinitely 372781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 372881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 372981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // there has recently been an "empty" underrun: decrement the retry counter 373081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (--(track->mRetryCount) > 0) { 373181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 373281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 373381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // indicate to client process that the track was disabled because of underrun; 373481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it will then automatically call start() when data is available 373596f60d8f04432a1ed503b3e24d5736d28c63c9a2Glenn Kasten android_atomic_or(CBLK_DISABLED, &track->mCblk->mFlags); 373681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // remove from active list, but state remains ACTIVE [confusing but true] 373781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent isActive = false; 373881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 373981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 374081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // fall through 374181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::STOPPING_2: 374281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::PAUSED: 374381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::STOPPED: 374481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::FLUSHED: // flush() while active 374581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Check for presentation complete if track is inactive 374681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // We have consumed all the buffers of this track. 374781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // This would be incomplete if we auto-paused on underrun 374881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 374981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t audioHALFrames = 375081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; 375181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t framesWritten = mBytesWritten / mFrameSize; 375281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { 375381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track stays in active list until presentation is complete 375481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 375581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 375681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 375781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isStopping_2()) { 375881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mState = TrackBase::STOPPED; 375981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 376081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isStopped()) { 376181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Can't reset directly, as fast mixer is still polling this track 376281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track->reset(); 376381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // So instead mark this track as needing to be reset after push with ack 376481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent resetMask |= 1 << i; 376581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 376681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent isActive = false; 376781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 376881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::IDLE: 376981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent default: 3770adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("unexpected track state %d", track->mState); 377181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 377281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 377381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (isActive) { 377481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // was it previously inactive? 377581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!(state->mTrackMask & (1 << j))) { 377681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ExtendedAudioBufferProvider *eabp = track; 377781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent VolumeProvider *vp = track; 377881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mBufferProvider = eabp; 377981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mVolumeProvider = vp; 378081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mChannelMask = track->mChannelMask; 3781e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung fastTrack->mFormat = track->mFormat; 378281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mGeneration++; 378381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mTrackMask |= 1 << j; 378481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent didModify = true; 378581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // no acknowledgement required for newly active tracks 378681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 378781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // cache the combined master volume and stream type volume for fast mixer; this 378881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // lacks any synchronization or barrier so VolumeProvider may read a stale value 3789e4756fe3a387615acb63c6a05788c8db9b5786cbGlenn Kasten track->mCachedVolume = masterVolume * mStreamTypes[track->streamType()].volume; 379081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ++fastTracks; 379181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 379281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // was it previously active? 379381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (state->mTrackMask & (1 << j)) { 379481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mBufferProvider = NULL; 379581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mGeneration++; 379681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mTrackMask &= ~(1 << j); 379781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent didModify = true; 379881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If any fast tracks were removed, we must wait for acknowledgement 379981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // because we're about to decrement the last sp<> on those tracks. 380081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; 380181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 3802adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("fast track %d should have been active", j); 380381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 380481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent tracksToRemove->add(track); 380581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Avoids a misleading display in dumpsys 380681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL; 380781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 380881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 380981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 381081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 381181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // local variable scope to avoid goto warning 381281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 381381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_track_cblk_t* cblk = track->cblk(); 381481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 381581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The first time a track is added we wait 381681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // for all its buffers to be filled before processing it 381781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int name = track->name(); 381881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // make sure that we have enough frames to mix one full buffer. 381981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // enforce this condition only once to enable draining the buffer in case the client 382081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // app does not call stop() and relies on underrun to stop: 382181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed 382281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // during last round 38239f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten size_t desiredFrames; 38248edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung const uint32_t sampleRate = track->mAudioTrackServerProxy->getSampleRate(); 38255a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); 38268edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung 38278edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung desiredFrames = sourceFramesNeededWithTimestretch( 38285a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia sampleRate, mNormalFrameCount, mSampleRate, playbackRate.mSpeed); 38298edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung // TODO: ONLY USED FOR LEGACY RESAMPLERS, remove when they are removed. 38308edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung // add frames already consumed but not yet released by the resampler 38318edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung // because mAudioTrackServerProxy->framesReady() will include these frames 38328edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung desiredFrames += mAudioMixer->getUnreleasedFrames(track->name()); 38338edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung 383481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t minFrames = 1; 383581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && 383681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { 38379f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten minFrames = desiredFrames; 38389f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 383913e4c960ea3db03a43e084fbd85d52aa77f7b871Eric Laurent 384013e4c960ea3db03a43e084fbd85d52aa77f7b871Eric Laurent size_t framesReady = track->framesReady(); 3841e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten if (ATRACE_ENABLED()) { 3842e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten // I wish we had formatted trace names 3843e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten char traceName[16]; 3844e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten strcpy(traceName, "nRdy"); 3845e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten int name = track->name(); 3846e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten if (AudioMixer::TRACK0 <= name && 3847e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten name < (int) (AudioMixer::TRACK0 + AudioMixer::MAX_NUM_TRACKS)) { 3848e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten name -= AudioMixer::TRACK0; 3849e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten traceName[4] = (name / 10) + '0'; 3850e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten traceName[5] = (name % 10) + '0'; 3851e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten } else { 3852e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten traceName[4] = '?'; 3853e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten traceName[5] = '?'; 3854e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten } 3855e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten traceName[6] = '\0'; 3856e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten ATRACE_INT(traceName, framesReady); 3857e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten } 38589f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if ((framesReady >= minFrames) && track->isReady() && 385981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent !track->isPaused() && !track->isTerminated()) 386081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 3861f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten ALOGVV("track %d s=%08x [OK] on thread %p", name, cblk->mServer, this); 386281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 386381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixedTracks++; 386481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 386569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // track->mainBuffer() != mSinkBuffer or mMixerBuffer means 386669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // there is an effect chain connected to the track 386781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain.clear(); 386869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung if (track->mainBuffer() != mSinkBuffer && 386969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung track->mainBuffer() != mMixerBuffer) { 387098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mEffectBufferEnabled) { 387198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferValid = true; // Later can set directly. 387298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 387381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain = getEffectChain_l(track->sessionId()); 387481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Delegate volume control to effect in track effect chain if needed 387581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 387681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent tracksWithEffect++; 387781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 387881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on " 387981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "session %d", 388081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent name, track->sessionId()); 388181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 388281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 388381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 388481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 388581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int param = AudioMixer::VOLUME; 388681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->mFillingUpStatus == Track::FS_FILLED) { 388781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // no ramp for the first volume setting 388881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mFillingUpStatus = Track::FS_ACTIVE; 388981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->mState == TrackBase::RESUMING) { 389081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mState = TrackBase::ACTIVE; 389181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent param = AudioMixer::RAMP_VOLUME; 389281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 389381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); 3894f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten // FIXME should not make a decision based on mServer 3895f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten } else if (cblk->mServer != 0) { 389681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If the track is stopped before the first frame was mixed, 389781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // do not apply ramp 389881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent param = AudioMixer::RAMP_VOLUME; 389981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 390081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 390181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // compute volume for this track 39026be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung uint32_t vl, vr; // in U8.24 integer format 39036be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung float vlf, vrf, vaf; // in [0.0, 1.0] float format 3904e4756fe3a387615acb63c6a05788c8db9b5786cbGlenn Kasten if (track->isPausing() || mStreamTypes[track->streamType()].mute) { 39056be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vl = vr = 0; 39066be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vlf = vrf = vaf = 0.; 390781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isPausing()) { 390881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->setPaused(); 390981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 391081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 391181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 391281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // read original volumes with volume control 391381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent float typeVolume = mStreamTypes[track->streamType()].volume; 391481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent float v = masterVolume * typeVolume; 39159f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; 3916c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten gain_minifloat_packed_t vlr = proxy->getVolumeLR(); 39176be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); 39186be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); 391981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track volumes come from shared memory, so can't be trusted and must be clamped 3920c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten if (vlf > GAIN_FLOAT_UNITY) { 3921c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten ALOGV("Track left volume out of range: %.3g", vlf); 3922c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten vlf = GAIN_FLOAT_UNITY; 392381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 3924c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten if (vrf > GAIN_FLOAT_UNITY) { 3925c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten ALOGV("Track right volume out of range: %.3g", vrf); 3926c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten vrf = GAIN_FLOAT_UNITY; 392781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 392881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // now apply the master volume and stream type volume 39296be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vlf *= v; 39306be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vrf *= v; 393181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // assuming master volume and stream type volume each go up to 1.0, 39326be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung // then derive vl and vr as U8.24 versions for the effect chain 39336be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT; 39346be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vl = (uint32_t) (scaleto8_24 * vlf); 39356be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vr = (uint32_t) (scaleto8_24 * vrf); 39366be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung // vl and vr are now in U8.24 format 3937e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten uint16_t sendLevel = proxy->getSendLevel_U4_12(); 393881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // send level comes from shared memory and so may be corrupt 393981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sendLevel > MAX_GAIN_INT) { 394081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("Track send level out of range: %04X", sendLevel); 394181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sendLevel = MAX_GAIN_INT; 394281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 39436be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung // vaf is represented as [0.0, 1.0] float by rescaling sendLevel 39446be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vaf = v * sendLevel * (1. / MAX_GAIN_INT); 394581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 3946bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 394781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Delegate volume control to effect in track effect chain if needed 394881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0 && chain->setVolume_l(&vl, &vr)) { 394981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Do not ramp volume if volume is controlled by effect 395081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent param = AudioMixer::VOLUME; 3951b6be7f22a82ee3bad8bcc709d21e72fc4727da09Bryant Liu // Update remaining floating point volume levels 3952b6be7f22a82ee3bad8bcc709d21e72fc4727da09Bryant Liu vlf = (float)vl / (1 << 24); 3953b6be7f22a82ee3bad8bcc709d21e72fc4727da09Bryant Liu vrf = (float)vr / (1 << 24); 395481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mHasVolumeController = true; 395581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 395681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // force no volume ramp when volume controller was just disabled or removed 395781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // from effect chain to avoid volume spike 395881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->mHasVolumeController) { 395981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent param = AudioMixer::VOLUME; 396081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 396181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mHasVolumeController = false; 396281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 396381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 396481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // XXX: these things DON'T need to be done each time 396581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->setBufferProvider(name, track); 396681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->enable(name); 396781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 39686be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, &vlf); 39696be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, &vrf); 39706be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, &vaf); 397181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->setParameter( 397281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent name, 397381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::TRACK, 397481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::FORMAT, (void *)track->format()); 397581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->setParameter( 397681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent name, 397781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::TRACK, 3978377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask()); 39799a59276fb465e492138e0576523b54079671e8f4Andy Hung mAudioMixer->setParameter( 39809a59276fb465e492138e0576523b54079671e8f4Andy Hung name, 39819a59276fb465e492138e0576523b54079671e8f4Andy Hung AudioMixer::TRACK, 39829a59276fb465e492138e0576523b54079671e8f4Andy Hung AudioMixer::MIXER_CHANNEL_MASK, (void *)(uintptr_t)mChannelMask); 3983e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten // limit track sample rate to 2 x output sample rate, which changes at re-configuration 3984cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX; 39859f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten uint32_t reqSampleRate = track->mAudioTrackServerProxy->getSampleRate(); 3986e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten if (reqSampleRate == 0) { 3987e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten reqSampleRate = mSampleRate; 3988e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten } else if (reqSampleRate > maxSampleRate) { 3989e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten reqSampleRate = maxSampleRate; 3990e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten } 399181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->setParameter( 399281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent name, 399381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::RESAMPLE, 399481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::SAMPLE_RATE, 3995377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT (void *)(uintptr_t)reqSampleRate); 39968edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung 39975a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); 39988edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung mAudioMixer->setParameter( 39998edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung name, 40008edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung AudioMixer::TIMESTRETCH, 40018edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung AudioMixer::PLAYBACK_RATE, 40025a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia &playbackRate); 40038edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung 400469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung /* 400569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * Select the appropriate output buffer for the track. 400669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * 400798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung * Tracks with effects go into their own effects chain buffer 400898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung * and from there into either mEffectBuffer or mSinkBuffer. 400969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * 401069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * Other tracks can use mMixerBuffer for higher precision 401169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * channel accumulation. If this buffer is enabled 401269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * (mMixerBufferEnabled true), then selected tracks will accumulate 401369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * into it. 401469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * 401569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung */ 401669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung if (mMixerBufferEnabled 401769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung && (track->mainBuffer() == mSinkBuffer 401869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung || track->mainBuffer() == mMixerBuffer)) { 401969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mAudioMixer->setParameter( 402069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung name, 402169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung AudioMixer::TRACK, 4022788207057ed4b8df4719ed8089f376ef52de9ca1Andy Hung AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat); 402369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mAudioMixer->setParameter( 402469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung name, 402569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung AudioMixer::TRACK, 402669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer); 402769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // TODO: override track->mainBuffer()? 402869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferValid = true; 402969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung } else { 403069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mAudioMixer->setParameter( 403169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung name, 403269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung AudioMixer::TRACK, 4033788207057ed4b8df4719ed8089f376ef52de9ca1Andy Hung AudioMixer::MIXER_FORMAT, (void *)AUDIO_FORMAT_PCM_16_BIT); 403469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mAudioMixer->setParameter( 403569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung name, 403669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung AudioMixer::TRACK, 403769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); 403869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung } 403981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->setParameter( 404081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent name, 404181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::TRACK, 404281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); 404381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 404481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // reset retry count 404581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mRetryCount = kMaxTrackRetries; 404681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 404781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If one track is ready, set the mixer ready if: 404881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // - the mixer was not ready during previous round OR 404981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // - no other track is not ready 405081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || 405181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixerStatus != MIXER_TRACKS_ENABLED) { 405281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixerStatus = MIXER_TRACKS_READY; 405381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 405481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 40559f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { 405608fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung ALOGV("track(%p) underrun, framesReady(%zu) < framesDesired(%zd)", 405708fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung track, framesReady, desiredFrames); 405882aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten track->mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames); 40599f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 406081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // clear effect chain input buffer if an active track underruns to avoid sending 406181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // previous audio buffer again to effects 406281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain = getEffectChain_l(track->sessionId()); 406381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 406481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->clearInputBuffer(); 406581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 406681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4067f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten ALOGVV("track %d s=%08x [NOT READY] on thread %p", name, cblk->mServer, this); 406881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((track->sharedBuffer() != 0) || track->isTerminated() || 406981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->isStopped() || track->isPaused()) { 407081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // We have consumed all the buffers of this track. 407181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Remove it from the list of active tracks. 407281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // TODO: use actual buffer filling status instead of latency when available from 407381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // audio HAL 407481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; 407581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t framesWritten = mBytesWritten / mFrameSize; 407681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { 407781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isStopped()) { 407881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->reset(); 407981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 408081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent tracksToRemove->add(track); 408181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 408281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 408381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // No buffers for this track. Give it a few chances to 408481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // fill a buffer, then remove it from active list. 408581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (--(track->mRetryCount) <= 0) { 4086c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten ALOGI("BUFFER TIMEOUT: remove(%d) from active list on thread %p", name, this); 408781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent tracksToRemove->add(track); 408881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // indicate to client process that the track was disabled because of underrun; 408981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it will then automatically call start() when data is available 409096f60d8f04432a1ed503b3e24d5736d28c63c9a2Glenn Kasten android_atomic_or(CBLK_DISABLED, &cblk->mFlags); 409181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If one track is not ready, mark the mixer also not ready if: 409281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // - the mixer was ready during previous round OR 409381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // - no other track is ready 409481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || 409581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixerStatus != MIXER_TRACKS_READY) { 409681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixerStatus = MIXER_TRACKS_ENABLED; 409781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 409881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 409981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->disable(name); 410081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 410181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 410281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } // local variable scope to avoid goto warning 410381784c37c61b09289654b979567a42bf73cd2b12Eric Laurenttrack_is_ready: ; 410481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 410581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 410681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 410781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Push the new FastMixer state if necessary 410881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool pauseAudioWatchdog = false; 410981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (didModify) { 411081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mFastTracksGen++; 411181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle 411281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (kUseFastMixer == FastMixer_Dynamic && 411381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { 411481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand = FastMixerState::COLD_IDLE; 411581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mColdFutexAddr = &mFastMixerFutex; 411681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mColdGen++; 411781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixerFutex = 0; 411881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (kUseFastMixer == FastMixer_Dynamic) { 411981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink = mOutputSink; 412081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 412181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If we go into cold idle, need to wait for acknowledgement 412281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // so that fast mixer stops doing I/O. 412381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; 412481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pauseAudioWatchdog = true; 412581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 412681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 412781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sq != NULL) { 412881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(didModify); 412981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->push(block); 413081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 413181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG 413281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (pauseAudioWatchdog && mAudioWatchdog != 0) { 413381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->pause(); 413481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 413581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 413681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 413781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Now perform the deferred reset on fast tracks that have stopped 413881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent while (resetMask != 0) { 413981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t i = __builtin_ctz(resetMask); 414081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(i < count); 414181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent resetMask &= ~(1 << i); 414281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> t = mActiveTracks[i].promote(); 414381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (t == 0) { 414481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 414581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 414681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Track* track = t.get(); 414781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(track->isFastTrack() && track->isStopped()); 414881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->reset(); 414981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 415081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 415181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // remove all the tracks that need to be... 4152bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent removeTracks_l(*tracksToRemove); 415381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 415497d547da43c9c41711d1ed1e3f4fa87c2ee3cb9aEric Laurent if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0) { 415597d547da43c9c41711d1ed1e3f4fa87c2ee3cb9aEric Laurent mEffectBufferValid = true; 4156ac302143551a8b964f026385a524dda9ff8ea5baMarco Nelissen } 4157ac302143551a8b964f026385a524dda9ff8ea5baMarco Nelissen 4158ac302143551a8b964f026385a524dda9ff8ea5baMarco Nelissen if (mEffectBufferValid) { 415957088b5c8e76855b99b3e6b3e410de5b6382670eMarco Nelissen // as long as there are effects we should clear the effects buffer, to avoid 416057088b5c8e76855b99b3e6b3e410de5b6382670eMarco Nelissen // passing a non-clean buffer to the effect chain 416157088b5c8e76855b99b3e6b3e410de5b6382670eMarco Nelissen memset(mEffectBuffer, 0, mEffectBufferSize); 416297d547da43c9c41711d1ed1e3f4fa87c2ee3cb9aEric Laurent } 416369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // sink or mix buffer must be cleared if all tracks are connected to an 416469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // effect chain as in this case the mixer will not write to the sink or mix buffer 416569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // and track effects will accumulate into it 4166bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || 4167bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent (mixedTracks == 0 && fastTracks > 0))) { 416881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME as a performance optimization, should remember previous zero status 416969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung if (mMixerBufferValid) { 417069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung memset(mMixerBuffer, 0, mMixerBufferSize); 417169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // TODO: In testing, mSinkBuffer below need not be cleared because 417269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer 417369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // after mixing. 417469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // 417569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // To enforce this guarantee: 417669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || 417769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // (mixedTracks == 0 && fastTracks > 0)) 417869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // must imply MIXER_TRACKS_READY. 417969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // Later, we may clear buffers regardless, and skip much of this logic. 418069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung } 418198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // FIXME as a performance optimization, should remember previous zero status 41825567aaf4818007cd8e77329683a91c0f5d7a8837Andy Hung memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize); 418381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 418481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 418581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if any fast tracks, then status is ready 418681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMixerStatusIgnoringFastTracks = mixerStatus; 418781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (fastTracks > 0) { 418881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixerStatus = MIXER_TRACKS_READY; 418981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 419081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mixerStatus; 419181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 419281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 419381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// getTrackName_l() must be called with ThreadBase::mLock held 4194e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hungint AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask, 4195e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung audio_format_t format, int sessionId) 419681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 4197e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung return mAudioMixer->getTrackName(channelMask, format, sessionId); 419881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 419981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 420081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// deleteTrackName_l() must be called with ThreadBase::mLock held 420181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::deleteTrackName_l(int name) 420281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 420381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("remove track (%d) and delete from mixer", name); 420481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->deleteTrackName(name); 420581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 420681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 42071035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// checkForNewParameter_l() must be called with ThreadBase::mLock held 42081035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentbool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair, 42091035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status_t& status) 421081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 421181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool reconfig = false; 421281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 42131035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = NO_ERROR; 421481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 42151035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // if !&IDLE, holds the FastMixer state to restore after new parameters processed 42161035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; 42174d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten if (mFastMixer != 0) { 42181035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 42191035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent FastMixerState *state = sq->begin(); 42201035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (!(state->mCommand & FastMixerState::IDLE)) { 42211035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent previousCommand = state->mCommand; 42221035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent state->mCommand = FastMixerState::HOT_IDLE; 42231035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sq->end(); 42241035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); 42251035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 42261035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sq->end(false /*didModify*/); 422781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 42281035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 422981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 42301035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent AudioParameter param = AudioParameter(keyValuePair); 42311035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent int value; 42321035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { 42331035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 42341035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 42351035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { 42369a59276fb465e492138e0576523b54079671e8f4Andy Hung if (!isValidPcmSinkFormat((audio_format_t) value)) { 42371035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = BAD_VALUE; 42381035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 42391035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // no need to save value, since it's constant 424081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent reconfig = true; 424181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 42421035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 42431035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { 42449a59276fb465e492138e0576523b54079671e8f4Andy Hung if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) { 42451035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = BAD_VALUE; 42461035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 42471035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // no need to save value, since it's constant 42481035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 424981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 42501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 42511035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { 42521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // do not accept frame count changes if tracks are open as the track buffer 42531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // size depends on frame count and correct behavior would not be guaranteed 42541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // if frame count is changed after track creation 42551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (!mTracks.isEmpty()) { 42561035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = INVALID_OPERATION; 42571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 42581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 425981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 42601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 42611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { 426281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef ADD_BATTERY_DATA 42631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // when changing the audio output device, call addBatteryData to notify 42641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // the change 42651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (mOutDevice != value) { 42661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent uint32_t params = 0; 42671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // check whether speaker is on 42681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (value & AUDIO_DEVICE_OUT_SPEAKER) { 42691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent params |= IMediaPlayerService::kBatteryDataSpeakerOn; 42701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 427181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 42721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent audio_devices_t deviceWithoutSpeaker 42731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; 42741035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // check if any other device (except speaker) is on 4275054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (value & deviceWithoutSpeaker) { 42761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; 42771035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 427881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 42791035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (params != 0) { 42801035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent addBatteryData(params); 428181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 42821035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 428381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 428481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 42851035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // forward device change to effects that have requested to be 42861035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // aware of attached audio device. 42871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (value != AUDIO_DEVICE_NONE) { 42881035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mOutDevice = value; 42891035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 42901035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mEffectChains[i]->setDevice_l(mOutDevice); 429181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 429281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 42931035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 429481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 42951035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == NO_ERROR) { 42961035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = mOutput->stream->common.set_parameters(&mOutput->stream->common, 42971035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent keyValuePair.string()); 42981035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (!mStandby && status == INVALID_OPERATION) { 4299062e67a26e0553dd142be622821f493df541f0c6Phil Burk mOutput->standby(); 43001035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mStandby = true; 43011035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mBytesWritten = 0; 430281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = mOutput->stream->common.set_parameters(&mOutput->stream->common, 43031035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent keyValuePair.string()); 43041035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 43051035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == NO_ERROR && reconfig) { 43061035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent readOutputParameters_l(); 43071035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent delete mAudioMixer; 43081035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); 43091035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent for (size_t i = 0; i < mTracks.size() ; i++) { 4310e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung int name = getTrackName_l(mTracks[i]->mChannelMask, 4311e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung mTracks[i]->mFormat, mTracks[i]->mSessionId); 43121035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (name < 0) { 43131035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent break; 431481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 43151035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mTracks[i]->mName = name; 431681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 431773e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); 431881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 431981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 432081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 432181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!(previousCommand & FastMixerState::IDLE)) { 43224d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten ALOG_ASSERT(mFastMixer != 0); 432381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 432481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerState *state = sq->begin(); 432581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); 432681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand = previousCommand; 432781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(); 432881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); 432981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 433081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 433181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return reconfig; 433281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 433381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 433481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 433581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) 433681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 433781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const size_t SIZE = 256; 433881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char buffer[SIZE]; 433981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent String8 result; 434081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 434181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread::dumpInternals(fd, args); 434281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 434387cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " AudioMixer tracks: 0x%08x\n", mAudioMixer->trackNames()); 434481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 434581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Make a non-atomic copy of fast mixer dump state so it won't change underneath us 43464182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten const FastMixerDumpState copy(mFastMixerDumpState); 434781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent copy.dump(fd); 434881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 434981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef STATE_QUEUE_DUMP 435081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Similar for state queue 435181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent StateQueueObserverDump observerCopy = mStateQueueObserverDump; 435281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent observerCopy.dump(fd); 435381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; 435481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mutatorCopy.dump(fd); 435581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 435681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 435746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 435881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Write the tee output to a .wav file 435981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpTee(fd, mTeeSource, mId); 436046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 436181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 436281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG 436381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mAudioWatchdog != 0) { 436481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Make a non-atomic copy of audio watchdog dump so it won't change underneath us 436581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioWatchdogDump wdCopy = mAudioWatchdogDump; 436681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent wdCopy.dump(fd); 436781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 436881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 436981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 437081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 437181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const 437281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 437381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; 437481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 437581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 437681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const 437781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 437881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); 437981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 438081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 438181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::cacheParameters_l() 438281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 438381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread::cacheParameters_l(); 438481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 438581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME: Relaxed timing because of a certain device that can't meet latency 438681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Should be reduced to 2x after the vendor fixes the driver issue 438781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // increase threshold again due to low power audio mode. The way this warning 438881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // threshold is calculated and its usefulness should be reconsidered anyway. 438981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; 439081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 439181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 439281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 439381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 439481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, 439572e3f39146fce4686bd96f11057c051bea376dfbEric Laurent AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device, bool systemReady) 439672e3f39146fce4686bd96f11057c051bea376dfbEric Laurent : PlaybackThread(audioFlinger, output, id, device, DIRECT, systemReady) 439781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mLeftVolFloat, mRightVolFloat 439881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 439981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 440081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4401bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, 4402bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioStreamOut* output, audio_io_handle_t id, uint32_t device, 440372e3f39146fce4686bd96f11057c051bea376dfbEric Laurent ThreadBase::type_t type, bool systemReady) 440472e3f39146fce4686bd96f11057c051bea376dfbEric Laurent : PlaybackThread(audioFlinger, output, id, device, type, systemReady) 4405bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // mLeftVolFloat, mRightVolFloat 4406bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4407bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4408bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 440981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::DirectOutputThread::~DirectOutputThread() 441081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 441181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 441281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4413bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack) 4414bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4415bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent audio_track_cblk_t* cblk = track->cblk(); 4416bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent float left, right; 4417bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4418bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mMasterMute || mStreamTypes[track->streamType()].mute) { 4419bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent left = right = 0; 4420bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else { 4421bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent float typeVolume = mStreamTypes[track->streamType()].volume; 4422bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent float v = mMasterVolume * typeVolume; 4423bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; 4424c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten gain_minifloat_packed_t vlr = proxy->getVolumeLR(); 4425c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten left = float_from_gain(gain_minifloat_unpack_left(vlr)); 4426c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten if (left > GAIN_FLOAT_UNITY) { 4427c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten left = GAIN_FLOAT_UNITY; 4428c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten } 4429c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten left *= v; 4430c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten right = float_from_gain(gain_minifloat_unpack_right(vlr)); 4431c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten if (right > GAIN_FLOAT_UNITY) { 4432c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten right = GAIN_FLOAT_UNITY; 4433c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten } 4434c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten right *= v; 4435bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4436bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4437bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (lastTrack) { 4438bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (left != mLeftVolFloat || right != mRightVolFloat) { 4439bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mLeftVolFloat = left; 4440bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mRightVolFloat = right; 4441bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4442bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Convert volumes from float to 8.24 4443bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent uint32_t vl = (uint32_t)(left * (1 << 24)); 4444bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent uint32_t vr = (uint32_t)(right * (1 << 24)); 4445bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4446bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Delegate volume control to effect in track effect chain if needed 4447bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // only one effect chain can be present on DirectOutputThread, so if 4448bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // there is one, the track is connected to it 4449bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (!mEffectChains.isEmpty()) { 4450bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mEffectChains[0]->setVolume_l(&vl, &vr); 4451bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent left = (float)vl / (1 << 24); 4452bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent right = (float)vr / (1 << 24); 4453bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4454bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mOutput->stream->set_volume) { 4455bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mOutput->stream->set_volume(mOutput->stream, left, right); 4456bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4457bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4458bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4459bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4460bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 446143b4dcc660e6da96285e4672ae371070ab845401Phil Burkvoid AudioFlinger::DirectOutputThread::onAddNewTrack_l() 446243b4dcc660e6da96285e4672ae371070ab845401Phil Burk{ 446343b4dcc660e6da96285e4672ae371070ab845401Phil Burk sp<Track> previousTrack = mPreviousTrack.promote(); 446443b4dcc660e6da96285e4672ae371070ab845401Phil Burk sp<Track> latestTrack = mLatestActiveTrack.promote(); 446543b4dcc660e6da96285e4672ae371070ab845401Phil Burk 446643b4dcc660e6da96285e4672ae371070ab845401Phil Burk if (previousTrack != 0 && latestTrack != 0 && 446743b4dcc660e6da96285e4672ae371070ab845401Phil Burk (previousTrack->sessionId() != latestTrack->sessionId())) { 446843b4dcc660e6da96285e4672ae371070ab845401Phil Burk mFlushPending = true; 446943b4dcc660e6da96285e4672ae371070ab845401Phil Burk } 447043b4dcc660e6da96285e4672ae371070ab845401Phil Burk PlaybackThread::onAddNewTrack_l(); 447143b4dcc660e6da96285e4672ae371070ab845401Phil Burk} 4472bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 447381784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( 447481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<Track> > *tracksToRemove 447581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent) 447681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 4477d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent size_t count = mActiveTracks.size(); 447881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixer_state mixerStatus = MIXER_IDLE; 4479d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent bool doHwPause = false; 4480d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent bool doHwResume = false; 448181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 448281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // find out which tracks need to be processed 4483d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent for (size_t i = 0; i < count; i++) { 4484d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent sp<Track> t = mActiveTracks[i].promote(); 448581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The track died recently 448681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (t == 0) { 4487d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent continue; 448881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 448981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 449043b4dcc660e6da96285e4672ae371070ab845401Phil Burk if (t->isInvalid()) { 449143b4dcc660e6da96285e4672ae371070ab845401Phil Burk ALOGW("An invalidated track shouldn't be in active list"); 449243b4dcc660e6da96285e4672ae371070ab845401Phil Burk tracksToRemove->add(t); 449343b4dcc660e6da96285e4672ae371070ab845401Phil Burk continue; 449443b4dcc660e6da96285e4672ae371070ab845401Phil Burk } 449543b4dcc660e6da96285e4672ae371070ab845401Phil Burk 449681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Track* const track = t.get(); 449781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_track_cblk_t* cblk = track->cblk(); 4498fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // Only consider last track started for volume and mixer state control. 4499fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // In theory an older track could underrun and restart after the new one starts 4500fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // but as we only care about the transition phase between two tracks on a 4501fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // direct output, it is not a problem to ignore the underrun case. 4502fd4779740ec3e9e865d5514464df26d015354388Eric Laurent sp<Track> l = mLatestActiveTrack.promote(); 4503fd4779740ec3e9e865d5514464df26d015354388Eric Laurent bool last = l.get() == track; 450481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 45056fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk if (track->isPausing()) { 4506d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent track->setPaused(); 45076fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk if (mHwSupportsPause && last && !mHwPaused) { 4508d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent doHwPause = true; 4509d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent mHwPaused = true; 4510d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4511d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent tracksToRemove->add(track); 4512d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } else if (track->isFlushPending()) { 4513d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent track->flushAck(); 4514d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent if (last) { 451543b4dcc660e6da96285e4672ae371070ab845401Phil Burk mFlushPending = true; 4516d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 45176fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk } else if (track->isResumePending()) { 4518d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent track->resumeAck(); 45196fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk if (last && mHwPaused) { 45206fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk doHwResume = true; 45216fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk mHwPaused = false; 4522d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4523d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4524d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent 452581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The first time a track is added we wait 452699adee3c3d9cde6819741a38163954808fea270aPhil Burk // for all its buffers to be filled before processing it. 452799adee3c3d9cde6819741a38163954808fea270aPhil Burk // Allow draining the buffer in case the client 452899adee3c3d9cde6819741a38163954808fea270aPhil Burk // app does not call stop() and relies on underrun to stop: 452999adee3c3d9cde6819741a38163954808fea270aPhil Burk // hence the test on (track->mRetryCount > 1). 453099adee3c3d9cde6819741a38163954808fea270aPhil Burk // If retryCount<=1 then track is about to underrun and be removed. 453181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t minFrames; 453299adee3c3d9cde6819741a38163954808fea270aPhil Burk if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing() 453399adee3c3d9cde6819741a38163954808fea270aPhil Burk && (track->mRetryCount > 1)) { 453481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minFrames = mNormalFrameCount; 453581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 453681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minFrames = 1; 453781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 4538bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4539ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent if ((track->framesReady() >= minFrames) && track->isReady() && !track->isPaused() && 4540ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent !track->isStopping_2() && !track->isStopped()) 454181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 4542f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten ALOGVV("track %d s=%08x [OK]", track->name(), cblk->mServer); 454381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 454481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->mFillingUpStatus == Track::FS_FILLED) { 454581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mFillingUpStatus = Track::FS_ACTIVE; 45461abbdb4429479975718421c4fef3f79ce7c820e3Eric Laurent // make sure processVolume_l() will apply new volume even if 0 45471abbdb4429479975718421c4fef3f79ce7c820e3Eric Laurent mLeftVolFloat = mRightVolFloat = -1.0; 4548d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent if (!mHwSupportsPause) { 4549d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent track->resumeAck(); 455081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 455181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 455281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 455381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // compute volume for this track 4554bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent processVolume_l(track, last); 4555bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (last) { 455643b4dcc660e6da96285e4672ae371070ab845401Phil Burk sp<Track> previousTrack = mPreviousTrack.promote(); 455743b4dcc660e6da96285e4672ae371070ab845401Phil Burk if (previousTrack != 0) { 455843b4dcc660e6da96285e4672ae371070ab845401Phil Burk if (track != previousTrack.get()) { 455943b4dcc660e6da96285e4672ae371070ab845401Phil Burk // Flush any data still being written from last track 456043b4dcc660e6da96285e4672ae371070ab845401Phil Burk mBytesRemaining = 0; 456143b4dcc660e6da96285e4672ae371070ab845401Phil Burk // flush data already sent if changing audio session as audio 456243b4dcc660e6da96285e4672ae371070ab845401Phil Burk // comes from a different source. Also invalidate previous track to force a 456343b4dcc660e6da96285e4672ae371070ab845401Phil Burk // seek when resuming. 456443b4dcc660e6da96285e4672ae371070ab845401Phil Burk if (previousTrack->sessionId() != track->sessionId()) { 456543b4dcc660e6da96285e4672ae371070ab845401Phil Burk previousTrack->invalidate(); 456643b4dcc660e6da96285e4672ae371070ab845401Phil Burk } 456743b4dcc660e6da96285e4672ae371070ab845401Phil Burk } 456843b4dcc660e6da96285e4672ae371070ab845401Phil Burk } 456943b4dcc660e6da96285e4672ae371070ab845401Phil Burk mPreviousTrack = track; 457043b4dcc660e6da96285e4672ae371070ab845401Phil Burk 4571d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent // reset retry count 4572d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent track->mRetryCount = kMaxTrackRetriesDirect; 4573d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent mActiveTrack = t; 4574d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent mixerStatus = MIXER_TRACKS_READY; 45755cff403679fc44c8293de81aed31c459c6129243Eric Laurent if (mHwPaused) { 45760f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent doHwResume = true; 45770f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent mHwPaused = false; 45780f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent } 4579d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent } 458081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 4581d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent // clear effect chain input buffer if the last active track started underruns 4582d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent // to avoid sending previous audio buffer again to effects 4583fd4779740ec3e9e865d5514464df26d015354388Eric Laurent if (!mEffectChains.isEmpty() && last) { 458481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains[0]->clearInputBuffer(); 458581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 4586ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent if (track->isStopping_1()) { 4587ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent track->mState = TrackBase::STOPPING_2; 4588b369cafd67beb63dd0278dba543f519956208a7fEric Laurent if (last && mHwPaused) { 4589b369cafd67beb63dd0278dba543f519956208a7fEric Laurent doHwResume = true; 4590b369cafd67beb63dd0278dba543f519956208a7fEric Laurent mHwPaused = false; 4591b369cafd67beb63dd0278dba543f519956208a7fEric Laurent } 4592ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent } 4593ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent if ((track->sharedBuffer() != 0) || track->isStopped() || 4594ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent track->isStopping_2() || track->isPaused()) { 459581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // We have consumed all the buffers of this track. 459681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Remove it from the list of active tracks. 4597ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent size_t audioHALFrames; 4598ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent if (audio_is_linear_pcm(mFormat)) { 4599ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent audioHALFrames = (latency_l() * mSampleRate) / 1000; 4600ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent } else { 4601ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent audioHALFrames = 0; 4602ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent } 4603ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent 460481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t framesWritten = mBytesWritten / mFrameSize; 4605fd4779740ec3e9e865d5514464df26d015354388Eric Laurent if (mStandby || !last || 4606fd4779740ec3e9e865d5514464df26d015354388Eric Laurent track->presentationComplete(framesWritten, audioHALFrames)) { 4607ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent if (track->isStopping_2()) { 4608ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent track->mState = TrackBase::STOPPED; 4609ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent } 461081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isStopped()) { 461181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->reset(); 461281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 4613d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent tracksToRemove->add(track); 461481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 461581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 461681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // No buffers for this track. Give it a few chances to 461781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // fill a buffer, then remove it from active list. 4618d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent // Only consider last track started for mixer state control 461981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (--(track->mRetryCount) <= 0) { 462081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); 4621d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent tracksToRemove->add(track); 4622a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent // indicate to client process that the track was disabled because of underrun; 4623a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent // it will then automatically call start() when data is available 4624a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent android_atomic_or(CBLK_DISABLED, &cblk->mFlags); 4625bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if (last) { 462681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixerStatus = MIXER_TRACKS_ENABLED; 46275cff403679fc44c8293de81aed31c459c6129243Eric Laurent if (mHwSupportsPause && !mHwPaused && !mStandby) { 46280f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent doHwPause = true; 46290f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent mHwPaused = true; 46300f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent } 463181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 463281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 463381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 463481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 463581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4636d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent // if an active track did not command a flush, check for pending flush on stopped tracks 463743b4dcc660e6da96285e4672ae371070ab845401Phil Burk if (!mFlushPending) { 4638d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 4639d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent if (mTracks[i]->isFlushPending()) { 4640d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent mTracks[i]->flushAck(); 464143b4dcc660e6da96285e4672ae371070ab845401Phil Burk mFlushPending = true; 4642d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4643d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4644d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4645d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent 4646d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent // make sure the pause/flush/resume sequence is executed in the right order. 4647d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent // If a flush is pending and a track is active but the HW is not paused, force a HW pause 4648d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent // before flush and then resume HW. This can happen in case of pause/flush/resume 4649d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent // if resume is received before pause is executed. 4650d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent if (mHwSupportsPause && !mStandby && 465143b4dcc660e6da96285e4672ae371070ab845401Phil Burk (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { 4652d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent mOutput->stream->pause(mOutput->stream); 4653d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 465443b4dcc660e6da96285e4672ae371070ab845401Phil Burk if (mFlushPending) { 4655d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent flushHw_l(); 4656d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4657d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent if (mHwSupportsPause && !mStandby && doHwResume) { 4658d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent mOutput->stream->resume(mOutput->stream); 4659d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 466081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // remove all the tracks that need to be... 4661bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent removeTracks_l(*tracksToRemove); 466281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 466381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mixerStatus; 466481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 466581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 466681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DirectOutputThread::threadLoop_mix() 466781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 466881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t frameCount = mFrameCount; 46692098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung int8_t *curBuf = (int8_t *)mSinkBuffer; 467081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // output audio to hardware 467181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent while (frameCount) { 467234542acfa25c6413c87a94b6f7cc315a0c496277Glenn Kasten AudioBufferProvider::Buffer buffer; 467381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent buffer.frameCount = frameCount; 4674062e67a26e0553dd142be622821f493df541f0c6Phil Burk status_t status = mActiveTrack->getNextBuffer(&buffer); 4675062e67a26e0553dd142be622821f493df541f0c6Phil Burk if (status != NO_ERROR || buffer.raw == NULL) { 467681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent memset(curBuf, 0, frameCount * mFrameSize); 467781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 467881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 467981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); 468081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent frameCount -= buffer.frameCount; 468181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent curBuf += buffer.frameCount * mFrameSize; 468281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mActiveTrack->releaseBuffer(&buffer); 468381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 46842098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer; 4685ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mSleepTimeUs = 0; 4686ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mStandbyTimeNs = systemTime() + mStandbyDelayNs; 468781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mActiveTrack.clear(); 468881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 468981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 469081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DirectOutputThread::threadLoop_sleepTime() 469181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 4692d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent // do not write to HAL when paused 46930f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent if (mHwPaused || (usesHwAvSync() && mStandby)) { 4694ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mSleepTimeUs = mIdleSleepTimeUs; 4695d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent return; 4696d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4697ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent if (mSleepTimeUs == 0) { 469881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mMixerStatus == MIXER_TRACKS_ENABLED) { 4699ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mSleepTimeUs = mActiveSleepTimeUs; 470081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 4701ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mSleepTimeUs = mIdleSleepTimeUs; 470281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 470381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) { 47042098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung memset(mSinkBuffer, 0, mFrameCount * mFrameSize); 4705ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mSleepTimeUs = 0; 470681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 470781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 470881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4709d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurentvoid AudioFlinger::DirectOutputThread::threadLoop_exit() 4710d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent{ 4711d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent { 4712d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent Mutex::Autolock _l(mLock); 4713d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 4714d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent if (mTracks[i]->isFlushPending()) { 4715d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent mTracks[i]->flushAck(); 471643b4dcc660e6da96285e4672ae371070ab845401Phil Burk mFlushPending = true; 4717d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4718d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 471943b4dcc660e6da96285e4672ae371070ab845401Phil Burk if (mFlushPending) { 4720d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent flushHw_l(); 4721d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4722d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4723d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent PlaybackThread::threadLoop_exit(); 4724d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent} 4725d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent 4726d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent// must be called with thread mutex locked 4727d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurentbool AudioFlinger::DirectOutputThread::shouldStandby_l() 4728d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent{ 4729d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent bool trackPaused = false; 4730b369cafd67beb63dd0278dba543f519956208a7fEric Laurent bool trackStopped = false; 4731d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent 4732d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack 4733d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent // after a timeout and we will enter standby then. 4734d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent if (mTracks.size() > 0) { 4735d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent trackPaused = mTracks[mTracks.size() - 1]->isPaused(); 4736b369cafd67beb63dd0278dba543f519956208a7fEric Laurent trackStopped = mTracks[mTracks.size() - 1]->isStopped() || 4737b369cafd67beb63dd0278dba543f519956208a7fEric Laurent mTracks[mTracks.size() - 1]->mState == TrackBase::IDLE; 4738d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4739d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent 47405cff403679fc44c8293de81aed31c459c6129243Eric Laurent return !mStandby && !(trackPaused || (mHwPaused && !trackStopped)); 4741d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent} 4742d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent 474381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// getTrackName_l() must be called with ThreadBase::mLock held 47440f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenint AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask __unused, 4745e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung audio_format_t format __unused, int sessionId __unused) 474681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 474781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 474881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 474981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 475081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// deleteTrackName_l() must be called with ThreadBase::mLock held 47510f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::DirectOutputThread::deleteTrackName_l(int name __unused) 475281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 475381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 475481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 47551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// checkForNewParameter_l() must be called with ThreadBase::mLock held 47561035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentbool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair, 47571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status_t& status) 475881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 475981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool reconfig = false; 476081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 47611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = NO_ERROR; 47621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 47631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent AudioParameter param = AudioParameter(keyValuePair); 47641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent int value; 47651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { 47661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // forward device change to effects that have requested to be 47671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // aware of attached audio device. 47681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (value != AUDIO_DEVICE_NONE) { 47691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mOutDevice = value; 47701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 47711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mEffectChains[i]->setDevice_l(mOutDevice); 4772c125f38cd0ae35409a01b98a99e483550daa1313Glenn Kasten } 4773c125f38cd0ae35409a01b98a99e483550daa1313Glenn Kasten } 47741035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 47751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { 47761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // do not accept frame count changes if tracks are open as the track buffer 47771035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // size depends on frame count and correct behavior would not be garantied 47781035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // if frame count is changed after track creation 47791035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (!mTracks.isEmpty()) { 47801035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = INVALID_OPERATION; 47811035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 47821035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 478381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 47841035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 47851035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == NO_ERROR) { 47861035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = mOutput->stream->common.set_parameters(&mOutput->stream->common, 47871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent keyValuePair.string()); 47881035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (!mStandby && status == INVALID_OPERATION) { 4789062e67a26e0553dd142be622821f493df541f0c6Phil Burk mOutput->standby(); 47901035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mStandby = true; 47911035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mBytesWritten = 0; 479281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = mOutput->stream->common.set_parameters(&mOutput->stream->common, 47931035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent keyValuePair.string()); 47941035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 47951035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == NO_ERROR && reconfig) { 47961035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent readOutputParameters_l(); 479773e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); 479881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 479981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 48001035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 480181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return reconfig; 480281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 480381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 480481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const 480581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 480681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t time; 480781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (audio_is_linear_pcm(mFormat)) { 480881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent time = PlaybackThread::activeSleepTimeUs(); 480981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 481081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent time = 10000; 481181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 481281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return time; 481381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 481481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 481581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const 481681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 481781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t time; 481881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (audio_is_linear_pcm(mFormat)) { 481981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; 482081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 482181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent time = 10000; 482281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 482381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return time; 482481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 482581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 482681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const 482781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 482881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t time; 482981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (audio_is_linear_pcm(mFormat)) { 483081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); 483181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 483281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent time = 10000; 483381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 483481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return time; 483581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 483681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 483781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DirectOutputThread::cacheParameters_l() 483881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 483981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread::cacheParameters_l(); 484081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 484181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // use shorter standby delay as on normal output to release 484281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // hardware resources as soon as possible 4843b369cafd67beb63dd0278dba543f519956208a7fEric Laurent // no delay on outputs with HW A/V sync 4844b369cafd67beb63dd0278dba543f519956208a7fEric Laurent if (usesHwAvSync()) { 4845ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mStandbyDelayNs = 0; 48465cff403679fc44c8293de81aed31c459c6129243Eric Laurent } else if ((mType == OFFLOAD) && !audio_is_linear_pcm(mFormat)) { 4847ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mStandbyDelayNs = kOffloadStandbyDelayNs; 48485cff403679fc44c8293de81aed31c459c6129243Eric Laurent } else { 4849ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mStandbyDelayNs = microseconds(mActiveSleepTimeUs*2); 4850972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent } 485181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 485281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4853e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurentvoid AudioFlinger::DirectOutputThread::flushHw_l() 4854e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent{ 4855062e67a26e0553dd142be622821f493df541f0c6Phil Burk mOutput->flush(); 4856d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent mHwPaused = false; 485743b4dcc660e6da96285e4672ae371070ab845401Phil Burk mFlushPending = false; 4858e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent} 4859e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent 486081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 486181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4862bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::AsyncCallbackThread::AsyncCallbackThread( 48634de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent const wp<AudioFlinger::PlaybackThread>& playbackThread) 4864bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent : Thread(false /*canCallJava*/), 48654de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent mPlaybackThread(playbackThread), 48663b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence(0), 48673b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence(0) 4868bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4869bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4870bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4871bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::AsyncCallbackThread::~AsyncCallbackThread() 4872bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4873bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4874bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4875bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::AsyncCallbackThread::onFirstRef() 4876bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4877bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO); 4878bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4879bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4880bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::AsyncCallbackThread::threadLoop() 4881bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4882bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent while (!exitPending()) { 48833b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent uint32_t writeAckSequence; 48843b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent uint32_t drainSequence; 4885bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4886bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent { 4887bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 488824a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George while (!((mWriteAckSequence & 1) || 488924a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George (mDrainSequence & 1) || 489024a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George exitPending())) { 489124a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George mWaitWorkCV.wait(mLock); 489224a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George } 489324a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George 4894bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (exitPending()) { 4895bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent break; 4896bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 48973b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d", 48983b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence, mDrainSequence); 48993b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent writeAckSequence = mWriteAckSequence; 49003b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence &= ~1; 49013b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent drainSequence = mDrainSequence; 49023b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence &= ~1; 4903bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4904bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent { 49054de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote(); 49064de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent if (playbackThread != 0) { 49073b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if (writeAckSequence & 1) { 49084de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent playbackThread->resetWriteBlocked(writeAckSequence >> 1); 4909bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 49103b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if (drainSequence & 1) { 49114de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent playbackThread->resetDraining(drainSequence >> 1); 4912bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4913bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4914bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4915bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4916bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return false; 4917bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4918bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4919bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::AsyncCallbackThread::exit() 4920bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4921bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("AsyncCallbackThread::exit"); 4922bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 4923bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent requestExit(); 4924bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mWaitWorkCV.broadcast(); 4925bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4926bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 49273b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence) 4928bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4929bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 49303b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // bit 0 is cleared 49313b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence = sequence << 1; 49323b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent} 49333b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent 49343b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::AsyncCallbackThread::resetWriteBlocked() 49353b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent{ 49363b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent Mutex::Autolock _l(mLock); 49373b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // ignore unexpected callbacks 49383b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if (mWriteAckSequence & 2) { 49393b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence |= 1; 4940bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mWaitWorkCV.signal(); 4941bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4942bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4943bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 49443b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence) 49453b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent{ 49463b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent Mutex::Autolock _l(mLock); 49473b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // bit 0 is cleared 49483b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence = sequence << 1; 49493b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent} 49503b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent 49513b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::AsyncCallbackThread::resetDraining() 4952bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4953bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 49543b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // ignore unexpected callbacks 49553b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if (mDrainSequence & 2) { 49563b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence |= 1; 4957bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mWaitWorkCV.signal(); 4958bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4959bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4960bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4961bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4962bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// ---------------------------------------------------------------------------- 4963bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger, 496472e3f39146fce4686bd96f11057c051bea376dfbEric Laurent AudioStreamOut* output, audio_io_handle_t id, uint32_t device, bool systemReady) 496572e3f39146fce4686bd96f11057c051bea376dfbEric Laurent : DirectOutputThread(audioFlinger, output, id, device, OFFLOAD, systemReady), 4966d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent mPausedBytesRemaining(0) 4967bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4968fd4779740ec3e9e865d5514464df26d015354388Eric Laurent //FIXME: mStandby should be set to true by ThreadBase constructor 4969fd4779740ec3e9e865d5514464df26d015354388Eric Laurent mStandby = true; 4970bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4971bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4972bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::OffloadThread::threadLoop_exit() 4973bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4974bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mFlushPending || mHwPaused) { 4975bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // If a flush is pending or track was paused, just discard buffered data 4976bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent flushHw_l(); 4977bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else { 4978bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mMixerStatus = MIXER_DRAIN_ALL; 4979bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent threadLoop_drain(); 4980bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 498156604aa3a56dc8e15532597a0a74b3c7b165e006Uday Gupta if (mUseAsyncWrite) { 498256604aa3a56dc8e15532597a0a74b3c7b165e006Uday Gupta ALOG_ASSERT(mCallbackThread != 0); 498356604aa3a56dc8e15532597a0a74b3c7b165e006Uday Gupta mCallbackThread->exit(); 498456604aa3a56dc8e15532597a0a74b3c7b165e006Uday Gupta } 4985bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent PlaybackThread::threadLoop_exit(); 4986bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4987bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4988bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l( 4989bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Vector< sp<Track> > *tracksToRemove 4990bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent) 4991bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4992bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent size_t count = mActiveTracks.size(); 4993bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4994bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mixer_state mixerStatus = MIXER_IDLE; 4995972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent bool doHwPause = false; 4996972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent bool doHwResume = false; 4997972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent 4998ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent ALOGV("OffloadThread::prepareTracks_l active tracks %d", count); 4999ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent 5000bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // find out which tracks need to be processed 5001bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent for (size_t i = 0; i < count; i++) { 5002bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent sp<Track> t = mActiveTracks[i].promote(); 5003bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // The track died recently 5004bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (t == 0) { 5005bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent continue; 5006bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 5007bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Track* const track = t.get(); 5008bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent audio_track_cblk_t* cblk = track->cblk(); 5009fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // Only consider last track started for volume and mixer state control. 5010fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // In theory an older track could underrun and restart after the new one starts 5011fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // but as we only care about the transition phase between two tracks on a 5012fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // direct output, it is not a problem to ignore the underrun case. 5013fd4779740ec3e9e865d5514464df26d015354388Eric Laurent sp<Track> l = mLatestActiveTrack.promote(); 5014fd4779740ec3e9e865d5514464df26d015354388Eric Laurent bool last = l.get() == track; 5015fd4779740ec3e9e865d5514464df26d015354388Eric Laurent 50167844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George if (track->isInvalid()) { 50177844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George ALOGW("An invalidated track shouldn't be in active list"); 50187844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George tracksToRemove->add(track); 50197844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George continue; 50207844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George } 50217844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George 50227844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George if (track->mState == TrackBase::IDLE) { 50237844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George ALOGW("An idle track shouldn't be in active list"); 50247844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George continue; 50257844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George } 50267844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George 5027bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (track->isPausing()) { 5028bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->setPaused(); 5029bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (last) { 50305cff403679fc44c8293de81aed31c459c6129243Eric Laurent if (mHwSupportsPause && !mHwPaused) { 5031972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent doHwPause = true; 5032bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mHwPaused = true; 5033bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 5034bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // If we were part way through writing the mixbuffer to 5035bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // the HAL we must save this until we resume 5036bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // BUG - this will be wrong if a different track is made active, 5037bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // in that case we want to discard the pending data in the 5038bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // mixbuffer and tell the client to present it again when the 5039bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // track is resumed 5040bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mPausedWriteLength = mCurrentWriteLength; 5041bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mPausedBytesRemaining = mBytesRemaining; 5042bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining = 0; // stop writing 5043bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 5044bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent tracksToRemove->add(track); 50457844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George } else if (track->isFlushPending()) { 50467844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George track->flushAck(); 50477844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George if (last) { 50487844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George mFlushPending = true; 50497844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George } 50502d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George } else if (track->isResumePending()){ 50512d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George track->resumeAck(); 50522d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George if (last) { 50532d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George if (mPausedBytesRemaining) { 50542d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George // Need to continue write that was interrupted 50552d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George mCurrentWriteLength = mPausedWriteLength; 50562d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George mBytesRemaining = mPausedBytesRemaining; 50572d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George mPausedBytesRemaining = 0; 50582d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George } 50592d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George if (mHwPaused) { 50602d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George doHwResume = true; 50612d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George mHwPaused = false; 50622d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George // threadLoop_mix() will handle the case that we need to 50632d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George // resume an interrupted write 50642d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George } 50652d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George // enable write to audio HAL 5066ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mSleepTimeUs = 0; 50672d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George 50682d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George // Do not handle new data in this iteration even if track->framesReady() 50692d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George mixerStatus = MIXER_TRACKS_ENABLED; 50702d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George } 50712d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George } else if (track->framesReady() && track->isReady() && 50723b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) { 5073f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten ALOGVV("OffloadThread: track %d s=%08x [OK]", track->name(), cblk->mServer); 5074bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (track->mFillingUpStatus == Track::FS_FILLED) { 5075bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mFillingUpStatus = Track::FS_ACTIVE; 50761abbdb4429479975718421c4fef3f79ce7c820e3Eric Laurent // make sure processVolume_l() will apply new volume even if 0 50771abbdb4429479975718421c4fef3f79ce7c820e3Eric Laurent mLeftVolFloat = mRightVolFloat = -1.0; 5078bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 5079bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 5080bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (last) { 5081d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent sp<Track> previousTrack = mPreviousTrack.promote(); 5082d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent if (previousTrack != 0) { 5083d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent if (track != previousTrack.get()) { 50849da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // Flush any data still being written from last track 50859da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent mBytesRemaining = 0; 50869da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent if (mPausedBytesRemaining) { 50879da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // Last track was paused so we also need to flush saved 50889da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // mixbuffer state and invalidate track so that it will 50899da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // re-submit that unwritten data when it is next resumed 50909da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent mPausedBytesRemaining = 0; 50919da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // Invalidate is a bit drastic - would be more efficient 50929da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // to have a flag to tell client that some of the 50939da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // previously written data was lost 5094d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent previousTrack->invalidate(); 50959da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent } 50969da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // flush data already sent to the DSP if changing audio session as audio 50979da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // comes from a different source. Also invalidate previous track to force a 50989da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // seek when resuming. 5099d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent if (previousTrack->sessionId() != track->sessionId()) { 5100d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent previousTrack->invalidate(); 51019da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent } 51029da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent } 51039da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent } 51049da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent mPreviousTrack = track; 5105bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // reset retry count 5106bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mRetryCount = kMaxTrackRetriesOffload; 5107bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mActiveTrack = t; 5108bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mixerStatus = MIXER_TRACKS_READY; 5109bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 5110bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else { 5111f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten ALOGVV("OffloadThread: track %d s=%08x [NOT READY]", track->name(), cblk->mServer); 5112bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (track->isStopping_1()) { 5113bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Hardware buffer can hold a large amount of audio so we must 5114bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // wait for all current track's data to drain before we say 5115bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // that the track is stopped. 5116bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mBytesRemaining == 0) { 5117bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Only start draining when all data in mixbuffer 5118bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // has been written 5119bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2"); 5120bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mState = TrackBase::STOPPING_2; // so presentation completes after drain 51216a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent // do not drain if no data was ever sent to HAL (mStandby == true) 51226a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent if (last && !mStandby) { 51231b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent // do not modify drain sequence if we are already draining. This happens 51241b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent // when resuming from pause after drain. 51251b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent if ((mDrainSequence & 1) == 0) { 5126ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mSleepTimeUs = 0; 5127ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mStandbyTimeNs = systemTime() + mStandbyDelayNs; 51281b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent mixerStatus = MIXER_DRAIN_TRACK; 51291b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent mDrainSequence += 2; 51301b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent } 5131bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mHwPaused) { 5132bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // It is possible to move from PAUSED to STOPPING_1 without 5133bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // a resume so we must ensure hardware is running 51341b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent doHwResume = true; 5135bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mHwPaused = false; 5136bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 5137bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 5138bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 5139bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if (track->isStopping_2()) { 51406a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent // Drain has completed or we are in standby, signal presentation complete 51416a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent if (!(mDrainSequence & 1) || !last || mStandby) { 5142bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mState = TrackBase::STOPPED; 5143bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent size_t audioHALFrames = 5144bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; 5145bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent size_t framesWritten = 5146062e67a26e0553dd142be622821f493df541f0c6Phil Burk mBytesWritten / mOutput->getFrameSize(); 5147bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->presentationComplete(framesWritten, audioHALFrames); 5148bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->reset(); 5149bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent tracksToRemove->add(track); 5150bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 5151bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else { 5152bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // No buffers for this track. Give it a few chances to 5153bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // fill a buffer, then remove it from active list. 5154bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (--(track->mRetryCount) <= 0) { 5155bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("OffloadThread: BUFFER TIMEOUT: remove(%d) from active list", 5156bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->name()); 5157bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent tracksToRemove->add(track); 5158a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent // indicate to client process that the track was disabled because of underrun; 5159a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent // it will then automatically call start() when data is available 5160a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent android_atomic_or(CBLK_DISABLED, &cblk->mFlags); 5161bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if (last){ 5162bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mixerStatus = MIXER_TRACKS_ENABLED; 5163bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 5164bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 5165bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 5166bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // compute volume for this track 5167bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent processVolume_l(track, last); 5168bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 51696bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent 5170ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent // make sure the pause/flush/resume sequence is executed in the right order. 5171ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent // If a flush is pending and a track is active but the HW is not paused, force a HW pause 5172ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent // before flush and then resume HW. This can happen in case of pause/flush/resume 5173ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent // if resume is received before pause is executed. 5174fd4779740ec3e9e865d5514464df26d015354388Eric Laurent if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { 5175972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent mOutput->stream->pause(mOutput->stream); 5176972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent } 51776bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent if (mFlushPending) { 51786bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent flushHw_l(); 51796bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent } 5180fd4779740ec3e9e865d5514464df26d015354388Eric Laurent if (!mStandby && doHwResume) { 5181972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent mOutput->stream->resume(mOutput->stream); 5182972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent } 51836bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent 5184bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // remove all the tracks that need to be... 5185bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent removeTracks_l(*tracksToRemove); 5186bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 5187bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return mixerStatus; 5188bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 5189bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 5190bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// must be called with thread mutex locked 5191bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::OffloadThread::waitingAsyncCallback_l() 5192bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 51933b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d", 51943b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence, mDrainSequence); 51953b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) { 5196bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return true; 5197bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 5198bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return false; 5199bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 5200bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 5201bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::OffloadThread::waitingAsyncCallback() 5202bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 5203bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 5204bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return waitingAsyncCallback_l(); 5205bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 5206bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 5207bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::OffloadThread::flushHw_l() 5208bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 5209e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent DirectOutputThread::flushHw_l(); 5210bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Flush anything still waiting in the mixbuffer 5211bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mCurrentWriteLength = 0; 5212bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining = 0; 5213bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mPausedWriteLength = 0; 5214bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mPausedBytesRemaining = 0; 52150f02f265123b7ef2fd6ac09ff70cde26eb5559adHaynes Mathew George 5216bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mUseAsyncWrite) { 52173b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // discard any pending drain or write ack by incrementing sequence 52183b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence = (mWriteAckSequence + 2) & ~1; 52193b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence = (mDrainSequence + 2) & ~1; 5220bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 52213b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setWriteBlocked(mWriteAckSequence); 52223b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setDraining(mDrainSequence); 5223bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 5224bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 52254c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George 5226bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// ---------------------------------------------------------------------------- 5227bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 522881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, 522972e3f39146fce4686bd96f11057c051bea376dfbEric Laurent AudioFlinger::MixerThread* mainThread, audio_io_handle_t id, bool systemReady) 523081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->outDevice(), 523172e3f39146fce4686bd96f11057c051bea376dfbEric Laurent systemReady, DUPLICATING), 523281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitTimeMs(UINT_MAX) 523381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 523481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent addOutputTrack(mainThread); 523581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 523681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 523781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::DuplicatingThread::~DuplicatingThread() 523881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 523981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mOutputTracks.size(); i++) { 524081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutputTracks[i]->destroy(); 524181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 524281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 524381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 524481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::threadLoop_mix() 524581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 524681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mix buffers... 524781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (outputsReady(outputTracks)) { 524881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->process(AudioBufferProvider::kInvalidPTS); 524981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 525002b5708776ba2a9b4ff8c09008483aef7dbe38c7Eric Laurent if (mMixerBufferValid) { 525102b5708776ba2a9b4ff8c09008483aef7dbe38c7Eric Laurent memset(mMixerBuffer, 0, mMixerBufferSize); 525202b5708776ba2a9b4ff8c09008483aef7dbe38c7Eric Laurent } else { 525302b5708776ba2a9b4ff8c09008483aef7dbe38c7Eric Laurent memset(mSinkBuffer, 0, mSinkBufferSize); 525402b5708776ba2a9b4ff8c09008483aef7dbe38c7Eric Laurent } 525581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 5256ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mSleepTimeUs = 0; 525781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent writeFrames = mNormalFrameCount; 525825c2dac12114699e90deb1c579cadebce7b91a97Andy Hung mCurrentWriteLength = mSinkBufferSize; 5259ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mStandbyTimeNs = systemTime() + mStandbyDelayNs; 526081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 526181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 526281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::threadLoop_sleepTime() 526381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 5264ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent if (mSleepTimeUs == 0) { 526581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mMixerStatus == MIXER_TRACKS_ENABLED) { 5266ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mSleepTimeUs = mActiveSleepTimeUs; 526781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 5268ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mSleepTimeUs = mIdleSleepTimeUs; 526981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 527081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (mBytesWritten != 0) { 527181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mMixerStatus == MIXER_TRACKS_ENABLED) { 527281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent writeFrames = mNormalFrameCount; 527325c2dac12114699e90deb1c579cadebce7b91a97Andy Hung memset(mSinkBuffer, 0, mSinkBufferSize); 527481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 527581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // flush remaining overflow buffers in output tracks 527681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent writeFrames = 0; 527781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 5278ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent mSleepTimeUs = 0; 527981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 528081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 528181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5282bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentssize_t AudioFlinger::DuplicatingThread::threadLoop_write() 528381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 528481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < outputTracks.size(); i++) { 5285c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung outputTracks[i]->write(mSinkBuffer, writeFrames); 528681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 52872c3740f01acca69c3e0bcc5e01bb0edc51b6777fEric Laurent mStandby = false; 528825c2dac12114699e90deb1c579cadebce7b91a97Andy Hung return (ssize_t)mSinkBufferSize; 528981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 529081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 529181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::threadLoop_standby() 529281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 529381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // DuplicatingThread implements standby by stopping all tracks 529481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < outputTracks.size(); i++) { 529581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent outputTracks[i]->stop(); 529681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 529781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 529881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 529981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::saveOutputTracks() 530081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 530181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent outputTracks = mOutputTracks; 530281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 530381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 530481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::clearOutputTracks() 530581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 530681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent outputTracks.clear(); 530781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 530881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 530981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) 531081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 531181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 5312c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass. 5313c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung // Adjust for thread->sampleRate() to determine minimum buffer frame count. 5314c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung // Then triple buffer because Threads do not run synchronously and may not be clock locked. 5315c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung const size_t frameCount = 5316c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung 3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate()); 5317c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung // TODO: Consider asynchronous sample rate conversion to handle clock disparity 5318c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung // from different OutputTracks and their associated MixerThreads (e.g. one may 5319c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung // nearly empty and the other may be dropping data). 5320c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung 5321c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung sp<OutputTrack> outputTrack = new OutputTrack(thread, 532281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent this, 532381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSampleRate, 5324c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung mFormat, 532581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mChannelMask, 5326462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen frameCount, 5327462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen IPCThreadState::self()->getCallingUid()); 532881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (outputTrack->cblk() != NULL) { 5329223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f); 533081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutputTracks.add(outputTrack); 5331c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread); 533281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent updateWaitTime_l(); 533381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 533481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 533581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 533681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) 533781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 533881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 533981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mOutputTracks.size(); i++) { 534081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutputTracks[i]->thread() == thread) { 534181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutputTracks[i]->destroy(); 534281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutputTracks.removeAt(i); 534381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent updateWaitTime_l(); 5344f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent if (thread->getOutput() == mOutput) { 5345f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent mOutput = NULL; 5346f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent } 534781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 534881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 534981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 5350f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent ALOGV("removeOutputTrack(): unknown thread: %p", thread); 535181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 535281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 535381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// caller must hold mLock 535481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::updateWaitTime_l() 535581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 535681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitTimeMs = UINT_MAX; 535781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mOutputTracks.size(); i++) { 535881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); 535981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (strong != 0) { 536081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); 536181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (waitTimeMs < mWaitTimeMs) { 536281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitTimeMs = waitTimeMs; 536381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 536481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 536581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 536681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 536781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 536881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 536981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::DuplicatingThread::outputsReady( 537081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const SortedVector< sp<OutputTrack> > &outputTracks) 537181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 537281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < outputTracks.size(); i++) { 537381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<ThreadBase> thread = outputTracks[i]->thread().promote(); 537481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread == 0) { 537581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", 537681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent outputTracks[i].get()); 537781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 537881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 537981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); 538081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // see note at standby() declaration 538181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (playbackThread->standby() && !playbackThread->isSuspended()) { 538281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), 538381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread.get()); 538481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 538581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 538681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 538781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return true; 538881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 538981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 539081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const 539181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 539281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return (mWaitTimeMs * 1000) / 2; 539381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 539481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 539581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::cacheParameters_l() 539681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 539781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first 539881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent updateWaitTime_l(); 539981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 540081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent MixerThread::cacheParameters_l(); 540181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 540281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 540381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 540481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Record 540581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 540681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 540781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, 540881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamIn *input, 540981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t id, 5410d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent audio_devices_t outDevice, 541172e3f39146fce4686bd96f11057c051bea376dfbEric Laurent audio_devices_t inDevice, 541272e3f39146fce4686bd96f11057c051bea376dfbEric Laurent bool systemReady 541346909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 541446909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten , const sp<NBAIO_Sink>& teeSink 541546909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 541646909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten ) : 541772e3f39146fce4686bd96f11057c051bea376dfbEric Laurent ThreadBase(audioFlinger, id, outDevice, inDevice, RECORD, systemReady), 54186dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten mInput(input), mActiveTracksGen(0), mRsmpInBuffer(NULL), 5419deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten // mRsmpInFrames and mRsmpInFramesP2 are set by readInputParameters_l() 54204cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten mRsmpInRear(0) 542146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 542246909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten , mTeeSink(teeSink) 542346909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 5424b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize, 5425b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten "RecordThreadRO", MemoryHeapBase::READ_ONLY)) 54266dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // mFastCapture below 54276dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten , mFastCaptureFutex(0) 54286dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // mInputSource 54296dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // mPipeSink 54306dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // mPipeSource 54316dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten , mPipeFramesP2(0) 54326dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // mPipeMemory 54336dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // mFastCaptureNBLogWriter 54346e6704c06d61bc356e30c164081e5bcffb37920cGlenn Kasten , mFastTrackAvail(false) 543581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 5436d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id); 5437d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); 543881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5439deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten readInputParameters_l(); 54406dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 54416dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // create an NBAIO source for the HAL input stream, and negotiate 54426dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mInputSource = new AudioStreamInSource(input->stream); 54436dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten size_t numCounterOffers = 0; 54446dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; 54456dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ssize_t index = mInputSource->negotiate(offers, 1, NULL, numCounterOffers); 54466dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOG_ASSERT(index == 0); 54476dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 54486dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // initialize fast capture depending on configuration 54496dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten bool initFastCapture; 54506dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten switch (kUseFastCapture) { 54516dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten case FastCapture_Never: 54526dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten initFastCapture = false; 54536dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten break; 54546dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten case FastCapture_Always: 54556dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten initFastCapture = true; 54566dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten break; 54576dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten case FastCapture_Static: 54586dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten uint32_t primaryOutputSampleRate; 54596dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten { 54606dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten AutoMutex _l(audioFlinger->mHardwareLock); 54616dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten primaryOutputSampleRate = audioFlinger->mPrimaryOutputSampleRate; 54626dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 54636dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten initFastCapture = 54646dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // either capture sample rate is same as (a reasonable) primary output sample rate 5465db4c031f518ae5806af73756273ff32cd8d0e4f8Andy Hung ((isMusicRate(primaryOutputSampleRate) && 54666dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (mSampleRate == primaryOutputSampleRate)) || 54676dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // or primary output sample rate is unknown, and capture sample rate is reasonable 54686dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ((primaryOutputSampleRate == 0) && 5469db4c031f518ae5806af73756273ff32cd8d0e4f8Andy Hung isMusicRate(mSampleRate))) && 54709f81de3452dfb2385bd57dc05456a045174a1ab1Glenn Kasten // and the buffer size is < 12 ms 54719f81de3452dfb2385bd57dc05456a045174a1ab1Glenn Kasten (mFrameCount * 1000) / mSampleRate < 12; 54726dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten break; 54736dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // case FastCapture_Dynamic: 54746dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 54756dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 54766dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (initFastCapture) { 5477d198b85a163330b03e7507c9e8bfeb5f4d958a6cGlenn Kasten // create a Pipe for FastCapture to write to, and for us and fast tracks to read from 54786dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten NBAIO_Format format = mInputSource->format(); 547949d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten size_t pipeFramesP2 = roundup(mSampleRate / 25); // double-buffering of 20 ms each 54806dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten size_t pipeSize = pipeFramesP2 * Format_frameSize(format); 54816dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten void *pipeBuffer; 54826dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten const sp<MemoryDealer> roHeap(readOnlyHeap()); 54836dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sp<IMemory> pipeMemory; 54846dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if ((roHeap == 0) || 54856dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (pipeMemory = roHeap->allocate(pipeSize)) == 0 || 54866dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (pipeBuffer = pipeMemory->pointer()) == NULL) { 54876dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOGE("not enough memory for pipe buffer size=%zu", pipeSize); 54886dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten goto failed; 54896dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 54906dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // pipe will be shared directly with fast clients, so clear to avoid leaking old information 54916dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten memset(pipeBuffer, 0, pipeSize); 54926dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer); 54936dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten const NBAIO_Format offers[1] = {format}; 54946dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten size_t numCounterOffers = 0; 54956dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); 54966dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOG_ASSERT(index == 0); 54976dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mPipeSink = pipe; 54986dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten PipeReader *pipeReader = new PipeReader(*pipe); 54996dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten numCounterOffers = 0; 55006dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); 55016dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOG_ASSERT(index == 0); 55026dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mPipeSource = pipeReader; 55036dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mPipeFramesP2 = pipeFramesP2; 55046dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mPipeMemory = pipeMemory; 55056dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 55066dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // create fast capture 55076dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastCapture = new FastCapture(); 55086dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureStateQueue *sq = mFastCapture->sq(); 55096dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef STATE_QUEUE_DUMP 55106dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // FIXME 55116dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif 55126dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureState *state = sq->begin(); 55136dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mCblk = NULL; 55146dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mInputSource = mInputSource.get(); 55156dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mInputSourceGen++; 55166dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mPipeSink = pipe; 55176dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mPipeSinkGen++; 55186dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mFrameCount = mFrameCount; 55196dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mCommand = FastCaptureState::COLD_IDLE; 55206dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // already done in constructor initialization list 55216dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten //mFastCaptureFutex = 0; 55226dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mColdFutexAddr = &mFastCaptureFutex; 55236dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mColdGen++; 55246dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mDumpState = &mFastCaptureDumpState; 55256dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef TEE_SINK 55266dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // FIXME 55276dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif 55286dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture"); 55296dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mNBLogWriter = mFastCaptureNBLogWriter.get(); 55306dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sq->end(); 55316dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); 55326dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 55336dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // start the fast capture 55346dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO); 55356dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten pid_t tid = mFastCapture->getTid(); 553672e3f39146fce4686bd96f11057c051bea376dfbEric Laurent sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer); 55376dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef AUDIO_WATCHDOG 55386dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // FIXME 55396dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif 55406dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 55416e6704c06d61bc356e30c164081e5bcffb37920cGlenn Kasten mFastTrackAvail = true; 55426dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 55436dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kastenfailed: ; 55446dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 55456dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // FIXME mNormalSource 554681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 554781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 554881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::RecordThread::~RecordThread() 554981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 55506dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (mFastCapture != 0) { 55516dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureStateQueue *sq = mFastCapture->sq(); 55526dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureState *state = sq->begin(); 55536dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (state->mCommand == FastCaptureState::COLD_IDLE) { 55546dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten int32_t old = android_atomic_inc(&mFastCaptureFutex); 55556dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (old == -1) { 55566dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); 55576dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 55586dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 55596dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mCommand = FastCaptureState::EXIT; 55606dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sq->end(); 55616dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); 55626dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastCapture->join(); 55636dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastCapture.clear(); 55646dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 55656dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter); 5566481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten mAudioFlinger->unregisterWriter(mNBLogWriter); 55675744661e85981f8a9456bf470e2761235fc026daAndy Hung free(mRsmpInBuffer); 556881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 556981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 557081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::onFirstRef() 557181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 5572d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten run(mThreadName, PRIORITY_URGENT_AUDIO); 557381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 557481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 557581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::RecordThread::threadLoop() 557681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 557781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent nsecs_t lastWarning = 0; 557881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 557981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent inputStandBy(); 558081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5581f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kastenreacquire_wakelock: 5582f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten sp<RecordTrack> activeTrack; 55832b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten int activeTracksGen; 5584f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten { 5585f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten Mutex::Autolock _l(mLock); 55862b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten size_t size = mActiveTracks.size(); 55872b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten activeTracksGen = mActiveTracksGen; 55882b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (size > 0) { 55892b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten // FIXME an arbitrary choice 55902b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten activeTrack = mActiveTracks[0]; 55912b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten acquireWakeLock_l(activeTrack->uid()); 55922b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (size > 1) { 55932b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten SortedVector<int> tmp; 55942b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten for (size_t i = 0; i < size; i++) { 55952b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten tmp.add(mActiveTracks[i]->uid()); 55962b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten } 55972b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten updateWakeLockUids_l(tmp); 55982b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten } 55992b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten } else { 56002b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten acquireWakeLock_l(-1); 56012b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten } 5602f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten } 5603f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten 56046dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // used to request a deferred sleep, to be executed later while mutex is unlocked 56056dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten uint32_t sleepUs = 0; 56066dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 56076dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // loop while there is work to do 56084ef0b463a56c19bad9197aa9f90d792090461429Glenn Kasten for (;;) { 5609c527a7c2b1bfd26e8f3086e1b653d56e521379d9Glenn Kasten Vector< sp<EffectChain> > effectChains; 56102cfbf88b89854f30b295e8ae26a031edb8d712f8Glenn Kasten 56115edadd46c76c5ff1c3edabf2ea943c2278e82e1cGlenn Kasten // sleep with mutex unlocked 56126dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (sleepUs > 0) { 5613e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten ATRACE_BEGIN("sleep"); 56146dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten usleep(sleepUs); 5615e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten ATRACE_END(); 56166dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten sleepUs = 0; 56175edadd46c76c5ff1c3edabf2ea943c2278e82e1cGlenn Kasten } 56185edadd46c76c5ff1c3edabf2ea943c2278e82e1cGlenn Kasten 56196dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // activeTracks accumulates a copy of a subset of mActiveTracks 56206dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten Vector< sp<RecordTrack> > activeTracks; 56216dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 5622735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten // reference to the (first and only) active fast track 56236dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sp<RecordTrack> fastTrack; 56241035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 5625735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten // reference to a fast track which is about to be removed 5626735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten sp<RecordTrack> fastTrackToRemove; 5627735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten 562881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // scope for mLock 562981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 5630000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent 5631021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent processConfigEvents_l(); 5632f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten 5633000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent // check exitPending here because checkForNewParameters_l() and 5634000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent // checkForNewParameters_l() can temporarily release mLock 5635000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent if (exitPending()) { 5636000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent break; 5637000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent } 5638000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent 56392b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten // if no active track(s), then standby and release wakelock 56402b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten size_t size = mActiveTracks.size(); 56412b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (size == 0) { 564293e471f620454f7de73d190521568b1e25879767Glenn Kasten standbyIfNotAlreadyInStandby(); 56434ef0b463a56c19bad9197aa9f90d792090461429Glenn Kasten // exitPending() can't become true here 564481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock_l(); 564581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("RecordThread: loop stopping"); 564681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // go to sleep 564781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitWorkCV.wait(mLock); 564881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("RecordThread: loop starting"); 5649f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten goto reacquire_wakelock; 5650f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten } 5651f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten 56522b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (mActiveTracksGen != activeTracksGen) { 56532b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten activeTracksGen = mActiveTracksGen; 5654f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten SortedVector<int> tmp; 56552b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten for (size_t i = 0; i < size; i++) { 56562b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten tmp.add(mActiveTracks[i]->uid()); 56572b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten } 5658f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten updateWakeLockUids_l(tmp); 565981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 56609e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten 56616dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten bool doBroadcast = false; 56626dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten for (size_t i = 0; i < size; ) { 56639e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten 56646dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack = mActiveTracks[i]; 56656dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTrack->isTerminated()) { 5666735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten if (activeTrack->isFastTrack()) { 5667735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten ALOG_ASSERT(fastTrackToRemove == 0); 5668735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten fastTrackToRemove = activeTrack; 5669735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten } 56706dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten removeTrack_l(activeTrack); 56712b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracks.remove(activeTrack); 56722b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracksGen++; 56736dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size--; 56749e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten continue; 56759e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten } 56766dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 56776dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten TrackBase::track_state activeTrackState = activeTrack->mState; 56786dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten switch (activeTrackState) { 56796dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 56806dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case TrackBase::PAUSING: 56816dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten mActiveTracks.remove(activeTrack); 56826dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten mActiveTracksGen++; 56836dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten doBroadcast = true; 56846dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size--; 56856dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten continue; 56866dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 56876dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case TrackBase::STARTING_1: 56886dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten sleepUs = 10000; 56896dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten i++; 56906dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten continue; 56916dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 56926dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case TrackBase::STARTING_2: 56936dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten doBroadcast = true; 56946dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten mStandby = false; 56959e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten activeTrack->mState = TrackBase::ACTIVE; 56966dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten break; 56976dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 56986dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case TrackBase::ACTIVE: 56996dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten break; 57006dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 57016dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case TrackBase::IDLE: 57026dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten i++; 57036dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten continue; 57046dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 57056dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten default: 5706adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("Unexpected activeTrackState %d", activeTrackState); 57079e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten } 57082d94426cd3302cb1215c92c5f1c4b90c24ceb72bGlenn Kasten 57096dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTracks.add(activeTrack); 57106dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten i++; 57112d94426cd3302cb1215c92c5f1c4b90c24ceb72bGlenn Kasten 57126dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (activeTrack->isFastTrack()) { 57136dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOG_ASSERT(!mFastTrackAvail); 57146dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOG_ASSERT(fastTrack == 0); 57156dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten fastTrack = activeTrack; 57166dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 57176dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 57186dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (doBroadcast) { 57196dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten mStartStopCond.broadcast(); 57206dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 5721d9fc34fb0fcfcc739f868b116edf50c62af19d5eGlenn Kasten 57226dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // sleep if there are no active tracks to process 57236dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTracks.size() == 0) { 57246dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (sleepUs == 0) { 57256dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten sleepUs = kRecordThreadSleepUs; 57266dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 57276dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten continue; 572881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 57296dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten sleepUs = 0; 57309e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten 573181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lockEffectChains_l(effectChains); 573281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 573381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 57346dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0 57357165268ffa6c7b6b405b6afad82e2a346500e8eeGlenn Kasten 57366dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size_t size = effectChains.size(); 57376dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten for (size_t i = 0; i < size; i++) { 57381ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten // thread mutex is not locked, but effect chain is locked 57391ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten effectChains[i]->process_l(); 57401ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten } 574181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5742735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten // Push a new fast capture state if fast capture is not already running, or cblk change 57436dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (mFastCapture != 0) { 57446dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureStateQueue *sq = mFastCapture->sq(); 57456dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureState *state = sq->begin(); 5746735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten bool didModify = false; 5747735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED; 57486dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME && 57496dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) { 57506dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (state->mCommand == FastCaptureState::COLD_IDLE) { 57516dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten int32_t old = android_atomic_inc(&mFastCaptureFutex); 57526dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (old == -1) { 57536dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); 57546dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 57556dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 57566dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mCommand = FastCaptureState::READ_WRITE; 57576dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#if 0 // FIXME 57586dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? 5759fbdb2aceab7317aa44bc8f301a93eb49e17b2bceGlenn Kasten FastThreadDumpState::kSamplingNforLowRamDevice : 5760fbdb2aceab7317aa44bc8f301a93eb49e17b2bceGlenn Kasten FastThreadDumpState::kSamplingN); 57616dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif 5762735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten didModify = true; 5763735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten } 5764735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten audio_track_cblk_t *cblkOld = state->mCblk; 5765735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL; 5766735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten if (cblkNew != cblkOld) { 5767735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten state->mCblk = cblkNew; 5768735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten // block until acked if removing a fast track 5769735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten if (cblkOld != NULL) { 5770735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED; 5771735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten } 5772735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten didModify = true; 5773735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten } 5774735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten sq->end(didModify); 5775735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten if (didModify) { 5776735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten sq->push(block); 57776dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#if 0 57786dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (kUseFastCapture == FastCapture_Dynamic) { 57796dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mNormalSource = mPipeSource; 57806dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 57816dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif 57826dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 57836dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 57846dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 5785735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten // now run the fast track destructor with thread mutex unlocked 5786735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten fastTrackToRemove.clear(); 5787735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten 57886dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one. 57896dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // Only the client(s) that are too slow will overrun. But if even the fastest client is too 57906dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // slow, then this RecordThread will overrun by not calling HAL read often enough. 57916dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // If destination is non-contiguous, first read past the nominal end of buffer, then 57926dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // copy to the right place. Permitted because mRsmpInBuffer was over-allocated. 57936dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 57946dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1); 57956dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ssize_t framesRead; 57966dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 57976dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // If an NBAIO source is present, use it to read the normal capture's data 57986dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (mPipeSource != 0) { 57996dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten size_t framesToRead = mBufferSize / mFrameSize; 58005744661e85981f8a9456bf470e2761235fc026daAndy Hung framesRead = mPipeSource->read((uint8_t*)mRsmpInBuffer + rear * mFrameSize, 58016dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten framesToRead, AudioBufferProvider::kInvalidPTS); 58026dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (framesRead == 0) { 58036dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // since pipe is non-blocking, simulate blocking input 58046dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sleepUs = (framesToRead * 1000000LL) / mSampleRate; 58056dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 58066dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // otherwise use the HAL / AudioStreamIn directly 58076dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } else { 58086dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ssize_t bytesRead = mInput->stream->read(mInput->stream, 58095744661e85981f8a9456bf470e2761235fc026daAndy Hung (uint8_t*)mRsmpInBuffer + rear * mFrameSize, mBufferSize); 58106dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (bytesRead < 0) { 58116dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten framesRead = bytesRead; 58126dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } else { 58136dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten framesRead = bytesRead / mFrameSize; 58146dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 58156dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 58166dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 58176dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) { 58186dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOGE("read failed: framesRead=%d", framesRead); 58196dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // Force input into standby so that it tries to recover at next read attempt 58206dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten inputStandBy(); 58216dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten sleepUs = kRecordThreadSleepUs; 58226dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 58236dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (framesRead <= 0) { 58243d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten goto unlock; 58256dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 58266dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ALOG_ASSERT(framesRead > 0); 58276dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 58286dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (mTeeSink != 0) { 58295744661e85981f8a9456bf470e2761235fc026daAndy Hung (void) mTeeSink->write((uint8_t*)mRsmpInBuffer + rear * mFrameSize, framesRead); 58306dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 58316dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // If destination is non-contiguous, we now correct for reading past end of buffer. 58323d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten { 58333d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten size_t part1 = mRsmpInFramesP2 - rear; 58343d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten if ((size_t) framesRead > part1) { 58355744661e85981f8a9456bf470e2761235fc026daAndy Hung memcpy(mRsmpInBuffer, (uint8_t*)mRsmpInBuffer + mRsmpInFramesP2 * mFrameSize, 58363d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten (framesRead - part1) * mFrameSize); 58373d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten } 58386dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 58396dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten rear = mRsmpInRear += framesRead; 58406dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 58416dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size = activeTracks.size(); 58426dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // loop over each active track 58436dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten for (size_t i = 0; i < size; i++) { 58446dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack = activeTracks[i]; 58456dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 58466dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // skip fast tracks, as those are handled directly by FastCapture 58476dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (activeTrack->isFastTrack()) { 58486dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten continue; 58496dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 58506dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 585173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung // TODO: This code probably should be moved to RecordTrack. 585297a893eb34f8687485c88eaf15917974a203f20bAndy Hung // TODO: Update the activeTrack buffer converter in case of reconfigure. 585397a893eb34f8687485c88eaf15917974a203f20bAndy Hung 58546dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten enum { 58556dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten OVERRUN_UNKNOWN, 58566dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten OVERRUN_TRUE, 58576dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten OVERRUN_FALSE 58586dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } overrun = OVERRUN_UNKNOWN; 58596dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 58606dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // loop over getNextBuffer to handle circular sink 58616dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten for (;;) { 58626dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 58636dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mSink.frameCount = ~0; 58646dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten status_t status = activeTrack->getNextBuffer(&activeTrack->mSink); 58656dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size_t framesOut = activeTrack->mSink.frameCount; 58666dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0)); 58676dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 586873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung // check available frames and handle overrun conditions 586973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung // if the record track isn't draining fast enough. 587073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung bool hasOverrun; 58716dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size_t framesIn; 587273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung activeTrack->mResamplerBufferProvider->sync(&framesIn, &hasOverrun); 587373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung if (hasOverrun) { 58746dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten overrun = OVERRUN_TRUE; 58756dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 58764cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten if (framesOut == 0 || framesIn == 0) { 58774cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten break; 58784cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten } 58794cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten 58806770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung // Don't allow framesOut to be larger than what is possible with resampling 58816770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung // from framesIn. 58826770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung // This isn't strictly necessary but helps limit buffer resizing in 58836770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung // RecordBufferConverter. TODO: remove when no longer needed. 58846770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung framesOut = min(framesOut, 58856770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung destinationFramesPossible( 58866770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung framesIn, mSampleRate, activeTrack->mSampleRate)); 588797a893eb34f8687485c88eaf15917974a203f20bAndy Hung // process frames from the RecordThread buffer provider to the RecordTrack buffer 588897a893eb34f8687485c88eaf15917974a203f20bAndy Hung framesOut = activeTrack->mRecordBufferConverter->convert( 588997a893eb34f8687485c88eaf15917974a203f20bAndy Hung activeTrack->mSink.raw, activeTrack->mResamplerBufferProvider, framesOut); 58908594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten 58916dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) { 58926dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten overrun = OVERRUN_FALSE; 58931ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten } 589481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 58956dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTrack->mFramesToDrop == 0) { 58966dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (framesOut > 0) { 58976dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mSink.frameCount = framesOut; 58986dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->releaseBuffer(&activeTrack->mSink); 589981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 590081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 59016dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME could do a partial drop of framesOut 59026dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTrack->mFramesToDrop > 0) { 59036dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mFramesToDrop -= framesOut; 59046dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTrack->mFramesToDrop <= 0) { 590525f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten activeTrack->clearSyncStartEvent(); 59066dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 59076dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } else { 59086dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mFramesToDrop += framesOut; 59096dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 || 59106dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mSyncStartEvent->isCancelled()) { 59116dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ALOGW("Synced record %s, session %d, trigger session %d", 59126dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled", 59136dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->sessionId(), 59146dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten (activeTrack->mSyncStartEvent != 0) ? 59156dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mSyncStartEvent->triggerSession() : 0); 591625f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten activeTrack->clearSyncStartEvent(); 59176dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 591881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 591981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 59206dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 59216dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (framesOut == 0) { 59226dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten break; 59236dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 592481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 59256dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 59266dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten switch (overrun) { 59276dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case OVERRUN_TRUE: 59286dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // client isn't retrieving buffers fast enough 59296dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (!activeTrack->setOverflow()) { 59306dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten nsecs_t now = systemTime(); 59316dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME should lastWarning per track? 59326dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if ((now - lastWarning) > kWarningThrottleNs) { 59336dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ALOGW("RecordThread: buffer overflow"); 59346dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten lastWarning = now; 59356dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 593681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 59376dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten break; 59386dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case OVERRUN_FALSE: 59396dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->clearOverflow(); 59406dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten break; 59416dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case OVERRUN_UNKNOWN: 59426dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten break; 594381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 59446dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 594581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 59461ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten 59473d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kastenunlock: 594881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // enable changes in effect chain 594981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent unlockEffectChains(effectChains); 5950c527a7c2b1bfd26e8f3086e1b653d56e521379d9Glenn Kasten // effectChains doesn't need to be cleared, since it is cleared by destructor at scope end 595181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 595281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 595393e471f620454f7de73d190521568b1e25879767Glenn Kasten standbyIfNotAlreadyInStandby(); 595481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 595581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 595681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 59579a54bc27876acd5d8be5b1fc3dc46701fe76fbb3Eric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 59589a54bc27876acd5d8be5b1fc3dc46701fe76fbb3Eric Laurent sp<RecordTrack> track = mTracks[i]; 59599a54bc27876acd5d8be5b1fc3dc46701fe76fbb3Eric Laurent track->invalidate(); 59609a54bc27876acd5d8be5b1fc3dc46701fe76fbb3Eric Laurent } 59612b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracks.clear(); 59622b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracksGen++; 596381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStartStopCond.broadcast(); 596481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 596581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 596681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock(); 596781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 596881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("RecordThread %p exiting", this); 596981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 597081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 597181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 597293e471f620454f7de73d190521568b1e25879767Glenn Kastenvoid AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby() 597381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 597481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!mStandby) { 597581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent inputStandBy(); 597681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStandby = true; 597781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 597881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 597981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 598081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::inputStandBy() 598181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 59826dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // Idle the fast capture if it's currently running 59836dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (mFastCapture != 0) { 59846dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureStateQueue *sq = mFastCapture->sq(); 59856dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureState *state = sq->begin(); 59866dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (!(state->mCommand & FastCaptureState::IDLE)) { 59876dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mCommand = FastCaptureState::COLD_IDLE; 59886dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mColdFutexAddr = &mFastCaptureFutex; 59896dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mColdGen++; 59906dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastCaptureFutex = 0; 59916dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sq->end(); 59926dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now 59936dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED); 59946dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#if 0 59956dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (kUseFastCapture == FastCapture_Dynamic) { 59966dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // FIXME 59976dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 59986dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif 59996dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef AUDIO_WATCHDOG 60006dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // FIXME 60016dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif 60026dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } else { 60036dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sq->end(false /*didModify*/); 60046dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 60056dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 600681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mInput->stream->common.standby(&mInput->stream->common); 600781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 600881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 600905997e21af6c4517f375def6563af4b9ebe95f39Glenn Kasten// RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held 6010e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kastensp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( 601181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<AudioFlinger::Client>& client, 601281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t sampleRate, 601381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_format_t format, 601481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_channel_mask_t channelMask, 601574935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten size_t *pFrameCount, 601681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId, 60177df8c0b799d8f52d6386e03313286dbd7d5cdc7cGlenn Kasten size_t *notificationFrames, 6018462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen int uid, 6019ddb0ccf3fb6fe8da8c71a6deb30561b821f3c0a2Glenn Kasten IAudioFlinger::track_flags_t *flags, 602081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t tid, 602181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t *status) 602281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 602374935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten size_t frameCount = *pFrameCount; 602481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<RecordTrack> track; 602581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t lStatus; 602681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 602790e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten // client expresses a preference for FAST, but we get the final say 602890e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten if (*flags & IAudioFlinger::TRACK_FAST) { 602990e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten if ( 6030b7fbf7ecc6b034243ec64f79f3113675b5e3c941Glenn Kasten // we formerly checked for a callback handler (non-0 tid), 6031b7fbf7ecc6b034243ec64f79f3113675b5e3c941Glenn Kasten // but that is no longer required for TRANSFER_OBTAIN mode 6032b7fbf7ecc6b034243ec64f79f3113675b5e3c941Glenn Kasten // 60337410591dad836434c72ddee66680802708b70c10Glenn Kasten // frame count is not specified, or is exactly the pipe depth 60347410591dad836434c72ddee66680802708b70c10Glenn Kasten ((frameCount == 0) || (frameCount == mPipeFramesP2)) && 60353a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten // PCM data 60363a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten audio_is_linear_pcm(format) && 60376dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // native format 60386dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (format == mFormat) && 60396dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // native channel mask 60406dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (channelMask == mChannelMask) && 60416dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // native hardware sample rate 604290e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten (sampleRate == mSampleRate) && 60433a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten // record thread has an associated fast capture 60446dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten hasFastCapture() && 60456dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // there are sufficient fast track slots available 60466dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastTrackAvail 604790e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten ) { 60487410591dad836434c72ddee66680802708b70c10Glenn Kasten ALOGV("AUDIO_INPUT_FLAG_FAST accepted: frameCount=%u mFrameCount=%u", 604990e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten frameCount, mFrameCount); 605090e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten } else { 60517410591dad836434c72ddee66680802708b70c10Glenn Kasten ALOGV("AUDIO_INPUT_FLAG_FAST denied: frameCount=%u mFrameCount=%u mPipeFramesP2=%u " 60527410591dad836434c72ddee66680802708b70c10Glenn Kasten "format=%#x isLinear=%d channelMask=%#x sampleRate=%u mSampleRate=%u " 60536dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten "hasFastCapture=%d tid=%d mFastTrackAvail=%d", 60547410591dad836434c72ddee66680802708b70c10Glenn Kasten frameCount, mFrameCount, mPipeFramesP2, 60557410591dad836434c72ddee66680802708b70c10Glenn Kasten format, audio_is_linear_pcm(format), channelMask, sampleRate, mSampleRate, 60567410591dad836434c72ddee66680802708b70c10Glenn Kasten hasFastCapture(), tid, mFastTrackAvail); 605790e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten *flags &= ~IAudioFlinger::TRACK_FAST; 60587410591dad836434c72ddee66680802708b70c10Glenn Kasten } 60597410591dad836434c72ddee66680802708b70c10Glenn Kasten } 60607410591dad836434c72ddee66680802708b70c10Glenn Kasten 60617410591dad836434c72ddee66680802708b70c10Glenn Kasten // compute track buffer size in frames, and suggest the notification frame count 60627410591dad836434c72ddee66680802708b70c10Glenn Kasten if (*flags & IAudioFlinger::TRACK_FAST) { 60637410591dad836434c72ddee66680802708b70c10Glenn Kasten // fast track: frame count is exactly the pipe depth 60647410591dad836434c72ddee66680802708b70c10Glenn Kasten frameCount = mPipeFramesP2; 60657410591dad836434c72ddee66680802708b70c10Glenn Kasten // ignore requested notificationFrames, and always notify exactly once every HAL buffer 60667410591dad836434c72ddee66680802708b70c10Glenn Kasten *notificationFrames = mFrameCount; 60677410591dad836434c72ddee66680802708b70c10Glenn Kasten } else { 606849d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // not fast track: max notification period is resampled equivalent of one HAL buffer time 606949d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // or 20 ms if there is a fast capture 607049d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // TODO This could be a roundupRatio inline, and const 607149d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount) 607249d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten * sampleRate + mSampleRate - 1) / mSampleRate; 607349d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // minimum number of notification periods is at least kMinNotifications, 607449d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs) 607549d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten static const size_t kMinNotifications = 3; 607649d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten static const uint32_t kMinMs = 30; 607749d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // TODO This could be a roundupRatio inline 607849d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000; 607949d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // TODO This could be a roundupRatio inline 608049d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) / 608149d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten maxNotificationFrames; 608249d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten const size_t minFrameCount = maxNotificationFrames * 608349d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten max(kMinNotifications, minNotificationsByMs); 608449d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten frameCount = max(frameCount, minFrameCount); 608549d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten if (*notificationFrames == 0 || *notificationFrames > maxNotificationFrames) { 608649d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten *notificationFrames = maxNotificationFrames; 60877410591dad836434c72ddee66680802708b70c10Glenn Kasten } 608890e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten } 608974935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten *pFrameCount = frameCount; 609090e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten 609115e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten lStatus = initCheck(); 609215e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten if (lStatus != NO_ERROR) { 609315e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten ALOGE("createRecordTrack_l() audio driver not initialized"); 609415e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten goto Exit; 609515e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten } 609681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 609781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // scope for mLock 609881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 609981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 610081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track = new RecordTrack(this, client, sampleRate, 610183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent format, channelMask, frameCount, NULL, sessionId, uid, 610283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent *flags, TrackBase::TYPE_DEFAULT); 610381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 6104030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten lStatus = track->initCheck(); 6105030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten if (lStatus != NO_ERROR) { 610635295078ab59c8c5d143a54d5a55557c3ca62c51Glenn Kasten ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus); 610703e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George // track must be cleared from the caller as the caller has the AF lock 610881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 610981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 611081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mTracks.add(track); 611181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 611281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // disable AEC and NS if the device is a BT SCO headset supporting those pre processings 611381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool suspend = audio_is_bluetooth_sco_device(mInDevice) && 611481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioFlinger->btNrecIsOff(); 611581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); 611681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent setEffectSuspended_l(FX_IID_NS, suspend, sessionId); 611790e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten 611890e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { 611990e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten pid_t callingPid = IPCThreadState::self()->getCallingPid(); 612090e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, 612190e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten // so ask activity manager to do this on our behalf 612290e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); 612390e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten } 612481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 612505997e21af6c4517f375def6563af4b9ebe95f39Glenn Kasten 612681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = NO_ERROR; 612781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 612881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit: 61299156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten *status = lStatus; 613081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return track; 613181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 613281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 613381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, 613481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSystem::sync_event_t event, 613581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int triggerSession) 613681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 613781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); 613881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<ThreadBase> strongMe = this; 613981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status = NO_ERROR; 614081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 614181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (event == AudioSystem::SYNC_EVENT_NONE) { 614225f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten recordTrack->clearSyncStartEvent(); 614381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (event != AudioSystem::SYNC_EVENT_SAME) { 61446dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event, 614581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent triggerSession, 614681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent recordTrack->sessionId(), 614781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent syncStartEventCallback, 61486dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack); 614981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Sync event can be cancelled by the trigger session if the track is not in a 615081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // compatible state in which case we start record immediately 61516dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (recordTrack->mSyncStartEvent->isCancelled()) { 615225f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten recordTrack->clearSyncStartEvent(); 615381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 615481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs 61556dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack->mFramesToDrop = - 61564cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000); 615781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 615881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 615981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 616081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 616147c2070b2ce8aedb7300c0aad91caccf3c383841Glenn Kasten // This section is a rendezvous between binder thread executing start() and RecordThread 616281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AutoMutex lock(mLock); 61636dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (mActiveTracks.indexOf(recordTrack) >= 0) { 61646dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (recordTrack->mState == TrackBase::PAUSING) { 61656dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ALOGV("active record track PAUSING -> ACTIVE"); 6166f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten recordTrack->mState = TrackBase::ACTIVE; 61676dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } else { 61686dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ALOGV("active record track state %d", recordTrack->mState); 616981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 617081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 617181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 617281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 61734cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten // TODO consider other ways of handling this, such as changing the state to :STARTING and 61744cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten // adding the track to mActiveTracks after returning from AudioSystem::startInput(), 61754cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten // or using a separate command thread 61766dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack->mState = TrackBase::STARTING_1; 61772b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracks.add(recordTrack); 61782b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracksGen++; 617983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent status_t status = NO_ERROR; 618083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent if (recordTrack->isExternalTrack()) { 618183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent mLock.unlock(); 61824dc680607181e6a76f4e91a39366c4f5dfb7b03eEric Laurent status = AudioSystem::startInput(mId, (audio_session_t)recordTrack->sessionId()); 618383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent mLock.lock(); 618483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent // FIXME should verify that recordTrack is still in mActiveTracks 618583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent if (status != NO_ERROR) { 618683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent mActiveTracks.remove(recordTrack); 618783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent mActiveTracksGen++; 618883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent recordTrack->clearSyncStartEvent(); 618983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent ALOGV("RecordThread::start error %d", status); 619083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent return status; 619183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent } 619281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 61936dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // Catch up with current buffer indices if thread is already running. 61946dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // This is what makes a new client discard all buffered data. If the track's mRsmpInFront 61956dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // was initialized to some value closer to the thread's mRsmpInFront, then the track could 61966dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // see previously buffered data before it called start(), but with greater risk of overrun. 61976dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 619873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung recordTrack->mResamplerBufferProvider->reset(); 619997a893eb34f8687485c88eaf15917974a203f20bAndy Hung // clear any converter state as new data will be discontinuous 620097a893eb34f8687485c88eaf15917974a203f20bAndy Hung recordTrack->mRecordBufferConverter->reset(); 62016dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack->mState = TrackBase::STARTING_2; 620281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // signal thread to start 620381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitWorkCV.broadcast(); 62042b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (mActiveTracks.indexOf(recordTrack) < 0) { 620581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("Record failed to start"); 620681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = BAD_VALUE; 620781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto startError; 620881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 620981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 621081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 62117c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten 621281784c37c61b09289654b979567a42bf73cd2b12Eric LaurentstartError: 621383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent if (recordTrack->isExternalTrack()) { 62144dc680607181e6a76f4e91a39366c4f5dfb7b03eEric Laurent AudioSystem::stopInput(mId, (audio_session_t)recordTrack->sessionId()); 621583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent } 621625f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten recordTrack->clearSyncStartEvent(); 62176dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME I wonder why we do not reset the state here? 621881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 621981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 622081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 622181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) 622281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 622381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<SyncEvent> strongEvent = event.promote(); 622481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 622581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (strongEvent != 0) { 62268ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent sp<RefBase> ptr = strongEvent->cookie().promote(); 62278ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent if (ptr != 0) { 62288ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent RecordTrack *recordTrack = (RecordTrack *)ptr.get(); 62298ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent recordTrack->handleSyncStartEvent(strongEvent); 62308ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent } 623181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 623281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 623381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 6234a8356f663014e7d4c27869629af83d8bb3441e19Glenn Kastenbool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { 623581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("RecordThread::stop"); 6236a8356f663014e7d4c27869629af83d8bb3441e19Glenn Kasten AutoMutex _l(mLock); 62372b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (mActiveTracks.indexOf(recordTrack) != 0 || recordTrack->mState == TrackBase::PAUSING) { 623881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 623981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 624047c2070b2ce8aedb7300c0aad91caccf3c383841Glenn Kasten // note that threadLoop may still be processing the track at this point [without lock] 624181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent recordTrack->mState = TrackBase::PAUSING; 624281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // do not wait for mStartStopCond if exiting 624381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (exitPending()) { 624481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return true; 624581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 624647c2070b2ce8aedb7300c0aad91caccf3c383841Glenn Kasten // FIXME incorrect usage of wait: no explicit predicate or loop 624781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStartStopCond.wait(mLock); 62482b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten // if we have been restarted, recordTrack is in mActiveTracks here 62492b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (exitPending() || mActiveTracks.indexOf(recordTrack) != 0) { 625081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("Record stopped OK"); 625181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return true; 625281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 625381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 625481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 625581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 62560f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenbool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const 625781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 625881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 625981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 626081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 62610f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenstatus_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused) 626281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 626381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#if 0 // This branch is currently dead code, but is preserved in case it will be needed in future 626481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!isValidSyncEvent(event)) { 626581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 626681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 626781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 626881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int eventSession = event->triggerSession(); 626981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t ret = NAME_NOT_FOUND; 627081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 627181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 627281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 627381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 627481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<RecordTrack> track = mTracks[i]; 627581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (eventSession == track->sessionId()) { 627681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (void) track->setSyncEvent(event); 627781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ret = NO_ERROR; 627881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 627981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 628081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return ret; 628181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#else 628281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 628381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 628481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 628581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 628681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// destroyTrack_l() must be called with ThreadBase::mLock held 628781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track) 628881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 6289bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->terminate(); 6290bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mState = TrackBase::STOPPED; 629181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // active tracks are removed by threadLoop() 62922b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (mActiveTracks.indexOf(track) < 0) { 629381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent removeTrack_l(track); 629481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 629581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 629681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 629781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track) 629881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 629981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mTracks.remove(track); 630081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // need anything related to effects here? 63016dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (track->isFastTrack()) { 63026dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOG_ASSERT(!mFastTrackAvail); 63036dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastTrackAvail = true; 63046dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 630581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 630681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 630781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) 630881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 630981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpInternals(fd, args); 631081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpTracks(fd, args); 631181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpEffectChains(fd, args); 631281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 631381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 631481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args) 631581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 631687cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, "\nInput thread %p:\n", this); 631781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 631844182c206f7c5584ef2cf504da6be98fab665dbfGlenn Kasten dumpBase(fd, args); 631944182c206f7c5584ef2cf504da6be98fab665dbfGlenn Kasten 632044182c206f7c5584ef2cf504da6be98fab665dbfGlenn Kasten if (mActiveTracks.size() == 0) { 632187cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " No active record clients\n"); 632281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 63236e6704c06d61bc356e30c164081e5bcffb37920cGlenn Kasten dprintf(fd, " Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no"); 63246dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten dprintf(fd, " Fast track available: %s\n", mFastTrackAvail ? "yes" : "no"); 632517c9c998afed5ed9df7495eeed5822f3ed53ebecGlenn Kasten 632617c9c998afed5ed9df7495eeed5822f3ed53ebecGlenn Kasten // Make a non-atomic copy of fast capture dump state so it won't change underneath us 632717c9c998afed5ed9df7495eeed5822f3ed53ebecGlenn Kasten const FastCaptureDumpState copy(mFastCaptureDumpState); 632817c9c998afed5ed9df7495eeed5822f3ed53ebecGlenn Kasten copy.dump(fd); 632981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 633081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 63310f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args __unused) 633281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 633381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const size_t SIZE = 256; 633481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char buffer[SIZE]; 633581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent String8 result; 633681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 6337b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numtracks = mTracks.size(); 6338b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numactive = mActiveTracks.size(); 6339b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numactiveseen = 0; 634087cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " %d Tracks", numtracks); 6341b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (numtracks) { 634287cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " of which %d are active\n", numactive); 6343b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen RecordTrack::appendDumpHeader(result); 6344b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen for (size_t i = 0; i < numtracks ; ++i) { 6345b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen sp<RecordTrack> track = mTracks[i]; 6346b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (track != 0) { 6347b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen bool active = mActiveTracks.indexOf(track) >= 0; 6348b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (active) { 6349b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen numactiveseen++; 6350b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 6351b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen track->dump(buffer, SIZE, active); 6352b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append(buffer); 6353b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 635481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 6355b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } else { 635687cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, "\n"); 635781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 635881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 6359b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (numactiveseen != numactive) { 6360b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen snprintf(buffer, SIZE, " The following tracks are in the active list but" 6361b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen " not in the track list\n"); 636281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result.append(buffer); 636381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent RecordTrack::appendDumpHeader(result); 6364b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen for (size_t i = 0; i < numactive; ++i) { 63652b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten sp<RecordTrack> track = mActiveTracks[i]; 6366b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mTracks.indexOf(track) < 0) { 6367b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen track->dump(buffer, SIZE, true); 6368b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append(buffer); 6369b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 63702b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten } 637181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 637281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 637381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent write(fd, result.string(), result.size()); 637481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 637581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 637673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung 637773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hungvoid AudioFlinger::RecordThread::ResamplerBufferProvider::reset() 637873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung{ 637973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); 638073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung RecordThread *recordThread = (RecordThread *) threadBase.get(); 638173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung mRsmpInFront = recordThread->mRsmpInRear; 638273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung mRsmpInUnrel = 0; 638373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung} 638473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung 638573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hungvoid AudioFlinger::RecordThread::ResamplerBufferProvider::sync( 638673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung size_t *framesAvailable, bool *hasOverrun) 638773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung{ 638873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); 638973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung RecordThread *recordThread = (RecordThread *) threadBase.get(); 639073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung const int32_t rear = recordThread->mRsmpInRear; 639173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung const int32_t front = mRsmpInFront; 639273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung const ssize_t filled = rear - front; 639373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung 639473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung size_t framesIn; 639573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung bool overrun = false; 639673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung if (filled < 0) { 639773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung // should not happen, but treat like a massive overrun and re-sync 639873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung framesIn = 0; 639973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung mRsmpInFront = rear; 640073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung overrun = true; 640173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung } else if ((size_t) filled <= recordThread->mRsmpInFrames) { 640273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung framesIn = (size_t) filled; 640373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung } else { 640473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung // client is not keeping up with server, but give it latest data 640573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung framesIn = recordThread->mRsmpInFrames; 640673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung mRsmpInFront = /* front = */ rear - framesIn; 640773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung overrun = true; 640873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung } 640973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung if (framesAvailable != NULL) { 641073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung *framesAvailable = framesIn; 641173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung } 641273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung if (hasOverrun != NULL) { 641373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung *hasOverrun = overrun; 641473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung } 641573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung} 641673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung 641781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// AudioBufferProvider interface 64186dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kastenstatus_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer( 64196dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten AudioBufferProvider::Buffer* buffer, int64_t pts __unused) 642081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 642173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); 64226dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (threadBase == 0) { 64236dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten buffer->frameCount = 0; 6424607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten buffer->raw = NULL; 64256dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten return NOT_ENOUGH_DATA; 64266dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 64276dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten RecordThread *recordThread = (RecordThread *) threadBase.get(); 64286dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten int32_t rear = recordThread->mRsmpInRear; 642973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung int32_t front = mRsmpInFront; 64308594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten ssize_t filled = rear - front; 64316dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME should not be P2 (don't want to increase latency) 64326dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME if client not keeping up, discard 6433607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames)); 64348594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten // 'filled' may be non-contiguous, so return only the first contiguous chunk 64356dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten front &= recordThread->mRsmpInFramesP2 - 1; 64366dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size_t part1 = recordThread->mRsmpInFramesP2 - front; 64378594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten if (part1 > (size_t) filled) { 64388594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten part1 = filled; 64398594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten } 64408594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten size_t ask = buffer->frameCount; 64418594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten ALOG_ASSERT(ask > 0); 64428594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten if (part1 > ask) { 64438594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten part1 = ask; 64448594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten } 64458594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten if (part1 == 0) { 644673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung // out of data is fine since the resampler will return a short-count. 64478594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten buffer->raw = NULL; 64488594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten buffer->frameCount = 0; 644973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung mRsmpInUnrel = 0; 64508594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten return NOT_ENOUGH_DATA; 64518594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten } 64528594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten 64535744661e85981f8a9456bf470e2761235fc026daAndy Hung buffer->raw = (uint8_t*)recordThread->mRsmpInBuffer + front * recordThread->mFrameSize; 64548594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten buffer->frameCount = part1; 645573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung mRsmpInUnrel = part1; 645681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 645781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 645881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 645981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// AudioBufferProvider interface 64606dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kastenvoid AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer( 64616dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten AudioBufferProvider::Buffer* buffer) 646281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 64638594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten size_t stepCount = buffer->frameCount; 64648594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten if (stepCount == 0) { 64658594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten return; 64668594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten } 646773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung ALOG_ASSERT(stepCount <= mRsmpInUnrel); 646873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung mRsmpInUnrel -= stepCount; 646973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung mRsmpInFront += stepCount; 64708594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten buffer->raw = NULL; 647181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent buffer->frameCount = 0; 647281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 647381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 647497a893eb34f8687485c88eaf15917974a203f20bAndy HungAudioFlinger::RecordThread::RecordBufferConverter::RecordBufferConverter( 647597a893eb34f8687485c88eaf15917974a203f20bAndy Hung audio_channel_mask_t srcChannelMask, audio_format_t srcFormat, 647697a893eb34f8687485c88eaf15917974a203f20bAndy Hung uint32_t srcSampleRate, 647797a893eb34f8687485c88eaf15917974a203f20bAndy Hung audio_channel_mask_t dstChannelMask, audio_format_t dstFormat, 647897a893eb34f8687485c88eaf15917974a203f20bAndy Hung uint32_t dstSampleRate) : 647997a893eb34f8687485c88eaf15917974a203f20bAndy Hung mSrcChannelMask(AUDIO_CHANNEL_INVALID), // updateParameters will set following vars 648097a893eb34f8687485c88eaf15917974a203f20bAndy Hung // mSrcFormat 648197a893eb34f8687485c88eaf15917974a203f20bAndy Hung // mSrcSampleRate 648297a893eb34f8687485c88eaf15917974a203f20bAndy Hung // mDstChannelMask 648397a893eb34f8687485c88eaf15917974a203f20bAndy Hung // mDstFormat 648497a893eb34f8687485c88eaf15917974a203f20bAndy Hung // mDstSampleRate 648597a893eb34f8687485c88eaf15917974a203f20bAndy Hung // mSrcChannelCount 648697a893eb34f8687485c88eaf15917974a203f20bAndy Hung // mDstChannelCount 648797a893eb34f8687485c88eaf15917974a203f20bAndy Hung // mDstFrameSize 648897a893eb34f8687485c88eaf15917974a203f20bAndy Hung mBuf(NULL), mBufFrames(0), mBufFrameSize(0), 6489d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mResampler(NULL), 6490d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mIsLegacyDownmix(false), 6491d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mIsLegacyUpmix(false), 6492d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mRequiresFloat(false), 6493d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mInputConverterProvider(NULL) 649497a893eb34f8687485c88eaf15917974a203f20bAndy Hung{ 649597a893eb34f8687485c88eaf15917974a203f20bAndy Hung (void)updateParameters(srcChannelMask, srcFormat, srcSampleRate, 649697a893eb34f8687485c88eaf15917974a203f20bAndy Hung dstChannelMask, dstFormat, dstSampleRate); 649797a893eb34f8687485c88eaf15917974a203f20bAndy Hung} 649897a893eb34f8687485c88eaf15917974a203f20bAndy Hung 649997a893eb34f8687485c88eaf15917974a203f20bAndy HungAudioFlinger::RecordThread::RecordBufferConverter::~RecordBufferConverter() { 650097a893eb34f8687485c88eaf15917974a203f20bAndy Hung free(mBuf); 650197a893eb34f8687485c88eaf15917974a203f20bAndy Hung delete mResampler; 6502d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung delete mInputConverterProvider; 650397a893eb34f8687485c88eaf15917974a203f20bAndy Hung} 650497a893eb34f8687485c88eaf15917974a203f20bAndy Hung 650597a893eb34f8687485c88eaf15917974a203f20bAndy Hungsize_t AudioFlinger::RecordThread::RecordBufferConverter::convert(void *dst, 650697a893eb34f8687485c88eaf15917974a203f20bAndy Hung AudioBufferProvider *provider, size_t frames) 650797a893eb34f8687485c88eaf15917974a203f20bAndy Hung{ 6508d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mInputConverterProvider != NULL) { 6509d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mInputConverterProvider->setBufferProvider(provider); 6510d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung provider = mInputConverterProvider; 6511d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } 6512d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung 6513d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mResampler == NULL) { 651497a893eb34f8687485c88eaf15917974a203f20bAndy Hung ALOGVV("NO RESAMPLING sampleRate:%u mSrcFormat:%#x mDstFormat:%#x", 651597a893eb34f8687485c88eaf15917974a203f20bAndy Hung mSrcSampleRate, mSrcFormat, mDstFormat); 651697a893eb34f8687485c88eaf15917974a203f20bAndy Hung 651797a893eb34f8687485c88eaf15917974a203f20bAndy Hung AudioBufferProvider::Buffer buffer; 651897a893eb34f8687485c88eaf15917974a203f20bAndy Hung for (size_t i = frames; i > 0; ) { 651997a893eb34f8687485c88eaf15917974a203f20bAndy Hung buffer.frameCount = i; 652097a893eb34f8687485c88eaf15917974a203f20bAndy Hung status_t status = provider->getNextBuffer(&buffer, 0); 652197a893eb34f8687485c88eaf15917974a203f20bAndy Hung if (status != OK || buffer.frameCount == 0) { 652297a893eb34f8687485c88eaf15917974a203f20bAndy Hung frames -= i; // cannot fill request. 652397a893eb34f8687485c88eaf15917974a203f20bAndy Hung break; 652497a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 6525d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // format convert to destination buffer 6526d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung convertNoResampler(dst, buffer.raw, buffer.frameCount); 652797a893eb34f8687485c88eaf15917974a203f20bAndy Hung 652897a893eb34f8687485c88eaf15917974a203f20bAndy Hung dst = (int8_t*)dst + buffer.frameCount * mDstFrameSize; 652997a893eb34f8687485c88eaf15917974a203f20bAndy Hung i -= buffer.frameCount; 653097a893eb34f8687485c88eaf15917974a203f20bAndy Hung provider->releaseBuffer(&buffer); 653197a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 653297a893eb34f8687485c88eaf15917974a203f20bAndy Hung } else { 653397a893eb34f8687485c88eaf15917974a203f20bAndy Hung ALOGVV("RESAMPLING mSrcSampleRate:%u mDstSampleRate:%u mSrcFormat:%#x mDstFormat:%#x", 653497a893eb34f8687485c88eaf15917974a203f20bAndy Hung mSrcSampleRate, mDstSampleRate, mSrcFormat, mDstFormat); 653597a893eb34f8687485c88eaf15917974a203f20bAndy Hung 6536d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // reallocate buffer if needed 6537d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mBufFrameSize != 0 && mBufFrames < frames) { 6538d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung free(mBuf); 6539d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mBufFrames = frames; 6540d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung (void)posix_memalign(&mBuf, 32, mBufFrames * mBufFrameSize); 6541d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } 654297a893eb34f8687485c88eaf15917974a203f20bAndy Hung // resampler accumulates, but we only have one source track 6543d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung memset(mBuf, 0, frames * mBufFrameSize); 6544d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung frames = mResampler->resample((int32_t*)mBuf, frames, provider); 6545d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // format convert to destination buffer 6546d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung convertResampler(dst, mBuf, frames); 654797a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 654897a893eb34f8687485c88eaf15917974a203f20bAndy Hung return frames; 654997a893eb34f8687485c88eaf15917974a203f20bAndy Hung} 655097a893eb34f8687485c88eaf15917974a203f20bAndy Hung 655197a893eb34f8687485c88eaf15917974a203f20bAndy Hungstatus_t AudioFlinger::RecordThread::RecordBufferConverter::updateParameters( 655297a893eb34f8687485c88eaf15917974a203f20bAndy Hung audio_channel_mask_t srcChannelMask, audio_format_t srcFormat, 655397a893eb34f8687485c88eaf15917974a203f20bAndy Hung uint32_t srcSampleRate, 655497a893eb34f8687485c88eaf15917974a203f20bAndy Hung audio_channel_mask_t dstChannelMask, audio_format_t dstFormat, 655597a893eb34f8687485c88eaf15917974a203f20bAndy Hung uint32_t dstSampleRate) 655697a893eb34f8687485c88eaf15917974a203f20bAndy Hung{ 655797a893eb34f8687485c88eaf15917974a203f20bAndy Hung // quick evaluation if there is any change. 655897a893eb34f8687485c88eaf15917974a203f20bAndy Hung if (mSrcFormat == srcFormat 655997a893eb34f8687485c88eaf15917974a203f20bAndy Hung && mSrcChannelMask == srcChannelMask 656097a893eb34f8687485c88eaf15917974a203f20bAndy Hung && mSrcSampleRate == srcSampleRate 656197a893eb34f8687485c88eaf15917974a203f20bAndy Hung && mDstFormat == dstFormat 656297a893eb34f8687485c88eaf15917974a203f20bAndy Hung && mDstChannelMask == dstChannelMask 656397a893eb34f8687485c88eaf15917974a203f20bAndy Hung && mDstSampleRate == dstSampleRate) { 656497a893eb34f8687485c88eaf15917974a203f20bAndy Hung return NO_ERROR; 656597a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 656697a893eb34f8687485c88eaf15917974a203f20bAndy Hung 6567db4c031f518ae5806af73756273ff32cd8d0e4f8Andy Hung ALOGV("RecordBufferConverter updateParameters srcMask:%#x dstMask:%#x" 6568db4c031f518ae5806af73756273ff32cd8d0e4f8Andy Hung " srcFormat:%#x dstFormat:%#x srcRate:%u dstRate:%u", 6569db4c031f518ae5806af73756273ff32cd8d0e4f8Andy Hung srcChannelMask, dstChannelMask, srcFormat, dstFormat, srcSampleRate, dstSampleRate); 657097a893eb34f8687485c88eaf15917974a203f20bAndy Hung const bool valid = 657197a893eb34f8687485c88eaf15917974a203f20bAndy Hung audio_is_input_channel(srcChannelMask) 657297a893eb34f8687485c88eaf15917974a203f20bAndy Hung && audio_is_input_channel(dstChannelMask) 657397a893eb34f8687485c88eaf15917974a203f20bAndy Hung && audio_is_valid_format(srcFormat) && audio_is_linear_pcm(srcFormat) 657497a893eb34f8687485c88eaf15917974a203f20bAndy Hung && audio_is_valid_format(dstFormat) && audio_is_linear_pcm(dstFormat) 657597a893eb34f8687485c88eaf15917974a203f20bAndy Hung && (srcSampleRate <= dstSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) 657697a893eb34f8687485c88eaf15917974a203f20bAndy Hung ; // no upsampling checks for now 657797a893eb34f8687485c88eaf15917974a203f20bAndy Hung if (!valid) { 657897a893eb34f8687485c88eaf15917974a203f20bAndy Hung return BAD_VALUE; 657997a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 658097a893eb34f8687485c88eaf15917974a203f20bAndy Hung 658197a893eb34f8687485c88eaf15917974a203f20bAndy Hung mSrcFormat = srcFormat; 658297a893eb34f8687485c88eaf15917974a203f20bAndy Hung mSrcChannelMask = srcChannelMask; 658397a893eb34f8687485c88eaf15917974a203f20bAndy Hung mSrcSampleRate = srcSampleRate; 658497a893eb34f8687485c88eaf15917974a203f20bAndy Hung mDstFormat = dstFormat; 658597a893eb34f8687485c88eaf15917974a203f20bAndy Hung mDstChannelMask = dstChannelMask; 658697a893eb34f8687485c88eaf15917974a203f20bAndy Hung mDstSampleRate = dstSampleRate; 658797a893eb34f8687485c88eaf15917974a203f20bAndy Hung 658897a893eb34f8687485c88eaf15917974a203f20bAndy Hung // compute derived parameters 658997a893eb34f8687485c88eaf15917974a203f20bAndy Hung mSrcChannelCount = audio_channel_count_from_in_mask(srcChannelMask); 659097a893eb34f8687485c88eaf15917974a203f20bAndy Hung mDstChannelCount = audio_channel_count_from_in_mask(dstChannelMask); 659197a893eb34f8687485c88eaf15917974a203f20bAndy Hung mDstFrameSize = mDstChannelCount * audio_bytes_per_sample(mDstFormat); 659297a893eb34f8687485c88eaf15917974a203f20bAndy Hung 6593d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // do we need to resample? 6594d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung delete mResampler; 6595d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mResampler = NULL; 6596d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mSrcSampleRate != mDstSampleRate) { 6597d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mResampler = AudioResampler::create(AUDIO_FORMAT_PCM_FLOAT, 6598d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mSrcChannelCount, mDstSampleRate); 6599d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mResampler->setSampleRate(mSrcSampleRate); 6600d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mResampler->setVolume(AudioMixer::UNITY_GAIN_FLOAT, AudioMixer::UNITY_GAIN_FLOAT); 6601d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } 6602d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung 6603d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // are we running legacy channel conversion modes? 6604d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mIsLegacyDownmix = (mSrcChannelMask == AUDIO_CHANNEL_IN_STEREO 6605d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung || mSrcChannelMask == AUDIO_CHANNEL_IN_FRONT_BACK) 6606d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung && mDstChannelMask == AUDIO_CHANNEL_IN_MONO; 6607d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mIsLegacyUpmix = mSrcChannelMask == AUDIO_CHANNEL_IN_MONO 6608d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung && (mDstChannelMask == AUDIO_CHANNEL_IN_STEREO 6609d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung || mDstChannelMask == AUDIO_CHANNEL_IN_FRONT_BACK); 6610d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung 6611d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // do we need to process in float? 6612d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mRequiresFloat = mResampler != NULL || mIsLegacyDownmix || mIsLegacyUpmix; 6613d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung 6614d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // do we need a staging buffer to convert for destination (we can still optimize this)? 6615d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // we use mBufFrameSize > 0 to indicate both frame size as well as buffer necessity 6616d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mResampler != NULL) { 6617d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mBufFrameSize = max(mSrcChannelCount, FCC_2) 6618d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung * audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT); 6619d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } else if ((mIsLegacyUpmix || mIsLegacyDownmix) && mDstFormat != AUDIO_FORMAT_PCM_FLOAT) { 6620d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mBufFrameSize = mDstChannelCount * audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT); 6621d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } else if (mSrcChannelMask != mDstChannelMask && mDstFormat != mSrcFormat) { 662297a893eb34f8687485c88eaf15917974a203f20bAndy Hung mBufFrameSize = mDstChannelCount * audio_bytes_per_sample(mSrcFormat); 662397a893eb34f8687485c88eaf15917974a203f20bAndy Hung } else { 662497a893eb34f8687485c88eaf15917974a203f20bAndy Hung mBufFrameSize = 0; 662597a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 662697a893eb34f8687485c88eaf15917974a203f20bAndy Hung mBufFrames = 0; // force the buffer to be resized. 662797a893eb34f8687485c88eaf15917974a203f20bAndy Hung 6628d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // do we need an input converter buffer provider to give us float? 6629d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung delete mInputConverterProvider; 6630d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mInputConverterProvider = NULL; 6631d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mRequiresFloat && mSrcFormat != AUDIO_FORMAT_PCM_FLOAT) { 6632d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mInputConverterProvider = new ReformatBufferProvider( 6633d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung audio_channel_count_from_in_mask(mSrcChannelMask), 6634d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mSrcFormat, 6635d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung AUDIO_FORMAT_PCM_FLOAT, 6636d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung 256 /* provider buffer frame count */); 6637d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } 6638d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung 6639d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // do we need a remixer to do channel mask conversion 6640d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (!mIsLegacyDownmix && !mIsLegacyUpmix && mSrcChannelMask != mDstChannelMask) { 6641d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung (void) memcpy_by_index_array_initialization_from_channel_mask( 6642d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mIdxAry, ARRAY_SIZE(mIdxAry), mDstChannelMask, mSrcChannelMask); 664397a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 664497a893eb34f8687485c88eaf15917974a203f20bAndy Hung return NO_ERROR; 664597a893eb34f8687485c88eaf15917974a203f20bAndy Hung} 664697a893eb34f8687485c88eaf15917974a203f20bAndy Hung 6647d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hungvoid AudioFlinger::RecordThread::RecordBufferConverter::convertNoResampler( 6648d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung void *dst, const void *src, size_t frames) 664997a893eb34f8687485c88eaf15917974a203f20bAndy Hung{ 6650d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // src is native type unless there is legacy upmix or downmix, whereupon it is float. 665197a893eb34f8687485c88eaf15917974a203f20bAndy Hung if (mBufFrameSize != 0 && mBufFrames < frames) { 665297a893eb34f8687485c88eaf15917974a203f20bAndy Hung free(mBuf); 665397a893eb34f8687485c88eaf15917974a203f20bAndy Hung mBufFrames = frames; 665497a893eb34f8687485c88eaf15917974a203f20bAndy Hung (void)posix_memalign(&mBuf, 32, mBufFrames * mBufFrameSize); 665597a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 6656d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // do we need to do legacy upmix and downmix? 6657d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mIsLegacyUpmix || mIsLegacyDownmix) { 665897a893eb34f8687485c88eaf15917974a203f20bAndy Hung void *dstBuf = mBuf != NULL ? mBuf : dst; 6659d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mIsLegacyUpmix) { 6660d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung upmix_to_stereo_float_from_mono_float((float *)dstBuf, 6661d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung (const float *)src, frames); 6662d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } else /*mIsLegacyDownmix */ { 6663d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung downmix_to_mono_float_from_stereo_float((float *)dstBuf, 6664d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung (const float *)src, frames); 666597a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 6666d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mBuf != NULL) { 6667d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung memcpy_by_audio_format(dst, mDstFormat, mBuf, AUDIO_FORMAT_PCM_FLOAT, 6668d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung frames * mDstChannelCount); 6669d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } 6670d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung return; 6671d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } 6672d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // do we need to do channel mask conversion? 6673d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mSrcChannelMask != mDstChannelMask) { 667497a893eb34f8687485c88eaf15917974a203f20bAndy Hung void *dstBuf = mBuf != NULL ? mBuf : dst; 6675d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung memcpy_by_index_array(dstBuf, mDstChannelCount, 6676d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung src, mSrcChannelCount, mIdxAry, audio_bytes_per_sample(mSrcFormat), frames); 6677d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (dstBuf == dst) { 6678d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung return; // format is the same 6679d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } 6680d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } 6681d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // convert to destination buffer 6682d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung const void *convertBuf = mBuf != NULL ? mBuf : src; 6683d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung memcpy_by_audio_format(dst, mDstFormat, convertBuf, mSrcFormat, 6684d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung frames * mDstChannelCount); 6685d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung} 6686d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung 6687d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hungvoid AudioFlinger::RecordThread::RecordBufferConverter::convertResampler( 6688d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung void *dst, /*not-a-const*/ void *src, size_t frames) 6689d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung{ 6690d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // src buffer format is ALWAYS float when entering this routine 6691d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mIsLegacyUpmix) { 6692d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung ; // mono to stereo already handled by resampler 6693d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } else if (mIsLegacyDownmix 6694d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung || (mSrcChannelMask == mDstChannelMask && mSrcChannelCount == 1)) { 6695d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // the resampler outputs stereo for mono input channel (a feature?) 6696d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // must convert to mono 6697d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung downmix_to_mono_float_from_stereo_float((float *)src, 6698d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung (const float *)src, frames); 6699d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } else if (mSrcChannelMask != mDstChannelMask) { 6700d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // convert to mono channel again for channel mask conversion (could be skipped 6701d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // with further optimization). 670297a893eb34f8687485c88eaf15917974a203f20bAndy Hung if (mSrcChannelCount == 1) { 6703d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung downmix_to_mono_float_from_stereo_float((float *)src, 6704d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung (const float *)src, frames); 670597a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 6706d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // convert to destination format (in place, OK as float is larger than other types) 6707d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mDstFormat != AUDIO_FORMAT_PCM_FLOAT) { 6708d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung memcpy_by_audio_format(src, mDstFormat, src, AUDIO_FORMAT_PCM_FLOAT, 6709d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung frames * mSrcChannelCount); 6710d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } 6711d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // channel convert and save to dst 6712d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung memcpy_by_index_array(dst, mDstChannelCount, 6713d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung src, mSrcChannelCount, mIdxAry, audio_bytes_per_sample(mDstFormat), frames); 6714d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung return; 671597a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 6716d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // convert to destination format and save to dst 6717d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung memcpy_by_audio_format(dst, mDstFormat, src, AUDIO_FORMAT_PCM_FLOAT, 6718d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung frames * mDstChannelCount); 671997a893eb34f8687485c88eaf15917974a203f20bAndy Hung} 672097a893eb34f8687485c88eaf15917974a203f20bAndy Hung 67211035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentbool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair, 67221035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status_t& status) 672381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 672481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool reconfig = false; 672581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 67261035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = NO_ERROR; 67271035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 67281035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent audio_format_t reqFormat = mFormat; 67291035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent uint32_t samplingRate = mSampleRate; 67301035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount); 6731d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // possible that we are > 2 channels, use channel index mask 6732d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (channelMask == AUDIO_CHANNEL_INVALID && mChannelCount <= FCC_8) { 6733d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung audio_channel_mask_for_index_assignment_from_count(mChannelCount); 6734d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } 67351035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 67361035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent AudioParameter param = AudioParameter(keyValuePair); 67371035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent int value; 67381035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // TODO Investigate when this code runs. Check with audio policy when a sample rate and 67391035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // channel count change can be requested. Do we mandate the first client defines the 67401035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // HAL sampling rate and channel count or do we allow changes on the fly? 67411035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { 67421035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent samplingRate = value; 67431035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 67441035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 67451035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { 674697a893eb34f8687485c88eaf15917974a203f20bAndy Hung if (!audio_is_linear_pcm((audio_format_t) value)) { 67471035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = BAD_VALUE; 67481035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 67491035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reqFormat = (audio_format_t) value; 675081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent reconfig = true; 675181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 67521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 67531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { 67541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent audio_channel_mask_t mask = (audio_channel_mask_t) value; 6755d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (!audio_is_input_channel(mask) || 6756d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung audio_channel_count_from_in_mask(mask) > FCC_8) { 67571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = BAD_VALUE; 67581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 67591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent channelMask = mask; 67601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 676181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 67621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 67631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { 67641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // do not accept frame count changes if tracks are open as the track buffer 67651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // size depends on frame count and correct behavior would not be guaranteed 67661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // if frame count is changed after track creation 67671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (mActiveTracks.size() > 0) { 67681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = INVALID_OPERATION; 67691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 67701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 677181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 67721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 67731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { 67741035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // forward device change to effects that have requested to be 67751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // aware of attached audio device. 67761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 67771035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mEffectChains[i]->setDevice_l(value); 677881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 677981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 67801035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // store input device and output device but do not forward output device to audio HAL. 67811035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // Note that status is ignored by the caller for output device 67821035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // (see AudioFlinger::setParameters() 67831035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (audio_is_output_devices(value)) { 67841035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mOutDevice = value; 67851035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = BAD_VALUE; 67861035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 67871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mInDevice = value; 67881035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // disable AEC and NS if the device is a BT SCO headset supporting those 67891035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // pre processings 67901035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (mTracks.size() > 0) { 67911035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent bool suspend = audio_is_bluetooth_sco_device(mInDevice) && 67921035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mAudioFlinger->btNrecIsOff(); 67931035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 67941035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sp<RecordTrack> track = mTracks[i]; 67951035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); 67961035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); 679781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 679881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 679981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 68001035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 68011035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && 68021035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mAudioSource != (audio_source_t)value) { 68031035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // forward device change to effects that have requested to be 68041035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // aware of attached audio device. 68051035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 68061035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mEffectChains[i]->setAudioSource_l((audio_source_t)value); 680781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 68081035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mAudioSource = (audio_source_t)value; 68091035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 6810e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten 68111035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == NO_ERROR) { 68121035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = mInput->stream->common.set_parameters(&mInput->stream->common, 68131035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent keyValuePair.string()); 68141035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == INVALID_OPERATION) { 68151035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent inputStandBy(); 681681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = mInput->stream->common.set_parameters(&mInput->stream->common, 681781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent keyValuePair.string()); 68181035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 68191035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (reconfig) { 68201035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == BAD_VALUE && 682197a893eb34f8687485c88eaf15917974a203f20bAndy Hung audio_is_linear_pcm(mInput->stream->common.get_format(&mInput->stream->common)) && 682297a893eb34f8687485c88eaf15917974a203f20bAndy Hung audio_is_linear_pcm(reqFormat) && 68231035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent (mInput->stream->common.get_sample_rate(&mInput->stream->common) 682497a893eb34f8687485c88eaf15917974a203f20bAndy Hung <= (AUDIO_RESAMPLER_DOWN_RATIO_MAX * samplingRate)) && 6825e541269be94f3a1072932d51537905b120ef4733Andy Hung audio_channel_count_from_in_mask( 6826d1abb8f94d7ed749ee959655db1e07d26dad074dAndy Hung mInput->stream->common.get_channels(&mInput->stream->common)) <= FCC_8) { 68271035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = NO_ERROR; 682881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 68291035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == NO_ERROR) { 68301035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent readInputParameters_l(); 683173e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); 683281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 683381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 683481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 68351035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 683681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return reconfig; 683781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 683881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 683981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentString8 AudioFlinger::RecordThread::getParameters(const String8& keys) 684081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 684181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 684281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (initCheck() != NO_ERROR) { 6843d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten return String8(); 684481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 684581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 6846d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten char *s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); 6847d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten const String8 out_s8(s); 684881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent free(s); 684981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return out_s8; 685081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 685181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 685273e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurentvoid AudioFlinger::RecordThread::ioConfigChanged(audio_io_config_event event) { 685373e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); 685473e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent 685573e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent desc->mIoHandle = mId; 685681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 685781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent switch (event) { 685873e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent case AUDIO_INPUT_OPENED: 685973e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent case AUDIO_INPUT_CONFIG_CHANGED: 6860296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent desc->mPatch = mPatch; 686173e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent desc->mChannelMask = mChannelMask; 686273e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent desc->mSamplingRate = mSampleRate; 686373e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent desc->mFormat = mFormat; 686473e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent desc->mFrameCount = mFrameCount; 686573e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent desc->mLatency = 0; 686681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 686781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 686873e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent case AUDIO_INPUT_CLOSED: 686981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent default: 687081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 687181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 687273e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent mAudioFlinger->ioConfigChanged(event, desc); 687381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 687481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 6875deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kastenvoid AudioFlinger::RecordThread::readInputParameters_l() 687681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 687781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); 687881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); 6879e541269be94f3a1072932d51537905b120ef4733Andy Hung mChannelCount = audio_channel_count_from_in_mask(mChannelMask); 6880d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mChannelCount > FCC_8) { 6881d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung ALOGE("HAL channel count %d > %d", mChannelCount, FCC_8); 6882d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } 6883463be250de73907965faa6a216c00312bf81e049Andy Hung mHALFormat = mInput->stream->common.get_format(&mInput->stream->common); 6884463be250de73907965faa6a216c00312bf81e049Andy Hung mFormat = mHALFormat; 6885d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (!audio_is_linear_pcm(mFormat)) { 6886d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung ALOGE("HAL format %#x is not linear pcm", mFormat); 6887291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten } 6888665470b36f202bcc8ee2f7417f68fd2608dd07c1Eric Laurent mFrameSize = audio_stream_in_frame_size(mInput->stream); 6889548efc94813c1dec6e8cf6c085ae41ccb04827f1Glenn Kasten mBufferSize = mInput->stream->common.get_buffer_size(&mInput->stream->common); 6890548efc94813c1dec6e8cf6c085ae41ccb04827f1Glenn Kasten mFrameCount = mBufferSize / mFrameSize; 68916dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // This is the formula for calculating the temporary buffer size. 6892e842614837e5401adf77e90485300c288b9a7876Glenn Kasten // With 7 HAL buffers, we can guarantee ability to down-sample the input by ratio of 6:1 to 68938594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten // 1 full output buffer, regardless of the alignment of the available input. 6894e842614837e5401adf77e90485300c288b9a7876Glenn Kasten // The value is somewhat arbitrary, and could probably be even larger. 68956dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // A larger value should allow more old data to be read after a track calls start(), 68966dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // without increasing latency. 689797a893eb34f8687485c88eaf15917974a203f20bAndy Hung // 689897a893eb34f8687485c88eaf15917974a203f20bAndy Hung // Note this is independent of the maximum downsampling ratio permitted for capture. 6899e842614837e5401adf77e90485300c288b9a7876Glenn Kasten mRsmpInFrames = mFrameCount * 7; 69008594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten mRsmpInFramesP2 = roundup(mRsmpInFrames); 69015744661e85981f8a9456bf470e2761235fc026daAndy Hung free(mRsmpInBuffer); 690249d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten 690349d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // TODO optimize audio capture buffer sizes ... 690449d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // Here we calculate the size of the sliding buffer used as a source 690549d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // for resampling. mRsmpInFramesP2 is currently roundup(mFrameCount * 7). 690649d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // For current HAL frame counts, this is usually 2048 = 40 ms. It would 690749d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // be better to have it derived from the pipe depth in the long term. 690849d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // The current value is higher than necessary. However it should not add to latency. 690949d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten 69108594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten // Over-allocate beyond mRsmpInFramesP2 to permit a HAL read past end of buffer 69115744661e85981f8a9456bf470e2761235fc026daAndy Hung (void)posix_memalign(&mRsmpInBuffer, 32, (mRsmpInFramesP2 + mFrameCount - 1) * mFrameSize); 69126dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 69134cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints. 69144cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten // But if thread's mSampleRate or mChannelCount changes, how will that affect active tracks? 691581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 691681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 69175f972c031d4061f4f037c9fda1ea4bd9b6a756cdGlenn Kastenuint32_t AudioFlinger::RecordThread::getInputFramesLost() 691881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 691981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 692081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (initCheck() != NO_ERROR) { 692181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 692281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 692381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 692481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mInput->stream->get_input_frames_lost(mInput->stream); 692581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 692681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 692781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) const 692881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 692981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 693081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t result = 0; 693181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (getEffectChain_l(sessionId) != 0) { 693281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result = EFFECT_SESSION; 693381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 693481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 693581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 693681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId == mTracks[i]->sessionId()) { 693781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result |= TRACK_SESSION; 693881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 693981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 694081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 694181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 694281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return result; 694381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 694481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 694581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentKeyedVector<int, bool> AudioFlinger::RecordThread::sessionIds() const 694681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 694781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent KeyedVector<int, bool> ids; 694881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 694981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t j = 0; j < mTracks.size(); ++j) { 695081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<RecordThread::RecordTrack> track = mTracks[j]; 695181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId = track->sessionId(); 695281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (ids.indexOfKey(sessionId) < 0) { 695381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ids.add(sessionId, true); 695481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 695581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 695681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return ids; 695781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 695881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 695981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() 696081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 696181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 696281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamIn *input = mInput; 696381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mInput = NULL; 696481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return input; 696581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 696681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 696781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// this method must always be called either with ThreadBase mLock held or inside the thread loop 696881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_stream_t* AudioFlinger::RecordThread::stream() const 696981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 697081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mInput == NULL) { 697181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NULL; 697281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 697381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return &mInput->stream->common; 697481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 697581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 697681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) 697781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 697881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // only one chain per input thread 697981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mEffectChains.size() != 0) { 6980aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent ALOGW("addEffectChain_l() already one chain %p on thread %p", chain.get(), this); 698181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return INVALID_OPERATION; 698281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 698381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); 6984aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent chain->setThread(this); 698581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setInBuffer(NULL); 698681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setOutBuffer(NULL); 698781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 698881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent checkSuspendOnAddEffectChain_l(chain); 698981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 69901b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent // make sure enabled pre processing effects state is communicated to the HAL as we 69911b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent // just moved them to a new input stream. 69921b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent chain->syncHalEffectsState(); 69931b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent 699481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains.add(chain); 699581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 699681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 699781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 699881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 699981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsize_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) 700081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 700181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); 700281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW_IF(mEffectChains.size() != 1, 700381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "removeEffectChain_l() %p invalid chain size %d on thread %p", 700481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain.get(), mEffectChains.size(), this); 700581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mEffectChains.size() == 1) { 700681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains.removeAt(0); 700781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 700881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 700981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 701081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 70111c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch, 70121c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_patch_handle_t *handle) 70131c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{ 70141c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status_t status = NO_ERROR; 7015054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 7016054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent // store new device and send to effects 7017054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent mInDevice = patch->sources[0].ext.device.type; 7018296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent mPatch = *patch; 7019054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 7020054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent mEffectChains[i]->setDevice_l(mInDevice); 7021054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 7022054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 7023054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent // disable AEC and NS if the device is a BT SCO headset supporting those 7024054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent // pre processings 7025054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (mTracks.size() > 0) { 7026054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent bool suspend = audio_is_bluetooth_sco_device(mInDevice) && 7027054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent mAudioFlinger->btNrecIsOff(); 7028054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 7029054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent sp<RecordTrack> track = mTracks[i]; 7030054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); 7031054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); 70321c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 7033054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 70341c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 7035054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent // store new source and send to effects 7036054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { 7037054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent mAudioSource = patch->sinks[0].ext.mix.usecase.source; 7038054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 7039054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent mEffectChains[i]->setAudioSource_l(mAudioSource); 70401c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 7041054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 70421c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 7043054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { 70441c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); 70451c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status = hwDevice->create_audio_patch(hwDevice, 70461c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent patch->num_sources, 70471c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent patch->sources, 70481c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent patch->num_sinks, 70491c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent patch->sinks, 70501c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent handle); 70511c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } else { 7052054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent char *address; 7053054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (strcmp(patch->sources[0].ext.device.address, "") != 0) { 7054054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent address = audio_device_address_to_parameter( 7055054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent patch->sources[0].ext.device.type, 7056054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent patch->sources[0].ext.device.address); 7057054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } else { 7058054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent address = (char *)calloc(1, 1); 7059054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 7060054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent AudioParameter param = AudioParameter(String8(address)); 7061054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent free(address); 7062054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 7063054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent (int)patch->sources[0].ext.device.type); 7064054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent param.addInt(String8(AUDIO_PARAMETER_STREAM_INPUT_SOURCE), 7065054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent (int)patch->sinks[0].ext.mix.usecase.source); 7066054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent status = mInput->stream->common.set_parameters(&mInput->stream->common, 7067054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent param.toString().string()); 7068054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent *handle = AUDIO_PATCH_HANDLE_NONE; 70691c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 7070054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 7071296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); 7072296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent 70731c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent return status; 70741c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent} 70751c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 70761c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle) 70771c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{ 70781c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status_t status = NO_ERROR; 7079054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 7080054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent mInDevice = AUDIO_DEVICE_NONE; 7081054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 70821c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { 70831c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); 70841c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status = hwDevice->release_audio_patch(hwDevice, handle); 70851c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } else { 7086054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent AudioParameter param; 7087054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0); 7088054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent status = mInput->stream->common.set_parameters(&mInput->stream->common, 7089054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent param.toString().string()); 70901c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 70911c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent return status; 70921c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent} 70931c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 709483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::RecordThread::addPatchRecord(const sp<PatchRecord>& record) 709583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{ 709683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent Mutex::Autolock _l(mLock); 709783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent mTracks.add(record); 709883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent} 709983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 710083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::RecordThread::deletePatchRecord(const sp<PatchRecord>& record) 710183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{ 710283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent Mutex::Autolock _l(mLock); 710383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent destroyTrack_l(record); 710483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent} 710583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 710683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::RecordThread::getAudioPortConfig(struct audio_port_config *config) 710783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{ 710883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent ThreadBase::getAudioPortConfig(config); 710983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->role = AUDIO_PORT_ROLE_SINK; 711083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->ext.mix.hw_module = mInput->audioHwDev->handle(); 711183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->ext.mix.usecase.source = mAudioSource; 711283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent} 71131c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 711463238efb0d674758902918e3cdaac322126484b7Glenn Kasten} // namespace android 7115