Threads.cpp revision f98ec8d0d42e6952c0a7cc5027935851073f7426
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", 3590f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten AUDIO_DEVICE_OUT_TELEPHONY_TX, "TELEPHONY_TX", 3600f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten AUDIO_DEVICE_NONE, "NONE", // must be last 3610f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten }, mappingsIn[] = { 3620f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten AUDIO_DEVICE_IN_BUILTIN_MIC, "BUILTIN_MIC", 3630f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten AUDIO_DEVICE_IN_WIRED_HEADSET, "WIRED_HEADSET", 3640f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten AUDIO_DEVICE_IN_VOICE_CALL, "VOICE_CALL", 3650f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten AUDIO_DEVICE_IN_REMOTE_SUBMIX, "REMOTE_SUBMIX", 3660f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten AUDIO_DEVICE_NONE, "NONE", // must be last 3670f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten }; 3680f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten String8 result; 3690f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten audio_devices_t allDevices = AUDIO_DEVICE_NONE; 3700f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten const mapping *entry; 3710f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten if (devices & AUDIO_DEVICE_BIT_IN) { 3720f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten devices &= ~AUDIO_DEVICE_BIT_IN; 3730f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten entry = mappingsIn; 3740f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } else { 3750f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten entry = mappingsOut; 3760f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 3770f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten for ( ; entry->mDevices != AUDIO_DEVICE_NONE; entry++) { 3780f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten allDevices = (audio_devices_t) (allDevices | entry->mDevices); 3790f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten if (devices & entry->mDevices) { 3800f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten if (!result.isEmpty()) { 3810f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten result.append("|"); 3820f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 3830f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten result.append(entry->mString); 3840f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 3850f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 3860f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten if (devices & ~allDevices) { 3870f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten if (!result.isEmpty()) { 3880f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten result.append("|"); 3890f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 3900f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten result.appendFormat("0x%X", devices & ~allDevices); 3910f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 3920f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten if (result.isEmpty()) { 3930f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten result.append(entry->mString); 3940f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 3950f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten return result; 3960f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten} 3970f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten 3980f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn KastenString8 inputFlagsToString(audio_input_flags_t flags) 3990f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten{ 4000f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten static const struct mapping { 4010f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten audio_input_flags_t mFlag; 4020f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten const char * mString; 4030f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } mappings[] = { 4040f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten AUDIO_INPUT_FLAG_FAST, "FAST", 4050f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten AUDIO_INPUT_FLAG_HW_HOTWORD, "HW_HOTWORD", 4060f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten AUDIO_INPUT_FLAG_NONE, "NONE", // must be last 4070f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten }; 4080f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten String8 result; 4090f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten audio_input_flags_t allFlags = AUDIO_INPUT_FLAG_NONE; 4100f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten const mapping *entry; 4110f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten for (entry = mappings; entry->mFlag != AUDIO_INPUT_FLAG_NONE; entry++) { 4120f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten allFlags = (audio_input_flags_t) (allFlags | entry->mFlag); 4130f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten if (flags & entry->mFlag) { 4140f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten if (!result.isEmpty()) { 4150f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten result.append("|"); 4160f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 4170f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten result.append(entry->mString); 4180f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 4190f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 4200f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten if (flags & ~allFlags) { 4210f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten if (!result.isEmpty()) { 4220f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten result.append("|"); 4230f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 4240f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten result.appendFormat("0x%X", flags & ~allFlags); 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 outputFlagsToString(audio_output_flags_t flags) 43397b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten{ 43497b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten static const struct mapping { 43597b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten audio_output_flags_t mFlag; 43697b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten const char * mString; 43797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten } mappings[] = { 43897b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten AUDIO_OUTPUT_FLAG_DIRECT, "DIRECT", 43997b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten AUDIO_OUTPUT_FLAG_PRIMARY, "PRIMARY", 44097b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten AUDIO_OUTPUT_FLAG_FAST, "FAST", 44197b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten AUDIO_OUTPUT_FLAG_DEEP_BUFFER, "DEEP_BUFFER", 442dfb0e115d827887e2f56a8877fe41b256d24360eGlenn Kasten AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD, "COMPRESS_OFFLOAD", 44397b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten AUDIO_OUTPUT_FLAG_NON_BLOCKING, "NON_BLOCKING", 44497b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten AUDIO_OUTPUT_FLAG_HW_AV_SYNC, "HW_AV_SYNC", 44597b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten AUDIO_OUTPUT_FLAG_NONE, "NONE", // must be last 44697b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten }; 44797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten String8 result; 44897b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten audio_output_flags_t allFlags = AUDIO_OUTPUT_FLAG_NONE; 44997b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten const mapping *entry; 45097b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten for (entry = mappings; entry->mFlag != AUDIO_OUTPUT_FLAG_NONE; entry++) { 45197b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten allFlags = (audio_output_flags_t) (allFlags | entry->mFlag); 45297b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten if (flags & entry->mFlag) { 45397b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten if (!result.isEmpty()) { 45497b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten result.append("|"); 45597b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten } 45697b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten result.append(entry->mString); 45797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten } 45897b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten } 45997b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten if (flags & ~allFlags) { 46097b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten if (!result.isEmpty()) { 46197b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten result.append("|"); 46297b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten } 46397b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten result.appendFormat("0x%X", flags & ~allFlags); 46497b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten } 46597b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten if (result.isEmpty()) { 46697b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten result.append(entry->mString); 46797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten } 46897b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten return result; 46997b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten} 47097b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten 4710f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kastenconst char *sourceToString(audio_source_t source) 4720f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten{ 4730f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten switch (source) { 4740f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten case AUDIO_SOURCE_DEFAULT: return "default"; 4750f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten case AUDIO_SOURCE_MIC: return "mic"; 4760f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten case AUDIO_SOURCE_VOICE_UPLINK: return "voice uplink"; 4770f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten case AUDIO_SOURCE_VOICE_DOWNLINK: return "voice downlink"; 4780f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten case AUDIO_SOURCE_VOICE_CALL: return "voice call"; 4790f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten case AUDIO_SOURCE_CAMCORDER: return "camcorder"; 4800f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten case AUDIO_SOURCE_VOICE_RECOGNITION: return "voice recognition"; 4810f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten case AUDIO_SOURCE_VOICE_COMMUNICATION: return "voice communication"; 4820f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten case AUDIO_SOURCE_REMOTE_SUBMIX: return "remote submix"; 4830f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten case AUDIO_SOURCE_FM_TUNER: return "FM tuner"; 4840f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten case AUDIO_SOURCE_HOTWORD: return "hotword"; 4850f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten default: return "unknown"; 4860f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten } 4870f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten} 4880f5b562737d6b5457aa83a4fdce9c6fb32584d9dGlenn Kasten 48981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, 49081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_devices_t outDevice, audio_devices_t inDevice, type_t type) 49181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent : Thread(false /*canCallJava*/), 49281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mType(type), 4939b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten mAudioFlinger(audioFlinger), 49470949c47fbae3f836d15f040551d7631be3ed7c2Glenn Kasten // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize 495deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten // are set by PlaybackThread::readOutputParameters_l() or 496deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten // RecordThread::readInputParameters_l() 497fd4779740ec3e9e865d5514464df26d015354388Eric Laurent //FIXME: mStandby should be true here. Is this some kind of hack? 49881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStandby(false), mOutDevice(outDevice), mInDevice(inDevice), 49981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id), 50081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mName will be set by concrete (non-virtual) subclass 50181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mDeathRecipient(new PMDeathRecipient(this)) 50281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 503296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent memset(&mPatch, 0, sizeof(struct audio_patch)); 50481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 50581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 50681784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::ThreadBase::~ThreadBase() 50781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 508c6ae3c8a261794fd4445e4e152d1ada074a3f92fGlenn Kasten // mConfigEvents should be empty, but just in case it isn't, free the memory it owns 509c6ae3c8a261794fd4445e4e152d1ada074a3f92fGlenn Kasten mConfigEvents.clear(); 510c6ae3c8a261794fd4445e4e152d1ada074a3f92fGlenn Kasten 51181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // do not lock the mutex in destructor 51281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock_l(); 51381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mPowerManager != 0) { 51406b46062d2f8bc82ca3061a23d197734ae51918bMarco Nelissen sp<IBinder> binder = IInterface::asBinder(mPowerManager); 51581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent binder->unlinkToDeath(mDeathRecipient); 51681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 51781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 51881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 519cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kastenstatus_t AudioFlinger::ThreadBase::readyToRun() 520cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten{ 521cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten status_t status = initCheck(); 522cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten if (status == NO_ERROR) { 523cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten ALOGI("AudioFlinger's thread %p ready to run", this); 524cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten } else { 525cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten ALOGE("No working audio driver found."); 526cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten } 527cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten return status; 528cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten} 529cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten 53081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::exit() 53181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 53281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("ThreadBase::exit"); 53381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // do any cleanup required for exit to succeed 53481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent preExit(); 53581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 53681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // This lock prevents the following race in thread (uniprocessor for illustration): 53781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if (!exitPending()) { 53881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // // context switch from here to exit() 53981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // // exit() calls requestExit(), what exitPending() observes 54081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // // exit() calls signal(), which is dropped since no waiters 54181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // // context switch back from exit() to here 54281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mWaitWorkCV.wait(...); 54381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // // now thread is hung 54481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // } 54581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AutoMutex lock(mLock); 54681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent requestExit(); 54781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitWorkCV.broadcast(); 54881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 54981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // When Thread::requestExitAndWait is made virtual and this method is renamed to 55081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" 55181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent requestExitAndWait(); 55281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 55381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 55481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) 55581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 55681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status; 55781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 55881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); 55981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 56081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent return sendSetParameterConfigEvent_l(keyValuePairs); 5621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent} 5631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 5641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// sendConfigEvent_l() must be called with ThreadBase::mLock held 5651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// Can temporarily release the lock if waiting for a reply from processConfigEvents_l(). 5661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentstatus_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event) 5671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent{ 5681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status_t status = NO_ERROR; 5691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 5701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mConfigEvents.add(event); 5711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent ALOGV("sendConfigEvent_l() num events %d event %d", mConfigEvents.size(), event->mType); 57281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitWorkCV.signal(); 5731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mLock.unlock(); 5741035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent { 5751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent Mutex::Autolock _l(event->mLock); 5761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent while (event->mWaitStatus) { 5771035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) { 5781035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent event->mStatus = TIMED_OUT; 5791035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent event->mWaitStatus = false; 5801035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 5811035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 5821035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = event->mStatus; 58381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 5841035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mLock.lock(); 58581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 58681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 58781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 58873e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurentvoid AudioFlinger::ThreadBase::sendIoConfigEvent(audio_io_config_event event) 58981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 59081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 59173e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent sendIoConfigEvent_l(event); 59281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 59381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 59481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// sendIoConfigEvent_l() must be called with ThreadBase::mLock held 59573e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurentvoid AudioFlinger::ThreadBase::sendIoConfigEvent_l(audio_io_config_event event) 59681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 59773e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event); 5981035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sendConfigEvent_l(configEvent); 59981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 60081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 60181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// sendPrioConfigEvent_l() must be called with ThreadBase::mLock held 60281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio) 60381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 6041035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio); 6051035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sendConfigEvent_l(configEvent); 60681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 60781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 6081035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held 6091035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentstatus_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair) 61081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 6111035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sp<ConfigEvent> configEvent = (ConfigEvent *)new SetParameterConfigEvent(keyValuePair); 6121035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent return sendConfigEvent_l(configEvent); 613f777331418a86cd9fd709af898ef24a69967aeb4Glenn Kasten} 614f777331418a86cd9fd709af898ef24a69967aeb4Glenn Kasten 6151c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::ThreadBase::sendCreateAudioPatchConfigEvent( 6161c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent const struct audio_patch *patch, 6171c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_patch_handle_t *handle) 6181c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{ 6191c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent Mutex::Autolock _l(mLock); 6201c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle); 6211c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status_t status = sendConfigEvent_l(configEvent); 6221c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent if (status == NO_ERROR) { 6231c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent CreateAudioPatchConfigEventData *data = 6241c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent (CreateAudioPatchConfigEventData *)configEvent->mData.get(); 6251c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent *handle = data->mHandle; 6261c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 6271c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent return status; 6281c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent} 6291c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 6301c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::ThreadBase::sendReleaseAudioPatchConfigEvent( 6311c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent const audio_patch_handle_t handle) 6321c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{ 6331c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent Mutex::Autolock _l(mLock); 6341c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle); 6351c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent return sendConfigEvent_l(configEvent); 6361c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent} 6371c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 6381c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 6392cfbf88b89854f30b295e8ae26a031edb8d712f8Glenn Kasten// post condition: mConfigEvents.isEmpty() 640021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurentvoid AudioFlinger::ThreadBase::processConfigEvents_l() 641f777331418a86cd9fd709af898ef24a69967aeb4Glenn Kasten{ 6421035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent bool configChanged = false; 6431035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 64481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent while (!mConfigEvents.isEmpty()) { 6451035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent ALOGV("processConfigEvents_l() remaining events %d", mConfigEvents.size()); 6461035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sp<ConfigEvent> event = mConfigEvents[0]; 64781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mConfigEvents.removeAt(0); 6481035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent switch (event->mType) { 6493468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten case CFG_EVENT_PRIO: { 6501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get(); 6511035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // FIXME Need to understand why this has to be done asynchronously 6521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent int err = requestPriority(data->mPid, data->mTid, data->mPrio, 6533468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten true /*asynchronous*/); 6543468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten if (err != 0) { 6553468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", 6561035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent data->mPrio, data->mPid, data->mTid, err); 6573468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten } 6583468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten } break; 6593468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten case CFG_EVENT_IO: { 6601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent IoConfigEventData *data = (IoConfigEventData *)event->mData.get(); 66173e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent ioConfigChanged(data->mEvent); 6621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } break; 6631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent case CFG_EVENT_SET_PARAMETER: { 6641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get(); 6651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) { 6661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent configChanged = true; 667d5418eb594435c958d6c37fa9938161a0112adbdGlenn Kasten } 6683468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten } break; 6691c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent case CFG_EVENT_CREATE_AUDIO_PATCH: { 6701c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent CreateAudioPatchConfigEventData *data = 6711c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent (CreateAudioPatchConfigEventData *)event->mData.get(); 6721c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle); 6731c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } break; 6741c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent case CFG_EVENT_RELEASE_AUDIO_PATCH: { 6751c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent ReleaseAudioPatchConfigEventData *data = 6761c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent (ReleaseAudioPatchConfigEventData *)event->mData.get(); 6771c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent event->mStatus = releaseAudioPatch_l(data->mHandle); 6781c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } break; 6793468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten default: 6801035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType); 6813468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten break; 68281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 6831035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent { 6841035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent Mutex::Autolock _l(event->mLock); 6851035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (event->mWaitStatus) { 6861035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent event->mWaitStatus = false; 6871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent event->mCond.signal(); 6881035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 6891035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 6901035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this); 6911035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 6921035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 6931035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (configChanged) { 6941035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent cacheParameters_l(); 69581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 69681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 69781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 698b220884bf3129253cc5bc8d030bc475411ea4911Marco NelissenString8 channelMaskToString(audio_channel_mask_t mask, bool output) { 699b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen String8 s; 700f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung const audio_channel_representation_t representation = audio_channel_mask_get_representation(mask); 701f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung 702f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung switch (representation) { 703f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung case AUDIO_CHANNEL_REPRESENTATION_POSITION: { 704f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (output) { 705f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, "); 706f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, "); 707f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, "); 708f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, "); 709f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, "); 710f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, "); 711f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, "); 712f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, "); 713f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, "); 714f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, "); 715f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, "); 716f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,"); 717f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, "); 718f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, "); 719f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, "); 720f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, "); 721f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " ); 722f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " ); 723f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown, "); 724f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung } else { 725f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, "); 726f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, "); 727f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, "); 728f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, "); 729f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, "); 730f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, "); 731f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, "); 732f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, "); 733f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, "); 734f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, "); 735f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, "); 736f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, "); 737f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, "); 738f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, "); 739f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown, "); 740f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung } 741f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung const int len = s.length(); 742f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung if (len > 2) { 743f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung char *str = s.lockBuffer(len); // needed? 744f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung s.unlockBuffer(len - 2); // remove trailing ", " 745f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung } 746f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung return s; 747b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 748f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung case AUDIO_CHANNEL_REPRESENTATION_INDEX: 749f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung s.appendFormat("index mask, bits:%#x", audio_channel_mask_get_bits(mask)); 750f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung return s; 751f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung default: 752f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung s.appendFormat("unknown mask, representation:%d bits:%#x", 753f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung representation, audio_channel_mask_get_bits(mask)); 754f98ec8d0d42e6952c0a7cc5027935851073f7426Andy Hung return s; 755b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 756b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen} 757b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen 7580f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args __unused) 75981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 76081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const size_t SIZE = 256; 76181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char buffer[SIZE]; 76281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent String8 result; 76381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 76481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool locked = AudioFlinger::dumpTryLock(mLock); 76581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!locked) { 76697b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten dprintf(fd, "thread %p may be deadlocked\n", this); 767b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 768b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen 7690b89bc0d285b8fd4798df1ff0ba9f93851a3bd48Glenn Kasten dprintf(fd, " Thread name: %s\n", mThreadName); 77087cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " I/O handle: %d\n", mId); 77187cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " TID: %d\n", getTid()); 77287cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Standby: %s\n", mStandby ? "yes" : "no"); 77397b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten dprintf(fd, " Sample rate: %u Hz\n", mSampleRate); 77487cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " HAL frame count: %zu\n", mFrameCount); 77597b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten dprintf(fd, " HAL format: 0x%x (%s)\n", mHALFormat, formatToString(mHALFormat)); 77687cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " HAL buffer size: %u bytes\n", mBufferSize); 77797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten dprintf(fd, " Channel count: %u\n", mChannelCount); 77897b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten dprintf(fd, " Channel mask: 0x%08x (%s)\n", mChannelMask, 779b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen channelMaskToString(mChannelMask, mType != RECORD).string()); 78097b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten dprintf(fd, " Format: 0x%x (%s)\n", mFormat, formatToString(mFormat)); 78197b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten dprintf(fd, " Frame size: %zu bytes\n", mFrameSize); 78287cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Pending config events:"); 783b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numConfig = mConfigEvents.size(); 784b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (numConfig) { 785b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen for (size_t i = 0; i < numConfig; i++) { 786b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen mConfigEvents[i]->dump(buffer, SIZE); 78787cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, "\n %s", buffer); 788b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 78987cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, "\n"); 790b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } else { 79187cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " none\n"); 79281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 7930b89bc0d285b8fd4798df1ff0ba9f93851a3bd48Glenn Kasten dprintf(fd, " Output device: %#x (%s)\n", mOutDevice, devicesToString(mOutDevice).string()); 7940b89bc0d285b8fd4798df1ff0ba9f93851a3bd48Glenn Kasten dprintf(fd, " Input device: %#x (%s)\n", mInDevice, devicesToString(mInDevice).string()); 7950b89bc0d285b8fd4798df1ff0ba9f93851a3bd48Glenn Kasten dprintf(fd, " Audio source: %d (%s)\n", mAudioSource, sourceToString(mAudioSource)); 79681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 79781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (locked) { 79881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mLock.unlock(); 79981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 80081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 80181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 80281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) 80381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 80481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const size_t SIZE = 256; 80581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char buffer[SIZE]; 80681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent String8 result; 80781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 808b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numEffectChains = mEffectChains.size(); 8091d6fa7af1288b550faabe4ec2cf98684236723dbNarayan Kamath snprintf(buffer, SIZE, " %zu Effect Chains\n", numEffectChains); 81081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent write(fd, buffer, strlen(buffer)); 81181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 812b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen for (size_t i = 0; i < numEffectChains; ++i) { 81381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = mEffectChains[i]; 81481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 81581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->dump(fd, args); 81681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 81781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 81881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 81981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 820e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissenvoid AudioFlinger::ThreadBase::acquireWakeLock(int uid) 82181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 82281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 823e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen acquireWakeLock_l(uid); 82481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 82581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 826014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan KamathString16 AudioFlinger::ThreadBase::getWakeLockTag() 827014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath{ 828014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath switch (mType) { 829bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten case MIXER: 830bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten return String16("AudioMix"); 831bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten case DIRECT: 832bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten return String16("AudioDirectOut"); 833bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten case DUPLICATING: 834bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten return String16("AudioDup"); 835bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten case RECORD: 836bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten return String16("AudioIn"); 837bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten case OFFLOAD: 838bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten return String16("AudioOffload"); 839bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten default: 840bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten ALOG_ASSERT(false); 841bcb1486d052e329ae4790d93055d1c51017286c3Glenn Kasten return String16("AudioUnknown"); 842014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath } 843014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath} 844014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath 845e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissenvoid AudioFlinger::ThreadBase::acquireWakeLock_l(int uid) 84681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 847462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen getPowerManager_l(); 84881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mPowerManager != 0) { 84981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<IBinder> binder = new BBinder(); 850e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen status_t status; 851e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen if (uid >= 0) { 852547789d25dc6bd6561553bcf6b384fb0d4fee834Eric Laurent status = mPowerManager->acquireWakeLockWithUid(POWERMANAGER_PARTIAL_WAKE_LOCK, 853e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen binder, 854014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath getWakeLockTag(), 855e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen String16("media"), 8563abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten uid, 8573abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten true /* FIXME force oneway contrary to .aidl */); 858e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen } else { 859547789d25dc6bd6561553bcf6b384fb0d4fee834Eric Laurent status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, 860e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen binder, 861014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath getWakeLockTag(), 8623abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten String16("media"), 8633abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten true /* FIXME force oneway contrary to .aidl */); 864e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen } 86581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (status == NO_ERROR) { 86681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWakeLockToken = binder; 86781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 868d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status); 86981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 87081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 87181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 87281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::releaseWakeLock() 87381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 87481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 87581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock_l(); 87681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 87781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 87881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::releaseWakeLock_l() 87981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 88081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mWakeLockToken != 0) { 881d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten ALOGV("releaseWakeLock_l() %s", mThreadName); 88281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mPowerManager != 0) { 8833abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten mPowerManager->releaseWakeLock(mWakeLockToken, 0, 8843abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten true /* FIXME force oneway contrary to .aidl */); 88581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 88681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWakeLockToken.clear(); 88781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 88881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 88981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 890462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissenvoid AudioFlinger::ThreadBase::updateWakeLockUids(const SortedVector<int> &uids) { 891462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen Mutex::Autolock _l(mLock); 892462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen updateWakeLockUids_l(uids); 893462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen} 894462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 895462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissenvoid AudioFlinger::ThreadBase::getPowerManager_l() { 896462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 897462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen if (mPowerManager == 0) { 898462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen // use checkService() to avoid blocking if power service is not up yet 899462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen sp<IBinder> binder = 900462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen defaultServiceManager()->checkService(String16("power")); 901462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen if (binder == 0) { 902d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten ALOGW("Thread %s cannot connect to the power manager service", mThreadName); 903462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } else { 904462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mPowerManager = interface_cast<IPowerManager>(binder); 905462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen binder->linkToDeath(mDeathRecipient); 906462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } 907462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } 908462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen} 909462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 910462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissenvoid AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<int> &uids) { 911462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 912462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen getPowerManager_l(); 913462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen if (mWakeLockToken == NULL) { 914462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen ALOGE("no wake lock to update!"); 915462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen return; 916462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } 917462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen if (mPowerManager != 0) { 918462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen sp<IBinder> binder = new BBinder(); 919462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen status_t status; 9203abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten status = mPowerManager->updateWakeLockUids(mWakeLockToken, uids.size(), uids.array(), 9213abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten true /* FIXME force oneway contrary to .aidl */); 922d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status); 923462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } 924462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen} 925462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 92681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::clearPowerManager() 92781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 92881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 92981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock_l(); 93081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPowerManager.clear(); 93181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 93281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 9330f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) 93481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 93581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<ThreadBase> thread = mThread.promote(); 93681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread != 0) { 93781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread->clearPowerManager(); 93881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 93981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("power manager service died !!!"); 94081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 94181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 94281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::setEffectSuspended( 94381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const effect_uuid_t *type, bool suspend, int sessionId) 94481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 94581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 94681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent setEffectSuspended_l(type, suspend, sessionId); 94781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 94881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 94981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::setEffectSuspended_l( 95081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const effect_uuid_t *type, bool suspend, int sessionId) 95181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 95281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(sessionId); 95381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 95481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (type != NULL) { 95581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setEffectSuspended_l(type, suspend); 95681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 95781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setEffectSuspendedAll_l(suspend); 95881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 95981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 96081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 96181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent updateSuspendedSessions_l(type, suspend, sessionId); 96281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 96381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 96481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) 96581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 96681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); 96781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (index < 0) { 96881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 96981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 97081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 97181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = 97281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSuspendedSessions.valueAt(index); 97381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 97481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < sessionEffects.size(); i++) { 97581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<SuspendedSessionDesc> desc = sessionEffects.valueAt(i); 97681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (int j = 0; j < desc->mRefCount; j++) { 97781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { 97881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setEffectSuspendedAll_l(true); 97981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 98081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", 98181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc->mType.timeLow); 98281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setEffectSuspended_l(&desc->mType, true); 98381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 98481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 98581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 98681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 98781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 98881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, 98981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool suspend, 99081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId) 99181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 99281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ssize_t index = mSuspendedSessions.indexOfKey(sessionId); 99381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 99481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; 99581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 99681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (suspend) { 99781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (index >= 0) { 99881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionEffects = mSuspendedSessions.valueAt(index); 99981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 100081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSuspendedSessions.add(sessionId, sessionEffects); 100181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 100281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 100381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (index < 0) { 100481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 100581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 100681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionEffects = mSuspendedSessions.valueAt(index); 100781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 100881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 100981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 101081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int key = EffectChain::kKeyForSuspendAll; 101181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (type != NULL) { 101281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent key = type->timeLow; 101381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 101481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent index = sessionEffects.indexOfKey(key); 101581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 101681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<SuspendedSessionDesc> desc; 101781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (suspend) { 101881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (index >= 0) { 101981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc = sessionEffects.valueAt(index); 102081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 102181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc = new SuspendedSessionDesc(); 102281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (type != NULL) { 102381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc->mType = *type; 102481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 102581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionEffects.add(key, desc); 102681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); 102781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 102881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc->mRefCount++; 102981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 103081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (index < 0) { 103181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 103281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 103381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc = sessionEffects.valueAt(index); 103481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (--desc->mRefCount == 0) { 103581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); 103681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionEffects.removeItemsAt(index); 103781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionEffects.isEmpty()) { 103881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("updateSuspendedSessions_l() restore removing session %d", 103981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionId); 104081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSuspendedSessions.removeItem(sessionId); 104181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 104281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 104381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 104481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!sessionEffects.isEmpty()) { 104581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); 104681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 104781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 104881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 104981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, 105081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool enabled, 105181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId) 105281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 105381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 105481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); 105581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 105681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 105781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, 105881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool enabled, 105981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId) 106081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 106181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mType != RECORD) { 106281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on 106381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // another session. This gives the priority to well behaved effect control panels 106481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // and applications not using global effects. 106581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect 106681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // global effects 106781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) { 106881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); 106981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 107081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 107181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 107281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(sessionId); 107381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 107481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->checkSuspendOnEffectEnabled(effect, enabled); 107581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 107681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 107781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 107881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held 107981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( 108081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<AudioFlinger::Client>& client, 108181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<IEffectClient>& effectClient, 108281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int32_t priority, 108381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId, 108481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect_descriptor_t *desc, 108581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int *enabled, 10869156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten status_t *status) 108781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 108881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectModule> effect; 108981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectHandle> handle; 109081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t lStatus; 109181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain; 109281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool chainCreated = false; 109381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool effectCreated = false; 109481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool effectRegistered = false; 109581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 109681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = initCheck(); 109781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR) { 109881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("createEffect_l() Audio driver not initialized."); 109981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 110081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 110181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 110298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Reject any effect on Direct output threads for now, since the format of 110398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo). 110498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mType == DIRECT) { 110598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung ALOGW("createEffect_l() Cannot add effect %s on Direct output type thread %s", 1106d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten desc->name, mThreadName); 110798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung lStatus = BAD_VALUE; 110898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung goto Exit; 110998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 111098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung 1111389cfdbb9a92a438a0d7710321c2964c7ad55ecaAndy Hung // Reject any effect on mixer or duplicating multichannel sinks. 11129a59276fb465e492138e0576523b54079671e8f4Andy Hung // TODO: fix both format and multichannel issues with effects. 1113389cfdbb9a92a438a0d7710321c2964c7ad55ecaAndy Hung if ((mType == MIXER || mType == DUPLICATING) && mChannelCount != FCC_2) { 1114389cfdbb9a92a438a0d7710321c2964c7ad55ecaAndy Hung ALOGW("createEffect_l() Cannot add effect %s for multichannel(%d) %s threads", 1115389cfdbb9a92a438a0d7710321c2964c7ad55ecaAndy Hung desc->name, mChannelCount, mType == MIXER ? "MIXER" : "DUPLICATING"); 11169a59276fb465e492138e0576523b54079671e8f4Andy Hung lStatus = BAD_VALUE; 11179a59276fb465e492138e0576523b54079671e8f4Andy Hung goto Exit; 11189a59276fb465e492138e0576523b54079671e8f4Andy Hung } 11199a59276fb465e492138e0576523b54079671e8f4Andy Hung 11205baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent // Allow global effects only on offloaded and mixer threads 11215baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { 11225baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent switch (mType) { 11235baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent case MIXER: 11245baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent case OFFLOAD: 11255baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent break; 11265baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent case DIRECT: 11275baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent case DUPLICATING: 11285baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent case RECORD: 11295baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent default: 1130d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten ALOGW("createEffect_l() Cannot add global effect %s on thread %s", 1131d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten desc->name, mThreadName); 11325baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent lStatus = BAD_VALUE; 11335baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent goto Exit; 11345baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent } 113581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 11365baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent 113781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Only Pre processor effects are allowed on input threads and only on input threads 113881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { 113981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d", 114081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc->name, desc->flags, mType); 114181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 114281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 114381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 114481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 114581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); 114681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 114781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // scope for mLock 114881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 114981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 115081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // check for existing effect chain with the requested audio session 115181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain = getEffectChain_l(sessionId); 115281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain == 0) { 115381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create a new chain for this session 115481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("createEffect_l() new effect chain for session %d", sessionId); 115581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain = new EffectChain(this, sessionId); 115681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent addEffectChain_l(chain); 115781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setStrategy(getStrategyForSession_l(sessionId)); 115881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chainCreated = true; 115981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 116081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect = chain->getEffectFromDesc_l(desc); 116181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 116281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 116381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); 116481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 116581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (effect == 0) { 116681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int id = mAudioFlinger->nextUniqueId(); 116781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Check CPU and memory usage 116881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); 116981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR) { 117081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 117181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 117281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effectRegistered = true; 117381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create a new effect module if none present in the chain 117481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect = new EffectModule(this, chain, desc, id, sessionId); 117581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = effect->status(); 117681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR) { 117781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 117881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 11795baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent effect->setOffloaded(mType == OFFLOAD, mId); 11805baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent 118181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = chain->addEffect_l(effect); 118281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR) { 118381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 118481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 118581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effectCreated = true; 118681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 118781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setDevice(mOutDevice); 118881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setDevice(mInDevice); 118981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setMode(mAudioFlinger->getMode()); 119081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setAudioSource(mAudioSource); 119181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 119281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create effect handle and connect it to effect module 119381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent handle = new EffectHandle(effect, client, effectClient, priority); 1194e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten lStatus = handle->initCheck(); 1195e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten if (lStatus == OK) { 1196e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten lStatus = effect->addHandle(handle.get()); 1197e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten } 119881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (enabled != NULL) { 119981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *enabled = (int)effect->isEnabled(); 120081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 120181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 120281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 120381784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit: 120481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { 120581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 120681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (effectCreated) { 120781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->removeEffect_l(effect); 120881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 120981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (effectRegistered) { 121081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSystem::unregisterEffect(effect->id()); 121181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 121281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chainCreated) { 121381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent removeEffectChain_l(chain); 121481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 121581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent handle.clear(); 121681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 121781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 12189156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten *status = lStatus; 121981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return handle; 122081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 122181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 122281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(int sessionId, int effectId) 122381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 122481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 122581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return getEffect_l(sessionId, effectId); 122681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 122781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 122881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId) 122981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 123081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(sessionId); 123181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; 123281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 123381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 123481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and 123581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// PlaybackThread::mLock held 123681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) 123781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 123881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // check for existing effect chain with the requested audio session 123981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId = effect->sessionId(); 124081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(sessionId); 124181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool chainCreated = false; 124281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 12435baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(), 12445baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %x", 12455baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent this, effect->desc().name, effect->desc().flags); 12465baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent 124781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain == 0) { 124881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create a new chain for this session 124981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffect_l() new effect chain for session %d", sessionId); 125081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain = new EffectChain(this, sessionId); 125181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent addEffectChain_l(chain); 125281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setStrategy(getStrategyForSession_l(sessionId)); 125381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chainCreated = true; 125481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 125581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); 125681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 125781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain->getEffectFromId_l(effect->id()) != 0) { 125881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("addEffect_l() %p effect %s already present in chain %p", 125981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent this, effect->desc().name, chain.get()); 126081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 126181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 126281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 12635baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent effect->setOffloaded(mType == OFFLOAD, mId); 12645baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent 126581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status = chain->addEffect_l(effect); 126681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (status != NO_ERROR) { 126781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chainCreated) { 126881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent removeEffectChain_l(chain); 126981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 127081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 127181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 127281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 127381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setDevice(mOutDevice); 127481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setDevice(mInDevice); 127581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setMode(mAudioFlinger->getMode()); 127681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->setAudioSource(mAudioSource); 127781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 127881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 127981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 128081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { 128181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 128281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("removeEffect_l() %p effect %p", this, effect.get()); 128381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect_descriptor_t desc = effect->desc(); 128481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 128581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent detachAuxEffect_l(effect->id()); 128681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 128781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 128881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = effect->chain().promote(); 128981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 129081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // remove effect chain if removing last effect 129181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain->removeEffect_l(effect) == 0) { 129281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent removeEffectChain_l(chain); 129381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 129481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 129581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); 129681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 129781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 129881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 129981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::lockEffectChains_l( 130081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<AudioFlinger::EffectChain> >& effectChains) 130181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 130281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effectChains = mEffectChains; 130381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 130481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains[i]->lock(); 130581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 130681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 130781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 130881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::unlockEffectChains( 130981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const Vector< sp<AudioFlinger::EffectChain> >& effectChains) 131081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 131181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < effectChains.size(); i++) { 131281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effectChains[i]->unlock(); 131381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 131481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 131581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 131681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId) 131781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 131881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 131981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return getEffectChain_l(sessionId); 132081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 132181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 132281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) const 132381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 132481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t size = mEffectChains.size(); 132581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < size; i++) { 132681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mEffectChains[i]->sessionId() == sessionId) { 132781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mEffectChains[i]; 132881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 132981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 133081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 133181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 133281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 133381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::setMode(audio_mode_t mode) 133481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 133581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 133681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t size = mEffectChains.size(); 133781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < size; i++) { 133881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains[i]->setMode_l(mode); 133981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 134081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 134181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 134283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::ThreadBase::getAudioPortConfig(struct audio_port_config *config) 134383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{ 134483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->type = AUDIO_PORT_TYPE_MIX; 134583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->ext.mix.handle = mId; 134683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->sample_rate = mSampleRate; 134783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->format = mFormat; 134883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->channel_mask = mChannelMask; 134983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| 135083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent AUDIO_PORT_CONFIG_FORMAT; 135183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent} 135283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 135383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 135481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 135581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Playback 135681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 135781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 135881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, 135981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamOut* output, 136081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t id, 136181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_devices_t device, 136281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent type_t type) 136381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent : ThreadBase(audioFlinger, id, device, AUDIO_DEVICE_NONE, type), 13642098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung mNormalFrameCount(0), mSinkBuffer(NULL), 13656146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision), 136669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBuffer(NULL), 136769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferSize(0), 136869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferFormat(AUDIO_FORMAT_INVALID), 136969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferValid(false), 13706146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision), 137198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBuffer(NULL), 137298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferSize(0), 137398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferFormat(AUDIO_FORMAT_INVALID), 137498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferValid(false), 1375c1fac191069774c7bfcb062edbb821ea56e7dbc0Glenn Kasten mSuspended(0), mBytesWritten(0), 1376462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mActiveTracksGeneration(0), 137781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mStreamTypes[] initialized in constructor body 137881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutput(output), 137981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), 138081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMixerStatus(MIXER_IDLE), 138181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMixerStatusIgnoringFastTracks(MIXER_IDLE), 138281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent standbyDelay(AudioFlinger::mStandbyTimeInNsecs), 1383bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining(0), 1384bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mCurrentWriteLength(0), 1385bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mUseAsyncWrite(false), 13863b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence(0), 13873b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence(0), 1388ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent mSignalPending(false), 138981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mScreenState(AudioFlinger::mScreenState), 139081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // index 0 is reserved for normal mixer's submix 1391bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mFastTrackAvailMask(((1 << FastMixerState::kMaxFastTracks) - 1) & ~1), 1392d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent mHwSupportsPause(false), mHwPaused(false), mFlushPending(false), 1393bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten // mLatchD, mLatchQ, 1394bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mLatchDValid(false), mLatchQValid(false) 139581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 1396d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id); 1397d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); 139881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 139981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Assumes constructor is called by AudioFlinger with it's mLock held, but 140081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it would be safer to explicitly pass initial masterVolume/masterMute as 140181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // parameter. 140281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // 140381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If the HAL we are using has support for master volume or master mute, 140481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // then do not attenuate or mute during mixing (just leave the volume at 1.0 140581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // and the mute set to false). 140681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterVolume = audioFlinger->masterVolume_l(); 140781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterMute = audioFlinger->masterMute_l(); 140881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutput && mOutput->audioHwDev) { 140981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutput->audioHwDev->canSetMasterVolume()) { 141081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterVolume = 1.0; 141181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 141281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 141381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutput->audioHwDev->canSetMasterMute()) { 141481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterMute = false; 141581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 141681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 141781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1418deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten readOutputParameters_l(); 141981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1420223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent // ++ operator does not compile 142166e4635cb09fadcaccf912f37c387396c428378aGlenn Kasten for (audio_stream_type_t stream = AUDIO_STREAM_MIN; stream < AUDIO_STREAM_CNT; 142281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent stream = (audio_stream_type_t) (stream + 1)) { 142381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream); 142481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); 142581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 142681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 142781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 142881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread::~PlaybackThread() 142981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 14309e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten mAudioFlinger->unregisterWriter(mNBLogWriter); 1431010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung free(mSinkBuffer); 143269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung free(mMixerBuffer); 143398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung free(mEffectBuffer); 143481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 143581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 143681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) 143781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 143881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpInternals(fd, args); 143981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpTracks(fd, args); 144081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpEffectChains(fd, args); 144181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 144281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 14430f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args __unused) 144481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 144581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const size_t SIZE = 256; 144681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char buffer[SIZE]; 144781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent String8 result; 144881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1449b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.appendFormat(" Stream volumes in dB: "); 145081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { 145181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const stream_type_t *st = &mStreamTypes[i]; 145281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (i > 0) { 145381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result.appendFormat(", "); 145481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 145581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); 145681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (st->mute) { 145781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result.append("M"); 145881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 145981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 146081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result.append("\n"); 146181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent write(fd, result.string(), result.length()); 146281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result.clear(); 146381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1464b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen // These values are "raw"; they will wrap around. See prepareTracks_l() for a better way. 1465b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen FastTrackUnderruns underruns = getFastTrackUnderruns(0); 146687cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Normal mixer raw underrun counters: partial=%u empty=%u\n", 1467b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); 1468b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen 1469b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numtracks = mTracks.size(); 1470b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numactive = mActiveTracks.size(); 147187cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " %d Tracks", numtracks); 1472b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numactiveseen = 0; 1473b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (numtracks) { 147487cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " of which %d are active\n", numactive); 1475b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen Track::appendDumpHeader(result); 1476b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen for (size_t i = 0; i < numtracks; ++i) { 1477b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen sp<Track> track = mTracks[i]; 1478b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (track != 0) { 1479b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen bool active = mActiveTracks.indexOf(track) >= 0; 1480b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (active) { 1481b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen numactiveseen++; 1482b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 1483b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen track->dump(buffer, SIZE, active); 1484b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append(buffer); 1485b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 148681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1487b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } else { 1488b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append("\n"); 148981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1490b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (numactiveseen != numactive) { 1491b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen // some tracks in the active list were not in the tracks list 1492b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen snprintf(buffer, SIZE, " The following tracks are in the active list but" 1493b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen " not in the track list\n"); 1494b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append(buffer); 1495b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen Track::appendDumpHeader(result); 1496b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen for (size_t i = 0; i < numactive; ++i) { 1497b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen sp<Track> track = mActiveTracks[i].promote(); 1498b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (track != 0 && mTracks.indexOf(track) < 0) { 1499b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen track->dump(buffer, SIZE, true); 1500b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append(buffer); 1501b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 150281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 150381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1504b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen 150581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent write(fd, result.string(), result.size()); 150681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 150781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 150881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) 150981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 151097b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten dprintf(fd, "\nOutput thread %p type %d (%s):\n", this, type(), threadTypeToString(type())); 151144182c206f7c5584ef2cf504da6be98fab665dbfGlenn Kasten 151244182c206f7c5584ef2cf504da6be98fab665dbfGlenn Kasten dumpBase(fd, args); 151344182c206f7c5584ef2cf504da6be98fab665dbfGlenn Kasten 151487cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Normal frame count: %zu\n", mNormalFrameCount); 151587cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime)); 151687cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Total writes: %d\n", mNumWrites); 151787cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Delayed writes: %d\n", mNumDelayedWrites); 151887cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Blocked in write: %s\n", mInWrite ? "yes" : "no"); 151987cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Suspend count: %d\n", mSuspended); 152087cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Sink buffer : %p\n", mSinkBuffer); 152187cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Mixer buffer: %p\n", mMixerBuffer); 152287cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Effect buffer: %p\n", mEffectBuffer); 152387cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " Fast track availMask=%#x\n", mFastTrackAvailMask); 152497b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten AudioStreamOut *output = mOutput; 152597b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten audio_output_flags_t flags = output != NULL ? output->flags : AUDIO_OUTPUT_FLAG_NONE; 152697b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten String8 flagsAsString = outputFlagsToString(flags); 152797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten dprintf(fd, " AudioStreamOut: %p flags %#x (%s)\n", output, flags, flagsAsString.string()); 152881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 152981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 153081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Thread virtuals 153181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 153281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::onFirstRef() 153381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 1534d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); 153581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 153681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 153781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ThreadBase virtuals 153881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::preExit() 153981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 154081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV(" preExit()"); 154181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME this is using hard-coded strings but in the future, this functionality will be 154281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // converted to use audio HAL extensions required to support tunneling 154381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutput->stream->common.set_parameters(&mOutput->stream->common, "exiting=1"); 154481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 154581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 154681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held 154781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( 154881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<AudioFlinger::Client>& client, 154981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_stream_type_t streamType, 155081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t sampleRate, 155181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_format_t format, 155281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_channel_mask_t channelMask, 155374935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten size_t *pFrameCount, 155481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<IMemory>& sharedBuffer, 155581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId, 155681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent IAudioFlinger::track_flags_t *flags, 155781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t tid, 1558462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen int uid, 155981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t *status) 156081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 156174935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten size_t frameCount = *pFrameCount; 156281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track; 156381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t lStatus; 156481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 156581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool isTimed = (*flags & IAudioFlinger::TRACK_TIMED) != 0; 156681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 156781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // client expresses a preference for FAST, but we get the final say 156881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (*flags & IAudioFlinger::TRACK_FAST) { 156981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ( 157081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // not timed 157181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (!isTimed) && 157281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // either of these use cases: 157381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ( 157481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // use case 1: shared buffer with any frame count 157581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ( 157681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (sharedBuffer != 0) 157781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ) || 15781dfe2f9c2d03fc8d0ed0cdfe0b9fb894bc0bcc11Glenn Kasten // use case 2: frame count is default or at least as large as HAL 157981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ( 15801dfe2f9c2d03fc8d0ed0cdfe0b9fb894bc0bcc11Glenn Kasten // we formerly checked for a callback handler (non-0 tid), 15811dfe2f9c2d03fc8d0ed0cdfe0b9fb894bc0bcc11Glenn Kasten // but that is no longer required for TRANSFER_OBTAIN mode 158281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ((frameCount == 0) || 1583b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten (frameCount >= mFrameCount)) 158481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ) 158581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ) && 158681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // PCM data 158781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_is_linear_pcm(format) && 15889a59276fb465e492138e0576523b54079671e8f4Andy Hung // identical channel mask to sink, or mono in and stereo sink 15899a59276fb465e492138e0576523b54079671e8f4Andy Hung (channelMask == mChannelMask || 15909a59276fb465e492138e0576523b54079671e8f4Andy Hung (channelMask == AUDIO_CHANNEL_OUT_MONO && 15919a59276fb465e492138e0576523b54079671e8f4Andy Hung mChannelMask == AUDIO_CHANNEL_OUT_STEREO)) && 159281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // hardware sample rate 159381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (sampleRate == mSampleRate) && 159481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // normal mixer has an associated fast mixer 159581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent hasFastMixer() && 159681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // there are sufficient fast track slots available 159781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (mFastTrackAvailMask != 0) 159881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME test that MixerThread for this fast track has a capable output HAL 159981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME add a permission test also? 160081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ) { 160181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if frameCount not specified, then it defaults to fast mixer (HAL) frame count 160281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (frameCount == 0) { 16030349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten // read the fast track multiplier property the first time it is needed 16040349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit); 16050349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten if (ok != 0) { 16060349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten ALOGE("%s pthread_once failed: %d", __func__, ok); 16070349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten } 16080349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten frameCount = mFrameCount * sFastTrackMultiplier; 160981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 161081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%d mFrameCount=%d", 161181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent frameCount, mFrameCount); 161281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 161381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: isTimed=%d sharedBuffer=%p frameCount=%d " 16146146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung "mFrameCount=%d format=%#x mFormat=%#x isLinear=%d channelMask=%#x " 16156146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung "sampleRate=%u mSampleRate=%u " 161681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", 16176146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung isTimed, sharedBuffer.get(), frameCount, mFrameCount, format, mFormat, 161881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_is_linear_pcm(format), 161981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); 162081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *flags &= ~IAudioFlinger::TRACK_FAST; 16210e48d25606c82def035ad10a5b3923767a765cddAndy Hung } 16220e48d25606c82def035ad10a5b3923767a765cddAndy Hung } 16230e48d25606c82def035ad10a5b3923767a765cddAndy Hung // For normal PCM streaming tracks, update minimum frame count. 16240e48d25606c82def035ad10a5b3923767a765cddAndy Hung // For compatibility with AudioTrack calculation, buffer depth is forced 16250e48d25606c82def035ad10a5b3923767a765cddAndy Hung // to be at least 2 x the normal mixer frame count and cover audio hardware latency. 16260e48d25606c82def035ad10a5b3923767a765cddAndy Hung // This is probably too conservative, but legacy application code may depend on it. 16270e48d25606c82def035ad10a5b3923767a765cddAndy Hung // If you change this calculation, also review the start threshold which is related. 16280e48d25606c82def035ad10a5b3923767a765cddAndy Hung if (!(*flags & IAudioFlinger::TRACK_FAST) 16290e48d25606c82def035ad10a5b3923767a765cddAndy Hung && audio_is_linear_pcm(format) && sharedBuffer == 0) { 16308edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung // this must match AudioTrack.cpp calculateMinFrameCount(). 16318edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung // TODO: Move to a common library 163281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream); 163381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate); 163481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (minBufCount < 2) { 163581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minBufCount = 2; 163681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 16378edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung // For normal mixing tracks, if speed is > 1.0f (normal), AudioTrack 16388edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung // or the client should compute and pass in a larger buffer request. 16390e48d25606c82def035ad10a5b3923767a765cddAndy Hung size_t minFrameCount = 16408edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung minBufCount * sourceFramesNeededWithTimestretch( 16418edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung sampleRate, mNormalFrameCount, 16428edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung mSampleRate, AUDIO_TIMESTRETCH_SPEED_NORMAL /*speed*/); 16430e48d25606c82def035ad10a5b3923767a765cddAndy Hung if (frameCount < minFrameCount) { // including frameCount == 0 164481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent frameCount = minFrameCount; 164581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 164681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 164774935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten *pFrameCount = frameCount; 164881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1649c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten switch (mType) { 1650c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten 1651c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten case DIRECT: 1652993fa0603707e94ce259e95e56838a85b5ccbdc5Glenn Kasten if (audio_is_linear_pcm(format)) { 165381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { 1654cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x " 1655cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten "for output %p with format %#x", 165681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sampleRate, format, channelMask, mOutput, mFormat); 165781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 165881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 165981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 166081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1661c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten break; 1662c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten 1663c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten case OFFLOAD: 1664bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { 1665cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \"" 1666cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten "for output %p with format %#x", 1667bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent sampleRate, format, channelMask, mOutput, mFormat); 1668bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent lStatus = BAD_VALUE; 1669bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent goto Exit; 1670bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1671c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten break; 1672c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten 1673c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten default: 1674993fa0603707e94ce259e95e56838a85b5ccbdc5Glenn Kasten if (!audio_is_linear_pcm(format)) { 1675cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten ALOGE("createTrack_l() Bad parameter: format %#x \"" 1676cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten "for output %p with format %#x", 1677bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent format, mOutput, mFormat); 1678bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent lStatus = BAD_VALUE; 1679bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent goto Exit; 1680bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1681cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) { 168281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); 168381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 168481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 168581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1686c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten break; 1687c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten 168881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 168981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 169081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = initCheck(); 169181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR) { 169215e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten ALOGE("createTrack_l() audio driver not initialized"); 169381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 169481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 169581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 169681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // scope for mLock 169781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 169881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 169981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // all tracks in same audio session must share the same routing strategy otherwise 170081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // conflicts will happen when tracks are moved from one output to another by audio policy 170181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // manager 170281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t strategy = AudioSystem::getStrategyForStream(streamType); 170381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 170481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> t = mTracks[i]; 170583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent if (t != 0 && t->isExternalTrack()) { 170681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); 170781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId == t->sessionId() && strategy != actual) { 170881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", 170981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent strategy, actual); 171081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 171181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 171281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 171381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 171481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 171581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 171681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!isTimed) { 171781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track = new Track(this, client, streamType, sampleRate, format, 171883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent channelMask, frameCount, NULL, sharedBuffer, 171983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent sessionId, uid, *flags, TrackBase::TYPE_DEFAULT); 172081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 172181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track = TimedTrack::create(this, client, streamType, sampleRate, format, 1722462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen channelMask, frameCount, sharedBuffer, sessionId, uid); 172381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1724030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten 1725030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten // new Track always returns non-NULL, 1726030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten // but TimedTrack::create() is a factory that could fail by returning NULL 1727030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY; 1728030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten if (lStatus != NO_ERROR) { 17290cde076ddb283c84c3801a2df4cc3df99bd1577fGlenn Kasten ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus); 173003e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George // track must be cleared from the caller as the caller has the AF lock 173181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 173281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 173381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mTracks.add(track); 173481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 173581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(sessionId); 173681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 173781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); 173881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->setMainBuffer(chain->inBuffer()); 173981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); 174081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->incTrackCnt(); 174181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 174281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 174381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { 174481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t callingPid = IPCThreadState::self()->getCallingPid(); 174581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, 174681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // so ask activity manager to do this on our behalf 174781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); 174881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 174981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 175081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 175181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = NO_ERROR; 175281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 175381784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit: 17549156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten *status = lStatus; 175581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return track; 175681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 175781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 175881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const 175981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 176081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return latency; 176181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 176281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 176381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::latency() const 176481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 176581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 176681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return latency_l(); 176781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 176881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::latency_l() const 176981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 177081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (initCheck() == NO_ERROR) { 177181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return correctLatency_l(mOutput->stream->get_latency(mOutput->stream)); 177281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 177381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 177481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 177581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 177681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 177781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::setMasterVolume(float value) 177881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 177981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 178081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Don't apply master volume in SW if our HAL can do it for us. 178181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutput && mOutput->audioHwDev && 178281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutput->audioHwDev->canSetMasterVolume()) { 178381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterVolume = 1.0; 178481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 178581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterVolume = value; 178681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 178781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 178881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 178981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::setMasterMute(bool muted) 179081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 179181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 179281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Don't apply master mute in SW if our HAL can do it for us. 179381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutput && mOutput->audioHwDev && 179481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutput->audioHwDev->canSetMasterMute()) { 179581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterMute = false; 179681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 179781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterMute = muted; 179881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 179981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 180081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 180181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) 180281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 180381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 180481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStreamTypes[stream].volume = value; 1805ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent broadcast_l(); 180681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 180781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 180881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) 180981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 181081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 181181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStreamTypes[stream].mute = muted; 1812ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent broadcast_l(); 181381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 181481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 181581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentfloat AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const 181681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 181781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 181881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mStreamTypes[stream].volume; 181981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 182081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 182181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// addTrack_l() must be called with ThreadBase::mLock held 182281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) 182381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 182481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status = ALREADY_EXISTS; 182581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 182681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // set retry count for buffer fill 182781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mRetryCount = kMaxTrackStartupRetries; 182881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mActiveTracks.indexOf(track) < 0) { 182981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // the track is newly added, make sure it fills up all its 183081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // buffers before playing. This is to ensure the client will 183181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // effectively get the latency it requested. 183283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent if (track->isExternalTrack()) { 1833bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent TrackBase::track_state state = track->mState; 1834bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mLock.unlock(); 1835e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent status = AudioSystem::startOutput(mId, track->streamType(), 1836e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent (audio_session_t)track->sessionId()); 1837bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mLock.lock(); 1838bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // abort track was stopped/paused while we released the lock 1839bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (state != track->mState) { 1840bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (status == NO_ERROR) { 1841bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mLock.unlock(); 1842e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent AudioSystem::stopOutput(mId, track->streamType(), 1843e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent (audio_session_t)track->sessionId()); 1844bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mLock.lock(); 1845bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1846bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return INVALID_OPERATION; 1847bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1848bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // abort if start is rejected by audio policy manager 1849bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (status != NO_ERROR) { 1850bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return PERMISSION_DENIED; 1851bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1852bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent#ifdef ADD_BATTERY_DATA 1853bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // to track the speaker usage 1854bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); 1855bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent#endif 1856bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1857bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 18589f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten track->mFillingUpStatus = track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING; 185981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mResetDone = false; 186081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mPresentationCompleteFrames = 0; 186181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mActiveTracks.add(track); 1862462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mWakeLockUids.add(track->uid()); 1863462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mActiveTracksGeneration++; 1864fd4779740ec3e9e865d5514464df26d015354388Eric Laurent mLatestActiveTrack = track; 1865d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent sp<EffectChain> chain = getEffectChain_l(track->sessionId()); 1866d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent if (chain != 0) { 1867d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), 1868d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent track->sessionId()); 1869d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent chain->incActiveTrackCnt(); 187081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 187181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 187281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = NO_ERROR; 187381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 187481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 18754c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George onAddNewTrack_l(); 187681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 187781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 187881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1879bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) 188081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 1881bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->terminate(); 188281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // active tracks are removed by threadLoop() 1883bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent bool trackActive = (mActiveTracks.indexOf(track) >= 0); 1884bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mState = TrackBase::STOPPED; 1885bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (!trackActive) { 188681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent removeTrack_l(track); 1887ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) { 1888bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mState = TrackBase::STOPPING_1; 188981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1890bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1891bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return trackActive; 189281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 189381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 189481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) 189581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 189681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); 189781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mTracks.remove(track); 189881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent deleteTrackName_l(track->name()); 189981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // redundant as track is about to be destroyed, for dumpsys only 190081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mName = -1; 190181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isFastTrack()) { 190281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int index = track->mFastIndex; 190381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(0 < index && index < (int)FastMixerState::kMaxFastTracks); 190481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); 190581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastTrackAvailMask |= 1 << index; 190681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // redundant as track is about to be destroyed, for dumpsys only 190781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mFastIndex = -1; 190881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 190981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(track->sessionId()); 191081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 191181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->decTrackCnt(); 191281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 191381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 191481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1915ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurentvoid AudioFlinger::PlaybackThread::broadcast_l() 1916bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 1917bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Thread could be blocked waiting for async 1918bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // so signal it to handle state changes immediately 1919bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // If threadLoop is currently unlocked a signal of mWaitWorkCV will 1920bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // be lost so we also flag to prevent it blocking on mWaitWorkCV 1921bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mSignalPending = true; 1922ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent mWaitWorkCV.broadcast(); 1923bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 1924bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 192581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentString8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) 192681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 192781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 192881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (initCheck() != NO_ERROR) { 1929d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten return String8(); 193081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 193181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1932d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten char *s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); 1933d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten const String8 out_s8(s); 193481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent free(s); 193581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return out_s8; 193681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 193781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 193873e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurentvoid AudioFlinger::PlaybackThread::ioConfigChanged(audio_io_config_event event) { 193973e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); 194073e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent ALOGV("PlaybackThread::ioConfigChanged, thread %p, event %d", this, event); 194181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 194273e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent desc->mIoHandle = mId; 194381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 194481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent switch (event) { 194573e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent case AUDIO_OUTPUT_OPENED: 194673e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent case AUDIO_OUTPUT_CONFIG_CHANGED: 1947296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent desc->mPatch = mPatch; 194873e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent desc->mChannelMask = mChannelMask; 194973e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent desc->mSamplingRate = mSampleRate; 195073e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent desc->mFormat = mFormat; 195173e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent desc->mFrameCount = mNormalFrameCount; // FIXME see 195281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // AudioFlinger::frameCount(audio_io_handle_t) 195373e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent desc->mLatency = latency_l(); 195481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 195581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 195673e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent case AUDIO_OUTPUT_CLOSED: 195781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent default: 195881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 195981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 196073e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent mAudioFlinger->ioConfigChanged(event, desc); 196181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 196281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1963bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::writeCallback() 1964bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 1965bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 19663b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->resetWriteBlocked(); 1967bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 1968bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1969bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::drainCallback() 1970bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 1971bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 19723b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->resetDraining(); 1973bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 1974bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 19753b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence) 1976bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 1977bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 19783b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // reject out of sequence requests 19793b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) { 19803b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence &= ~1; 1981bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mWaitWorkCV.signal(); 1982bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1983bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 1984bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 19853b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence) 1986bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 1987bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 19883b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // reject out of sequence requests 19893b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if ((mDrainSequence & 1) && (sequence == mDrainSequence)) { 19903b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence &= ~1; 1991bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mWaitWorkCV.signal(); 1992bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1993bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 1994bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1995bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// static 1996bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentint AudioFlinger::PlaybackThread::asyncCallback(stream_callback_event_t event, 19970f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten void *param __unused, 1998bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent void *cookie) 1999bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 2000bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioFlinger::PlaybackThread *me = (AudioFlinger::PlaybackThread *)cookie; 2001bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("asyncCallback() event %d", event); 2002bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent switch (event) { 2003bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent case STREAM_CBK_EVENT_WRITE_READY: 2004bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent me->writeCallback(); 2005bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent break; 2006bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent case STREAM_CBK_EVENT_DRAIN_READY: 2007bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent me->drainCallback(); 2008bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent break; 2009bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent default: 2010bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGW("asyncCallback() unknown event %d", event); 2011bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent break; 2012bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2013bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return 0; 2014bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 2015bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 2016deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kastenvoid AudioFlinger::PlaybackThread::readOutputParameters_l() 201781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 2018adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten // unfortunately we have no way of recovering from errors here, hence the LOG_ALWAYS_FATAL 201981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common); 202081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common); 20217fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten if (!audio_is_output_channel(mChannelMask)) { 2022adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask); 20237fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten } 20249a59276fb465e492138e0576523b54079671e8f4Andy Hung if ((mType == MIXER || mType == DUPLICATING) 20259a59276fb465e492138e0576523b54079671e8f4Andy Hung && !isValidPcmSinkChannelMask(mChannelMask)) { 20269a59276fb465e492138e0576523b54079671e8f4Andy Hung LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output", 20279a59276fb465e492138e0576523b54079671e8f4Andy Hung mChannelMask); 20287fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten } 2029e541269be94f3a1072932d51537905b120ef4733Andy Hung mChannelCount = audio_channel_count_from_out_mask(mChannelMask); 2030463be250de73907965faa6a216c00312bf81e049Andy Hung mHALFormat = mOutput->stream->common.get_format(&mOutput->stream->common); 2031463be250de73907965faa6a216c00312bf81e049Andy Hung mFormat = mHALFormat; 20327fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten if (!audio_is_valid_format(mFormat)) { 2033adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat); 20347fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten } 20356146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung if ((mType == MIXER || mType == DUPLICATING) 20366146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung && !isValidPcmSinkFormat(mFormat)) { 20376146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung LOG_FATAL("HAL format %#x not supported for mixed output", 20386146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung mFormat); 20397fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten } 2040062e67a26e0553dd142be622821f493df541f0c6Phil Burk mFrameSize = mOutput->getFrameSize(); 204170949c47fbae3f836d15f040551d7631be3ed7c2Glenn Kasten mBufferSize = mOutput->stream->common.get_buffer_size(&mOutput->stream->common); 204270949c47fbae3f836d15f040551d7631be3ed7c2Glenn Kasten mFrameCount = mBufferSize / mFrameSize; 204381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mFrameCount & 15) { 204481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("HAL output buffer size is %u frames but AudioMixer requires multiples of 16 frames", 204581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFrameCount); 204681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 204781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2048bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if ((mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) && 2049bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent (mOutput->stream->set_callback != NULL)) { 2050bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mOutput->stream->set_callback(mOutput->stream, 2051bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioFlinger::PlaybackThread::asyncCallback, this) == 0) { 2052bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mUseAsyncWrite = true; 20534de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent mCallbackThread = new AudioFlinger::AsyncCallbackThread(this); 2054bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2055bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2056bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 2057d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent mHwSupportsPause = false; 2058d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) { 2059d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent if (mOutput->stream->pause != NULL) { 2060d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent if (mOutput->stream->resume != NULL) { 2061d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent mHwSupportsPause = true; 2062d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } else { 2063d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent ALOGW("direct output implements pause but not resume"); 2064d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 2065d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } else if (mOutput->stream->resume != NULL) { 2066d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent ALOGW("direct output implements resume but not pause"); 2067d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 2068d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 20696fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk if (!mHwSupportsPause && mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) { 20706fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk LOG_ALWAYS_FATAL("HW_AV_SYNC requested but HAL does not implement pause and resume"); 20716fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk } 2072d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent 2073fbfc3959f4aac839445edc7075532067fef497c2Andy Hung if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) { 2074fbfc3959f4aac839445edc7075532067fef497c2Andy Hung // For best precision, we use float instead of the associated output 2075fbfc3959f4aac839445edc7075532067fef497c2Andy Hung // device format (typically PCM 16 bit). 2076fbfc3959f4aac839445edc7075532067fef497c2Andy Hung 2077fbfc3959f4aac839445edc7075532067fef497c2Andy Hung mFormat = AUDIO_FORMAT_PCM_FLOAT; 2078fbfc3959f4aac839445edc7075532067fef497c2Andy Hung mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); 2079fbfc3959f4aac839445edc7075532067fef497c2Andy Hung mBufferSize = mFrameSize * mFrameCount; 2080fbfc3959f4aac839445edc7075532067fef497c2Andy Hung 2081fbfc3959f4aac839445edc7075532067fef497c2Andy Hung // TODO: We currently use the associated output device channel mask and sample rate. 2082fbfc3959f4aac839445edc7075532067fef497c2Andy Hung // (1) Perhaps use the ORed channel mask of all downstream MixerThreads 2083fbfc3959f4aac839445edc7075532067fef497c2Andy Hung // (if a valid mask) to avoid premature downmix. 2084fbfc3959f4aac839445edc7075532067fef497c2Andy Hung // (2) Perhaps use the maximum sample rate of all downstream MixerThreads 2085fbfc3959f4aac839445edc7075532067fef497c2Andy Hung // instead of the output device sample rate to avoid loss of high frequency information. 2086fbfc3959f4aac839445edc7075532067fef497c2Andy Hung // This may need to be updated as MixerThread/OutputTracks are added and not here. 2087fbfc3959f4aac839445edc7075532067fef497c2Andy Hung } 2088fbfc3959f4aac839445edc7075532067fef497c2Andy Hung 208909a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung // Calculate size of normal sink buffer relative to the HAL output buffer size 209081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent double multiplier = 1.0; 209181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mType == MIXER && (kUseFastMixer == FastMixer_Static || 209281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent kUseFastMixer == FastMixer_Dynamic)) { 209309a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000; 209409a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000; 209581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer 209681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minNormalFrameCount = (minNormalFrameCount + 15) & ~15; 209781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent maxNormalFrameCount = maxNormalFrameCount & ~15; 209881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (maxNormalFrameCount < minNormalFrameCount) { 209981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent maxNormalFrameCount = minNormalFrameCount; 210081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 210181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent multiplier = (double) minNormalFrameCount / (double) mFrameCount; 210281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (multiplier <= 1.0) { 210381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent multiplier = 1.0; 210481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (multiplier <= 2.0) { 210581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (2 * mFrameCount <= maxNormalFrameCount) { 210681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent multiplier = 2.0; 210781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 210881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent multiplier = (double) maxNormalFrameCount / (double) mFrameCount; 210981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 211081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 211181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // prefer an even multiplier, for compatibility with doubling of fast tracks due to HAL 211209a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung // SRC (it would be unusual for the normal sink buffer size to not be a multiple of fast 211381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track, but we sometimes have to do this to satisfy the maximum frame count 211481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // constraint) 211581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME this rounding up should not be done if no HAL SRC 211681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t truncMult = (uint32_t) multiplier; 211781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((truncMult & 1)) { 211881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((truncMult + 1) * mFrameCount <= maxNormalFrameCount) { 211981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ++truncMult; 212081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 212181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 212281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent multiplier = (double) truncMult; 212381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 212481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 212581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalFrameCount = multiplier * mFrameCount; 212681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // round up to nearest 16 frames to satisfy AudioMixer 2127ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent if (mType == MIXER || mType == DUPLICATING) { 2128ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent mNormalFrameCount = (mNormalFrameCount + 15) & ~15; 2129ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent } 213009a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung ALOGI("HAL output buffer size %u frames, normal sink buffer size %u frames", mFrameCount, 213181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalFrameCount); 213281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2133010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // mSinkBuffer is the sink buffer. Size is always multiple-of-16 frames. 2134010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // Originally this was int16_t[] array, need to remove legacy implications. 2135010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung free(mSinkBuffer); 2136010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung mSinkBuffer = NULL; 21375b10a2037a835e790994b9ebec3c2e55052f1f3bAndy Hung // For sink buffer size, we use the frame size from the downstream sink to avoid problems 21385b10a2037a835e790994b9ebec3c2e55052f1f3bAndy Hung // with non PCM formats for compressed music, e.g. AAC, and Offload threads. 21395b10a2037a835e790994b9ebec3c2e55052f1f3bAndy Hung const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; 2140010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); 214181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 214269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // We resize the mMixerBuffer according to the requirements of the sink buffer which 214369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // drives the output. 214469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung free(mMixerBuffer); 214569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBuffer = NULL; 214669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung if (mMixerBufferEnabled) { 214769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // also valid: AUDIO_FORMAT_PCM_16_BIT. 214869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferSize = mNormalFrameCount * mChannelCount 214969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * audio_bytes_per_sample(mMixerBufferFormat); 215069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize); 215169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung } 215298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung free(mEffectBuffer); 215398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBuffer = NULL; 215498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mEffectBufferEnabled) { 215598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferFormat = AUDIO_FORMAT_PCM_16_BIT; // Note: Effects support 16b only 215698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferSize = mNormalFrameCount * mChannelCount 215798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung * audio_bytes_per_sample(mEffectBufferFormat); 215898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize); 215998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 216069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung 216181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // force reconfiguration of effect chains and engines to take new buffer size and audio 216281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // parameters into account 2163deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten // Note that mLock is not held when readOutputParameters_l() is called from the constructor 216481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // but in this case nothing is done below as no audio sessions have effect yet so it doesn't 216581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // matter. 216681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains 216781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<EffectChain> > effectChains = mEffectChains; 216881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < effectChains.size(); i ++) { 216981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); 217081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 217181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 217281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 217381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2174377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETITstatus_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) 217581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 217681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (halFrames == NULL || dspFrames == NULL) { 217781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 217881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 217981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 218081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (initCheck() != NO_ERROR) { 218181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return INVALID_OPERATION; 218281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 218381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t framesWritten = mBytesWritten / mFrameSize; 218481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *halFrames = framesWritten; 218581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 218681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (isSuspended()) { 218781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // return an estimation of rendered frames when the output is suspended 218881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t latencyFrames = (latency_l() * mSampleRate) / 1000; 218981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *dspFrames = framesWritten >= latencyFrames ? framesWritten - latencyFrames : 0; 219081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 219181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 2192377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT status_t status; 2193377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT uint32_t frames; 2194062e67a26e0553dd142be622821f493df541f0c6Phil Burk status = mOutput->getRenderPosition(&frames); 2195377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT *dspFrames = (size_t)frames; 2196377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT return status; 219781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 219881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 219981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 220081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) const 220181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 220281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 220381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t result = 0; 220481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (getEffectChain_l(sessionId) != 0) { 220581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result = EFFECT_SESSION; 220681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 220781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 220881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 220981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mTracks[i]; 22105736c35b841de56ce394b4879389f669b61425e6Glenn Kasten if (sessionId == track->sessionId() && !track->isInvalid()) { 221181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result |= TRACK_SESSION; 221281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 221381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 221481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 221581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 221681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return result; 221781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 221881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 221981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId) 222081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 222181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that 222281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it is moved to correct output by audio policy manager when A2DP is connected or disconnected 222381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { 222481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); 222581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 222681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 222781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mTracks[i]; 22285736c35b841de56ce394b4879389f669b61425e6Glenn Kasten if (sessionId == track->sessionId() && !track->isInvalid()) { 222981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return AudioSystem::getStrategyForStream(track->streamType()); 223081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 223181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 223281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); 223381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 223481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 223581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2236062e67a26e0553dd142be622821f493df541f0c6Phil BurkAudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const 223781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 223881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 223981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mOutput; 224081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 224181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2242062e67a26e0553dd142be622821f493df541f0c6Phil BurkAudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() 224381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 224481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 224581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamOut *output = mOutput; 224681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutput = NULL; 224781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME FastMixer might also have a raw ptr to mOutputSink; 224881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // must push a NULL and wait for ack 224981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutputSink.clear(); 225081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPipeSink.clear(); 225181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink.clear(); 225281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return output; 225381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 225481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 225581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// this method must always be called either with ThreadBase mLock held or inside the thread loop 225681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_stream_t* AudioFlinger::PlaybackThread::stream() const 225781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 225881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutput == NULL) { 225981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NULL; 226081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 226181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return &mOutput->stream->common; 226281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 226381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 226481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const 226581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 226681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); 226781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 226881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 226981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) 227081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 227181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!isValidSyncEvent(event)) { 227281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 227381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 227481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 227581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 227681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 227781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 227881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mTracks[i]; 227981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (event->triggerSession() == track->sessionId()) { 228081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (void) track->setSyncEvent(event); 228181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 228281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 228381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 228481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 228581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NAME_NOT_FOUND; 228681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 228781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 228881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const 228981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 229081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; 229181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 229281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 229381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::threadLoop_removeTracks( 229481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const Vector< sp<Track> >& tracksToRemove) 229581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 229681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t count = tracksToRemove.size(); 229734fca34606b448e6b71c2942f63cb13a0aebd620Glenn Kasten if (count > 0) { 229881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0 ; i < count ; i++) { 229981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<Track>& track = tracksToRemove.itemAt(i); 230083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent if (track->isExternalTrack()) { 2301e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent AudioSystem::stopOutput(mId, track->streamType(), 2302e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent (audio_session_t)track->sessionId()); 2303bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent#ifdef ADD_BATTERY_DATA 2304bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // to track the speaker usage 2305bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); 2306bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent#endif 2307bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (track->isTerminated()) { 2308e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent AudioSystem::releaseOutput(mId, track->streamType(), 2309e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent (audio_session_t)track->sessionId()); 2310bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 231181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 231281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 231381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 231481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 231581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 231681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::checkSilentMode_l() 231781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 231881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!mMasterMute) { 231981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char value[PROPERTY_VALUE_MAX]; 232081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (property_get("ro.audio.silent", value, "0") > 0) { 232181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char *endptr; 232281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent unsigned long ul = strtoul(value, &endptr, 0); 232381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (*endptr == '\0' && ul != 0) { 232481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGD("Silence is golden"); 232581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The setprop command will not allow a property to be changed after 232681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // the first time it is set, so we don't have to worry about un-muting. 232781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent setMasterMute_l(true); 232881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 232981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 233081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 233181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 233281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 233381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// shared by MIXER and DIRECT, overridden by DUPLICATING 2334bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentssize_t AudioFlinger::PlaybackThread::threadLoop_write() 233581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 233681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME rewrite to reduce number of system calls 233781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mLastWriteTime = systemTime(); 233881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mInWrite = true; 2339bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ssize_t bytesWritten; 2340010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung const size_t offset = mCurrentWriteLength - mBytesRemaining; 234181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 234281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If an NBAIO sink is present, use it to write the normal mixer's submix 234381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mNormalSink != 0) { 23444c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten 2345010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung const size_t count = mBytesRemaining / mFrameSize; 2346010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung 23472d590964aa58e137d17a43e095e6443dd0fe2e98Simon Wilson ATRACE_BEGIN("write"); 234881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // update the setpoint when AudioFlinger::mScreenState changes 234981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t screenState = AudioFlinger::mScreenState; 235081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (screenState != mScreenState) { 235181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mScreenState = screenState; 235281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); 235381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (pipe != NULL) { 235481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pipe->setAvgFrames((mScreenState & 1) ? 235581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); 235681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 235781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 2358010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count); 23592d590964aa58e137d17a43e095e6443dd0fe2e98Simon Wilson ATRACE_END(); 236081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (framesWritten > 0) { 2361010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung bytesWritten = framesWritten * mFrameSize; 236281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 236381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bytesWritten = framesWritten; 236481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 2365efaa7ab620b2ee936efa4fb74dc4c670ed757a46Glenn Kasten mLatchDValid = false; 2366767094dd98b01baf21de2ad09c27b3c98776cf73Glenn Kasten status_t status = mNormalSink->getTimestamp(mLatchD.mTimestamp); 2367bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten if (status == NO_ERROR) { 2368bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten size_t totalFramesWritten = mNormalSink->framesWritten(); 2369bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten if (totalFramesWritten >= mLatchD.mTimestamp.mPosition) { 2370bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mLatchD.mUnpresentedFrames = totalFramesWritten - mLatchD.mTimestamp.mPosition; 23714c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten // mLatchD.mFramesReleased is set immediately before D is clocked into Q 2372bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mLatchDValid = true; 2373bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten } 2374bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten } 237581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // otherwise use the HAL / AudioStreamOut directly 237681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 2377bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Direct output and offload threads 2378010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung 2379bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mUseAsyncWrite) { 23803b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request"); 23813b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence += 2; 23823b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence |= 1; 2383bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 23843b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setWriteBlocked(mWriteAckSequence); 2385bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2386767094dd98b01baf21de2ad09c27b3c98776cf73Glenn Kasten // FIXME We should have an implementation of timestamps for direct output threads. 2387767094dd98b01baf21de2ad09c27b3c98776cf73Glenn Kasten // They are used e.g for multichannel PCM playback over HDMI. 2388062e67a26e0553dd142be622821f493df541f0c6Phil Burk bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining); 2389bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mUseAsyncWrite && 2390bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) { 2391bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // do not wait for async callback in case of error of full write 23923b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence &= ~1; 2393bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 23943b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setWriteBlocked(mWriteAckSequence); 2395bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 239681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 239781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 239881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNumWrites++; 239981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mInWrite = false; 2400fd4779740ec3e9e865d5514464df26d015354388Eric Laurent mStandby = false; 2401bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return bytesWritten; 2402bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 2403bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 2404bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::threadLoop_drain() 2405bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 2406bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mOutput->stream->drain) { 2407bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full"); 2408bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mUseAsyncWrite) { 24093b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request"); 24103b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence |= 1; 2411bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 24123b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setDraining(mDrainSequence); 2413bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2414bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mOutput->stream->drain(mOutput->stream, 2415bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent (mMixerStatus == MIXER_DRAIN_TRACK) ? AUDIO_DRAIN_EARLY_NOTIFY 2416bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent : AUDIO_DRAIN_ALL); 2417bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2418bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 2419bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 2420bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::threadLoop_exit() 2421bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 2422275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent { 2423275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent Mutex::Autolock _l(mLock); 2424275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 2425275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent sp<Track> track = mTracks[i]; 2426275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent track->invalidate(); 2427275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent } 2428275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent } 242981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 243081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 243181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent/* 243281784c37c61b09289654b979567a42bf73cd2b12Eric LaurentThe derived values that are cached: 243325c2dac12114699e90deb1c579cadebce7b91a97Andy Hung - mSinkBufferSize from frame count * frame size 243481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - activeSleepTime from activeSleepTimeUs() 243581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - idleSleepTime from idleSleepTimeUs() 243681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - standbyDelay from mActiveSleepTimeUs (DIRECT only) 243781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - maxPeriod from frame count and sample rate (MIXER only) 243881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 243981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentThe parameters that affect these derived values are: 244081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - frame count 244181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - frame size 244281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - sample rate 244381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - device type: A2DP or not 244481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - device latency 244581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - format: PCM or not 244681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - active sleep time 244781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - idle sleep time 244881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent*/ 244981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 245081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::cacheParameters_l() 245181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 245225c2dac12114699e90deb1c579cadebce7b91a97Andy Hung mSinkBufferSize = mNormalFrameCount * mFrameSize; 245381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent activeSleepTime = activeSleepTimeUs(); 245481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent idleSleepTime = idleSleepTimeUs(); 245581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 245681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 245781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType) 245881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 24597c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d", 246081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent this, streamType, mTracks.size()); 246181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 246281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 246381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t size = mTracks.size(); 246481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < size; i++) { 246581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> t = mTracks[i]; 246681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (t->streamType() == streamType) { 24675736c35b841de56ce394b4879389f669b61425e6Glenn Kasten t->invalidate(); 246881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 246981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 247081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 247181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 247281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) 247381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 247481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int session = chain->sessionId(); 2475010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung int16_t* buffer = reinterpret_cast<int16_t*>(mEffectBufferEnabled 2476010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung ? mEffectBuffer : mSinkBuffer); 247781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool ownsBuffer = false; 247881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 247981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); 248081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (session > 0) { 248181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Only one effect chain can be present in direct output thread and it uses 24822098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung // the sink buffer as input 248381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mType != DIRECT) { 248481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t numSamples = mNormalFrameCount * mChannelCount; 248581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent buffer = new int16_t[numSamples]; 248681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent memset(buffer, 0, numSamples * sizeof(int16_t)); 248781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); 248881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ownsBuffer = true; 248981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 249081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 249181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Attach all tracks with same session ID to this chain. 249281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 249381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mTracks[i]; 249481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (session == track->sessionId()) { 249581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), 249681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent buffer); 249781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->setMainBuffer(buffer); 249881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->incTrackCnt(); 249981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 250081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 250181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 250281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // indicate all active tracks in the chain 250381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { 250481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mActiveTracks[i].promote(); 250581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track == 0) { 250681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 250781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 250881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (session == track->sessionId()) { 250981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); 251081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->incActiveTrackCnt(); 251181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 251281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 251381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 2514aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent chain->setThread(this); 251581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setInBuffer(buffer, ownsBuffer); 2516010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung chain->setOutBuffer(reinterpret_cast<int16_t*>(mEffectBufferEnabled 2517010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung ? mEffectBuffer : mSinkBuffer)); 251881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect 251981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // chains list in order to be processed last as it contains output stage effects 252081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before 252181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // session AUDIO_SESSION_OUTPUT_STAGE to be processed 252281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // after track specific effects and before output stage 252381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and 252481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX 252581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Effect chain for other sessions are inserted at beginning of effect 252681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // chains list to be processed before output mix effects. Relative order between other 252781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // sessions is not important 252881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t size = mEffectChains.size(); 252981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t i = 0; 253081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (i = 0; i < size; i++) { 253181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mEffectChains[i]->sessionId() < session) { 253281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 253381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 253481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 253581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains.insertAt(chain, i); 253681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent checkSuspendOnAddEffectChain_l(chain); 253781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 253881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 253981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 254081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 254181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsize_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) 254281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 254381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int session = chain->sessionId(); 254481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 254581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); 254681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 254781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 254881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain == mEffectChains[i]) { 254981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains.removeAt(i); 255081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // detach all active tracks from the chain 255181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { 255281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mActiveTracks[i].promote(); 255381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track == 0) { 255481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 255581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 255681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (session == track->sessionId()) { 255781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", 255881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain.get(), session); 255981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->decActiveTrackCnt(); 256081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 256181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 256281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 256381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // detach all tracks with same session ID from this chain 256481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 256581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mTracks[i]; 256681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (session == track->sessionId()) { 2567010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung track->setMainBuffer(reinterpret_cast<int16_t*>(mSinkBuffer)); 256881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->decTrackCnt(); 256981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 257081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 257181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 257281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 257381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 257481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mEffectChains.size(); 257581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 257681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 257781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::attachAuxEffect( 257881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) 257981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 258081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 258181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return attachAuxEffect_l(track, EffectId); 258281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 258381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 258481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::attachAuxEffect_l( 258581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) 258681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 258781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status = NO_ERROR; 258881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 258981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (EffectId == 0) { 259081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->setAuxBuffer(0, NULL); 259181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 259281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX 259381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); 259481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (effect != 0) { 259581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 259681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); 259781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 259881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = INVALID_OPERATION; 259981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 260081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 260181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = BAD_VALUE; 260281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 260381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 260481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 260581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 260681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 260781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) 260881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 260981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 261081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> track = mTracks[i]; 261181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->auxEffectId() == effectId) { 261281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent attachAuxEffect_l(track, 0); 261381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 261481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 261581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 261681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 261781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::PlaybackThread::threadLoop() 261881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 261981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<Track> > tracksToRemove; 262081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 262181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent standbyTime = systemTime(); 262281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 262381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // MIXER 262481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent nsecs_t lastWarning = 0; 262581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 262681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // DUPLICATING 262781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME could this be made local to while loop? 262881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent writeFrames = 0; 262981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2630462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen int lastGeneration = 0; 2631462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 263281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent cacheParameters_l(); 263381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = idleSleepTime; 263481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 263581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mType == MIXER) { 263681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTimeShift = 0; 263781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 263881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 263981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent CpuStats cpuStats; 264081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); 264181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 264281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent acquireWakeLock(); 264381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 26449e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten // mNBLogWriter->log can only be called while thread mutex mLock is held. 26459e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten // So if you need to log when mutex is unlocked, set logString to a non-NULL string, 26469e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten // and then that string will be logged at the next convenient opportunity. 26479e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten const char *logString = NULL; 26489e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten 2649664539d25180ab8f77e0521533ea2821cf28985fEric Laurent checkSilentMode_l(); 2650664539d25180ab8f77e0521533ea2821cf28985fEric Laurent 265181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent while (!exitPending()) 265281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 265381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent cpuStats.sample(myName); 265481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 265581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<EffectChain> > effectChains; 265681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 265781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // scope for mLock 265881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 265981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 266081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2661021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent processConfigEvents_l(); 26621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 26639e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten if (logString != NULL) { 26649e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten mNBLogWriter->logTimestamp(); 26659e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten mNBLogWriter->log(logString); 26669e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten logString = NULL; 26679e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten } 26689e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten 26694c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten // Gather the framesReleased counters for all active tracks, 26704c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten // and latch them atomically with the timestamp. 26714c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten // FIXME We're using raw pointers as indices. A unique track ID would be a better index. 26724c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten mLatchD.mFramesReleased.clear(); 26734c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten size_t size = mActiveTracks.size(); 26744c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten for (size_t i = 0; i < size; i++) { 26754c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten sp<Track> t = mActiveTracks[i].promote(); 26764c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten if (t != 0) { 26774c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten mLatchD.mFramesReleased.add(t.get(), 26784c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten t->mAudioTrackServerProxy->framesReleased()); 26794c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten } 26804c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten } 2681bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten if (mLatchDValid) { 2682bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mLatchQ = mLatchD; 2683bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mLatchDValid = false; 2684bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten mLatchQValid = true; 2685bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten } 2686bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten 268781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent saveOutputTracks(); 2688bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mSignalPending) { 2689bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // A signal was raised while we were unlocked 2690bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mSignalPending = false; 2691bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if (waitingAsyncCallback_l()) { 2692bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (exitPending()) { 2693bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent break; 2694bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2695078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen bool released = false; 2696078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen // The following works around a bug in the offload driver. Ideally we would release 2697078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen // the wake lock every time, but that causes the last offload buffer(s) to be 2698078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen // dropped while the device is on battery, so we need to hold a wake lock during 2699078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen // the drain phase. 2700078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen if (mBytesRemaining && !(mDrainSequence & 1)) { 2701078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen releaseWakeLock_l(); 2702078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen released = true; 2703078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen } 2704462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mWakeLockUids.clear(); 2705462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mActiveTracksGeneration++; 2706bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("wait async completion"); 2707bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mWaitWorkCV.wait(mLock); 2708bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("async completion/wake"); 2709078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen if (released) { 2710078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen acquireWakeLock_l(); 2711078538cfc6c4682889ed52de460d29c0d15bb9ebMarco Nelissen } 2712972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent standbyTime = systemTime() + standbyDelay; 2713972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent sleepTime = 0; 2714ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent 2715ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent continue; 2716ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent } 2717ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent if ((!mActiveTracks.size() && systemTime() > standbyTime) || 2718bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent isSuspended()) { 2719bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // put audio hardware into standby after short delay 2720bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (shouldStandby_l()) { 272181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 272281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent threadLoop_standby(); 272381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 272481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStandby = true; 272581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 272681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 272781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { 272881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // we're about to wait, flush the binder command buffer 272981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent IPCThreadState::self()->flushCommands(); 273081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 273181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent clearOutputTracks(); 273281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 273381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (exitPending()) { 273481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 273581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 273681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 273781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock_l(); 2738462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mWakeLockUids.clear(); 2739462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mActiveTracksGeneration++; 274081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // wait until we have something to do... 274181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("%s going to sleep", myName.string()); 274281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitWorkCV.wait(mLock); 274381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("%s waking up", myName.string()); 274481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent acquireWakeLock_l(); 274581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 274681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMixerStatus = MIXER_IDLE; 274781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMixerStatusIgnoringFastTracks = MIXER_IDLE; 274881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mBytesWritten = 0; 2749bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining = 0; 275081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent checkSilentMode_l(); 275181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 275281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent standbyTime = systemTime() + standbyDelay; 275381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = idleSleepTime; 275481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mType == MIXER) { 275581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTimeShift = 0; 275681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 275781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 275881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 275981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 276081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 276181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mMixerStatusIgnoringFastTracks is also updated internally 276281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMixerStatus = prepareTracks_l(&tracksToRemove); 276381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2764462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen // compare with previously applied list 2765462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen if (lastGeneration != mActiveTracksGeneration) { 2766462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen // update wakelock 2767462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen updateWakeLockUids_l(mWakeLockUids); 2768462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen lastGeneration = mActiveTracksGeneration; 2769462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } 2770462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen 277181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // prevent any changes in effect chain list and in each effect chain 277281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // during mixing and effect process as the audio buffers could be deleted 277381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // or modified if an effect is created or deleted 277481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lockEffectChains_l(effectChains); 2775462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen } // mLock scope ends 277681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2777bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mBytesRemaining == 0) { 2778bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mCurrentWriteLength = 0; 2779bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mMixerStatus == MIXER_TRACKS_READY) { 2780bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // threadLoop_mix() sets mCurrentWriteLength 2781bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent threadLoop_mix(); 2782bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if ((mMixerStatus != MIXER_DRAIN_TRACK) 2783bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent && (mMixerStatus != MIXER_DRAIN_ALL)) { 2784bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // threadLoop_sleepTime sets sleepTime to 0 if data 2785bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // must be written to HAL 2786bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent threadLoop_sleepTime(); 2787bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (sleepTime == 0) { 278825c2dac12114699e90deb1c579cadebce7b91a97Andy Hung mCurrentWriteLength = mSinkBufferSize; 2789bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2790bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 279198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Either threadLoop_mix() or threadLoop_sleepTime() should have set 279298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // mMixerBuffer with data if mMixerBufferValid is true and sleepTime == 0. 279398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid) 279498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // or mSinkBuffer (if there are no effects). 279598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // 279698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // This is done pre-effects computation; if effects change to 279798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // support higher precision, this needs to move. 279898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // 279998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // mMixerBufferValid is only set true by MixerThread::prepareTracks_l(). 280098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // TODO use sleepTime == 0 as an additional condition. 280198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mMixerBufferValid) { 280298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer; 280398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat; 280498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung 280598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat, 280698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mNormalFrameCount * mChannelCount); 280798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 280898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung 2809bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining = mCurrentWriteLength; 2810bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (isSuspended()) { 2811bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent sleepTime = suspendSleepTimeUs(); 2812bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // simulate write to HAL when suspended 281325c2dac12114699e90deb1c579cadebce7b91a97Andy Hung mBytesWritten += mSinkBufferSize; 2814bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining = 0; 2815bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 281681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2817bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // only process effects if we're going to write 281859fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent if (sleepTime == 0 && mType != OFFLOAD) { 2819bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent for (size_t i = 0; i < effectChains.size(); i ++) { 2820bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent effectChains[i]->process_l(); 2821bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 282281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 282381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 282459fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent // Process effect chains for offloaded thread even if no audio 282559fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent // was read from audio track: process only updates effect state 282659fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent // and thus does have to be synchronized with audio writes but may have 282759fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent // to be called while waiting for async write callback 282859fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent if (mType == OFFLOAD) { 282959fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent for (size_t i = 0; i < effectChains.size(); i ++) { 283059fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent effectChains[i]->process_l(); 283159fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent } 283259fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent } 283381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 283498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Only if the Effects buffer is enabled and there is data in the 283598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Effects buffer (buffer valid), we need to 283698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // copy into the sink buffer. 283798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // TODO use sleepTime == 0 as an additional condition. 283898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mEffectBufferValid) { 283998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung //ALOGV("writing effect buffer to sink buffer format %#x", mFormat); 284098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat, 284198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mNormalFrameCount * mChannelCount); 284298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 284398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung 284481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // enable changes in effect chain 284581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent unlockEffectChains(effectChains); 284681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2847bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (!waitingAsyncCallback()) { 2848bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // sleepTime == 0 means we must write to audio hardware 2849bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (sleepTime == 0) { 2850bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mBytesRemaining) { 2851bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ssize_t ret = threadLoop_write(); 2852bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (ret < 0) { 2853bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining = 0; 2854bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else { 2855bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesWritten += ret; 2856bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining -= ret; 2857bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2858bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if ((mMixerStatus == MIXER_DRAIN_TRACK) || 2859bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent (mMixerStatus == MIXER_DRAIN_ALL)) { 2860bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent threadLoop_drain(); 2861bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 28624944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten if (mType == MIXER) { 28634944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten // write blocked detection 28644944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten nsecs_t now = systemTime(); 28654944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten nsecs_t delta = now - mLastWriteTime; 28664944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten if (!mStandby && delta > maxPeriod) { 28674944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten mNumDelayedWrites++; 28684944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten if ((now - lastWarning) > kWarningThrottleNs) { 28694944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten ATRACE_NAME("underrun"); 28704944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", 28714944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten ns2ms(delta), mNumDelayedWrites, this); 28724944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten lastWarning = now; 28734944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten } 2874bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 287581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 287681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2877bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else { 2878e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten ATRACE_BEGIN("sleep"); 2879bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent usleep(sleepTime); 2880e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten ATRACE_END(); 2881bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 288281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 288381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 288481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Finally let go of removed track(s), without the lock held 288581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // since we can't guarantee the destructors won't acquire that 288681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // same lock. This will also mutate and push a new fast mixer state. 288781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent threadLoop_removeTracks(tracksToRemove); 288881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent tracksToRemove.clear(); 288981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 289081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME I don't understand the need for this here; 289181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it was in the original code but maybe the 289281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // assignment in saveOutputTracks() makes this unnecessary? 289381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent clearOutputTracks(); 289481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 289581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Effect chains will be actually deleted here if they were removed from 289681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mEffectChains list during mixing or effects processing 289781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effectChains.clear(); 289881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 289981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME Note that the above .clear() is no longer necessary since effectChains 290081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // is now local to this block, but will keep it for now (at least until merge done). 290181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 290281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2903bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent threadLoop_exit(); 2904bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 2905cf817a2330936947df94c11859f48771f5596a59Eric Laurent if (!mStandby) { 2906cf817a2330936947df94c11859f48771f5596a59Eric Laurent threadLoop_standby(); 2907cf817a2330936947df94c11859f48771f5596a59Eric Laurent mStandby = true; 290881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 290981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 291081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock(); 2911462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mWakeLockUids.clear(); 2912462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mActiveTracksGeneration++; 291381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 291481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("Thread %p type %d exiting", this, mType); 291581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 291681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 291781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2918bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// removeTracks_l() must be called with ThreadBase::mLock held 2919bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove) 2920bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 2921bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent size_t count = tracksToRemove.size(); 292234fca34606b448e6b71c2942f63cb13a0aebd620Glenn Kasten if (count > 0) { 2923bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent for (size_t i=0 ; i<count ; i++) { 2924bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent const sp<Track>& track = tracksToRemove.itemAt(i); 2925bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mActiveTracks.remove(track); 2926462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mWakeLockUids.remove(track->uid()); 2927462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen mActiveTracksGeneration++; 2928bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("removeTracks_l removing track on session %d", track->sessionId()); 2929bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent sp<EffectChain> chain = getEffectChain_l(track->sessionId()); 2930bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (chain != 0) { 2931bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("stopping track on chain %p for session Id: %d", chain.get(), 2932bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->sessionId()); 2933bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent chain->decActiveTrackCnt(); 2934bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2935bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (track->isTerminated()) { 2936bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent removeTrack_l(track); 2937bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2938bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2939bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 2940bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 2941bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 294281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2943accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurentstatus_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp) 2944accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent{ 2945accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent if (mNormalSink != 0) { 2946accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent return mNormalSink->getTimestamp(timestamp); 2947accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent } 29489a1c8892f6835325db6931529dc74bf98cf0aee8Andy Hung if ((mType == OFFLOAD || mType == DIRECT) 29499a1c8892f6835325db6931529dc74bf98cf0aee8Andy Hung && mOutput != NULL && mOutput->stream->get_presentation_position) { 2950accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent uint64_t position64; 2951062e67a26e0553dd142be622821f493df541f0c6Phil Burk int ret = mOutput->getPresentationPosition(&position64, ×tamp.mTime); 2952accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent if (ret == 0) { 2953accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent timestamp.mPosition = (uint32_t)position64; 2954accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent return NO_ERROR; 2955accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent } 2956accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent } 2957accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent return INVALID_OPERATION; 2958accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent} 29591c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 2960054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurentstatus_t AudioFlinger::MixerThread::createAudioPatch_l(const struct audio_patch *patch, 2961054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent audio_patch_handle_t *handle) 2962054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent{ 2963054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent // if !&IDLE, holds the FastMixer state to restore after new parameters processed 2964054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; 2965054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (mFastMixer != 0) { 2966054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 2967054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent FastMixerState *state = sq->begin(); 2968054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (!(state->mCommand & FastMixerState::IDLE)) { 2969054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent previousCommand = state->mCommand; 2970054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent state->mCommand = FastMixerState::HOT_IDLE; 2971054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent sq->end(); 2972054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); 2973054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } else { 2974054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent sq->end(false /*didModify*/); 2975054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 2976054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 2977054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent status_t status = PlaybackThread::createAudioPatch_l(patch, handle); 2978054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 2979054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (!(previousCommand & FastMixerState::IDLE)) { 2980054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent ALOG_ASSERT(mFastMixer != 0); 2981054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 2982054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent FastMixerState *state = sq->begin(); 2983054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); 2984054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent state->mCommand = previousCommand; 2985054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent sq->end(); 2986054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); 2987054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 2988054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 2989054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent return status; 2990054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent} 2991054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 29921c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch, 29931c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_patch_handle_t *handle) 29941c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{ 29951c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status_t status = NO_ERROR; 2996054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 2997054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent // store new device and send to effects 2998054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent audio_devices_t type = AUDIO_DEVICE_NONE; 2999054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent for (unsigned int i = 0; i < patch->num_sinks; i++) { 3000054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent type |= patch->sinks[i].ext.device.type; 3001054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 3002054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 3003054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent#ifdef ADD_BATTERY_DATA 3004054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent // when changing the audio output device, call addBatteryData to notify 3005054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent // the change 3006054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (mOutDevice != type) { 3007054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent uint32_t params = 0; 3008054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent // check whether speaker is on 3009054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (type & AUDIO_DEVICE_OUT_SPEAKER) { 3010054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent params |= IMediaPlayerService::kBatteryDataSpeakerOn; 30111c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 3012054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 3013054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent audio_devices_t deviceWithoutSpeaker 3014054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; 3015054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent // check if any other device (except speaker) is on 3016054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (type & deviceWithoutSpeaker) { 3017054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; 3018054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 3019054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 3020054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (params != 0) { 3021054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent addBatteryData(params); 30221c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 3023054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 3024054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent#endif 30251c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 3026054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 3027054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent mEffectChains[i]->setDevice_l(type); 3028054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 3029054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent mOutDevice = type; 3030296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent mPatch = *patch; 3031054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 3032054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { 30331c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); 30341c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status = hwDevice->create_audio_patch(hwDevice, 30351c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent patch->num_sources, 30361c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent patch->sources, 30371c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent patch->num_sinks, 30381c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent patch->sinks, 30391c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent handle); 30401c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } else { 3041054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent char *address; 3042054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (strcmp(patch->sinks[0].ext.device.address, "") != 0) { 3043054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent //FIXME: we only support address on first sink with HAL version < 3.0 3044054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent address = audio_device_address_to_parameter( 3045054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent patch->sinks[0].ext.device.type, 3046054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent patch->sinks[0].ext.device.address); 3047054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } else { 3048054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent address = (char *)calloc(1, 1); 3049054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 3050054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent AudioParameter param = AudioParameter(String8(address)); 3051054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent free(address); 3052054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), (int)type); 3053054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent status = mOutput->stream->common.set_parameters(&mOutput->stream->common, 3054054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent param.toString().string()); 3055054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent *handle = AUDIO_PATCH_HANDLE_NONE; 3056054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 3057296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); 3058054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent return status; 3059054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent} 3060054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 3061054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurentstatus_t AudioFlinger::MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle) 3062054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent{ 3063054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent // if !&IDLE, holds the FastMixer state to restore after new parameters processed 3064054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; 3065054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (mFastMixer != 0) { 3066054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 3067054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent FastMixerState *state = sq->begin(); 3068054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (!(state->mCommand & FastMixerState::IDLE)) { 3069054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent previousCommand = state->mCommand; 3070054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent state->mCommand = FastMixerState::HOT_IDLE; 3071054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent sq->end(); 3072054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); 3073054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } else { 3074054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent sq->end(false /*didModify*/); 3075054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 30761c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 3077054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 3078054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent status_t status = PlaybackThread::releaseAudioPatch_l(handle); 3079054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 3080054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (!(previousCommand & FastMixerState::IDLE)) { 3081054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent ALOG_ASSERT(mFastMixer != 0); 3082054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 3083054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent FastMixerState *state = sq->begin(); 3084054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); 3085054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent state->mCommand = previousCommand; 3086054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent sq->end(); 3087054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); 3088054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 3089054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 30901c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent return status; 30911c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent} 30921c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 30931c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle) 30941c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{ 30951c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status_t status = NO_ERROR; 3096054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 3097054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent mOutDevice = AUDIO_DEVICE_NONE; 3098054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 30991c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { 31001c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); 31011c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status = hwDevice->release_audio_patch(hwDevice, handle); 31021c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } else { 3103054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent AudioParameter param; 3104054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0); 3105054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent status = mOutput->stream->common.set_parameters(&mOutput->stream->common, 3106054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent param.toString().string()); 31071c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 31081c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent return status; 31091c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent} 31101c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 311183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track) 311283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{ 311383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent Mutex::Autolock _l(mLock); 311483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent mTracks.add(track); 311583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent} 311683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 311783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track) 311883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{ 311983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent Mutex::Autolock _l(mLock); 312083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent destroyTrack_l(track); 312183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent} 312283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 312383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::PlaybackThread::getAudioPortConfig(struct audio_port_config *config) 312483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{ 312583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent ThreadBase::getAudioPortConfig(config); 312683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->role = AUDIO_PORT_ROLE_SOURCE; 312783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->ext.mix.hw_module = mOutput->audioHwDev->handle(); 312883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; 312983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent} 313083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 313181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 313281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 313381784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, 313481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t id, audio_devices_t device, type_t type) 313581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent : PlaybackThread(audioFlinger, output, id, device, type), 313681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mAudioMixer below 313781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mFastMixer below 313881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixerFutex(0) 313981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mOutputSink below 314081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mPipeSink below 314181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mNormalSink below 314281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 314381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type); 3144f6ed423af92a56ef54bba23eba883b1f21448b54Glenn Kasten ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%d, mFrameSize=%u, " 314581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "mFrameCount=%d, mNormalFrameCount=%d", 314681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, 314781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalFrameCount); 314881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); 314981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3150fbfc3959f4aac839445edc7075532067fef497c2Andy Hung if (type == DUPLICATING) { 3151fbfc3959f4aac839445edc7075532067fef497c2Andy Hung // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks 3152fbfc3959f4aac839445edc7075532067fef497c2Andy Hung // (downstream MixerThreads) in DuplicatingThread::threadLoop_write(). 3153fbfc3959f4aac839445edc7075532067fef497c2Andy Hung // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink. 3154fbfc3959f4aac839445edc7075532067fef497c2Andy Hung return; 3155fbfc3959f4aac839445edc7075532067fef497c2Andy Hung } 315681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create an NBAIO sink for the HAL output stream, and negotiate 315781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutputSink = new AudioStreamOutSink(output->stream); 315881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t numCounterOffers = 0; 3159f69f9869514730aebe5724c461768507084dfff7Glenn Kasten const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; 316081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ssize_t index = mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); 316181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(index == 0); 316281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 316381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // initialize fast mixer depending on configuration 316481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool initFastMixer; 316581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent switch (kUseFastMixer) { 316681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Never: 316781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent initFastMixer = false; 316881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 316981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Always: 317081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent initFastMixer = true; 317181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 317281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Static: 317381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Dynamic: 317481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent initFastMixer = mFrameCount < mNormalFrameCount; 317581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 317681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 317781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (initFastMixer) { 31781258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung audio_format_t fastMixerFormat; 31791258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung if (mMixerBufferEnabled && mEffectBufferEnabled) { 31801258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT; 31811258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung } else { 31821258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT; 31831258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung } 31841258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung if (mFormat != fastMixerFormat) { 31851258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung // change our Sink format to accept our intermediate precision 31861258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung mFormat = fastMixerFormat; 31871258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung free(mSinkBuffer); 31881258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); 31891258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; 31901258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); 31911258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung } 319281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 319381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create a MonoPipe to connect our submix to FastMixer 319481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent NBAIO_Format format = mOutputSink->format(); 3195ba0b34c18da93681c0813ecdab19b0e215b6d261Glenn Kasten NBAIO_Format origformat = format; 31961258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung // adjust format to match that of the Fast Mixer 319797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten ALOGV("format changed from %d to %d", format.mFormat, fastMixerFormat); 31981258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung format.mFormat = fastMixerFormat; 31991258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount; 32001258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung 320181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // This pipe depth compensates for scheduling latency of the normal mixer thread. 320281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // When it wakes up after a maximum latency, it runs a few cycles quickly before 320381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // finally blocking. Note the pipe implementation rounds up the request to a power of 2. 320481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); 320581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const NBAIO_Format offers[1] = {format}; 320681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t numCounterOffers = 0; 320781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ssize_t index = monoPipe->negotiate(offers, 1, NULL, numCounterOffers); 320881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(index == 0); 320981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent monoPipe->setAvgFrames((mScreenState & 1) ? 321081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); 321181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPipeSink = monoPipe; 321281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 321346909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 3214da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (mTeeSinkOutputEnabled) { 3215da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten // create a Pipe to archive a copy of FastMixer's output for dumpsys 3216ba0b34c18da93681c0813ecdab19b0e215b6d261Glenn Kasten Pipe *teeSink = new Pipe(mTeeSinkOutputFrames, origformat); 3217ba0b34c18da93681c0813ecdab19b0e215b6d261Glenn Kasten const NBAIO_Format offers2[1] = {origformat}; 3218da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten numCounterOffers = 0; 3219ba0b34c18da93681c0813ecdab19b0e215b6d261Glenn Kasten index = teeSink->negotiate(offers2, 1, NULL, numCounterOffers); 3220da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten ALOG_ASSERT(index == 0); 3221da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten mTeeSink = teeSink; 3222da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten PipeReader *teeSource = new PipeReader(*teeSink); 3223da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten numCounterOffers = 0; 3224ba0b34c18da93681c0813ecdab19b0e215b6d261Glenn Kasten index = teeSource->negotiate(offers2, 1, NULL, numCounterOffers); 3225da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten ALOG_ASSERT(index == 0); 3226da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten mTeeSource = teeSource; 3227da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 322846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 322981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 323081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create fast mixer and configure it initially with just one fast track for our submix 323181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixer = new FastMixer(); 323281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 323381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef STATE_QUEUE_DUMP 323481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->setObserverDump(&mStateQueueObserverDump); 323581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->setMutatorDump(&mStateQueueMutatorDump); 323681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 323781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerState *state = sq->begin(); 323881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastTrack *fastTrack = &state->mFastTracks[0]; 323981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // wrap the source side of the MonoPipe to make it an AudioBufferProvider 324081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); 324181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mVolumeProvider = NULL; 3242e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung fastTrack->mChannelMask = mChannelMask; // mPipeSink channel mask for audio to FastMixer 3243e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer 324481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mGeneration++; 324581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mFastTracksGen++; 324681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mTrackMask = 1; 324781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // fast mixer will use the HAL output sink 324881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mOutputSink = mOutputSink.get(); 324981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mOutputSinkGen++; 325081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mFrameCount = mFrameCount; 325181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand = FastMixerState::COLD_IDLE; 325281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // already done in constructor initialization list 325381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent //mFastMixerFutex = 0; 325481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mColdFutexAddr = &mFastMixerFutex; 325581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mColdGen++; 325681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mDumpState = &mFastMixerDumpState; 325746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 325881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mTeeSink = mTeeSink.get(); 325946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 32609e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer"); 32619e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten state->mNBLogWriter = mFastMixerNBLogWriter.get(); 326281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(); 326381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); 326481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 326581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // start the fast mixer 326681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); 326781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t tid = mFastMixer->getTid(); 326881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int err = requestPriority(getpid_cached, tid, kPriorityFastMixer); 326981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (err != 0) { 327081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", 327181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent kPriorityFastMixer, getpid_cached, tid, err); 327281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 327381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 327481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG 327581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create and start the watchdog 327681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog = new AudioWatchdog(); 327781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->setDump(&mAudioWatchdogDump); 327881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); 327981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent tid = mAudioWatchdog->getTid(); 328081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent err = requestPriority(getpid_cached, tid, kPriorityFastMixer); 328181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (err != 0) { 328281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", 328381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent kPriorityFastMixer, getpid_cached, tid, err); 328481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 328581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 328681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 328781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 328881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 328981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent switch (kUseFastMixer) { 329081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Never: 329181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Dynamic: 329281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink = mOutputSink; 329381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 329481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Always: 329581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink = mPipeSink; 329681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 329781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case FastMixer_Static: 329881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink = initFastMixer ? mPipeSink : mOutputSink; 329981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 330081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 330181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 330281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 330381784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::MixerThread::~MixerThread() 330481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 33054d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten if (mFastMixer != 0) { 330681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 330781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerState *state = sq->begin(); 330881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (state->mCommand == FastMixerState::COLD_IDLE) { 330981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int32_t old = android_atomic_inc(&mFastMixerFutex); 331081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (old == -1) { 3311ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); 331281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 331381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 331481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand = FastMixerState::EXIT; 331581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(); 331681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); 331781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixer->join(); 331881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Though the fast mixer thread has exited, it's state queue is still valid. 331981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // We'll use that extract the final state which contains one remaining fast track 332081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // corresponding to our sub-mix. 332181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state = sq->begin(); 332281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(state->mTrackMask == 1); 332381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastTrack *fastTrack = &state->mFastTracks[0]; 332481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(fastTrack->mBufferProvider != NULL); 332581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent delete fastTrack->mBufferProvider; 332681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(false /*didModify*/); 33274d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten mFastMixer.clear(); 332881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG 332981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mAudioWatchdog != 0) { 333081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->requestExit(); 333181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->requestExitAndWait(); 333281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog.clear(); 333381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 333481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 333581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 33369e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter); 333781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent delete mAudioMixer; 333881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 333981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 334081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 334181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const 334281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 33434d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten if (mFastMixer != 0) { 334481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); 334581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent latency += (pipe->getAvgFrames() * 1000) / mSampleRate; 334681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 334781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return latency; 334881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 334981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 335081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 335181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) 335281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 335381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread::threadLoop_removeTracks(tracksToRemove); 335481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 335581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3356bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentssize_t AudioFlinger::MixerThread::threadLoop_write() 335781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 335881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME we should only do one push per cycle; confirm this is true 335981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Start the fast mixer if it's not already running 33604d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten if (mFastMixer != 0) { 336181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 336281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerState *state = sq->begin(); 336381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (state->mCommand != FastMixerState::MIX_WRITE && 336481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { 336581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (state->mCommand == FastMixerState::COLD_IDLE) { 336681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int32_t old = android_atomic_inc(&mFastMixerFutex); 336781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (old == -1) { 3368ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); 336981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 337081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG 337181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mAudioWatchdog != 0) { 337281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->resume(); 337381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 337481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 337581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 337681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand = FastMixerState::MIX_WRITE; 3377d797a9d5daad3051f9ac1d348abc2f434ea3ddcfGlenn Kasten#ifdef FAST_THREAD_STATISTICS 33784182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? 3379fbdb2aceab7317aa44bc8f301a93eb49e17b2bceGlenn Kasten FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN); 3380d797a9d5daad3051f9ac1d348abc2f434ea3ddcfGlenn Kasten#endif 338181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(); 338281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); 338381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (kUseFastMixer == FastMixer_Dynamic) { 338481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink = mPipeSink; 338581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 338681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 338781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(false /*didModify*/); 338881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 338981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 3390bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return PlaybackThread::threadLoop_write(); 339181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 339281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 339381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::threadLoop_standby() 339481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 339581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Idle the fast mixer if it's currently running 33964d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten if (mFastMixer != 0) { 339781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 339881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerState *state = sq->begin(); 339981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!(state->mCommand & FastMixerState::IDLE)) { 340081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand = FastMixerState::COLD_IDLE; 340181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mColdFutexAddr = &mFastMixerFutex; 340281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mColdGen++; 340381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixerFutex = 0; 340481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(); 340581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now 340681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); 340781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (kUseFastMixer == FastMixer_Dynamic) { 340881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink = mOutputSink; 340981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 341081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG 341181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mAudioWatchdog != 0) { 341281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->pause(); 341381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 341481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 341581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 341681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(false /*didModify*/); 341781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 341881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 341981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread::threadLoop_standby(); 342081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 342181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3422bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::PlaybackThread::waitingAsyncCallback_l() 3423bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 3424bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return false; 3425bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 3426bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 3427bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::PlaybackThread::shouldStandby_l() 3428bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 3429bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return !mStandby; 3430bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 3431bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 3432bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::PlaybackThread::waitingAsyncCallback() 3433bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 3434bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 3435bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return waitingAsyncCallback_l(); 3436bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 3437bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 343881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// shared by MIXER and DIRECT, overridden by DUPLICATING 343981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::threadLoop_standby() 344081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 344181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended); 3442062e67a26e0553dd142be622821f493df541f0c6Phil Burk mOutput->standby(); 3443bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mUseAsyncWrite != 0) { 34443b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // discard any pending drain or write ack by incrementing sequence 34453b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence = (mWriteAckSequence + 2) & ~1; 34463b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence = (mDrainSequence + 2) & ~1; 3447bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 34483b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setWriteBlocked(mWriteAckSequence); 34493b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setDraining(mDrainSequence); 3450bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 3451d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent mHwPaused = false; 345281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 345381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 34544c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew Georgevoid AudioFlinger::PlaybackThread::onAddNewTrack_l() 34554c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George{ 34564c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George ALOGV("signal playback thread"); 34574c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George broadcast_l(); 34584c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George} 34594c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George 346081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::threadLoop_mix() 346181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 346281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // obtain the presentation timestamp of the next output buffer 346381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int64_t pts; 346481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status = INVALID_OPERATION; 346581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 346681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mNormalSink != 0) { 346781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = mNormalSink->getNextWriteTimestamp(&pts); 346881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 346981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = mOutputSink->getNextWriteTimestamp(&pts); 347081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 347181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 347281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (status != NO_ERROR) { 347381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pts = AudioBufferProvider::kInvalidPTS; 347481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 347581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 347681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mix buffers... 347781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->process(pts); 347825c2dac12114699e90deb1c579cadebce7b91a97Andy Hung mCurrentWriteLength = mSinkBufferSize; 347981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // increase sleep time progressively when application underrun condition clears. 348081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Only increase sleep time if the mixer is ready for two consecutive times to avoid 348181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // that a steady state of alternating ready/not ready conditions keeps the sleep time 348281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // such that we would underrun the audio HAL. 348381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((sleepTime == 0) && (sleepTimeShift > 0)) { 348481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTimeShift--; 348581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 348681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = 0; 348781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent standbyTime = systemTime() + standbyDelay; 348881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent //TODO: delay standby when effects have a tail 34894c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten 349081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 349181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 349281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::threadLoop_sleepTime() 349381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 349481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If no tracks are ready, sleep once for the duration of an output 349581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // buffer size, then write 0s to the output 349681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sleepTime == 0) { 349781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mMixerStatus == MIXER_TRACKS_ENABLED) { 349881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = activeSleepTime >> sleepTimeShift; 349981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sleepTime < kMinThreadSleepTimeUs) { 350081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = kMinThreadSleepTimeUs; 350181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 350281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // reduce sleep time in case of consecutive application underruns to avoid 350381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer 350481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // duration we would end up writing less data than needed by the audio HAL if 350581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // the condition persists. 350681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sleepTimeShift < kMaxThreadSleepTimeShift) { 350781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTimeShift++; 350881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 350981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 351081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = idleSleepTime; 351181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 351281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { 351398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared 351498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // before effects processing or output. 351598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mMixerBufferValid) { 351698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung memset(mMixerBuffer, 0, mMixerBufferSize); 351798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } else { 351898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung memset(mSinkBuffer, 0, mSinkBufferSize); 351998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 352081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = 0; 352181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), 352281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "anticipated start"); 352381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 352481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // TODO add standby time extension fct of effect tail 352581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 352681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 352781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// prepareTracks_l() must be called with ThreadBase::mLock held 352881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( 352981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<Track> > *tracksToRemove) 353081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 353181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 353281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixer_state mixerStatus = MIXER_IDLE; 353381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // find out which tracks need to be processed 353481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t count = mActiveTracks.size(); 353581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t mixedTracks = 0; 353681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t tracksWithEffect = 0; 353781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // counts only _active_ fast tracks 353881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t fastTracks = 0; 353981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset 354081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 354181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent float masterVolume = mMasterVolume; 354281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool masterMute = mMasterMute; 354381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 354481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (masterMute) { 354581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent masterVolume = 0; 354681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 354781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Delegate master volume control to effect in output mix effect chain if needed 354881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); 354981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 355081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t v = (uint32_t)(masterVolume * (1 << 24)); 355181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setVolume_l(&v, &v); 355281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent masterVolume = (float)((v + (1 << 23)) >> 24); 355381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain.clear(); 355481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 355581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 355681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // prepare a new state to push 355781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue *sq = NULL; 355881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerState *state = NULL; 355981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool didModify = false; 356081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; 35614d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten if (mFastMixer != 0) { 356281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq = mFastMixer->sq(); 356381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state = sq->begin(); 356481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 356581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 356669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferValid = false; // mMixerBuffer has no valid data until appropriate tracks found. 356798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found. 356869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung 356981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i=0 ; i<count ; i++) { 35709fdcb0a9497ca290bcf364b10868587b6bde3a34Glenn Kasten const sp<Track> t = mActiveTracks[i].promote(); 357181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (t == 0) { 357281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 357381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 357481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 357581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // this const just means the local variable doesn't change 357681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Track* const track = t.get(); 357781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 357881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // process fast tracks 357981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isFastTrack()) { 358081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 358181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // It's theoretically possible (though unlikely) for a fast track to be created 358281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // and then removed within the same normal mix cycle. This is not a problem, as 358381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // the track never becomes active so it's fast mixer slot is never touched. 358481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The converse, of removing an (active) track and then creating a new track 358581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // at the identical fast mixer slot within the same normal mix cycle, 358681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // is impossible because the slot isn't marked available until the end of each cycle. 358781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int j = track->mFastIndex; 358881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(0 < j && j < (int)FastMixerState::kMaxFastTracks); 358981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); 359081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastTrack *fastTrack = &state->mFastTracks[j]; 359181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 359281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Determine whether the track is currently in underrun condition, 359381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // and whether it had a recent underrun. 359481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; 359581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastTrackUnderruns underruns = ftDump->mUnderruns; 359681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t recentFull = (underruns.mBitFields.mFull - 359781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK; 359881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t recentPartial = (underruns.mBitFields.mPartial - 359981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK; 360081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t recentEmpty = (underruns.mBitFields.mEmpty - 360181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK; 360281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t recentUnderruns = recentPartial + recentEmpty; 360381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mObservedUnderruns = underruns; 360481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // don't count underruns that occur while stopping or pausing 360581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // or stopped which can occur when flush() is called while active 360682aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten if (!(track->isStopping() || track->isPausing() || track->isStopped()) && 360782aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten recentUnderruns > 0) { 360882aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun 360982aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten track->mAudioTrackServerProxy->tallyUnderrunFrames(recentUnderruns * mFrameCount); 361081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 361181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 361281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // This is similar to the state machine for normal tracks, 361381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // with a few modifications for fast tracks. 361481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool isActive = true; 361581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent switch (track->mState) { 361681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::STOPPING_1: 361781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track stays active in STOPPING_1 state until first underrun 3618bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (recentUnderruns > 0 || track->isTerminated()) { 361981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mState = TrackBase::STOPPING_2; 362081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 362181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 362281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::PAUSING: 362381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // ramp down is not yet implemented 362481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->setPaused(); 362581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 362681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::RESUMING: 362781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // ramp up is not yet implemented 362881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mState = TrackBase::ACTIVE; 362981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 363081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::ACTIVE: 363181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (recentFull > 0 || recentPartial > 0) { 363281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track has provided at least some frames recently: reset retry count 363381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mRetryCount = kMaxTrackRetries; 363481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 363581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (recentUnderruns == 0) { 363681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // no recent underruns: stay active 363781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 363881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 363981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // there has recently been an underrun of some kind 364081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->sharedBuffer() == 0) { 364181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // were any of the recent underruns "empty" (no frames available)? 364281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (recentEmpty == 0) { 364381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // no, then ignore the partial underruns as they are allowed indefinitely 364481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 364581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 364681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // there has recently been an "empty" underrun: decrement the retry counter 364781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (--(track->mRetryCount) > 0) { 364881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 364981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 365081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // indicate to client process that the track was disabled because of underrun; 365181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it will then automatically call start() when data is available 365296f60d8f04432a1ed503b3e24d5736d28c63c9a2Glenn Kasten android_atomic_or(CBLK_DISABLED, &track->mCblk->mFlags); 365381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // remove from active list, but state remains ACTIVE [confusing but true] 365481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent isActive = false; 365581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 365681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 365781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // fall through 365881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::STOPPING_2: 365981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::PAUSED: 366081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::STOPPED: 366181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::FLUSHED: // flush() while active 366281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Check for presentation complete if track is inactive 366381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // We have consumed all the buffers of this track. 366481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // This would be incomplete if we auto-paused on underrun 366581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 366681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t audioHALFrames = 366781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; 366881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t framesWritten = mBytesWritten / mFrameSize; 366981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { 367081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track stays in active list until presentation is complete 367181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 367281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 367381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 367481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isStopping_2()) { 367581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mState = TrackBase::STOPPED; 367681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 367781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isStopped()) { 367881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Can't reset directly, as fast mixer is still polling this track 367981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track->reset(); 368081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // So instead mark this track as needing to be reset after push with ack 368181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent resetMask |= 1 << i; 368281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 368381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent isActive = false; 368481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 368581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TrackBase::IDLE: 368681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent default: 3687adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("unexpected track state %d", track->mState); 368881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 368981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 369081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (isActive) { 369181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // was it previously inactive? 369281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!(state->mTrackMask & (1 << j))) { 369381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ExtendedAudioBufferProvider *eabp = track; 369481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent VolumeProvider *vp = track; 369581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mBufferProvider = eabp; 369681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mVolumeProvider = vp; 369781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mChannelMask = track->mChannelMask; 3698e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung fastTrack->mFormat = track->mFormat; 369981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mGeneration++; 370081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mTrackMask |= 1 << j; 370181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent didModify = true; 370281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // no acknowledgement required for newly active tracks 370381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 370481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // cache the combined master volume and stream type volume for fast mixer; this 370581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // lacks any synchronization or barrier so VolumeProvider may read a stale value 3706e4756fe3a387615acb63c6a05788c8db9b5786cbGlenn Kasten track->mCachedVolume = masterVolume * mStreamTypes[track->streamType()].volume; 370781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ++fastTracks; 370881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 370981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // was it previously active? 371081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (state->mTrackMask & (1 << j)) { 371181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mBufferProvider = NULL; 371281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent fastTrack->mGeneration++; 371381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mTrackMask &= ~(1 << j); 371481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent didModify = true; 371581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If any fast tracks were removed, we must wait for acknowledgement 371681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // because we're about to decrement the last sp<> on those tracks. 371781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; 371881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 3719adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("fast track %d should have been active", j); 372081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 372181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent tracksToRemove->add(track); 372281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Avoids a misleading display in dumpsys 372381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL; 372481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 372581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 372681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 372781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 372881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // local variable scope to avoid goto warning 372981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 373081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_track_cblk_t* cblk = track->cblk(); 373181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 373281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The first time a track is added we wait 373381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // for all its buffers to be filled before processing it 373481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int name = track->name(); 373581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // make sure that we have enough frames to mix one full buffer. 373681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // enforce this condition only once to enable draining the buffer in case the client 373781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // app does not call stop() and relies on underrun to stop: 373881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed 373981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // during last round 37409f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten size_t desiredFrames; 37418edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung const uint32_t sampleRate = track->mAudioTrackServerProxy->getSampleRate(); 37425a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); 37438edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung 37448edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung desiredFrames = sourceFramesNeededWithTimestretch( 37455a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia sampleRate, mNormalFrameCount, mSampleRate, playbackRate.mSpeed); 37468edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung // TODO: ONLY USED FOR LEGACY RESAMPLERS, remove when they are removed. 37478edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung // add frames already consumed but not yet released by the resampler 37488edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung // because mAudioTrackServerProxy->framesReady() will include these frames 37498edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung desiredFrames += mAudioMixer->getUnreleasedFrames(track->name()); 37508edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung 375181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t minFrames = 1; 375281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && 375381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { 37549f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten minFrames = desiredFrames; 37559f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 375613e4c960ea3db03a43e084fbd85d52aa77f7b871Eric Laurent 375713e4c960ea3db03a43e084fbd85d52aa77f7b871Eric Laurent size_t framesReady = track->framesReady(); 3758e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten if (ATRACE_ENABLED()) { 3759e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten // I wish we had formatted trace names 3760e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten char traceName[16]; 3761e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten strcpy(traceName, "nRdy"); 3762e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten int name = track->name(); 3763e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten if (AudioMixer::TRACK0 <= name && 3764e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten name < (int) (AudioMixer::TRACK0 + AudioMixer::MAX_NUM_TRACKS)) { 3765e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten name -= AudioMixer::TRACK0; 3766e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten traceName[4] = (name / 10) + '0'; 3767e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten traceName[5] = (name % 10) + '0'; 3768e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten } else { 3769e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten traceName[4] = '?'; 3770e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten traceName[5] = '?'; 3771e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten } 3772e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten traceName[6] = '\0'; 3773e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten ATRACE_INT(traceName, framesReady); 3774e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten } 37759f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if ((framesReady >= minFrames) && track->isReady() && 377681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent !track->isPaused() && !track->isTerminated()) 377781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 3778f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten ALOGVV("track %d s=%08x [OK] on thread %p", name, cblk->mServer, this); 377981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 378081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixedTracks++; 378181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 378269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // track->mainBuffer() != mSinkBuffer or mMixerBuffer means 378369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // there is an effect chain connected to the track 378481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain.clear(); 378569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung if (track->mainBuffer() != mSinkBuffer && 378669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung track->mainBuffer() != mMixerBuffer) { 378798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung if (mEffectBufferEnabled) { 378898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung mEffectBufferValid = true; // Later can set directly. 378998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung } 379081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain = getEffectChain_l(track->sessionId()); 379181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Delegate volume control to effect in track effect chain if needed 379281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 379381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent tracksWithEffect++; 379481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 379581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on " 379681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "session %d", 379781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent name, track->sessionId()); 379881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 379981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 380081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 380181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 380281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int param = AudioMixer::VOLUME; 380381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->mFillingUpStatus == Track::FS_FILLED) { 380481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // no ramp for the first volume setting 380581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mFillingUpStatus = Track::FS_ACTIVE; 380681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->mState == TrackBase::RESUMING) { 380781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mState = TrackBase::ACTIVE; 380881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent param = AudioMixer::RAMP_VOLUME; 380981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 381081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); 3811f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten // FIXME should not make a decision based on mServer 3812f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten } else if (cblk->mServer != 0) { 381381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If the track is stopped before the first frame was mixed, 381481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // do not apply ramp 381581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent param = AudioMixer::RAMP_VOLUME; 381681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 381781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 381881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // compute volume for this track 38196be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung uint32_t vl, vr; // in U8.24 integer format 38206be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung float vlf, vrf, vaf; // in [0.0, 1.0] float format 3821e4756fe3a387615acb63c6a05788c8db9b5786cbGlenn Kasten if (track->isPausing() || mStreamTypes[track->streamType()].mute) { 38226be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vl = vr = 0; 38236be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vlf = vrf = vaf = 0.; 382481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isPausing()) { 382581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->setPaused(); 382681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 382781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 382881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 382981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // read original volumes with volume control 383081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent float typeVolume = mStreamTypes[track->streamType()].volume; 383181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent float v = masterVolume * typeVolume; 38329f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; 3833c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten gain_minifloat_packed_t vlr = proxy->getVolumeLR(); 38346be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); 38356be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); 383681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track volumes come from shared memory, so can't be trusted and must be clamped 3837c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten if (vlf > GAIN_FLOAT_UNITY) { 3838c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten ALOGV("Track left volume out of range: %.3g", vlf); 3839c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten vlf = GAIN_FLOAT_UNITY; 384081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 3841c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten if (vrf > GAIN_FLOAT_UNITY) { 3842c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten ALOGV("Track right volume out of range: %.3g", vrf); 3843c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten vrf = GAIN_FLOAT_UNITY; 384481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 384581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // now apply the master volume and stream type volume 38466be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vlf *= v; 38476be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vrf *= v; 384881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // assuming master volume and stream type volume each go up to 1.0, 38496be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung // then derive vl and vr as U8.24 versions for the effect chain 38506be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT; 38516be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vl = (uint32_t) (scaleto8_24 * vlf); 38526be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vr = (uint32_t) (scaleto8_24 * vrf); 38536be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung // vl and vr are now in U8.24 format 3854e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten uint16_t sendLevel = proxy->getSendLevel_U4_12(); 385581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // send level comes from shared memory and so may be corrupt 385681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sendLevel > MAX_GAIN_INT) { 385781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("Track send level out of range: %04X", sendLevel); 385881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sendLevel = MAX_GAIN_INT; 385981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 38606be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung // vaf is represented as [0.0, 1.0] float by rescaling sendLevel 38616be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung vaf = v * sendLevel * (1. / MAX_GAIN_INT); 386281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 3863bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 386481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Delegate volume control to effect in track effect chain if needed 386581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0 && chain->setVolume_l(&vl, &vr)) { 386681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Do not ramp volume if volume is controlled by effect 386781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent param = AudioMixer::VOLUME; 3868b6be7f22a82ee3bad8bcc709d21e72fc4727da09Bryant Liu // Update remaining floating point volume levels 3869b6be7f22a82ee3bad8bcc709d21e72fc4727da09Bryant Liu vlf = (float)vl / (1 << 24); 3870b6be7f22a82ee3bad8bcc709d21e72fc4727da09Bryant Liu vrf = (float)vr / (1 << 24); 387181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mHasVolumeController = true; 387281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 387381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // force no volume ramp when volume controller was just disabled or removed 387481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // from effect chain to avoid volume spike 387581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->mHasVolumeController) { 387681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent param = AudioMixer::VOLUME; 387781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 387881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mHasVolumeController = false; 387981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 388081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 388181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // XXX: these things DON'T need to be done each time 388281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->setBufferProvider(name, track); 388381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->enable(name); 388481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 38856be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, &vlf); 38866be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, &vrf); 38876be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, &vaf); 388881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->setParameter( 388981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent name, 389081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::TRACK, 389181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::FORMAT, (void *)track->format()); 389281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->setParameter( 389381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent name, 389481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::TRACK, 3895377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask()); 38969a59276fb465e492138e0576523b54079671e8f4Andy Hung mAudioMixer->setParameter( 38979a59276fb465e492138e0576523b54079671e8f4Andy Hung name, 38989a59276fb465e492138e0576523b54079671e8f4Andy Hung AudioMixer::TRACK, 38999a59276fb465e492138e0576523b54079671e8f4Andy Hung AudioMixer::MIXER_CHANNEL_MASK, (void *)(uintptr_t)mChannelMask); 3900e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten // limit track sample rate to 2 x output sample rate, which changes at re-configuration 3901cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX; 39029f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten uint32_t reqSampleRate = track->mAudioTrackServerProxy->getSampleRate(); 3903e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten if (reqSampleRate == 0) { 3904e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten reqSampleRate = mSampleRate; 3905e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten } else if (reqSampleRate > maxSampleRate) { 3906e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten reqSampleRate = maxSampleRate; 3907e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten } 390881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->setParameter( 390981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent name, 391081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::RESAMPLE, 391181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::SAMPLE_RATE, 3912377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT (void *)(uintptr_t)reqSampleRate); 39138edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung 39145a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); 39158edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung mAudioMixer->setParameter( 39168edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung name, 39178edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung AudioMixer::TIMESTRETCH, 39188edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung AudioMixer::PLAYBACK_RATE, 39195a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia &playbackRate); 39208edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung 392169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung /* 392269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * Select the appropriate output buffer for the track. 392369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * 392498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung * Tracks with effects go into their own effects chain buffer 392598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung * and from there into either mEffectBuffer or mSinkBuffer. 392669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * 392769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * Other tracks can use mMixerBuffer for higher precision 392869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * channel accumulation. If this buffer is enabled 392969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * (mMixerBufferEnabled true), then selected tracks will accumulate 393069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * into it. 393169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung * 393269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung */ 393369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung if (mMixerBufferEnabled 393469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung && (track->mainBuffer() == mSinkBuffer 393569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung || track->mainBuffer() == mMixerBuffer)) { 393669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mAudioMixer->setParameter( 393769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung name, 393869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung AudioMixer::TRACK, 3939788207057ed4b8df4719ed8089f376ef52de9ca1Andy Hung AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat); 394069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mAudioMixer->setParameter( 394169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung name, 394269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung AudioMixer::TRACK, 394369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer); 394469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // TODO: override track->mainBuffer()? 394569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mMixerBufferValid = true; 394669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung } else { 394769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mAudioMixer->setParameter( 394869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung name, 394969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung AudioMixer::TRACK, 3950788207057ed4b8df4719ed8089f376ef52de9ca1Andy Hung AudioMixer::MIXER_FORMAT, (void *)AUDIO_FORMAT_PCM_16_BIT); 395169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung mAudioMixer->setParameter( 395269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung name, 395369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung AudioMixer::TRACK, 395469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); 395569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung } 395681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->setParameter( 395781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent name, 395881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::TRACK, 395981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); 396081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 396181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // reset retry count 396281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mRetryCount = kMaxTrackRetries; 396381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 396481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If one track is ready, set the mixer ready if: 396581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // - the mixer was not ready during previous round OR 396681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // - no other track is not ready 396781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || 396881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixerStatus != MIXER_TRACKS_ENABLED) { 396981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixerStatus = MIXER_TRACKS_READY; 397081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 397181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 39729f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { 397382aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten track->mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames); 39749f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 397581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // clear effect chain input buffer if an active track underruns to avoid sending 397681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // previous audio buffer again to effects 397781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain = getEffectChain_l(track->sessionId()); 397881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain != 0) { 397981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->clearInputBuffer(); 398081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 398181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3982f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten ALOGVV("track %d s=%08x [NOT READY] on thread %p", name, cblk->mServer, this); 398381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((track->sharedBuffer() != 0) || track->isTerminated() || 398481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->isStopped() || track->isPaused()) { 398581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // We have consumed all the buffers of this track. 398681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Remove it from the list of active tracks. 398781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // TODO: use actual buffer filling status instead of latency when available from 398881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // audio HAL 398981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; 399081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t framesWritten = mBytesWritten / mFrameSize; 399181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { 399281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isStopped()) { 399381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->reset(); 399481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 399581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent tracksToRemove->add(track); 399681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 399781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 399881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // No buffers for this track. Give it a few chances to 399981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // fill a buffer, then remove it from active list. 400081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (--(track->mRetryCount) <= 0) { 4001c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten ALOGI("BUFFER TIMEOUT: remove(%d) from active list on thread %p", name, this); 400281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent tracksToRemove->add(track); 400381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // indicate to client process that the track was disabled because of underrun; 400481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it will then automatically call start() when data is available 400596f60d8f04432a1ed503b3e24d5736d28c63c9a2Glenn Kasten android_atomic_or(CBLK_DISABLED, &cblk->mFlags); 400681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If one track is not ready, mark the mixer also not ready if: 400781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // - the mixer was ready during previous round OR 400881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // - no other track is ready 400981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || 401081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixerStatus != MIXER_TRACKS_READY) { 401181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixerStatus = MIXER_TRACKS_ENABLED; 401281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 401381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 401481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->disable(name); 401581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 401681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 401781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } // local variable scope to avoid goto warning 401881784c37c61b09289654b979567a42bf73cd2b12Eric Laurenttrack_is_ready: ; 401981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 402081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 402181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 402281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Push the new FastMixer state if necessary 402381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool pauseAudioWatchdog = false; 402481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (didModify) { 402581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mFastTracksGen++; 402681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle 402781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (kUseFastMixer == FastMixer_Dynamic && 402881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { 402981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand = FastMixerState::COLD_IDLE; 403081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mColdFutexAddr = &mFastMixerFutex; 403181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mColdGen++; 403281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mFastMixerFutex = 0; 403381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (kUseFastMixer == FastMixer_Dynamic) { 403481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mNormalSink = mOutputSink; 403581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 403681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If we go into cold idle, need to wait for acknowledgement 403781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // so that fast mixer stops doing I/O. 403881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; 403981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pauseAudioWatchdog = true; 404081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 404181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 404281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sq != NULL) { 404381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(didModify); 404481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->push(block); 404581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 404681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG 404781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (pauseAudioWatchdog && mAudioWatchdog != 0) { 404881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioWatchdog->pause(); 404981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 405081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 405181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 405281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Now perform the deferred reset on fast tracks that have stopped 405381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent while (resetMask != 0) { 405481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t i = __builtin_ctz(resetMask); 405581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(i < count); 405681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent resetMask &= ~(1 << i); 405781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> t = mActiveTracks[i].promote(); 405881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (t == 0) { 405981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 406081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 406181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Track* track = t.get(); 406281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(track->isFastTrack() && track->isStopped()); 406381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->reset(); 406481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 406581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 406681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // remove all the tracks that need to be... 4067bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent removeTracks_l(*tracksToRemove); 406881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 406997d547da43c9c41711d1ed1e3f4fa87c2ee3cb9aEric Laurent if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0) { 407097d547da43c9c41711d1ed1e3f4fa87c2ee3cb9aEric Laurent mEffectBufferValid = true; 4071ac302143551a8b964f026385a524dda9ff8ea5baMarco Nelissen } 4072ac302143551a8b964f026385a524dda9ff8ea5baMarco Nelissen 4073ac302143551a8b964f026385a524dda9ff8ea5baMarco Nelissen if (mEffectBufferValid) { 407457088b5c8e76855b99b3e6b3e410de5b6382670eMarco Nelissen // as long as there are effects we should clear the effects buffer, to avoid 407557088b5c8e76855b99b3e6b3e410de5b6382670eMarco Nelissen // passing a non-clean buffer to the effect chain 407657088b5c8e76855b99b3e6b3e410de5b6382670eMarco Nelissen memset(mEffectBuffer, 0, mEffectBufferSize); 407797d547da43c9c41711d1ed1e3f4fa87c2ee3cb9aEric Laurent } 407869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // sink or mix buffer must be cleared if all tracks are connected to an 407969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // effect chain as in this case the mixer will not write to the sink or mix buffer 408069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // and track effects will accumulate into it 4081bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || 4082bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent (mixedTracks == 0 && fastTracks > 0))) { 408381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME as a performance optimization, should remember previous zero status 408469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung if (mMixerBufferValid) { 408569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung memset(mMixerBuffer, 0, mMixerBufferSize); 408669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // TODO: In testing, mSinkBuffer below need not be cleared because 408769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer 408869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // after mixing. 408969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // 409069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // To enforce this guarantee: 409169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || 409269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // (mixedTracks == 0 && fastTracks > 0)) 409369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // must imply MIXER_TRACKS_READY. 409469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // Later, we may clear buffers regardless, and skip much of this logic. 409569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung } 409698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // FIXME as a performance optimization, should remember previous zero status 40975567aaf4818007cd8e77329683a91c0f5d7a8837Andy Hung memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize); 409881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 409981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 410081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if any fast tracks, then status is ready 410181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMixerStatusIgnoringFastTracks = mixerStatus; 410281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (fastTracks > 0) { 410381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixerStatus = MIXER_TRACKS_READY; 410481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 410581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mixerStatus; 410681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 410781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 410881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// getTrackName_l() must be called with ThreadBase::mLock held 4109e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hungint AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask, 4110e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung audio_format_t format, int sessionId) 411181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 4112e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung return mAudioMixer->getTrackName(channelMask, format, sessionId); 411381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 411481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 411581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// deleteTrackName_l() must be called with ThreadBase::mLock held 411681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::deleteTrackName_l(int name) 411781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 411881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("remove track (%d) and delete from mixer", name); 411981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->deleteTrackName(name); 412081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 412181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 41221035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// checkForNewParameter_l() must be called with ThreadBase::mLock held 41231035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentbool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair, 41241035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status_t& status) 412581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 412681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool reconfig = false; 412781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 41281035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = NO_ERROR; 412981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 41301035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // if !&IDLE, holds the FastMixer state to restore after new parameters processed 41311035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; 41324d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten if (mFastMixer != 0) { 41331035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 41341035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent FastMixerState *state = sq->begin(); 41351035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (!(state->mCommand & FastMixerState::IDLE)) { 41361035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent previousCommand = state->mCommand; 41371035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent state->mCommand = FastMixerState::HOT_IDLE; 41381035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sq->end(); 41391035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); 41401035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 41411035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sq->end(false /*didModify*/); 414281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 41431035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 414481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 41451035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent AudioParameter param = AudioParameter(keyValuePair); 41461035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent int value; 41471035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { 41481035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 41491035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 41501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { 41519a59276fb465e492138e0576523b54079671e8f4Andy Hung if (!isValidPcmSinkFormat((audio_format_t) value)) { 41521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = BAD_VALUE; 41531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 41541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // no need to save value, since it's constant 415581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent reconfig = true; 415681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 41571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 41581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { 41599a59276fb465e492138e0576523b54079671e8f4Andy Hung if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) { 41601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = BAD_VALUE; 41611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 41621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // no need to save value, since it's constant 41631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 416481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 41651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 41661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { 41671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // do not accept frame count changes if tracks are open as the track buffer 41681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // size depends on frame count and correct behavior would not be guaranteed 41691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // if frame count is changed after track creation 41701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (!mTracks.isEmpty()) { 41711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = INVALID_OPERATION; 41721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 41731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 417481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 41751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 41761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { 417781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef ADD_BATTERY_DATA 41781035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // when changing the audio output device, call addBatteryData to notify 41791035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // the change 41801035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (mOutDevice != value) { 41811035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent uint32_t params = 0; 41821035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // check whether speaker is on 41831035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (value & AUDIO_DEVICE_OUT_SPEAKER) { 41841035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent params |= IMediaPlayerService::kBatteryDataSpeakerOn; 41851035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 418681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 41871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent audio_devices_t deviceWithoutSpeaker 41881035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; 41891035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // check if any other device (except speaker) is on 4190054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (value & deviceWithoutSpeaker) { 41911035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; 41921035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 419381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 41941035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (params != 0) { 41951035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent addBatteryData(params); 419681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 41971035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 419881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 419981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 42001035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // forward device change to effects that have requested to be 42011035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // aware of attached audio device. 42021035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (value != AUDIO_DEVICE_NONE) { 42031035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mOutDevice = value; 42041035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 42051035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mEffectChains[i]->setDevice_l(mOutDevice); 420681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 420781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 42081035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 420981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 42101035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == NO_ERROR) { 42111035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = mOutput->stream->common.set_parameters(&mOutput->stream->common, 42121035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent keyValuePair.string()); 42131035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (!mStandby && status == INVALID_OPERATION) { 4214062e67a26e0553dd142be622821f493df541f0c6Phil Burk mOutput->standby(); 42151035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mStandby = true; 42161035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mBytesWritten = 0; 421781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = mOutput->stream->common.set_parameters(&mOutput->stream->common, 42181035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent keyValuePair.string()); 42191035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 42201035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == NO_ERROR && reconfig) { 42211035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent readOutputParameters_l(); 42221035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent delete mAudioMixer; 42231035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); 42241035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent for (size_t i = 0; i < mTracks.size() ; i++) { 4225e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung int name = getTrackName_l(mTracks[i]->mChannelMask, 4226e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung mTracks[i]->mFormat, mTracks[i]->mSessionId); 42271035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (name < 0) { 42281035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent break; 422981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 42301035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mTracks[i]->mName = name; 423181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 423273e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); 423381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 423481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 423581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 423681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!(previousCommand & FastMixerState::IDLE)) { 42374d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten ALOG_ASSERT(mFastMixer != 0); 423881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerStateQueue *sq = mFastMixer->sq(); 423981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerState *state = sq->begin(); 424081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); 424181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent state->mCommand = previousCommand; 424281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->end(); 424381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); 424481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 424581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 424681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return reconfig; 424781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 424881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 424981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 425081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) 425181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 425281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const size_t SIZE = 256; 425381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char buffer[SIZE]; 425481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent String8 result; 425581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 425681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread::dumpInternals(fd, args); 425781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 425887cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " AudioMixer tracks: 0x%08x\n", mAudioMixer->trackNames()); 425981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 426081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Make a non-atomic copy of fast mixer dump state so it won't change underneath us 42614182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten const FastMixerDumpState copy(mFastMixerDumpState); 426281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent copy.dump(fd); 426381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 426481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef STATE_QUEUE_DUMP 426581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Similar for state queue 426681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent StateQueueObserverDump observerCopy = mStateQueueObserverDump; 426781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent observerCopy.dump(fd); 426881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; 426981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mutatorCopy.dump(fd); 427081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 427181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 427246909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 427381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Write the tee output to a .wav file 427481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpTee(fd, mTeeSource, mId); 427546909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 427681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 427781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG 427881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mAudioWatchdog != 0) { 427981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Make a non-atomic copy of audio watchdog dump so it won't change underneath us 428081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioWatchdogDump wdCopy = mAudioWatchdogDump; 428181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent wdCopy.dump(fd); 428281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 428381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 428481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 428581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 428681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const 428781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 428881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; 428981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 429081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 429181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const 429281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 429381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); 429481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 429581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 429681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::cacheParameters_l() 429781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 429881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread::cacheParameters_l(); 429981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 430081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME: Relaxed timing because of a certain device that can't meet latency 430181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Should be reduced to 2x after the vendor fixes the driver issue 430281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // increase threshold again due to low power audio mode. The way this warning 430381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // threshold is calculated and its usefulness should be reconsidered anyway. 430481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; 430581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 430681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 430781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 430881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 430981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, 431081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device) 431181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent : PlaybackThread(audioFlinger, output, id, device, DIRECT) 431281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mLeftVolFloat, mRightVolFloat 431381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 431481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 431581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4316bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, 4317bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioStreamOut* output, audio_io_handle_t id, uint32_t device, 4318bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ThreadBase::type_t type) 4319bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent : PlaybackThread(audioFlinger, output, id, device, type) 4320bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // mLeftVolFloat, mRightVolFloat 4321bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4322bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4323bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 432481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::DirectOutputThread::~DirectOutputThread() 432581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 432681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 432781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4328bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack) 4329bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4330bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent audio_track_cblk_t* cblk = track->cblk(); 4331bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent float left, right; 4332bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4333bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mMasterMute || mStreamTypes[track->streamType()].mute) { 4334bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent left = right = 0; 4335bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else { 4336bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent float typeVolume = mStreamTypes[track->streamType()].volume; 4337bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent float v = mMasterVolume * typeVolume; 4338bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; 4339c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten gain_minifloat_packed_t vlr = proxy->getVolumeLR(); 4340c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten left = float_from_gain(gain_minifloat_unpack_left(vlr)); 4341c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten if (left > GAIN_FLOAT_UNITY) { 4342c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten left = GAIN_FLOAT_UNITY; 4343c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten } 4344c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten left *= v; 4345c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten right = float_from_gain(gain_minifloat_unpack_right(vlr)); 4346c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten if (right > GAIN_FLOAT_UNITY) { 4347c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten right = GAIN_FLOAT_UNITY; 4348c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten } 4349c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten right *= v; 4350bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4351bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4352bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (lastTrack) { 4353bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (left != mLeftVolFloat || right != mRightVolFloat) { 4354bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mLeftVolFloat = left; 4355bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mRightVolFloat = right; 4356bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4357bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Convert volumes from float to 8.24 4358bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent uint32_t vl = (uint32_t)(left * (1 << 24)); 4359bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent uint32_t vr = (uint32_t)(right * (1 << 24)); 4360bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4361bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Delegate volume control to effect in track effect chain if needed 4362bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // only one effect chain can be present on DirectOutputThread, so if 4363bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // there is one, the track is connected to it 4364bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (!mEffectChains.isEmpty()) { 4365bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mEffectChains[0]->setVolume_l(&vl, &vr); 4366bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent left = (float)vl / (1 << 24); 4367bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent right = (float)vr / (1 << 24); 4368bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4369bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mOutput->stream->set_volume) { 4370bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mOutput->stream->set_volume(mOutput->stream, left, right); 4371bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4372bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4373bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4374bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4375bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4376bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 437781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( 437881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<Track> > *tracksToRemove 437981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent) 438081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 4381d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent size_t count = mActiveTracks.size(); 438281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixer_state mixerStatus = MIXER_IDLE; 4383d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent bool doHwPause = false; 4384d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent bool doHwResume = false; 4385d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent bool flushPending = false; 438681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 438781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // find out which tracks need to be processed 4388d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent for (size_t i = 0; i < count; i++) { 4389d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent sp<Track> t = mActiveTracks[i].promote(); 439081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The track died recently 439181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (t == 0) { 4392d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent continue; 439381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 439481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 439581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Track* const track = t.get(); 439681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_track_cblk_t* cblk = track->cblk(); 4397fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // Only consider last track started for volume and mixer state control. 4398fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // In theory an older track could underrun and restart after the new one starts 4399fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // but as we only care about the transition phase between two tracks on a 4400fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // direct output, it is not a problem to ignore the underrun case. 4401fd4779740ec3e9e865d5514464df26d015354388Eric Laurent sp<Track> l = mLatestActiveTrack.promote(); 4402fd4779740ec3e9e865d5514464df26d015354388Eric Laurent bool last = l.get() == track; 440381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 44046fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk if (track->isPausing()) { 4405d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent track->setPaused(); 44066fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk if (mHwSupportsPause && last && !mHwPaused) { 4407d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent doHwPause = true; 4408d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent mHwPaused = true; 4409d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4410d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent tracksToRemove->add(track); 4411d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } else if (track->isFlushPending()) { 4412d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent track->flushAck(); 4413d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent if (last) { 4414d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent flushPending = true; 4415d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 44166fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk } else if (track->isResumePending()) { 4417d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent track->resumeAck(); 44186fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk if (last && mHwPaused) { 44196fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk doHwResume = true; 44206fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk mHwPaused = false; 4421d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4422d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4423d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent 442481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The first time a track is added we wait 442599adee3c3d9cde6819741a38163954808fea270aPhil Burk // for all its buffers to be filled before processing it. 442699adee3c3d9cde6819741a38163954808fea270aPhil Burk // Allow draining the buffer in case the client 442799adee3c3d9cde6819741a38163954808fea270aPhil Burk // app does not call stop() and relies on underrun to stop: 442899adee3c3d9cde6819741a38163954808fea270aPhil Burk // hence the test on (track->mRetryCount > 1). 442999adee3c3d9cde6819741a38163954808fea270aPhil Burk // If retryCount<=1 then track is about to underrun and be removed. 443081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t minFrames; 443199adee3c3d9cde6819741a38163954808fea270aPhil Burk if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing() 443299adee3c3d9cde6819741a38163954808fea270aPhil Burk && (track->mRetryCount > 1)) { 443381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minFrames = mNormalFrameCount; 443481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 443581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent minFrames = 1; 443681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 4437bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4438ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent if ((track->framesReady() >= minFrames) && track->isReady() && !track->isPaused() && 4439ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent !track->isStopping_2() && !track->isStopped()) 444081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 4441f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten ALOGVV("track %d s=%08x [OK]", track->name(), cblk->mServer); 444281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 444381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->mFillingUpStatus == Track::FS_FILLED) { 444481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->mFillingUpStatus = Track::FS_ACTIVE; 44451abbdb4429479975718421c4fef3f79ce7c820e3Eric Laurent // make sure processVolume_l() will apply new volume even if 0 44461abbdb4429479975718421c4fef3f79ce7c820e3Eric Laurent mLeftVolFloat = mRightVolFloat = -1.0; 4447d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent if (!mHwSupportsPause) { 4448d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent track->resumeAck(); 444981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 445081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 445181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 445281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // compute volume for this track 4453bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent processVolume_l(track, last); 4454bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (last) { 4455d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent // reset retry count 4456d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent track->mRetryCount = kMaxTrackRetriesDirect; 4457d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent mActiveTrack = t; 4458d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent mixerStatus = MIXER_TRACKS_READY; 44590f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent if (usesHwAvSync() && mHwPaused) { 44600f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent doHwResume = true; 44610f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent mHwPaused = false; 44620f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent } 4463d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent } 446481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 4465d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent // clear effect chain input buffer if the last active track started underruns 4466d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent // to avoid sending previous audio buffer again to effects 4467fd4779740ec3e9e865d5514464df26d015354388Eric Laurent if (!mEffectChains.isEmpty() && last) { 446881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains[0]->clearInputBuffer(); 446981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 4470ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent if (track->isStopping_1()) { 4471ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent track->mState = TrackBase::STOPPING_2; 4472b369cafd67beb63dd0278dba543f519956208a7fEric Laurent if (last && mHwPaused) { 4473b369cafd67beb63dd0278dba543f519956208a7fEric Laurent doHwResume = true; 4474b369cafd67beb63dd0278dba543f519956208a7fEric Laurent mHwPaused = false; 4475b369cafd67beb63dd0278dba543f519956208a7fEric Laurent } 4476ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent } 4477ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent if ((track->sharedBuffer() != 0) || track->isStopped() || 4478ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent track->isStopping_2() || track->isPaused()) { 447981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // We have consumed all the buffers of this track. 448081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Remove it from the list of active tracks. 4481ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent size_t audioHALFrames; 4482ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent if (audio_is_linear_pcm(mFormat)) { 4483ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent audioHALFrames = (latency_l() * mSampleRate) / 1000; 4484ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent } else { 4485ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent audioHALFrames = 0; 4486ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent } 4487ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent 448881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t framesWritten = mBytesWritten / mFrameSize; 4489fd4779740ec3e9e865d5514464df26d015354388Eric Laurent if (mStandby || !last || 4490fd4779740ec3e9e865d5514464df26d015354388Eric Laurent track->presentationComplete(framesWritten, audioHALFrames)) { 4491ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent if (track->isStopping_2()) { 4492ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent track->mState = TrackBase::STOPPED; 4493ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent } 449481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (track->isStopped()) { 449581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track->reset(); 449681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 4497d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent tracksToRemove->add(track); 449881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 449981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 450081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // No buffers for this track. Give it a few chances to 450181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // fill a buffer, then remove it from active list. 4502d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent // Only consider last track started for mixer state control 450381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (--(track->mRetryCount) <= 0) { 450481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); 4505d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent tracksToRemove->add(track); 4506a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent // indicate to client process that the track was disabled because of underrun; 4507a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent // it will then automatically call start() when data is available 4508a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent android_atomic_or(CBLK_DISABLED, &cblk->mFlags); 4509bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if (last) { 451081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixerStatus = MIXER_TRACKS_ENABLED; 45110f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent if (usesHwAvSync() && !mHwPaused && !mStandby) { 45120f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent doHwPause = true; 45130f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent mHwPaused = true; 45140f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent } 451581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 451681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 451781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 451881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 451981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4520d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent // if an active track did not command a flush, check for pending flush on stopped tracks 4521d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent if (!flushPending) { 4522d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 4523d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent if (mTracks[i]->isFlushPending()) { 4524d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent mTracks[i]->flushAck(); 4525d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent flushPending = true; 4526d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4527d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4528d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4529d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent 4530d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent // make sure the pause/flush/resume sequence is executed in the right order. 4531d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent // If a flush is pending and a track is active but the HW is not paused, force a HW pause 4532d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent // before flush and then resume HW. This can happen in case of pause/flush/resume 4533d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent // if resume is received before pause is executed. 4534d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent if (mHwSupportsPause && !mStandby && 4535d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent (doHwPause || (flushPending && !mHwPaused && (count != 0)))) { 4536d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent mOutput->stream->pause(mOutput->stream); 4537d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4538d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent if (flushPending) { 4539d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent flushHw_l(); 4540d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4541d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent if (mHwSupportsPause && !mStandby && doHwResume) { 4542d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent mOutput->stream->resume(mOutput->stream); 4543d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 454481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // remove all the tracks that need to be... 4545bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent removeTracks_l(*tracksToRemove); 454681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 454781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mixerStatus; 454881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 454981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 455081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DirectOutputThread::threadLoop_mix() 455181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 455281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t frameCount = mFrameCount; 45532098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung int8_t *curBuf = (int8_t *)mSinkBuffer; 455481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // output audio to hardware 455581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent while (frameCount) { 455634542acfa25c6413c87a94b6f7cc315a0c496277Glenn Kasten AudioBufferProvider::Buffer buffer; 455781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent buffer.frameCount = frameCount; 4558062e67a26e0553dd142be622821f493df541f0c6Phil Burk status_t status = mActiveTrack->getNextBuffer(&buffer); 4559062e67a26e0553dd142be622821f493df541f0c6Phil Burk if (status != NO_ERROR || buffer.raw == NULL) { 456081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent memset(curBuf, 0, frameCount * mFrameSize); 456181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 456281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 456381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); 456481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent frameCount -= buffer.frameCount; 456581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent curBuf += buffer.frameCount * mFrameSize; 456681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mActiveTrack->releaseBuffer(&buffer); 456781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 45682098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer; 456981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = 0; 457081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent standbyTime = systemTime() + standbyDelay; 457181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mActiveTrack.clear(); 457281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 457381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 457481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DirectOutputThread::threadLoop_sleepTime() 457581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 4576d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent // do not write to HAL when paused 45770f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent if (mHwPaused || (usesHwAvSync() && mStandby)) { 4578d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent sleepTime = idleSleepTime; 4579d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent return; 4580d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 458181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sleepTime == 0) { 458281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mMixerStatus == MIXER_TRACKS_ENABLED) { 458381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = activeSleepTime; 458481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 458581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = idleSleepTime; 458681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 458781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) { 45882098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung memset(mSinkBuffer, 0, mFrameCount * mFrameSize); 458981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = 0; 459081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 459181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 459281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4593d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurentvoid AudioFlinger::DirectOutputThread::threadLoop_exit() 4594d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent{ 4595d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent { 4596d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent Mutex::Autolock _l(mLock); 4597d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent bool flushPending = false; 4598d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 4599d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent if (mTracks[i]->isFlushPending()) { 4600d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent mTracks[i]->flushAck(); 4601d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent flushPending = true; 4602d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4603d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4604d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent if (flushPending) { 4605d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent flushHw_l(); 4606d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4607d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4608d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent PlaybackThread::threadLoop_exit(); 4609d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent} 4610d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent 4611d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent// must be called with thread mutex locked 4612d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurentbool AudioFlinger::DirectOutputThread::shouldStandby_l() 4613d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent{ 4614d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent bool trackPaused = false; 4615b369cafd67beb63dd0278dba543f519956208a7fEric Laurent bool trackStopped = false; 4616d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent 4617d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack 4618d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent // after a timeout and we will enter standby then. 4619d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent if (mTracks.size() > 0) { 4620d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent trackPaused = mTracks[mTracks.size() - 1]->isPaused(); 4621b369cafd67beb63dd0278dba543f519956208a7fEric Laurent trackStopped = mTracks[mTracks.size() - 1]->isStopped() || 4622b369cafd67beb63dd0278dba543f519956208a7fEric Laurent mTracks[mTracks.size() - 1]->mState == TrackBase::IDLE; 4623d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent } 4624d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent 4625b369cafd67beb63dd0278dba543f519956208a7fEric Laurent return !mStandby && !(trackPaused || (usesHwAvSync() && mHwPaused && !trackStopped)); 4626d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent} 4627d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent 462881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// getTrackName_l() must be called with ThreadBase::mLock held 46290f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenint AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask __unused, 4630e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung audio_format_t format __unused, int sessionId __unused) 463181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 463281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 463381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 463481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 463581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// deleteTrackName_l() must be called with ThreadBase::mLock held 46360f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::DirectOutputThread::deleteTrackName_l(int name __unused) 463781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 463881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 463981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 46401035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// checkForNewParameter_l() must be called with ThreadBase::mLock held 46411035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentbool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair, 46421035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status_t& status) 464381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 464481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool reconfig = false; 464581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 46461035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = NO_ERROR; 46471035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 46481035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent AudioParameter param = AudioParameter(keyValuePair); 46491035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent int value; 46501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { 46511035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // forward device change to effects that have requested to be 46521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // aware of attached audio device. 46531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (value != AUDIO_DEVICE_NONE) { 46541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mOutDevice = value; 46551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 46561035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mEffectChains[i]->setDevice_l(mOutDevice); 4657c125f38cd0ae35409a01b98a99e483550daa1313Glenn Kasten } 4658c125f38cd0ae35409a01b98a99e483550daa1313Glenn Kasten } 46591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 46601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { 46611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // do not accept frame count changes if tracks are open as the track buffer 46621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // size depends on frame count and correct behavior would not be garantied 46631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // if frame count is changed after track creation 46641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (!mTracks.isEmpty()) { 46651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = INVALID_OPERATION; 46661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 46671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 466881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 46691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 46701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == NO_ERROR) { 46711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = mOutput->stream->common.set_parameters(&mOutput->stream->common, 46721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent keyValuePair.string()); 46731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (!mStandby && status == INVALID_OPERATION) { 4674062e67a26e0553dd142be622821f493df541f0c6Phil Burk mOutput->standby(); 46751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mStandby = true; 46761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mBytesWritten = 0; 467781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = mOutput->stream->common.set_parameters(&mOutput->stream->common, 46781035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent keyValuePair.string()); 46791035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 46801035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == NO_ERROR && reconfig) { 46811035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent readOutputParameters_l(); 468273e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); 468381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 468481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 46851035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 468681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return reconfig; 468781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 468881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 468981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const 469081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 469181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t time; 469281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (audio_is_linear_pcm(mFormat)) { 469381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent time = PlaybackThread::activeSleepTimeUs(); 469481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 469581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent time = 10000; 469681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 469781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return time; 469881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 469981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 470081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const 470181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 470281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t time; 470381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (audio_is_linear_pcm(mFormat)) { 470481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; 470581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 470681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent time = 10000; 470781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 470881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return time; 470981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 471081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 471181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const 471281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 471381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t time; 471481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (audio_is_linear_pcm(mFormat)) { 471581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); 471681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 471781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent time = 10000; 471881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 471981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return time; 472081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 472181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 472281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DirectOutputThread::cacheParameters_l() 472381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 472481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread::cacheParameters_l(); 472581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 472681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // use shorter standby delay as on normal output to release 472781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // hardware resources as soon as possible 4728b369cafd67beb63dd0278dba543f519956208a7fEric Laurent // no delay on outputs with HW A/V sync 4729b369cafd67beb63dd0278dba543f519956208a7fEric Laurent if (usesHwAvSync()) { 4730b369cafd67beb63dd0278dba543f519956208a7fEric Laurent standbyDelay = 0; 4731b369cafd67beb63dd0278dba543f519956208a7fEric Laurent } else if (audio_is_linear_pcm(mFormat)) { 4732972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent standbyDelay = microseconds(activeSleepTime*2); 4733972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent } else { 4734972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent standbyDelay = kOffloadStandbyDelayNs; 4735972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent } 473681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 473781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4738e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurentvoid AudioFlinger::DirectOutputThread::flushHw_l() 4739e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent{ 4740062e67a26e0553dd142be622821f493df541f0c6Phil Burk mOutput->flush(); 4741d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent mHwPaused = false; 4742e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent} 4743e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent 474481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 474581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4746bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::AsyncCallbackThread::AsyncCallbackThread( 47474de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent const wp<AudioFlinger::PlaybackThread>& playbackThread) 4748bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent : Thread(false /*canCallJava*/), 47494de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent mPlaybackThread(playbackThread), 47503b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence(0), 47513b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence(0) 4752bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4753bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4754bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4755bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::AsyncCallbackThread::~AsyncCallbackThread() 4756bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4757bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4758bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4759bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::AsyncCallbackThread::onFirstRef() 4760bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4761bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO); 4762bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4763bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4764bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::AsyncCallbackThread::threadLoop() 4765bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4766bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent while (!exitPending()) { 47673b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent uint32_t writeAckSequence; 47683b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent uint32_t drainSequence; 4769bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4770bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent { 4771bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 477224a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George while (!((mWriteAckSequence & 1) || 477324a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George (mDrainSequence & 1) || 477424a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George exitPending())) { 477524a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George mWaitWorkCV.wait(mLock); 477624a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George } 477724a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George 4778bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (exitPending()) { 4779bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent break; 4780bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 47813b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d", 47823b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence, mDrainSequence); 47833b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent writeAckSequence = mWriteAckSequence; 47843b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence &= ~1; 47853b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent drainSequence = mDrainSequence; 47863b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence &= ~1; 4787bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4788bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent { 47894de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote(); 47904de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent if (playbackThread != 0) { 47913b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if (writeAckSequence & 1) { 47924de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent playbackThread->resetWriteBlocked(writeAckSequence >> 1); 4793bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 47943b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if (drainSequence & 1) { 47954de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent playbackThread->resetDraining(drainSequence >> 1); 4796bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4797bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4798bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4799bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4800bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return false; 4801bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4802bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4803bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::AsyncCallbackThread::exit() 4804bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4805bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("AsyncCallbackThread::exit"); 4806bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 4807bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent requestExit(); 4808bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mWaitWorkCV.broadcast(); 4809bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4810bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 48113b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence) 4812bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4813bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 48143b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // bit 0 is cleared 48153b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence = sequence << 1; 48163b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent} 48173b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent 48183b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::AsyncCallbackThread::resetWriteBlocked() 48193b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent{ 48203b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent Mutex::Autolock _l(mLock); 48213b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // ignore unexpected callbacks 48223b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if (mWriteAckSequence & 2) { 48233b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence |= 1; 4824bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mWaitWorkCV.signal(); 4825bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4826bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4827bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 48283b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence) 48293b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent{ 48303b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent Mutex::Autolock _l(mLock); 48313b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // bit 0 is cleared 48323b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence = sequence << 1; 48333b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent} 48343b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent 48353b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::AsyncCallbackThread::resetDraining() 4836bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4837bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 48383b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // ignore unexpected callbacks 48393b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if (mDrainSequence & 2) { 48403b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence |= 1; 4841bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mWaitWorkCV.signal(); 4842bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4843bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4844bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4845bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4846bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// ---------------------------------------------------------------------------- 4847bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger, 4848bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioStreamOut* output, audio_io_handle_t id, uint32_t device) 4849bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent : DirectOutputThread(audioFlinger, output, id, device, OFFLOAD), 4850d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent mPausedBytesRemaining(0) 4851bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4852fd4779740ec3e9e865d5514464df26d015354388Eric Laurent //FIXME: mStandby should be set to true by ThreadBase constructor 4853fd4779740ec3e9e865d5514464df26d015354388Eric Laurent mStandby = true; 4854bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4855bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4856bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::OffloadThread::threadLoop_exit() 4857bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4858bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mFlushPending || mHwPaused) { 4859bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // If a flush is pending or track was paused, just discard buffered data 4860bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent flushHw_l(); 4861bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else { 4862bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mMixerStatus = MIXER_DRAIN_ALL; 4863bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent threadLoop_drain(); 4864bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 486556604aa3a56dc8e15532597a0a74b3c7b165e006Uday Gupta if (mUseAsyncWrite) { 486656604aa3a56dc8e15532597a0a74b3c7b165e006Uday Gupta ALOG_ASSERT(mCallbackThread != 0); 486756604aa3a56dc8e15532597a0a74b3c7b165e006Uday Gupta mCallbackThread->exit(); 486856604aa3a56dc8e15532597a0a74b3c7b165e006Uday Gupta } 4869bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent PlaybackThread::threadLoop_exit(); 4870bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 4871bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4872bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l( 4873bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Vector< sp<Track> > *tracksToRemove 4874bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent) 4875bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 4876bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent size_t count = mActiveTracks.size(); 4877bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4878bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mixer_state mixerStatus = MIXER_IDLE; 4879972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent bool doHwPause = false; 4880972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent bool doHwResume = false; 4881972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent 4882ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent ALOGV("OffloadThread::prepareTracks_l active tracks %d", count); 4883ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent 4884bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // find out which tracks need to be processed 4885bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent for (size_t i = 0; i < count; i++) { 4886bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent sp<Track> t = mActiveTracks[i].promote(); 4887bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // The track died recently 4888bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (t == 0) { 4889bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent continue; 4890bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4891bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Track* const track = t.get(); 4892bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent audio_track_cblk_t* cblk = track->cblk(); 4893fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // Only consider last track started for volume and mixer state control. 4894fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // In theory an older track could underrun and restart after the new one starts 4895fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // but as we only care about the transition phase between two tracks on a 4896fd4779740ec3e9e865d5514464df26d015354388Eric Laurent // direct output, it is not a problem to ignore the underrun case. 4897fd4779740ec3e9e865d5514464df26d015354388Eric Laurent sp<Track> l = mLatestActiveTrack.promote(); 4898fd4779740ec3e9e865d5514464df26d015354388Eric Laurent bool last = l.get() == track; 4899fd4779740ec3e9e865d5514464df26d015354388Eric Laurent 49007844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George if (track->isInvalid()) { 49017844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George ALOGW("An invalidated track shouldn't be in active list"); 49027844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George tracksToRemove->add(track); 49037844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George continue; 49047844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George } 49057844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George 49067844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George if (track->mState == TrackBase::IDLE) { 49077844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George ALOGW("An idle track shouldn't be in active list"); 49087844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George continue; 49097844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George } 49107844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George 4911bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (track->isPausing()) { 4912bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->setPaused(); 4913bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (last) { 4914bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (!mHwPaused) { 4915972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent doHwPause = true; 4916bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mHwPaused = true; 4917bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4918bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // If we were part way through writing the mixbuffer to 4919bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // the HAL we must save this until we resume 4920bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // BUG - this will be wrong if a different track is made active, 4921bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // in that case we want to discard the pending data in the 4922bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // mixbuffer and tell the client to present it again when the 4923bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // track is resumed 4924bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mPausedWriteLength = mCurrentWriteLength; 4925bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mPausedBytesRemaining = mBytesRemaining; 4926bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining = 0; // stop writing 4927bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4928bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent tracksToRemove->add(track); 49297844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George } else if (track->isFlushPending()) { 49307844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George track->flushAck(); 49317844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George if (last) { 49327844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George mFlushPending = true; 49337844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George } 49342d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George } else if (track->isResumePending()){ 49352d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George track->resumeAck(); 49362d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George if (last) { 49372d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George if (mPausedBytesRemaining) { 49382d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George // Need to continue write that was interrupted 49392d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George mCurrentWriteLength = mPausedWriteLength; 49402d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George mBytesRemaining = mPausedBytesRemaining; 49412d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George mPausedBytesRemaining = 0; 49422d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George } 49432d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George if (mHwPaused) { 49442d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George doHwResume = true; 49452d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George mHwPaused = false; 49462d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George // threadLoop_mix() will handle the case that we need to 49472d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George // resume an interrupted write 49482d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George } 49492d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George // enable write to audio HAL 49502d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George sleepTime = 0; 49512d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George 49522d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George // Do not handle new data in this iteration even if track->framesReady() 49532d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George mixerStatus = MIXER_TRACKS_ENABLED; 49542d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George } 49552d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George } else if (track->framesReady() && track->isReady() && 49563b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) { 4957f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten ALOGVV("OffloadThread: track %d s=%08x [OK]", track->name(), cblk->mServer); 4958bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (track->mFillingUpStatus == Track::FS_FILLED) { 4959bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mFillingUpStatus = Track::FS_ACTIVE; 49601abbdb4429479975718421c4fef3f79ce7c820e3Eric Laurent // make sure processVolume_l() will apply new volume even if 0 49611abbdb4429479975718421c4fef3f79ce7c820e3Eric Laurent mLeftVolFloat = mRightVolFloat = -1.0; 4962bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4963bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 4964bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (last) { 4965d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent sp<Track> previousTrack = mPreviousTrack.promote(); 4966d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent if (previousTrack != 0) { 4967d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent if (track != previousTrack.get()) { 49689da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // Flush any data still being written from last track 49699da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent mBytesRemaining = 0; 49709da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent if (mPausedBytesRemaining) { 49719da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // Last track was paused so we also need to flush saved 49729da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // mixbuffer state and invalidate track so that it will 49739da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // re-submit that unwritten data when it is next resumed 49749da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent mPausedBytesRemaining = 0; 49759da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // Invalidate is a bit drastic - would be more efficient 49769da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // to have a flag to tell client that some of the 49779da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // previously written data was lost 4978d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent previousTrack->invalidate(); 49799da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent } 49809da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // flush data already sent to the DSP if changing audio session as audio 49819da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // comes from a different source. Also invalidate previous track to force a 49829da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent // seek when resuming. 4983d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent if (previousTrack->sessionId() != track->sessionId()) { 4984d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent previousTrack->invalidate(); 49859da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent } 49869da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent } 49879da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent } 49889da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent mPreviousTrack = track; 4989bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // reset retry count 4990bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mRetryCount = kMaxTrackRetriesOffload; 4991bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mActiveTrack = t; 4992bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mixerStatus = MIXER_TRACKS_READY; 4993bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 4994bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else { 4995f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten ALOGVV("OffloadThread: track %d s=%08x [NOT READY]", track->name(), cblk->mServer); 4996bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (track->isStopping_1()) { 4997bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Hardware buffer can hold a large amount of audio so we must 4998bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // wait for all current track's data to drain before we say 4999bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // that the track is stopped. 5000bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mBytesRemaining == 0) { 5001bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Only start draining when all data in mixbuffer 5002bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // has been written 5003bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2"); 5004bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mState = TrackBase::STOPPING_2; // so presentation completes after drain 50056a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent // do not drain if no data was ever sent to HAL (mStandby == true) 50066a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent if (last && !mStandby) { 50071b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent // do not modify drain sequence if we are already draining. This happens 50081b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent // when resuming from pause after drain. 50091b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent if ((mDrainSequence & 1) == 0) { 50101b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent sleepTime = 0; 50111b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent standbyTime = systemTime() + standbyDelay; 50121b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent mixerStatus = MIXER_DRAIN_TRACK; 50131b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent mDrainSequence += 2; 50141b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent } 5015bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mHwPaused) { 5016bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // It is possible to move from PAUSED to STOPPING_1 without 5017bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // a resume so we must ensure hardware is running 50181b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent doHwResume = true; 5019bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mHwPaused = false; 5020bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 5021bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 5022bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 5023bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if (track->isStopping_2()) { 50246a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent // Drain has completed or we are in standby, signal presentation complete 50256a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent if (!(mDrainSequence & 1) || !last || mStandby) { 5026bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mState = TrackBase::STOPPED; 5027bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent size_t audioHALFrames = 5028bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; 5029bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent size_t framesWritten = 5030062e67a26e0553dd142be622821f493df541f0c6Phil Burk mBytesWritten / mOutput->getFrameSize(); 5031bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->presentationComplete(framesWritten, audioHALFrames); 5032bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->reset(); 5033bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent tracksToRemove->add(track); 5034bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 5035bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else { 5036bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // No buffers for this track. Give it a few chances to 5037bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // fill a buffer, then remove it from active list. 5038bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (--(track->mRetryCount) <= 0) { 5039bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("OffloadThread: BUFFER TIMEOUT: remove(%d) from active list", 5040bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->name()); 5041bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent tracksToRemove->add(track); 5042a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent // indicate to client process that the track was disabled because of underrun; 5043a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent // it will then automatically call start() when data is available 5044a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent android_atomic_or(CBLK_DISABLED, &cblk->mFlags); 5045bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if (last){ 5046bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mixerStatus = MIXER_TRACKS_ENABLED; 5047bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 5048bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 5049bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 5050bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // compute volume for this track 5051bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent processVolume_l(track, last); 5052bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 50536bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent 5054ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent // make sure the pause/flush/resume sequence is executed in the right order. 5055ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent // If a flush is pending and a track is active but the HW is not paused, force a HW pause 5056ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent // before flush and then resume HW. This can happen in case of pause/flush/resume 5057ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent // if resume is received before pause is executed. 5058fd4779740ec3e9e865d5514464df26d015354388Eric Laurent if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { 5059972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent mOutput->stream->pause(mOutput->stream); 5060972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent } 50616bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent if (mFlushPending) { 50626bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent flushHw_l(); 50636bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent mFlushPending = false; 50646bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent } 5065fd4779740ec3e9e865d5514464df26d015354388Eric Laurent if (!mStandby && doHwResume) { 5066972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent mOutput->stream->resume(mOutput->stream); 5067972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent } 50686bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent 5069bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // remove all the tracks that need to be... 5070bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent removeTracks_l(*tracksToRemove); 5071bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 5072bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return mixerStatus; 5073bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 5074bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 5075bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// must be called with thread mutex locked 5076bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::OffloadThread::waitingAsyncCallback_l() 5077bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 50783b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d", 50793b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence, mDrainSequence); 50803b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) { 5081bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return true; 5082bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 5083bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return false; 5084bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 5085bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 5086bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::OffloadThread::waitingAsyncCallback() 5087bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 5088bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _l(mLock); 5089bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return waitingAsyncCallback_l(); 5090bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 5091bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 5092bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::OffloadThread::flushHw_l() 5093bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{ 5094e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent DirectOutputThread::flushHw_l(); 5095bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Flush anything still waiting in the mixbuffer 5096bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mCurrentWriteLength = 0; 5097bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mBytesRemaining = 0; 5098bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mPausedWriteLength = 0; 5099bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mPausedBytesRemaining = 0; 51000f02f265123b7ef2fd6ac09ff70cde26eb5559adHaynes Mathew George 5101bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mUseAsyncWrite) { 51023b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // discard any pending drain or write ack by incrementing sequence 51033b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mWriteAckSequence = (mWriteAckSequence + 2) & ~1; 51043b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mDrainSequence = (mDrainSequence + 2) & ~1; 5105bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOG_ASSERT(mCallbackThread != 0); 51063b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setWriteBlocked(mWriteAckSequence); 51073b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent mCallbackThread->setDraining(mDrainSequence); 5108bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 5109bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 5110bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 51114c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew Georgevoid AudioFlinger::OffloadThread::onAddNewTrack_l() 51124c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George{ 51134c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George sp<Track> previousTrack = mPreviousTrack.promote(); 51144c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George sp<Track> latestTrack = mLatestActiveTrack.promote(); 51154c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George 51164c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George if (previousTrack != 0 && latestTrack != 0 && 51174c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George (previousTrack->sessionId() != latestTrack->sessionId())) { 51184c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George mFlushPending = true; 51194c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George } 51204c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George PlaybackThread::onAddNewTrack_l(); 51214c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George} 51224c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George 5123bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// ---------------------------------------------------------------------------- 5124bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 512581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, 512681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioFlinger::MixerThread* mainThread, audio_io_handle_t id) 512781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->outDevice(), 512881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent DUPLICATING), 512981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitTimeMs(UINT_MAX) 513081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 513181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent addOutputTrack(mainThread); 513281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 513381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 513481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::DuplicatingThread::~DuplicatingThread() 513581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 513681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mOutputTracks.size(); i++) { 513781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutputTracks[i]->destroy(); 513881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 513981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 514081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 514181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::threadLoop_mix() 514281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 514381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mix buffers... 514481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (outputsReady(outputTracks)) { 514581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioMixer->process(AudioBufferProvider::kInvalidPTS); 514681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 514702b5708776ba2a9b4ff8c09008483aef7dbe38c7Eric Laurent if (mMixerBufferValid) { 514802b5708776ba2a9b4ff8c09008483aef7dbe38c7Eric Laurent memset(mMixerBuffer, 0, mMixerBufferSize); 514902b5708776ba2a9b4ff8c09008483aef7dbe38c7Eric Laurent } else { 515002b5708776ba2a9b4ff8c09008483aef7dbe38c7Eric Laurent memset(mSinkBuffer, 0, mSinkBufferSize); 515102b5708776ba2a9b4ff8c09008483aef7dbe38c7Eric Laurent } 515281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 515381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = 0; 515481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent writeFrames = mNormalFrameCount; 515525c2dac12114699e90deb1c579cadebce7b91a97Andy Hung mCurrentWriteLength = mSinkBufferSize; 515681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent standbyTime = systemTime() + standbyDelay; 515781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 515881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 515981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::threadLoop_sleepTime() 516081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 516181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sleepTime == 0) { 516281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mMixerStatus == MIXER_TRACKS_ENABLED) { 516381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = activeSleepTime; 516481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 516581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = idleSleepTime; 516681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 516781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (mBytesWritten != 0) { 516881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mMixerStatus == MIXER_TRACKS_ENABLED) { 516981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent writeFrames = mNormalFrameCount; 517025c2dac12114699e90deb1c579cadebce7b91a97Andy Hung memset(mSinkBuffer, 0, mSinkBufferSize); 517181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 517281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // flush remaining overflow buffers in output tracks 517381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent writeFrames = 0; 517481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 517581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sleepTime = 0; 517681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 517781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 517881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5179bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentssize_t AudioFlinger::DuplicatingThread::threadLoop_write() 518081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 518181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < outputTracks.size(); i++) { 5182c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung outputTracks[i]->write(mSinkBuffer, writeFrames); 518381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 51842c3740f01acca69c3e0bcc5e01bb0edc51b6777fEric Laurent mStandby = false; 518525c2dac12114699e90deb1c579cadebce7b91a97Andy Hung return (ssize_t)mSinkBufferSize; 518681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 518781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 518881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::threadLoop_standby() 518981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 519081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // DuplicatingThread implements standby by stopping all tracks 519181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < outputTracks.size(); i++) { 519281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent outputTracks[i]->stop(); 519381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 519481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 519581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 519681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::saveOutputTracks() 519781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 519881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent outputTracks = mOutputTracks; 519981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 520081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 520181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::clearOutputTracks() 520281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 520381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent outputTracks.clear(); 520481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 520581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 520681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) 520781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 520881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 5209c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass. 5210c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung // Adjust for thread->sampleRate() to determine minimum buffer frame count. 5211c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung // Then triple buffer because Threads do not run synchronously and may not be clock locked. 5212c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung const size_t frameCount = 5213c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung 3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate()); 5214c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung // TODO: Consider asynchronous sample rate conversion to handle clock disparity 5215c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung // from different OutputTracks and their associated MixerThreads (e.g. one may 5216c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung // nearly empty and the other may be dropping data). 5217c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung 5218c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung sp<OutputTrack> outputTrack = new OutputTrack(thread, 521981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent this, 522081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSampleRate, 5221c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung mFormat, 522281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mChannelMask, 5223462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen frameCount, 5224462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen IPCThreadState::self()->getCallingUid()); 522581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (outputTrack->cblk() != NULL) { 5226223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f); 522781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutputTracks.add(outputTrack); 5228c25b84abdd7ff229d0af663fbf3a37bd9512939dAndy Hung ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread); 522981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent updateWaitTime_l(); 523081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 523181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 523281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 523381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) 523481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 523581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 523681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mOutputTracks.size(); i++) { 523781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mOutputTracks[i]->thread() == thread) { 523881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutputTracks[i]->destroy(); 523981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mOutputTracks.removeAt(i); 524081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent updateWaitTime_l(); 5241f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent if (thread->getOutput() == mOutput) { 5242f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent mOutput = NULL; 5243f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent } 524481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 524581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 524681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 5247f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent ALOGV("removeOutputTrack(): unknown thread: %p", thread); 524881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 524981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 525081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// caller must hold mLock 525181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::updateWaitTime_l() 525281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 525381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitTimeMs = UINT_MAX; 525481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mOutputTracks.size(); i++) { 525581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); 525681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (strong != 0) { 525781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); 525881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (waitTimeMs < mWaitTimeMs) { 525981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitTimeMs = waitTimeMs; 526081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 526181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 526281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 526381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 526481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 526581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 526681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::DuplicatingThread::outputsReady( 526781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const SortedVector< sp<OutputTrack> > &outputTracks) 526881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 526981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < outputTracks.size(); i++) { 527081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<ThreadBase> thread = outputTracks[i]->thread().promote(); 527181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread == 0) { 527281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", 527381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent outputTracks[i].get()); 527481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 527581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 527681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); 527781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // see note at standby() declaration 527881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (playbackThread->standby() && !playbackThread->isSuspended()) { 527981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), 528081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread.get()); 528181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 528281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 528381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 528481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return true; 528581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 528681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 528781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const 528881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 528981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return (mWaitTimeMs * 1000) / 2; 529081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 529181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 529281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::cacheParameters_l() 529381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 529481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first 529581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent updateWaitTime_l(); 529681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 529781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent MixerThread::cacheParameters_l(); 529881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 529981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 530081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 530181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Record 530281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 530381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 530481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, 530581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamIn *input, 530681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t id, 5307d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent audio_devices_t outDevice, 530846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten audio_devices_t inDevice 530946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 531046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten , const sp<NBAIO_Sink>& teeSink 531146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 531246909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten ) : 5313d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent ThreadBase(audioFlinger, id, outDevice, inDevice, RECORD), 53146dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten mInput(input), mActiveTracksGen(0), mRsmpInBuffer(NULL), 5315deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten // mRsmpInFrames and mRsmpInFramesP2 are set by readInputParameters_l() 53164cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten mRsmpInRear(0) 531746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 531846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten , mTeeSink(teeSink) 531946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 5320b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize, 5321b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten "RecordThreadRO", MemoryHeapBase::READ_ONLY)) 53226dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // mFastCapture below 53236dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten , mFastCaptureFutex(0) 53246dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // mInputSource 53256dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // mPipeSink 53266dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // mPipeSource 53276dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten , mPipeFramesP2(0) 53286dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // mPipeMemory 53296dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // mFastCaptureNBLogWriter 53306e6704c06d61bc356e30c164081e5bcffb37920cGlenn Kasten , mFastTrackAvail(false) 533181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 5332d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id); 5333d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); 533481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5335deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten readInputParameters_l(); 53366dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 53376dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // create an NBAIO source for the HAL input stream, and negotiate 53386dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mInputSource = new AudioStreamInSource(input->stream); 53396dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten size_t numCounterOffers = 0; 53406dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; 53416dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ssize_t index = mInputSource->negotiate(offers, 1, NULL, numCounterOffers); 53426dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOG_ASSERT(index == 0); 53436dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 53446dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // initialize fast capture depending on configuration 53456dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten bool initFastCapture; 53466dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten switch (kUseFastCapture) { 53476dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten case FastCapture_Never: 53486dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten initFastCapture = false; 53496dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten break; 53506dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten case FastCapture_Always: 53516dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten initFastCapture = true; 53526dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten break; 53536dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten case FastCapture_Static: 53546dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten uint32_t primaryOutputSampleRate; 53556dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten { 53566dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten AutoMutex _l(audioFlinger->mHardwareLock); 53576dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten primaryOutputSampleRate = audioFlinger->mPrimaryOutputSampleRate; 53586dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 53596dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten initFastCapture = 53606dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // either capture sample rate is same as (a reasonable) primary output sample rate 5361db4c031f518ae5806af73756273ff32cd8d0e4f8Andy Hung ((isMusicRate(primaryOutputSampleRate) && 53626dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (mSampleRate == primaryOutputSampleRate)) || 53636dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // or primary output sample rate is unknown, and capture sample rate is reasonable 53646dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ((primaryOutputSampleRate == 0) && 5365db4c031f518ae5806af73756273ff32cd8d0e4f8Andy Hung isMusicRate(mSampleRate))) && 53669f81de3452dfb2385bd57dc05456a045174a1ab1Glenn Kasten // and the buffer size is < 12 ms 53679f81de3452dfb2385bd57dc05456a045174a1ab1Glenn Kasten (mFrameCount * 1000) / mSampleRate < 12; 53686dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten break; 53696dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // case FastCapture_Dynamic: 53706dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 53716dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 53726dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (initFastCapture) { 5373d198b85a163330b03e7507c9e8bfeb5f4d958a6cGlenn Kasten // create a Pipe for FastCapture to write to, and for us and fast tracks to read from 53746dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten NBAIO_Format format = mInputSource->format(); 537549d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten size_t pipeFramesP2 = roundup(mSampleRate / 25); // double-buffering of 20 ms each 53766dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten size_t pipeSize = pipeFramesP2 * Format_frameSize(format); 53776dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten void *pipeBuffer; 53786dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten const sp<MemoryDealer> roHeap(readOnlyHeap()); 53796dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sp<IMemory> pipeMemory; 53806dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if ((roHeap == 0) || 53816dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (pipeMemory = roHeap->allocate(pipeSize)) == 0 || 53826dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (pipeBuffer = pipeMemory->pointer()) == NULL) { 53836dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOGE("not enough memory for pipe buffer size=%zu", pipeSize); 53846dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten goto failed; 53856dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 53866dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // pipe will be shared directly with fast clients, so clear to avoid leaking old information 53876dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten memset(pipeBuffer, 0, pipeSize); 53886dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer); 53896dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten const NBAIO_Format offers[1] = {format}; 53906dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten size_t numCounterOffers = 0; 53916dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); 53926dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOG_ASSERT(index == 0); 53936dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mPipeSink = pipe; 53946dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten PipeReader *pipeReader = new PipeReader(*pipe); 53956dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten numCounterOffers = 0; 53966dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); 53976dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOG_ASSERT(index == 0); 53986dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mPipeSource = pipeReader; 53996dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mPipeFramesP2 = pipeFramesP2; 54006dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mPipeMemory = pipeMemory; 54016dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 54026dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // create fast capture 54036dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastCapture = new FastCapture(); 54046dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureStateQueue *sq = mFastCapture->sq(); 54056dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef STATE_QUEUE_DUMP 54066dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // FIXME 54076dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif 54086dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureState *state = sq->begin(); 54096dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mCblk = NULL; 54106dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mInputSource = mInputSource.get(); 54116dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mInputSourceGen++; 54126dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mPipeSink = pipe; 54136dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mPipeSinkGen++; 54146dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mFrameCount = mFrameCount; 54156dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mCommand = FastCaptureState::COLD_IDLE; 54166dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // already done in constructor initialization list 54176dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten //mFastCaptureFutex = 0; 54186dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mColdFutexAddr = &mFastCaptureFutex; 54196dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mColdGen++; 54206dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mDumpState = &mFastCaptureDumpState; 54216dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef TEE_SINK 54226dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // FIXME 54236dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif 54246dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture"); 54256dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mNBLogWriter = mFastCaptureNBLogWriter.get(); 54266dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sq->end(); 54276dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); 54286dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 54296dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // start the fast capture 54306dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO); 54316dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten pid_t tid = mFastCapture->getTid(); 54326dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten int err = requestPriority(getpid_cached, tid, kPriorityFastMixer); 54336dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (err != 0) { 54346dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", 54356dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten kPriorityFastCapture, getpid_cached, tid, err); 54366dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 54376dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 54386dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef AUDIO_WATCHDOG 54396dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // FIXME 54406dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif 54416dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 54426e6704c06d61bc356e30c164081e5bcffb37920cGlenn Kasten mFastTrackAvail = true; 54436dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 54446dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kastenfailed: ; 54456dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 54466dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // FIXME mNormalSource 544781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 544881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 544981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::RecordThread::~RecordThread() 545081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 54516dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (mFastCapture != 0) { 54526dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureStateQueue *sq = mFastCapture->sq(); 54536dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureState *state = sq->begin(); 54546dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (state->mCommand == FastCaptureState::COLD_IDLE) { 54556dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten int32_t old = android_atomic_inc(&mFastCaptureFutex); 54566dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (old == -1) { 54576dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); 54586dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 54596dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 54606dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mCommand = FastCaptureState::EXIT; 54616dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sq->end(); 54626dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); 54636dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastCapture->join(); 54646dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastCapture.clear(); 54656dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 54666dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter); 5467481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten mAudioFlinger->unregisterWriter(mNBLogWriter); 54685744661e85981f8a9456bf470e2761235fc026daAndy Hung free(mRsmpInBuffer); 546981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 547081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 547181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::onFirstRef() 547281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 5473d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten run(mThreadName, PRIORITY_URGENT_AUDIO); 547481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 547581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 547681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::RecordThread::threadLoop() 547781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 547881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent nsecs_t lastWarning = 0; 547981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 548081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent inputStandBy(); 548181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5482f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kastenreacquire_wakelock: 5483f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten sp<RecordTrack> activeTrack; 54842b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten int activeTracksGen; 5485f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten { 5486f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten Mutex::Autolock _l(mLock); 54872b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten size_t size = mActiveTracks.size(); 54882b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten activeTracksGen = mActiveTracksGen; 54892b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (size > 0) { 54902b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten // FIXME an arbitrary choice 54912b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten activeTrack = mActiveTracks[0]; 54922b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten acquireWakeLock_l(activeTrack->uid()); 54932b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (size > 1) { 54942b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten SortedVector<int> tmp; 54952b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten for (size_t i = 0; i < size; i++) { 54962b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten tmp.add(mActiveTracks[i]->uid()); 54972b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten } 54982b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten updateWakeLockUids_l(tmp); 54992b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten } 55002b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten } else { 55012b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten acquireWakeLock_l(-1); 55022b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten } 5503f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten } 5504f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten 55056dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // used to request a deferred sleep, to be executed later while mutex is unlocked 55066dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten uint32_t sleepUs = 0; 55076dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 55086dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // loop while there is work to do 55094ef0b463a56c19bad9197aa9f90d792090461429Glenn Kasten for (;;) { 5510c527a7c2b1bfd26e8f3086e1b653d56e521379d9Glenn Kasten Vector< sp<EffectChain> > effectChains; 55112cfbf88b89854f30b295e8ae26a031edb8d712f8Glenn Kasten 55125edadd46c76c5ff1c3edabf2ea943c2278e82e1cGlenn Kasten // sleep with mutex unlocked 55136dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (sleepUs > 0) { 5514e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten ATRACE_BEGIN("sleep"); 55156dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten usleep(sleepUs); 5516e77540228e1f60b1129a1615d2e43e0bf8015d3cGlenn Kasten ATRACE_END(); 55176dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten sleepUs = 0; 55185edadd46c76c5ff1c3edabf2ea943c2278e82e1cGlenn Kasten } 55195edadd46c76c5ff1c3edabf2ea943c2278e82e1cGlenn Kasten 55206dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // activeTracks accumulates a copy of a subset of mActiveTracks 55216dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten Vector< sp<RecordTrack> > activeTracks; 55226dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 5523735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten // reference to the (first and only) active fast track 55246dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sp<RecordTrack> fastTrack; 55251035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 5526735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten // reference to a fast track which is about to be removed 5527735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten sp<RecordTrack> fastTrackToRemove; 5528735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten 552981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // scope for mLock 553081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 5531000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent 5532021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent processConfigEvents_l(); 5533f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten 5534000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent // check exitPending here because checkForNewParameters_l() and 5535000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent // checkForNewParameters_l() can temporarily release mLock 5536000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent if (exitPending()) { 5537000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent break; 5538000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent } 5539000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent 55402b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten // if no active track(s), then standby and release wakelock 55412b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten size_t size = mActiveTracks.size(); 55422b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (size == 0) { 554393e471f620454f7de73d190521568b1e25879767Glenn Kasten standbyIfNotAlreadyInStandby(); 55444ef0b463a56c19bad9197aa9f90d792090461429Glenn Kasten // exitPending() can't become true here 554581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock_l(); 554681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("RecordThread: loop stopping"); 554781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // go to sleep 554881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitWorkCV.wait(mLock); 554981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("RecordThread: loop starting"); 5550f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten goto reacquire_wakelock; 5551f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten } 5552f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten 55532b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (mActiveTracksGen != activeTracksGen) { 55542b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten activeTracksGen = mActiveTracksGen; 5555f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten SortedVector<int> tmp; 55562b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten for (size_t i = 0; i < size; i++) { 55572b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten tmp.add(mActiveTracks[i]->uid()); 55582b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten } 5559f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten updateWakeLockUids_l(tmp); 556081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 55619e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten 55626dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten bool doBroadcast = false; 55636dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten for (size_t i = 0; i < size; ) { 55649e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten 55656dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack = mActiveTracks[i]; 55666dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTrack->isTerminated()) { 5567735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten if (activeTrack->isFastTrack()) { 5568735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten ALOG_ASSERT(fastTrackToRemove == 0); 5569735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten fastTrackToRemove = activeTrack; 5570735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten } 55716dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten removeTrack_l(activeTrack); 55722b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracks.remove(activeTrack); 55732b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracksGen++; 55746dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size--; 55759e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten continue; 55769e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten } 55776dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 55786dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten TrackBase::track_state activeTrackState = activeTrack->mState; 55796dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten switch (activeTrackState) { 55806dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 55816dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case TrackBase::PAUSING: 55826dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten mActiveTracks.remove(activeTrack); 55836dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten mActiveTracksGen++; 55846dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten doBroadcast = true; 55856dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size--; 55866dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten continue; 55876dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 55886dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case TrackBase::STARTING_1: 55896dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten sleepUs = 10000; 55906dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten i++; 55916dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten continue; 55926dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 55936dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case TrackBase::STARTING_2: 55946dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten doBroadcast = true; 55956dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten mStandby = false; 55969e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten activeTrack->mState = TrackBase::ACTIVE; 55976dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten break; 55986dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 55996dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case TrackBase::ACTIVE: 56006dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten break; 56016dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 56026dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case TrackBase::IDLE: 56036dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten i++; 56046dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten continue; 56056dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 56066dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten default: 5607adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("Unexpected activeTrackState %d", activeTrackState); 56089e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten } 56092d94426cd3302cb1215c92c5f1c4b90c24ceb72bGlenn Kasten 56106dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTracks.add(activeTrack); 56116dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten i++; 56122d94426cd3302cb1215c92c5f1c4b90c24ceb72bGlenn Kasten 56136dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (activeTrack->isFastTrack()) { 56146dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOG_ASSERT(!mFastTrackAvail); 56156dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOG_ASSERT(fastTrack == 0); 56166dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten fastTrack = activeTrack; 56176dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 56186dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 56196dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (doBroadcast) { 56206dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten mStartStopCond.broadcast(); 56216dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 5622d9fc34fb0fcfcc739f868b116edf50c62af19d5eGlenn Kasten 56236dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // sleep if there are no active tracks to process 56246dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTracks.size() == 0) { 56256dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (sleepUs == 0) { 56266dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten sleepUs = kRecordThreadSleepUs; 56276dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 56286dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten continue; 562981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 56306dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten sleepUs = 0; 56319e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten 563281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lockEffectChains_l(effectChains); 563381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 563481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 56356dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0 56367165268ffa6c7b6b405b6afad82e2a346500e8eeGlenn Kasten 56376dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size_t size = effectChains.size(); 56386dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten for (size_t i = 0; i < size; i++) { 56391ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten // thread mutex is not locked, but effect chain is locked 56401ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten effectChains[i]->process_l(); 56411ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten } 564281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 5643735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten // Push a new fast capture state if fast capture is not already running, or cblk change 56446dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (mFastCapture != 0) { 56456dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureStateQueue *sq = mFastCapture->sq(); 56466dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureState *state = sq->begin(); 5647735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten bool didModify = false; 5648735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED; 56496dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME && 56506dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) { 56516dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (state->mCommand == FastCaptureState::COLD_IDLE) { 56526dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten int32_t old = android_atomic_inc(&mFastCaptureFutex); 56536dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (old == -1) { 56546dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); 56556dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 56566dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 56576dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mCommand = FastCaptureState::READ_WRITE; 56586dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#if 0 // FIXME 56596dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? 5660fbdb2aceab7317aa44bc8f301a93eb49e17b2bceGlenn Kasten FastThreadDumpState::kSamplingNforLowRamDevice : 5661fbdb2aceab7317aa44bc8f301a93eb49e17b2bceGlenn Kasten FastThreadDumpState::kSamplingN); 56626dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif 5663735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten didModify = true; 5664735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten } 5665735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten audio_track_cblk_t *cblkOld = state->mCblk; 5666735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL; 5667735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten if (cblkNew != cblkOld) { 5668735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten state->mCblk = cblkNew; 5669735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten // block until acked if removing a fast track 5670735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten if (cblkOld != NULL) { 5671735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED; 5672735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten } 5673735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten didModify = true; 5674735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten } 5675735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten sq->end(didModify); 5676735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten if (didModify) { 5677735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten sq->push(block); 56786dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#if 0 56796dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (kUseFastCapture == FastCapture_Dynamic) { 56806dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mNormalSource = mPipeSource; 56816dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 56826dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif 56836dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 56846dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 56856dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 5686735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten // now run the fast track destructor with thread mutex unlocked 5687735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten fastTrackToRemove.clear(); 5688735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten 56896dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one. 56906dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // Only the client(s) that are too slow will overrun. But if even the fastest client is too 56916dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // slow, then this RecordThread will overrun by not calling HAL read often enough. 56926dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // If destination is non-contiguous, first read past the nominal end of buffer, then 56936dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // copy to the right place. Permitted because mRsmpInBuffer was over-allocated. 56946dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 56956dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1); 56966dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ssize_t framesRead; 56976dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 56986dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // If an NBAIO source is present, use it to read the normal capture's data 56996dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (mPipeSource != 0) { 57006dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten size_t framesToRead = mBufferSize / mFrameSize; 57015744661e85981f8a9456bf470e2761235fc026daAndy Hung framesRead = mPipeSource->read((uint8_t*)mRsmpInBuffer + rear * mFrameSize, 57026dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten framesToRead, AudioBufferProvider::kInvalidPTS); 57036dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (framesRead == 0) { 57046dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // since pipe is non-blocking, simulate blocking input 57056dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sleepUs = (framesToRead * 1000000LL) / mSampleRate; 57066dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 57076dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // otherwise use the HAL / AudioStreamIn directly 57086dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } else { 57096dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ssize_t bytesRead = mInput->stream->read(mInput->stream, 57105744661e85981f8a9456bf470e2761235fc026daAndy Hung (uint8_t*)mRsmpInBuffer + rear * mFrameSize, mBufferSize); 57116dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (bytesRead < 0) { 57126dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten framesRead = bytesRead; 57136dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } else { 57146dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten framesRead = bytesRead / mFrameSize; 57156dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 57166dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 57176dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 57186dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) { 57196dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOGE("read failed: framesRead=%d", framesRead); 57206dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // Force input into standby so that it tries to recover at next read attempt 57216dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten inputStandBy(); 57226dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten sleepUs = kRecordThreadSleepUs; 57236dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 57246dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (framesRead <= 0) { 57253d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten goto unlock; 57266dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 57276dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ALOG_ASSERT(framesRead > 0); 57286dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 57296dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (mTeeSink != 0) { 57305744661e85981f8a9456bf470e2761235fc026daAndy Hung (void) mTeeSink->write((uint8_t*)mRsmpInBuffer + rear * mFrameSize, framesRead); 57316dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 57326dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // If destination is non-contiguous, we now correct for reading past end of buffer. 57333d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten { 57343d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten size_t part1 = mRsmpInFramesP2 - rear; 57353d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten if ((size_t) framesRead > part1) { 57365744661e85981f8a9456bf470e2761235fc026daAndy Hung memcpy(mRsmpInBuffer, (uint8_t*)mRsmpInBuffer + mRsmpInFramesP2 * mFrameSize, 57373d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten (framesRead - part1) * mFrameSize); 57383d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten } 57396dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 57406dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten rear = mRsmpInRear += framesRead; 57416dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 57426dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size = activeTracks.size(); 57436dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // loop over each active track 57446dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten for (size_t i = 0; i < size; i++) { 57456dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack = activeTracks[i]; 57466dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 57476dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // skip fast tracks, as those are handled directly by FastCapture 57486dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (activeTrack->isFastTrack()) { 57496dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten continue; 57506dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 57516dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 575273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung // TODO: This code probably should be moved to RecordTrack. 575397a893eb34f8687485c88eaf15917974a203f20bAndy Hung // TODO: Update the activeTrack buffer converter in case of reconfigure. 575497a893eb34f8687485c88eaf15917974a203f20bAndy Hung 57556dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten enum { 57566dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten OVERRUN_UNKNOWN, 57576dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten OVERRUN_TRUE, 57586dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten OVERRUN_FALSE 57596dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } overrun = OVERRUN_UNKNOWN; 57606dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 57616dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // loop over getNextBuffer to handle circular sink 57626dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten for (;;) { 57636dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 57646dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mSink.frameCount = ~0; 57656dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten status_t status = activeTrack->getNextBuffer(&activeTrack->mSink); 57666dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size_t framesOut = activeTrack->mSink.frameCount; 57676dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0)); 57686dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 576973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung // check available frames and handle overrun conditions 577073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung // if the record track isn't draining fast enough. 577173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung bool hasOverrun; 57726dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size_t framesIn; 577373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung activeTrack->mResamplerBufferProvider->sync(&framesIn, &hasOverrun); 577473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung if (hasOverrun) { 57756dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten overrun = OVERRUN_TRUE; 57766dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 57774cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten if (framesOut == 0 || framesIn == 0) { 57784cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten break; 57794cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten } 57804cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten 57816770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung // Don't allow framesOut to be larger than what is possible with resampling 57826770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung // from framesIn. 57836770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung // This isn't strictly necessary but helps limit buffer resizing in 57846770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung // RecordBufferConverter. TODO: remove when no longer needed. 57856770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung framesOut = min(framesOut, 57866770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung destinationFramesPossible( 57876770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung framesIn, mSampleRate, activeTrack->mSampleRate)); 578897a893eb34f8687485c88eaf15917974a203f20bAndy Hung // process frames from the RecordThread buffer provider to the RecordTrack buffer 578997a893eb34f8687485c88eaf15917974a203f20bAndy Hung framesOut = activeTrack->mRecordBufferConverter->convert( 579097a893eb34f8687485c88eaf15917974a203f20bAndy Hung activeTrack->mSink.raw, activeTrack->mResamplerBufferProvider, framesOut); 57918594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten 57926dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) { 57936dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten overrun = OVERRUN_FALSE; 57941ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten } 579581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 57966dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTrack->mFramesToDrop == 0) { 57976dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (framesOut > 0) { 57986dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mSink.frameCount = framesOut; 57996dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->releaseBuffer(&activeTrack->mSink); 580081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 580181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 58026dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME could do a partial drop of framesOut 58036dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTrack->mFramesToDrop > 0) { 58046dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mFramesToDrop -= framesOut; 58056dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTrack->mFramesToDrop <= 0) { 580625f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten activeTrack->clearSyncStartEvent(); 58076dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 58086dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } else { 58096dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mFramesToDrop += framesOut; 58106dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 || 58116dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mSyncStartEvent->isCancelled()) { 58126dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ALOGW("Synced record %s, session %d, trigger session %d", 58136dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled", 58146dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->sessionId(), 58156dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten (activeTrack->mSyncStartEvent != 0) ? 58166dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->mSyncStartEvent->triggerSession() : 0); 581725f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten activeTrack->clearSyncStartEvent(); 58186dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 581981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 582081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 58216dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 58226dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (framesOut == 0) { 58236dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten break; 58246dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 582581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 58266dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 58276dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten switch (overrun) { 58286dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case OVERRUN_TRUE: 58296dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // client isn't retrieving buffers fast enough 58306dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (!activeTrack->setOverflow()) { 58316dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten nsecs_t now = systemTime(); 58326dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME should lastWarning per track? 58336dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if ((now - lastWarning) > kWarningThrottleNs) { 58346dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ALOGW("RecordThread: buffer overflow"); 58356dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten lastWarning = now; 58366dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 583781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 58386dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten break; 58396dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case OVERRUN_FALSE: 58406dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten activeTrack->clearOverflow(); 58416dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten break; 58426dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten case OVERRUN_UNKNOWN: 58436dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten break; 584481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 58456dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 584681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 58471ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten 58483d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kastenunlock: 584981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // enable changes in effect chain 585081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent unlockEffectChains(effectChains); 5851c527a7c2b1bfd26e8f3086e1b653d56e521379d9Glenn Kasten // effectChains doesn't need to be cleared, since it is cleared by destructor at scope end 585281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 585381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 585493e471f620454f7de73d190521568b1e25879767Glenn Kasten standbyIfNotAlreadyInStandby(); 585581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 585681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 585781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 58589a54bc27876acd5d8be5b1fc3dc46701fe76fbb3Eric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 58599a54bc27876acd5d8be5b1fc3dc46701fe76fbb3Eric Laurent sp<RecordTrack> track = mTracks[i]; 58609a54bc27876acd5d8be5b1fc3dc46701fe76fbb3Eric Laurent track->invalidate(); 58619a54bc27876acd5d8be5b1fc3dc46701fe76fbb3Eric Laurent } 58622b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracks.clear(); 58632b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracksGen++; 586481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStartStopCond.broadcast(); 586581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 586681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 586781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent releaseWakeLock(); 586881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 586981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("RecordThread %p exiting", this); 587081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 587181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 587281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 587393e471f620454f7de73d190521568b1e25879767Glenn Kastenvoid AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby() 587481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 587581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!mStandby) { 587681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent inputStandBy(); 587781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStandby = true; 587881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 587981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 588081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 588181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::inputStandBy() 588281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 58836dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // Idle the fast capture if it's currently running 58846dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (mFastCapture != 0) { 58856dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureStateQueue *sq = mFastCapture->sq(); 58866dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureState *state = sq->begin(); 58876dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (!(state->mCommand & FastCaptureState::IDLE)) { 58886dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mCommand = FastCaptureState::COLD_IDLE; 58896dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mColdFutexAddr = &mFastCaptureFutex; 58906dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten state->mColdGen++; 58916dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastCaptureFutex = 0; 58926dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sq->end(); 58936dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now 58946dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED); 58956dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#if 0 58966dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (kUseFastCapture == FastCapture_Dynamic) { 58976dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // FIXME 58986dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 58996dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif 59006dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef AUDIO_WATCHDOG 59016dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // FIXME 59026dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif 59036dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } else { 59046dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sq->end(false /*didModify*/); 59056dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 59066dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 590781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mInput->stream->common.standby(&mInput->stream->common); 590881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 590981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 591005997e21af6c4517f375def6563af4b9ebe95f39Glenn Kasten// RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held 5911e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kastensp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( 591281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<AudioFlinger::Client>& client, 591381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t sampleRate, 591481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_format_t format, 591581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_channel_mask_t channelMask, 591674935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten size_t *pFrameCount, 591781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId, 59187df8c0b799d8f52d6386e03313286dbd7d5cdc7cGlenn Kasten size_t *notificationFrames, 5919462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen int uid, 5920ddb0ccf3fb6fe8da8c71a6deb30561b821f3c0a2Glenn Kasten IAudioFlinger::track_flags_t *flags, 592181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t tid, 592281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t *status) 592381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 592474935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten size_t frameCount = *pFrameCount; 592581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<RecordTrack> track; 592681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t lStatus; 592781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 592890e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten // client expresses a preference for FAST, but we get the final say 592990e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten if (*flags & IAudioFlinger::TRACK_FAST) { 593090e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten if ( 5931b7fbf7ecc6b034243ec64f79f3113675b5e3c941Glenn Kasten // we formerly checked for a callback handler (non-0 tid), 5932b7fbf7ecc6b034243ec64f79f3113675b5e3c941Glenn Kasten // but that is no longer required for TRANSFER_OBTAIN mode 5933b7fbf7ecc6b034243ec64f79f3113675b5e3c941Glenn Kasten // 59347410591dad836434c72ddee66680802708b70c10Glenn Kasten // frame count is not specified, or is exactly the pipe depth 59357410591dad836434c72ddee66680802708b70c10Glenn Kasten ((frameCount == 0) || (frameCount == mPipeFramesP2)) && 59363a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten // PCM data 59373a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten audio_is_linear_pcm(format) && 59386dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // native format 59396dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (format == mFormat) && 59406dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // native channel mask 59416dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (channelMask == mChannelMask) && 59426dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // native hardware sample rate 594390e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten (sampleRate == mSampleRate) && 59443a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten // record thread has an associated fast capture 59456dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten hasFastCapture() && 59466dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // there are sufficient fast track slots available 59476dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastTrackAvail 594890e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten ) { 59497410591dad836434c72ddee66680802708b70c10Glenn Kasten ALOGV("AUDIO_INPUT_FLAG_FAST accepted: frameCount=%u mFrameCount=%u", 595090e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten frameCount, mFrameCount); 595190e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten } else { 59527410591dad836434c72ddee66680802708b70c10Glenn Kasten ALOGV("AUDIO_INPUT_FLAG_FAST denied: frameCount=%u mFrameCount=%u mPipeFramesP2=%u " 59537410591dad836434c72ddee66680802708b70c10Glenn Kasten "format=%#x isLinear=%d channelMask=%#x sampleRate=%u mSampleRate=%u " 59546dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten "hasFastCapture=%d tid=%d mFastTrackAvail=%d", 59557410591dad836434c72ddee66680802708b70c10Glenn Kasten frameCount, mFrameCount, mPipeFramesP2, 59567410591dad836434c72ddee66680802708b70c10Glenn Kasten format, audio_is_linear_pcm(format), channelMask, sampleRate, mSampleRate, 59577410591dad836434c72ddee66680802708b70c10Glenn Kasten hasFastCapture(), tid, mFastTrackAvail); 595890e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten *flags &= ~IAudioFlinger::TRACK_FAST; 59597410591dad836434c72ddee66680802708b70c10Glenn Kasten } 59607410591dad836434c72ddee66680802708b70c10Glenn Kasten } 59617410591dad836434c72ddee66680802708b70c10Glenn Kasten 59627410591dad836434c72ddee66680802708b70c10Glenn Kasten // compute track buffer size in frames, and suggest the notification frame count 59637410591dad836434c72ddee66680802708b70c10Glenn Kasten if (*flags & IAudioFlinger::TRACK_FAST) { 59647410591dad836434c72ddee66680802708b70c10Glenn Kasten // fast track: frame count is exactly the pipe depth 59657410591dad836434c72ddee66680802708b70c10Glenn Kasten frameCount = mPipeFramesP2; 59667410591dad836434c72ddee66680802708b70c10Glenn Kasten // ignore requested notificationFrames, and always notify exactly once every HAL buffer 59677410591dad836434c72ddee66680802708b70c10Glenn Kasten *notificationFrames = mFrameCount; 59687410591dad836434c72ddee66680802708b70c10Glenn Kasten } else { 596949d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // not fast track: max notification period is resampled equivalent of one HAL buffer time 597049d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // or 20 ms if there is a fast capture 597149d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // TODO This could be a roundupRatio inline, and const 597249d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount) 597349d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten * sampleRate + mSampleRate - 1) / mSampleRate; 597449d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // minimum number of notification periods is at least kMinNotifications, 597549d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs) 597649d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten static const size_t kMinNotifications = 3; 597749d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten static const uint32_t kMinMs = 30; 597849d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // TODO This could be a roundupRatio inline 597949d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000; 598049d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // TODO This could be a roundupRatio inline 598149d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) / 598249d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten maxNotificationFrames; 598349d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten const size_t minFrameCount = maxNotificationFrames * 598449d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten max(kMinNotifications, minNotificationsByMs); 598549d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten frameCount = max(frameCount, minFrameCount); 598649d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten if (*notificationFrames == 0 || *notificationFrames > maxNotificationFrames) { 598749d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten *notificationFrames = maxNotificationFrames; 59887410591dad836434c72ddee66680802708b70c10Glenn Kasten } 598990e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten } 599074935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten *pFrameCount = frameCount; 599190e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten 599215e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten lStatus = initCheck(); 599315e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten if (lStatus != NO_ERROR) { 599415e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten ALOGE("createRecordTrack_l() audio driver not initialized"); 599515e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten goto Exit; 599615e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten } 599781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 599881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // scope for mLock 599981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 600081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 600181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track = new RecordTrack(this, client, sampleRate, 600283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent format, channelMask, frameCount, NULL, sessionId, uid, 600383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent *flags, TrackBase::TYPE_DEFAULT); 600481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 6005030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten lStatus = track->initCheck(); 6006030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten if (lStatus != NO_ERROR) { 600735295078ab59c8c5d143a54d5a55557c3ca62c51Glenn Kasten ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus); 600803e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George // track must be cleared from the caller as the caller has the AF lock 600981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 601081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 601181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mTracks.add(track); 601281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 601381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // disable AEC and NS if the device is a BT SCO headset supporting those pre processings 601481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool suspend = audio_is_bluetooth_sco_device(mInDevice) && 601581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioFlinger->btNrecIsOff(); 601681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); 601781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent setEffectSuspended_l(FX_IID_NS, suspend, sessionId); 601890e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten 601990e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { 602090e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten pid_t callingPid = IPCThreadState::self()->getCallingPid(); 602190e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, 602290e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten // so ask activity manager to do this on our behalf 602390e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); 602490e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten } 602581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 602605997e21af6c4517f375def6563af4b9ebe95f39Glenn Kasten 602781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = NO_ERROR; 602881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 602981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit: 60309156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten *status = lStatus; 603181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return track; 603281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 603381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 603481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, 603581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSystem::sync_event_t event, 603681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int triggerSession) 603781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 603881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); 603981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<ThreadBase> strongMe = this; 604081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status = NO_ERROR; 604181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 604281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (event == AudioSystem::SYNC_EVENT_NONE) { 604325f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten recordTrack->clearSyncStartEvent(); 604481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (event != AudioSystem::SYNC_EVENT_SAME) { 60456dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event, 604681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent triggerSession, 604781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent recordTrack->sessionId(), 604881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent syncStartEventCallback, 60496dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack); 605081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Sync event can be cancelled by the trigger session if the track is not in a 605181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // compatible state in which case we start record immediately 60526dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (recordTrack->mSyncStartEvent->isCancelled()) { 605325f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten recordTrack->clearSyncStartEvent(); 605481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 605581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs 60566dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack->mFramesToDrop = - 60574cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000); 605881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 605981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 606081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 606181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 606247c2070b2ce8aedb7300c0aad91caccf3c383841Glenn Kasten // This section is a rendezvous between binder thread executing start() and RecordThread 606381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AutoMutex lock(mLock); 60646dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (mActiveTracks.indexOf(recordTrack) >= 0) { 60656dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (recordTrack->mState == TrackBase::PAUSING) { 60666dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ALOGV("active record track PAUSING -> ACTIVE"); 6067f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten recordTrack->mState = TrackBase::ACTIVE; 60686dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } else { 60696dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten ALOGV("active record track state %d", recordTrack->mState); 607081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 607181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 607281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 607381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 60744cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten // TODO consider other ways of handling this, such as changing the state to :STARTING and 60754cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten // adding the track to mActiveTracks after returning from AudioSystem::startInput(), 60764cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten // or using a separate command thread 60776dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack->mState = TrackBase::STARTING_1; 60782b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracks.add(recordTrack); 60792b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten mActiveTracksGen++; 608083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent status_t status = NO_ERROR; 608183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent if (recordTrack->isExternalTrack()) { 608283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent mLock.unlock(); 60834dc680607181e6a76f4e91a39366c4f5dfb7b03eEric Laurent status = AudioSystem::startInput(mId, (audio_session_t)recordTrack->sessionId()); 608483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent mLock.lock(); 608583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent // FIXME should verify that recordTrack is still in mActiveTracks 608683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent if (status != NO_ERROR) { 608783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent mActiveTracks.remove(recordTrack); 608883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent mActiveTracksGen++; 608983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent recordTrack->clearSyncStartEvent(); 609083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent ALOGV("RecordThread::start error %d", status); 609183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent return status; 609283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent } 609381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 60946dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // Catch up with current buffer indices if thread is already running. 60956dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // This is what makes a new client discard all buffered data. If the track's mRsmpInFront 60966dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // was initialized to some value closer to the thread's mRsmpInFront, then the track could 60976dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // see previously buffered data before it called start(), but with greater risk of overrun. 60986dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 609973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung recordTrack->mResamplerBufferProvider->reset(); 610097a893eb34f8687485c88eaf15917974a203f20bAndy Hung // clear any converter state as new data will be discontinuous 610197a893eb34f8687485c88eaf15917974a203f20bAndy Hung recordTrack->mRecordBufferConverter->reset(); 61026dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten recordTrack->mState = TrackBase::STARTING_2; 610381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // signal thread to start 610481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mWaitWorkCV.broadcast(); 61052b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (mActiveTracks.indexOf(recordTrack) < 0) { 610681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("Record failed to start"); 610781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = BAD_VALUE; 610881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto startError; 610981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 611081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 611181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 61127c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten 611381784c37c61b09289654b979567a42bf73cd2b12Eric LaurentstartError: 611483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent if (recordTrack->isExternalTrack()) { 61154dc680607181e6a76f4e91a39366c4f5dfb7b03eEric Laurent AudioSystem::stopInput(mId, (audio_session_t)recordTrack->sessionId()); 611683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent } 611725f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten recordTrack->clearSyncStartEvent(); 61186dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME I wonder why we do not reset the state here? 611981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return status; 612081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 612181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 612281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) 612381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 612481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<SyncEvent> strongEvent = event.promote(); 612581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 612681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (strongEvent != 0) { 61278ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent sp<RefBase> ptr = strongEvent->cookie().promote(); 61288ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent if (ptr != 0) { 61298ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent RecordTrack *recordTrack = (RecordTrack *)ptr.get(); 61308ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent recordTrack->handleSyncStartEvent(strongEvent); 61318ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent } 613281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 613381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 613481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 6135a8356f663014e7d4c27869629af83d8bb3441e19Glenn Kastenbool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { 613681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("RecordThread::stop"); 6137a8356f663014e7d4c27869629af83d8bb3441e19Glenn Kasten AutoMutex _l(mLock); 61382b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (mActiveTracks.indexOf(recordTrack) != 0 || recordTrack->mState == TrackBase::PAUSING) { 613981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 614081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 614147c2070b2ce8aedb7300c0aad91caccf3c383841Glenn Kasten // note that threadLoop may still be processing the track at this point [without lock] 614281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent recordTrack->mState = TrackBase::PAUSING; 614381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // do not wait for mStartStopCond if exiting 614481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (exitPending()) { 614581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return true; 614681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 614747c2070b2ce8aedb7300c0aad91caccf3c383841Glenn Kasten // FIXME incorrect usage of wait: no explicit predicate or loop 614881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mStartStopCond.wait(mLock); 61492b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten // if we have been restarted, recordTrack is in mActiveTracks here 61502b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (exitPending() || mActiveTracks.indexOf(recordTrack) != 0) { 615181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("Record stopped OK"); 615281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return true; 615381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 615481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 615581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 615681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 61570f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenbool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const 615881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 615981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 616081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 616181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 61620f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenstatus_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused) 616381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 616481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#if 0 // This branch is currently dead code, but is preserved in case it will be needed in future 616581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!isValidSyncEvent(event)) { 616681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 616781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 616881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 616981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int eventSession = event->triggerSession(); 617081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t ret = NAME_NOT_FOUND; 617181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 617281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 617381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 617481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 617581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<RecordTrack> track = mTracks[i]; 617681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (eventSession == track->sessionId()) { 617781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (void) track->setSyncEvent(event); 617881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ret = NO_ERROR; 617981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 618081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 618181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return ret; 618281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#else 618381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 618481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 618581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 618681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 618781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// destroyTrack_l() must be called with ThreadBase::mLock held 618881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track) 618981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 6190bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->terminate(); 6191bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent track->mState = TrackBase::STOPPED; 619281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // active tracks are removed by threadLoop() 61932b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten if (mActiveTracks.indexOf(track) < 0) { 619481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent removeTrack_l(track); 619581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 619681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 619781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 619881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track) 619981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 620081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mTracks.remove(track); 620181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // need anything related to effects here? 62026dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (track->isFastTrack()) { 62036dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten ALOG_ASSERT(!mFastTrackAvail); 62046dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mFastTrackAvail = true; 62056dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 620681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 620781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 620881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) 620981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 621081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpInternals(fd, args); 621181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpTracks(fd, args); 621281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dumpEffectChains(fd, args); 621381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 621481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 621581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args) 621681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 621787cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, "\nInput thread %p:\n", this); 621881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 621944182c206f7c5584ef2cf504da6be98fab665dbfGlenn Kasten dumpBase(fd, args); 622044182c206f7c5584ef2cf504da6be98fab665dbfGlenn Kasten 622144182c206f7c5584ef2cf504da6be98fab665dbfGlenn Kasten if (mActiveTracks.size() == 0) { 622287cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " No active record clients\n"); 622381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 62246e6704c06d61bc356e30c164081e5bcffb37920cGlenn Kasten dprintf(fd, " Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no"); 62256dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten dprintf(fd, " Fast track available: %s\n", mFastTrackAvail ? "yes" : "no"); 622617c9c998afed5ed9df7495eeed5822f3ed53ebecGlenn Kasten 622717c9c998afed5ed9df7495eeed5822f3ed53ebecGlenn Kasten // Make a non-atomic copy of fast capture dump state so it won't change underneath us 622817c9c998afed5ed9df7495eeed5822f3ed53ebecGlenn Kasten const FastCaptureDumpState copy(mFastCaptureDumpState); 622917c9c998afed5ed9df7495eeed5822f3ed53ebecGlenn Kasten copy.dump(fd); 623081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 623181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 62320f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args __unused) 623381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 623481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const size_t SIZE = 256; 623581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent char buffer[SIZE]; 623681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent String8 result; 623781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 6238b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numtracks = mTracks.size(); 6239b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numactive = mActiveTracks.size(); 6240b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen size_t numactiveseen = 0; 624187cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " %d Tracks", numtracks); 6242b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (numtracks) { 624387cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, " of which %d are active\n", numactive); 6244b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen RecordTrack::appendDumpHeader(result); 6245b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen for (size_t i = 0; i < numtracks ; ++i) { 6246b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen sp<RecordTrack> track = mTracks[i]; 6247b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (track != 0) { 6248b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen bool active = mActiveTracks.indexOf(track) >= 0; 6249b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (active) { 6250b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen numactiveseen++; 6251b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 6252b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen track->dump(buffer, SIZE, active); 6253b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append(buffer); 6254b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 625581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 6256b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } else { 625787cebadd48710e42474756fc3513df678de045ceElliott Hughes dprintf(fd, "\n"); 625881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 625981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 6260b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (numactiveseen != numactive) { 6261b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen snprintf(buffer, SIZE, " The following tracks are in the active list but" 6262b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen " not in the track list\n"); 626381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result.append(buffer); 626481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent RecordTrack::appendDumpHeader(result); 6265b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen for (size_t i = 0; i < numactive; ++i) { 62662b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten sp<RecordTrack> track = mActiveTracks[i]; 6267b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen if (mTracks.indexOf(track) < 0) { 6268b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen track->dump(buffer, SIZE, true); 6269b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append(buffer); 6270b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 62712b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten } 627281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 627381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 627481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent write(fd, result.string(), result.size()); 627581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 627681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 627773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung 627873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hungvoid AudioFlinger::RecordThread::ResamplerBufferProvider::reset() 627973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung{ 628073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); 628173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung RecordThread *recordThread = (RecordThread *) threadBase.get(); 628273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung mRsmpInFront = recordThread->mRsmpInRear; 628373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung mRsmpInUnrel = 0; 628473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung} 628573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung 628673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hungvoid AudioFlinger::RecordThread::ResamplerBufferProvider::sync( 628773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung size_t *framesAvailable, bool *hasOverrun) 628873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung{ 628973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); 629073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung RecordThread *recordThread = (RecordThread *) threadBase.get(); 629173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung const int32_t rear = recordThread->mRsmpInRear; 629273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung const int32_t front = mRsmpInFront; 629373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung const ssize_t filled = rear - front; 629473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung 629573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung size_t framesIn; 629673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung bool overrun = false; 629773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung if (filled < 0) { 629873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung // should not happen, but treat like a massive overrun and re-sync 629973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung framesIn = 0; 630073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung mRsmpInFront = rear; 630173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung overrun = true; 630273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung } else if ((size_t) filled <= recordThread->mRsmpInFrames) { 630373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung framesIn = (size_t) filled; 630473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung } else { 630573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung // client is not keeping up with server, but give it latest data 630673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung framesIn = recordThread->mRsmpInFrames; 630773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung mRsmpInFront = /* front = */ rear - framesIn; 630873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung overrun = true; 630973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung } 631073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung if (framesAvailable != NULL) { 631173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung *framesAvailable = framesIn; 631273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung } 631373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung if (hasOverrun != NULL) { 631473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung *hasOverrun = overrun; 631573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung } 631673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung} 631773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung 631881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// AudioBufferProvider interface 63196dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kastenstatus_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer( 63206dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten AudioBufferProvider::Buffer* buffer, int64_t pts __unused) 632181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 632273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); 63236dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten if (threadBase == 0) { 63246dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten buffer->frameCount = 0; 6325607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten buffer->raw = NULL; 63266dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten return NOT_ENOUGH_DATA; 63276dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten } 63286dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten RecordThread *recordThread = (RecordThread *) threadBase.get(); 63296dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten int32_t rear = recordThread->mRsmpInRear; 633073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung int32_t front = mRsmpInFront; 63318594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten ssize_t filled = rear - front; 63326dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME should not be P2 (don't want to increase latency) 63336dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // FIXME if client not keeping up, discard 6334607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames)); 63358594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten // 'filled' may be non-contiguous, so return only the first contiguous chunk 63366dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten front &= recordThread->mRsmpInFramesP2 - 1; 63376dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten size_t part1 = recordThread->mRsmpInFramesP2 - front; 63388594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten if (part1 > (size_t) filled) { 63398594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten part1 = filled; 63408594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten } 63418594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten size_t ask = buffer->frameCount; 63428594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten ALOG_ASSERT(ask > 0); 63438594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten if (part1 > ask) { 63448594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten part1 = ask; 63458594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten } 63468594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten if (part1 == 0) { 634773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung // out of data is fine since the resampler will return a short-count. 63488594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten buffer->raw = NULL; 63498594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten buffer->frameCount = 0; 635073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung mRsmpInUnrel = 0; 63518594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten return NOT_ENOUGH_DATA; 63528594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten } 63538594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten 63545744661e85981f8a9456bf470e2761235fc026daAndy Hung buffer->raw = (uint8_t*)recordThread->mRsmpInBuffer + front * recordThread->mFrameSize; 63558594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten buffer->frameCount = part1; 635673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung mRsmpInUnrel = part1; 635781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 635881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 635981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 636081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// AudioBufferProvider interface 63616dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kastenvoid AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer( 63626dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten AudioBufferProvider::Buffer* buffer) 636381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 63648594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten size_t stepCount = buffer->frameCount; 63658594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten if (stepCount == 0) { 63668594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten return; 63678594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten } 636873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung ALOG_ASSERT(stepCount <= mRsmpInUnrel); 636973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung mRsmpInUnrel -= stepCount; 637073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung mRsmpInFront += stepCount; 63718594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten buffer->raw = NULL; 637281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent buffer->frameCount = 0; 637381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 637481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 637597a893eb34f8687485c88eaf15917974a203f20bAndy HungAudioFlinger::RecordThread::RecordBufferConverter::RecordBufferConverter( 637697a893eb34f8687485c88eaf15917974a203f20bAndy Hung audio_channel_mask_t srcChannelMask, audio_format_t srcFormat, 637797a893eb34f8687485c88eaf15917974a203f20bAndy Hung uint32_t srcSampleRate, 637897a893eb34f8687485c88eaf15917974a203f20bAndy Hung audio_channel_mask_t dstChannelMask, audio_format_t dstFormat, 637997a893eb34f8687485c88eaf15917974a203f20bAndy Hung uint32_t dstSampleRate) : 638097a893eb34f8687485c88eaf15917974a203f20bAndy Hung mSrcChannelMask(AUDIO_CHANNEL_INVALID), // updateParameters will set following vars 638197a893eb34f8687485c88eaf15917974a203f20bAndy Hung // mSrcFormat 638297a893eb34f8687485c88eaf15917974a203f20bAndy Hung // mSrcSampleRate 638397a893eb34f8687485c88eaf15917974a203f20bAndy Hung // mDstChannelMask 638497a893eb34f8687485c88eaf15917974a203f20bAndy Hung // mDstFormat 638597a893eb34f8687485c88eaf15917974a203f20bAndy Hung // mDstSampleRate 638697a893eb34f8687485c88eaf15917974a203f20bAndy Hung // mSrcChannelCount 638797a893eb34f8687485c88eaf15917974a203f20bAndy Hung // mDstChannelCount 638897a893eb34f8687485c88eaf15917974a203f20bAndy Hung // mDstFrameSize 638997a893eb34f8687485c88eaf15917974a203f20bAndy Hung mBuf(NULL), mBufFrames(0), mBufFrameSize(0), 6390d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mResampler(NULL), 6391d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mIsLegacyDownmix(false), 6392d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mIsLegacyUpmix(false), 6393d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mRequiresFloat(false), 6394d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mInputConverterProvider(NULL) 639597a893eb34f8687485c88eaf15917974a203f20bAndy Hung{ 639697a893eb34f8687485c88eaf15917974a203f20bAndy Hung (void)updateParameters(srcChannelMask, srcFormat, srcSampleRate, 639797a893eb34f8687485c88eaf15917974a203f20bAndy Hung dstChannelMask, dstFormat, dstSampleRate); 639897a893eb34f8687485c88eaf15917974a203f20bAndy Hung} 639997a893eb34f8687485c88eaf15917974a203f20bAndy Hung 640097a893eb34f8687485c88eaf15917974a203f20bAndy HungAudioFlinger::RecordThread::RecordBufferConverter::~RecordBufferConverter() { 640197a893eb34f8687485c88eaf15917974a203f20bAndy Hung free(mBuf); 640297a893eb34f8687485c88eaf15917974a203f20bAndy Hung delete mResampler; 6403d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung delete mInputConverterProvider; 640497a893eb34f8687485c88eaf15917974a203f20bAndy Hung} 640597a893eb34f8687485c88eaf15917974a203f20bAndy Hung 640697a893eb34f8687485c88eaf15917974a203f20bAndy Hungsize_t AudioFlinger::RecordThread::RecordBufferConverter::convert(void *dst, 640797a893eb34f8687485c88eaf15917974a203f20bAndy Hung AudioBufferProvider *provider, size_t frames) 640897a893eb34f8687485c88eaf15917974a203f20bAndy Hung{ 6409d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mInputConverterProvider != NULL) { 6410d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mInputConverterProvider->setBufferProvider(provider); 6411d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung provider = mInputConverterProvider; 6412d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } 6413d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung 6414d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mResampler == NULL) { 641597a893eb34f8687485c88eaf15917974a203f20bAndy Hung ALOGVV("NO RESAMPLING sampleRate:%u mSrcFormat:%#x mDstFormat:%#x", 641697a893eb34f8687485c88eaf15917974a203f20bAndy Hung mSrcSampleRate, mSrcFormat, mDstFormat); 641797a893eb34f8687485c88eaf15917974a203f20bAndy Hung 641897a893eb34f8687485c88eaf15917974a203f20bAndy Hung AudioBufferProvider::Buffer buffer; 641997a893eb34f8687485c88eaf15917974a203f20bAndy Hung for (size_t i = frames; i > 0; ) { 642097a893eb34f8687485c88eaf15917974a203f20bAndy Hung buffer.frameCount = i; 642197a893eb34f8687485c88eaf15917974a203f20bAndy Hung status_t status = provider->getNextBuffer(&buffer, 0); 642297a893eb34f8687485c88eaf15917974a203f20bAndy Hung if (status != OK || buffer.frameCount == 0) { 642397a893eb34f8687485c88eaf15917974a203f20bAndy Hung frames -= i; // cannot fill request. 642497a893eb34f8687485c88eaf15917974a203f20bAndy Hung break; 642597a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 6426d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // format convert to destination buffer 6427d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung convertNoResampler(dst, buffer.raw, buffer.frameCount); 642897a893eb34f8687485c88eaf15917974a203f20bAndy Hung 642997a893eb34f8687485c88eaf15917974a203f20bAndy Hung dst = (int8_t*)dst + buffer.frameCount * mDstFrameSize; 643097a893eb34f8687485c88eaf15917974a203f20bAndy Hung i -= buffer.frameCount; 643197a893eb34f8687485c88eaf15917974a203f20bAndy Hung provider->releaseBuffer(&buffer); 643297a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 643397a893eb34f8687485c88eaf15917974a203f20bAndy Hung } else { 643497a893eb34f8687485c88eaf15917974a203f20bAndy Hung ALOGVV("RESAMPLING mSrcSampleRate:%u mDstSampleRate:%u mSrcFormat:%#x mDstFormat:%#x", 643597a893eb34f8687485c88eaf15917974a203f20bAndy Hung mSrcSampleRate, mDstSampleRate, mSrcFormat, mDstFormat); 643697a893eb34f8687485c88eaf15917974a203f20bAndy Hung 6437d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // reallocate buffer if needed 6438d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mBufFrameSize != 0 && mBufFrames < frames) { 6439d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung free(mBuf); 6440d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mBufFrames = frames; 6441d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung (void)posix_memalign(&mBuf, 32, mBufFrames * mBufFrameSize); 6442d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } 644397a893eb34f8687485c88eaf15917974a203f20bAndy Hung // resampler accumulates, but we only have one source track 6444d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung memset(mBuf, 0, frames * mBufFrameSize); 6445d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung frames = mResampler->resample((int32_t*)mBuf, frames, provider); 6446d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // format convert to destination buffer 6447d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung convertResampler(dst, mBuf, frames); 644897a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 644997a893eb34f8687485c88eaf15917974a203f20bAndy Hung return frames; 645097a893eb34f8687485c88eaf15917974a203f20bAndy Hung} 645197a893eb34f8687485c88eaf15917974a203f20bAndy Hung 645297a893eb34f8687485c88eaf15917974a203f20bAndy Hungstatus_t AudioFlinger::RecordThread::RecordBufferConverter::updateParameters( 645397a893eb34f8687485c88eaf15917974a203f20bAndy Hung audio_channel_mask_t srcChannelMask, audio_format_t srcFormat, 645497a893eb34f8687485c88eaf15917974a203f20bAndy Hung uint32_t srcSampleRate, 645597a893eb34f8687485c88eaf15917974a203f20bAndy Hung audio_channel_mask_t dstChannelMask, audio_format_t dstFormat, 645697a893eb34f8687485c88eaf15917974a203f20bAndy Hung uint32_t dstSampleRate) 645797a893eb34f8687485c88eaf15917974a203f20bAndy Hung{ 645897a893eb34f8687485c88eaf15917974a203f20bAndy Hung // quick evaluation if there is any change. 645997a893eb34f8687485c88eaf15917974a203f20bAndy Hung if (mSrcFormat == srcFormat 646097a893eb34f8687485c88eaf15917974a203f20bAndy Hung && mSrcChannelMask == srcChannelMask 646197a893eb34f8687485c88eaf15917974a203f20bAndy Hung && mSrcSampleRate == srcSampleRate 646297a893eb34f8687485c88eaf15917974a203f20bAndy Hung && mDstFormat == dstFormat 646397a893eb34f8687485c88eaf15917974a203f20bAndy Hung && mDstChannelMask == dstChannelMask 646497a893eb34f8687485c88eaf15917974a203f20bAndy Hung && mDstSampleRate == dstSampleRate) { 646597a893eb34f8687485c88eaf15917974a203f20bAndy Hung return NO_ERROR; 646697a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 646797a893eb34f8687485c88eaf15917974a203f20bAndy Hung 6468db4c031f518ae5806af73756273ff32cd8d0e4f8Andy Hung ALOGV("RecordBufferConverter updateParameters srcMask:%#x dstMask:%#x" 6469db4c031f518ae5806af73756273ff32cd8d0e4f8Andy Hung " srcFormat:%#x dstFormat:%#x srcRate:%u dstRate:%u", 6470db4c031f518ae5806af73756273ff32cd8d0e4f8Andy Hung srcChannelMask, dstChannelMask, srcFormat, dstFormat, srcSampleRate, dstSampleRate); 647197a893eb34f8687485c88eaf15917974a203f20bAndy Hung const bool valid = 647297a893eb34f8687485c88eaf15917974a203f20bAndy Hung audio_is_input_channel(srcChannelMask) 647397a893eb34f8687485c88eaf15917974a203f20bAndy Hung && audio_is_input_channel(dstChannelMask) 647497a893eb34f8687485c88eaf15917974a203f20bAndy Hung && audio_is_valid_format(srcFormat) && audio_is_linear_pcm(srcFormat) 647597a893eb34f8687485c88eaf15917974a203f20bAndy Hung && audio_is_valid_format(dstFormat) && audio_is_linear_pcm(dstFormat) 647697a893eb34f8687485c88eaf15917974a203f20bAndy Hung && (srcSampleRate <= dstSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) 647797a893eb34f8687485c88eaf15917974a203f20bAndy Hung ; // no upsampling checks for now 647897a893eb34f8687485c88eaf15917974a203f20bAndy Hung if (!valid) { 647997a893eb34f8687485c88eaf15917974a203f20bAndy Hung return BAD_VALUE; 648097a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 648197a893eb34f8687485c88eaf15917974a203f20bAndy Hung 648297a893eb34f8687485c88eaf15917974a203f20bAndy Hung mSrcFormat = srcFormat; 648397a893eb34f8687485c88eaf15917974a203f20bAndy Hung mSrcChannelMask = srcChannelMask; 648497a893eb34f8687485c88eaf15917974a203f20bAndy Hung mSrcSampleRate = srcSampleRate; 648597a893eb34f8687485c88eaf15917974a203f20bAndy Hung mDstFormat = dstFormat; 648697a893eb34f8687485c88eaf15917974a203f20bAndy Hung mDstChannelMask = dstChannelMask; 648797a893eb34f8687485c88eaf15917974a203f20bAndy Hung mDstSampleRate = dstSampleRate; 648897a893eb34f8687485c88eaf15917974a203f20bAndy Hung 648997a893eb34f8687485c88eaf15917974a203f20bAndy Hung // compute derived parameters 649097a893eb34f8687485c88eaf15917974a203f20bAndy Hung mSrcChannelCount = audio_channel_count_from_in_mask(srcChannelMask); 649197a893eb34f8687485c88eaf15917974a203f20bAndy Hung mDstChannelCount = audio_channel_count_from_in_mask(dstChannelMask); 649297a893eb34f8687485c88eaf15917974a203f20bAndy Hung mDstFrameSize = mDstChannelCount * audio_bytes_per_sample(mDstFormat); 649397a893eb34f8687485c88eaf15917974a203f20bAndy Hung 6494d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // do we need to resample? 6495d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung delete mResampler; 6496d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mResampler = NULL; 6497d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mSrcSampleRate != mDstSampleRate) { 6498d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mResampler = AudioResampler::create(AUDIO_FORMAT_PCM_FLOAT, 6499d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mSrcChannelCount, mDstSampleRate); 6500d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mResampler->setSampleRate(mSrcSampleRate); 6501d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mResampler->setVolume(AudioMixer::UNITY_GAIN_FLOAT, AudioMixer::UNITY_GAIN_FLOAT); 6502d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } 6503d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung 6504d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // are we running legacy channel conversion modes? 6505d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mIsLegacyDownmix = (mSrcChannelMask == AUDIO_CHANNEL_IN_STEREO 6506d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung || mSrcChannelMask == AUDIO_CHANNEL_IN_FRONT_BACK) 6507d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung && mDstChannelMask == AUDIO_CHANNEL_IN_MONO; 6508d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mIsLegacyUpmix = mSrcChannelMask == AUDIO_CHANNEL_IN_MONO 6509d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung && (mDstChannelMask == AUDIO_CHANNEL_IN_STEREO 6510d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung || mDstChannelMask == AUDIO_CHANNEL_IN_FRONT_BACK); 6511d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung 6512d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // do we need to process in float? 6513d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mRequiresFloat = mResampler != NULL || mIsLegacyDownmix || mIsLegacyUpmix; 6514d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung 6515d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // do we need a staging buffer to convert for destination (we can still optimize this)? 6516d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // we use mBufFrameSize > 0 to indicate both frame size as well as buffer necessity 6517d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mResampler != NULL) { 6518d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mBufFrameSize = max(mSrcChannelCount, FCC_2) 6519d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung * audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT); 6520d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } else if ((mIsLegacyUpmix || mIsLegacyDownmix) && mDstFormat != AUDIO_FORMAT_PCM_FLOAT) { 6521d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mBufFrameSize = mDstChannelCount * audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT); 6522d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } else if (mSrcChannelMask != mDstChannelMask && mDstFormat != mSrcFormat) { 652397a893eb34f8687485c88eaf15917974a203f20bAndy Hung mBufFrameSize = mDstChannelCount * audio_bytes_per_sample(mSrcFormat); 652497a893eb34f8687485c88eaf15917974a203f20bAndy Hung } else { 652597a893eb34f8687485c88eaf15917974a203f20bAndy Hung mBufFrameSize = 0; 652697a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 652797a893eb34f8687485c88eaf15917974a203f20bAndy Hung mBufFrames = 0; // force the buffer to be resized. 652897a893eb34f8687485c88eaf15917974a203f20bAndy Hung 6529d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // do we need an input converter buffer provider to give us float? 6530d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung delete mInputConverterProvider; 6531d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mInputConverterProvider = NULL; 6532d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mRequiresFloat && mSrcFormat != AUDIO_FORMAT_PCM_FLOAT) { 6533d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mInputConverterProvider = new ReformatBufferProvider( 6534d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung audio_channel_count_from_in_mask(mSrcChannelMask), 6535d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mSrcFormat, 6536d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung AUDIO_FORMAT_PCM_FLOAT, 6537d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung 256 /* provider buffer frame count */); 6538d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } 6539d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung 6540d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // do we need a remixer to do channel mask conversion 6541d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (!mIsLegacyDownmix && !mIsLegacyUpmix && mSrcChannelMask != mDstChannelMask) { 6542d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung (void) memcpy_by_index_array_initialization_from_channel_mask( 6543d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung mIdxAry, ARRAY_SIZE(mIdxAry), mDstChannelMask, mSrcChannelMask); 654497a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 654597a893eb34f8687485c88eaf15917974a203f20bAndy Hung return NO_ERROR; 654697a893eb34f8687485c88eaf15917974a203f20bAndy Hung} 654797a893eb34f8687485c88eaf15917974a203f20bAndy Hung 6548d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hungvoid AudioFlinger::RecordThread::RecordBufferConverter::convertNoResampler( 6549d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung void *dst, const void *src, size_t frames) 655097a893eb34f8687485c88eaf15917974a203f20bAndy Hung{ 6551d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // src is native type unless there is legacy upmix or downmix, whereupon it is float. 655297a893eb34f8687485c88eaf15917974a203f20bAndy Hung if (mBufFrameSize != 0 && mBufFrames < frames) { 655397a893eb34f8687485c88eaf15917974a203f20bAndy Hung free(mBuf); 655497a893eb34f8687485c88eaf15917974a203f20bAndy Hung mBufFrames = frames; 655597a893eb34f8687485c88eaf15917974a203f20bAndy Hung (void)posix_memalign(&mBuf, 32, mBufFrames * mBufFrameSize); 655697a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 6557d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // do we need to do legacy upmix and downmix? 6558d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mIsLegacyUpmix || mIsLegacyDownmix) { 655997a893eb34f8687485c88eaf15917974a203f20bAndy Hung void *dstBuf = mBuf != NULL ? mBuf : dst; 6560d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mIsLegacyUpmix) { 6561d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung upmix_to_stereo_float_from_mono_float((float *)dstBuf, 6562d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung (const float *)src, frames); 6563d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } else /*mIsLegacyDownmix */ { 6564d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung downmix_to_mono_float_from_stereo_float((float *)dstBuf, 6565d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung (const float *)src, frames); 656697a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 6567d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mBuf != NULL) { 6568d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung memcpy_by_audio_format(dst, mDstFormat, mBuf, AUDIO_FORMAT_PCM_FLOAT, 6569d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung frames * mDstChannelCount); 6570d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } 6571d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung return; 6572d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } 6573d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // do we need to do channel mask conversion? 6574d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mSrcChannelMask != mDstChannelMask) { 657597a893eb34f8687485c88eaf15917974a203f20bAndy Hung void *dstBuf = mBuf != NULL ? mBuf : dst; 6576d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung memcpy_by_index_array(dstBuf, mDstChannelCount, 6577d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung src, mSrcChannelCount, mIdxAry, audio_bytes_per_sample(mSrcFormat), frames); 6578d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (dstBuf == dst) { 6579d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung return; // format is the same 6580d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } 6581d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } 6582d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // convert to destination buffer 6583d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung const void *convertBuf = mBuf != NULL ? mBuf : src; 6584d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung memcpy_by_audio_format(dst, mDstFormat, convertBuf, mSrcFormat, 6585d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung frames * mDstChannelCount); 6586d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung} 6587d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung 6588d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hungvoid AudioFlinger::RecordThread::RecordBufferConverter::convertResampler( 6589d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung void *dst, /*not-a-const*/ void *src, size_t frames) 6590d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung{ 6591d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // src buffer format is ALWAYS float when entering this routine 6592d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mIsLegacyUpmix) { 6593d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung ; // mono to stereo already handled by resampler 6594d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } else if (mIsLegacyDownmix 6595d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung || (mSrcChannelMask == mDstChannelMask && mSrcChannelCount == 1)) { 6596d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // the resampler outputs stereo for mono input channel (a feature?) 6597d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // must convert to mono 6598d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung downmix_to_mono_float_from_stereo_float((float *)src, 6599d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung (const float *)src, frames); 6600d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } else if (mSrcChannelMask != mDstChannelMask) { 6601d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // convert to mono channel again for channel mask conversion (could be skipped 6602d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // with further optimization). 660397a893eb34f8687485c88eaf15917974a203f20bAndy Hung if (mSrcChannelCount == 1) { 6604d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung downmix_to_mono_float_from_stereo_float((float *)src, 6605d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung (const float *)src, frames); 660697a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 6607d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // convert to destination format (in place, OK as float is larger than other types) 6608d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mDstFormat != AUDIO_FORMAT_PCM_FLOAT) { 6609d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung memcpy_by_audio_format(src, mDstFormat, src, AUDIO_FORMAT_PCM_FLOAT, 6610d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung frames * mSrcChannelCount); 6611d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } 6612d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // channel convert and save to dst 6613d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung memcpy_by_index_array(dst, mDstChannelCount, 6614d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung src, mSrcChannelCount, mIdxAry, audio_bytes_per_sample(mDstFormat), frames); 6615d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung return; 661697a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 6617d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // convert to destination format and save to dst 6618d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung memcpy_by_audio_format(dst, mDstFormat, src, AUDIO_FORMAT_PCM_FLOAT, 6619d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung frames * mDstChannelCount); 662097a893eb34f8687485c88eaf15917974a203f20bAndy Hung} 662197a893eb34f8687485c88eaf15917974a203f20bAndy Hung 66221035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentbool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair, 66231035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status_t& status) 662481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 662581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool reconfig = false; 662681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 66271035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = NO_ERROR; 66281035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 66291035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent audio_format_t reqFormat = mFormat; 66301035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent uint32_t samplingRate = mSampleRate; 66311035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount); 6632d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // possible that we are > 2 channels, use channel index mask 6633d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (channelMask == AUDIO_CHANNEL_INVALID && mChannelCount <= FCC_8) { 6634d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung audio_channel_mask_for_index_assignment_from_count(mChannelCount); 6635d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } 66361035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 66371035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent AudioParameter param = AudioParameter(keyValuePair); 66381035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent int value; 66391035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // TODO Investigate when this code runs. Check with audio policy when a sample rate and 66401035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // channel count change can be requested. Do we mandate the first client defines the 66411035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // HAL sampling rate and channel count or do we allow changes on the fly? 66421035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { 66431035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent samplingRate = value; 66441035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 66451035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 66461035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { 664797a893eb34f8687485c88eaf15917974a203f20bAndy Hung if (!audio_is_linear_pcm((audio_format_t) value)) { 66481035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = BAD_VALUE; 66491035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 66501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reqFormat = (audio_format_t) value; 665181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent reconfig = true; 665281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 66531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 66541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { 66551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent audio_channel_mask_t mask = (audio_channel_mask_t) value; 6656d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (!audio_is_input_channel(mask) || 6657d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung audio_channel_count_from_in_mask(mask) > FCC_8) { 66581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = BAD_VALUE; 66591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 66601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent channelMask = mask; 66611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 666281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 66631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 66641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { 66651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // do not accept frame count changes if tracks are open as the track buffer 66661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // size depends on frame count and correct behavior would not be guaranteed 66671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // if frame count is changed after track creation 66681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (mActiveTracks.size() > 0) { 66691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = INVALID_OPERATION; 66701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 66711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent reconfig = true; 667281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 66731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 66741035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { 66751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // forward device change to effects that have requested to be 66761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // aware of attached audio device. 66771035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 66781035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mEffectChains[i]->setDevice_l(value); 667981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 668081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 66811035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // store input device and output device but do not forward output device to audio HAL. 66821035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // Note that status is ignored by the caller for output device 66831035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // (see AudioFlinger::setParameters() 66841035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (audio_is_output_devices(value)) { 66851035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mOutDevice = value; 66861035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = BAD_VALUE; 66871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } else { 66881035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mInDevice = value; 66891035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // disable AEC and NS if the device is a BT SCO headset supporting those 66901035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // pre processings 66911035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (mTracks.size() > 0) { 66921035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent bool suspend = audio_is_bluetooth_sco_device(mInDevice) && 66931035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mAudioFlinger->btNrecIsOff(); 66941035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 66951035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sp<RecordTrack> track = mTracks[i]; 66961035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); 66971035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); 669881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 669981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 670081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 67011035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 67021035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && 67031035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mAudioSource != (audio_source_t)value) { 67041035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // forward device change to effects that have requested to be 67051035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // aware of attached audio device. 67061035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 67071035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mEffectChains[i]->setAudioSource_l((audio_source_t)value); 670881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 67091035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mAudioSource = (audio_source_t)value; 67101035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 6711e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten 67121035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == NO_ERROR) { 67131035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = mInput->stream->common.set_parameters(&mInput->stream->common, 67141035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent keyValuePair.string()); 67151035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == INVALID_OPERATION) { 67161035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent inputStandBy(); 671781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = mInput->stream->common.set_parameters(&mInput->stream->common, 671881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent keyValuePair.string()); 67191035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 67201035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (reconfig) { 67211035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == BAD_VALUE && 672297a893eb34f8687485c88eaf15917974a203f20bAndy Hung audio_is_linear_pcm(mInput->stream->common.get_format(&mInput->stream->common)) && 672397a893eb34f8687485c88eaf15917974a203f20bAndy Hung audio_is_linear_pcm(reqFormat) && 67241035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent (mInput->stream->common.get_sample_rate(&mInput->stream->common) 672597a893eb34f8687485c88eaf15917974a203f20bAndy Hung <= (AUDIO_RESAMPLER_DOWN_RATIO_MAX * samplingRate)) && 6726e541269be94f3a1072932d51537905b120ef4733Andy Hung audio_channel_count_from_in_mask( 6727d1abb8f94d7ed749ee959655db1e07d26dad074dAndy Hung mInput->stream->common.get_channels(&mInput->stream->common)) <= FCC_8) { 67281035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status = NO_ERROR; 672981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 67301035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent if (status == NO_ERROR) { 67311035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent readInputParameters_l(); 673273e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); 673381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 673481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 673581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 67361035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 673781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return reconfig; 673881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 673981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 674081784c37c61b09289654b979567a42bf73cd2b12Eric LaurentString8 AudioFlinger::RecordThread::getParameters(const String8& keys) 674181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 674281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 674381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (initCheck() != NO_ERROR) { 6744d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten return String8(); 674581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 674681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 6747d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten char *s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); 6748d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten const String8 out_s8(s); 674981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent free(s); 675081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return out_s8; 675181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 675281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 675373e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurentvoid AudioFlinger::RecordThread::ioConfigChanged(audio_io_config_event event) { 675473e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); 675573e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent 675673e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent desc->mIoHandle = mId; 675781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 675881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent switch (event) { 675973e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent case AUDIO_INPUT_OPENED: 676073e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent case AUDIO_INPUT_CONFIG_CHANGED: 6761296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent desc->mPatch = mPatch; 676273e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent desc->mChannelMask = mChannelMask; 676373e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent desc->mSamplingRate = mSampleRate; 676473e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent desc->mFormat = mFormat; 676573e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent desc->mFrameCount = mFrameCount; 676673e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent desc->mLatency = 0; 676781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 676881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 676973e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent case AUDIO_INPUT_CLOSED: 677081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent default: 677181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 677281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 677373e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent mAudioFlinger->ioConfigChanged(event, desc); 677481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 677581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 6776deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kastenvoid AudioFlinger::RecordThread::readInputParameters_l() 677781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 677881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); 677981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); 6780e541269be94f3a1072932d51537905b120ef4733Andy Hung mChannelCount = audio_channel_count_from_in_mask(mChannelMask); 6781d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (mChannelCount > FCC_8) { 6782d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung ALOGE("HAL channel count %d > %d", mChannelCount, FCC_8); 6783d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung } 6784463be250de73907965faa6a216c00312bf81e049Andy Hung mHALFormat = mInput->stream->common.get_format(&mInput->stream->common); 6785463be250de73907965faa6a216c00312bf81e049Andy Hung mFormat = mHALFormat; 6786d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung if (!audio_is_linear_pcm(mFormat)) { 6787d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung ALOGE("HAL format %#x is not linear pcm", mFormat); 6788291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten } 6789665470b36f202bcc8ee2f7417f68fd2608dd07c1Eric Laurent mFrameSize = audio_stream_in_frame_size(mInput->stream); 6790548efc94813c1dec6e8cf6c085ae41ccb04827f1Glenn Kasten mBufferSize = mInput->stream->common.get_buffer_size(&mInput->stream->common); 6791548efc94813c1dec6e8cf6c085ae41ccb04827f1Glenn Kasten mFrameCount = mBufferSize / mFrameSize; 67926dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // This is the formula for calculating the temporary buffer size. 6793e842614837e5401adf77e90485300c288b9a7876Glenn Kasten // With 7 HAL buffers, we can guarantee ability to down-sample the input by ratio of 6:1 to 67948594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten // 1 full output buffer, regardless of the alignment of the available input. 6795e842614837e5401adf77e90485300c288b9a7876Glenn Kasten // The value is somewhat arbitrary, and could probably be even larger. 67966dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // A larger value should allow more old data to be read after a track calls start(), 67976dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // without increasing latency. 679897a893eb34f8687485c88eaf15917974a203f20bAndy Hung // 679997a893eb34f8687485c88eaf15917974a203f20bAndy Hung // Note this is independent of the maximum downsampling ratio permitted for capture. 6800e842614837e5401adf77e90485300c288b9a7876Glenn Kasten mRsmpInFrames = mFrameCount * 7; 68018594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten mRsmpInFramesP2 = roundup(mRsmpInFrames); 68025744661e85981f8a9456bf470e2761235fc026daAndy Hung free(mRsmpInBuffer); 680349d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten 680449d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // TODO optimize audio capture buffer sizes ... 680549d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // Here we calculate the size of the sliding buffer used as a source 680649d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // for resampling. mRsmpInFramesP2 is currently roundup(mFrameCount * 7). 680749d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // For current HAL frame counts, this is usually 2048 = 40 ms. It would 680849d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // be better to have it derived from the pipe depth in the long term. 680949d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten // The current value is higher than necessary. However it should not add to latency. 681049d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten 68118594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten // Over-allocate beyond mRsmpInFramesP2 to permit a HAL read past end of buffer 68125744661e85981f8a9456bf470e2761235fc026daAndy Hung (void)posix_memalign(&mRsmpInBuffer, 32, (mRsmpInFramesP2 + mFrameCount - 1) * mFrameSize); 68136dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 68144cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints. 68154cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten // But if thread's mSampleRate or mChannelCount changes, how will that affect active tracks? 681681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 681781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 68185f972c031d4061f4f037c9fda1ea4bd9b6a756cdGlenn Kastenuint32_t AudioFlinger::RecordThread::getInputFramesLost() 681981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 682081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 682181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (initCheck() != NO_ERROR) { 682281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 682381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 682481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 682581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mInput->stream->get_input_frames_lost(mInput->stream); 682681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 682781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 682881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) const 682981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 683081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 683181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t result = 0; 683281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (getEffectChain_l(sessionId) != 0) { 683381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result = EFFECT_SESSION; 683481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 683581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 683681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mTracks.size(); ++i) { 683781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId == mTracks[i]->sessionId()) { 683881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent result |= TRACK_SESSION; 683981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 684081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 684181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 684281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 684381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return result; 684481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 684581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 684681784c37c61b09289654b979567a42bf73cd2b12Eric LaurentKeyedVector<int, bool> AudioFlinger::RecordThread::sessionIds() const 684781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 684881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent KeyedVector<int, bool> ids; 684981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 685081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t j = 0; j < mTracks.size(); ++j) { 685181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<RecordThread::RecordTrack> track = mTracks[j]; 685281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId = track->sessionId(); 685381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (ids.indexOfKey(sessionId) < 0) { 685481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ids.add(sessionId, true); 685581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 685681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 685781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return ids; 685881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 685981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 686081784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() 686181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 686281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 686381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamIn *input = mInput; 686481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mInput = NULL; 686581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return input; 686681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 686781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 686881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// this method must always be called either with ThreadBase mLock held or inside the thread loop 686981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_stream_t* AudioFlinger::RecordThread::stream() const 687081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 687181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mInput == NULL) { 687281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NULL; 687381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 687481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return &mInput->stream->common; 687581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 687681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 687781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) 687881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 687981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // only one chain per input thread 688081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mEffectChains.size() != 0) { 6881aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent ALOGW("addEffectChain_l() already one chain %p on thread %p", chain.get(), this); 688281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return INVALID_OPERATION; 688381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 688481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); 6885aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent chain->setThread(this); 688681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setInBuffer(NULL); 688781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain->setOutBuffer(NULL); 688881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 688981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent checkSuspendOnAddEffectChain_l(chain); 689081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 68911b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent // make sure enabled pre processing effects state is communicated to the HAL as we 68921b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent // just moved them to a new input stream. 68931b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent chain->syncHalEffectsState(); 68941b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent 689581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains.add(chain); 689681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 689781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 689881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 689981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 690081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsize_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) 690181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 690281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); 690381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW_IF(mEffectChains.size() != 1, 690481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "removeEffectChain_l() %p invalid chain size %d on thread %p", 690581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chain.get(), mEffectChains.size(), this); 690681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mEffectChains.size() == 1) { 690781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mEffectChains.removeAt(0); 690881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 690981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 691081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 691181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 69121c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch, 69131c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_patch_handle_t *handle) 69141c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{ 69151c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status_t status = NO_ERROR; 6916054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 6917054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent // store new device and send to effects 6918054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent mInDevice = patch->sources[0].ext.device.type; 6919296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent mPatch = *patch; 6920054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 6921054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent mEffectChains[i]->setDevice_l(mInDevice); 6922054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 6923054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 6924054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent // disable AEC and NS if the device is a BT SCO headset supporting those 6925054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent // pre processings 6926054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (mTracks.size() > 0) { 6927054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent bool suspend = audio_is_bluetooth_sco_device(mInDevice) && 6928054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent mAudioFlinger->btNrecIsOff(); 6929054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent for (size_t i = 0; i < mTracks.size(); i++) { 6930054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent sp<RecordTrack> track = mTracks[i]; 6931054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); 6932054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); 69331c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 6934054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 69351c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 6936054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent // store new source and send to effects 6937054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { 6938054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent mAudioSource = patch->sinks[0].ext.mix.usecase.source; 6939054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent for (size_t i = 0; i < mEffectChains.size(); i++) { 6940054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent mEffectChains[i]->setAudioSource_l(mAudioSource); 69411c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 6942054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 69431c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 6944054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { 69451c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); 69461c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status = hwDevice->create_audio_patch(hwDevice, 69471c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent patch->num_sources, 69481c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent patch->sources, 69491c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent patch->num_sinks, 69501c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent patch->sinks, 69511c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent handle); 69521c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } else { 6953054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent char *address; 6954054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent if (strcmp(patch->sources[0].ext.device.address, "") != 0) { 6955054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent address = audio_device_address_to_parameter( 6956054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent patch->sources[0].ext.device.type, 6957054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent patch->sources[0].ext.device.address); 6958054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } else { 6959054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent address = (char *)calloc(1, 1); 6960054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent } 6961054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent AudioParameter param = AudioParameter(String8(address)); 6962054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent free(address); 6963054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 6964054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent (int)patch->sources[0].ext.device.type); 6965054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent param.addInt(String8(AUDIO_PARAMETER_STREAM_INPUT_SOURCE), 6966054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent (int)patch->sinks[0].ext.mix.usecase.source); 6967054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent status = mInput->stream->common.set_parameters(&mInput->stream->common, 6968054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent param.toString().string()); 6969054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent *handle = AUDIO_PATCH_HANDLE_NONE; 69701c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 6971054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 6972296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); 6973296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent 69741c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent return status; 69751c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent} 69761c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 69771c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle) 69781c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{ 69791c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status_t status = NO_ERROR; 6980054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 6981054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent mInDevice = AUDIO_DEVICE_NONE; 6982054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 69831c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { 69841c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); 69851c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status = hwDevice->release_audio_patch(hwDevice, handle); 69861c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } else { 6987054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent AudioParameter param; 6988054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0); 6989054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent status = mInput->stream->common.set_parameters(&mInput->stream->common, 6990054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent param.toString().string()); 69911c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 69921c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent return status; 69931c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent} 69941c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 699583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::RecordThread::addPatchRecord(const sp<PatchRecord>& record) 699683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{ 699783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent Mutex::Autolock _l(mLock); 699883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent mTracks.add(record); 699983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent} 700083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 700183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::RecordThread::deletePatchRecord(const sp<PatchRecord>& record) 700283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{ 700383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent Mutex::Autolock _l(mLock); 700483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent destroyTrack_l(record); 700583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent} 700683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 700783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::RecordThread::getAudioPortConfig(struct audio_port_config *config) 700883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{ 700983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent ThreadBase::getAudioPortConfig(config); 701083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->role = AUDIO_PORT_ROLE_SINK; 701183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->ext.mix.hw_module = mInput->audioHwDev->handle(); 701283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent config->ext.mix.usecase.source = mAudioSource; 701383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent} 70141c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 701563238efb0d674758902918e3cdaac322126484b7Glenn Kasten} // namespace android 7016