Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 1 | /* |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2 | ** |
| 3 | ** Copyright 2007, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | #define LOG_TAG "AudioFlinger" |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | |
| 22 | #include <math.h> |
| 23 | #include <signal.h> |
| 24 | #include <sys/time.h> |
| 25 | #include <sys/resource.h> |
| 26 | |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 27 | #include <binder/IPCThreadState.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 28 | #include <binder/IServiceManager.h> |
| 29 | #include <utils/Log.h> |
Glenn Kasten | d8e6fd3 | 2012-05-07 11:07:57 -0700 | [diff] [blame] | 30 | #include <utils/Trace.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 31 | #include <binder/Parcel.h> |
| 32 | #include <binder/IPCThreadState.h> |
| 33 | #include <utils/String16.h> |
| 34 | #include <utils/threads.h> |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 35 | #include <utils/Atomic.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 36 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 37 | #include <cutils/bitops.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 38 | #include <cutils/properties.h> |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 39 | #include <cutils/compiler.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 40 | |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 41 | #undef ADD_BATTERY_DATA |
| 42 | |
| 43 | #ifdef ADD_BATTERY_DATA |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 44 | #include <media/IMediaPlayerService.h> |
Glenn Kasten | 25b248e | 2012-01-03 15:28:29 -0800 | [diff] [blame] | 45 | #include <media/IMediaDeathNotifier.h> |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 46 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 47 | |
| 48 | #include <private/media/AudioTrackShared.h> |
| 49 | #include <private/media/AudioEffectShared.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 50 | |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 51 | #include <system/audio.h> |
Dima Zavin | 7394a4f | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 52 | #include <hardware/audio.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 53 | |
| 54 | #include "AudioMixer.h" |
| 55 | #include "AudioFlinger.h" |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 56 | #include "ServiceUtilities.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 57 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 58 | #include <media/EffectsFactoryApi.h> |
Eric Laurent | 6d8b694 | 2011-06-24 07:01:31 -0700 | [diff] [blame] | 59 | #include <audio_effects/effect_visualizer.h> |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 60 | #include <audio_effects/effect_ns.h> |
| 61 | #include <audio_effects/effect_aec.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 62 | |
Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 63 | #include <audio_utils/primitives.h> |
| 64 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 65 | #include <powermanager/PowerManager.h> |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 66 | |
Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 67 | // #define DEBUG_CPU_USAGE 10 // log statistics every n wall clock seconds |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 68 | #ifdef DEBUG_CPU_USAGE |
| 69 | #include <cpustats/CentralTendencyStatistics.h> |
| 70 | #include <cpustats/ThreadCpuUsage.h> |
| 71 | #endif |
Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 72 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 73 | #include <common_time/cc_helper.h> |
| 74 | #include <common_time/local_clock.h> |
| 75 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 76 | #include "FastMixer.h" |
| 77 | |
| 78 | // NBAIO implementations |
| 79 | #include "AudioStreamOutSink.h" |
| 80 | #include "MonoPipe.h" |
| 81 | #include "MonoPipeReader.h" |
Glenn Kasten | fbae5da | 2012-05-21 09:17:20 -0700 | [diff] [blame] | 82 | #include "Pipe.h" |
| 83 | #include "PipeReader.h" |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 84 | #include "SourceAudioBufferProvider.h" |
| 85 | |
Glenn Kasten | 1dc28b7 | 2012-04-24 10:01:03 -0700 | [diff] [blame] | 86 | #ifdef HAVE_REQUEST_PRIORITY |
| 87 | #include "SchedulingPolicyService.h" |
| 88 | #endif |
| 89 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 90 | #ifdef SOAKER |
| 91 | #include "Soaker.h" |
| 92 | #endif |
| 93 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 94 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 95 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 96 | // Note: the following macro is used for extremely verbose logging message. In |
| 97 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 98 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 99 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 100 | // turned on. Do not uncomment the #def below unless you really know what you |
| 101 | // are doing and want to see all of the extremely verbose messages. |
| 102 | //#define VERY_VERY_VERBOSE_LOGGING |
| 103 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 104 | #define ALOGVV ALOGV |
| 105 | #else |
| 106 | #define ALOGVV(a...) do { } while(0) |
| 107 | #endif |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 108 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 109 | namespace android { |
| 110 | |
Glenn Kasten | ec1d6b5 | 2011-12-12 09:04:45 -0800 | [diff] [blame] | 111 | static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n"; |
| 112 | static const char kHardwareLockedString[] = "Hardware lock is taken\n"; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 113 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 114 | static const float MAX_GAIN = 4096.0f; |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 115 | static const uint32_t MAX_GAIN_INT = 0x1000; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 116 | |
| 117 | // retry counts for buffer fill timeout |
| 118 | // 50 * ~20msecs = 1 second |
| 119 | static const int8_t kMaxTrackRetries = 50; |
| 120 | static const int8_t kMaxTrackStartupRetries = 50; |
| 121 | // allow less retry attempts on direct output thread. |
| 122 | // direct outputs can be a scarce resource in audio hardware and should |
| 123 | // be released as quickly as possible. |
| 124 | static const int8_t kMaxTrackRetriesDirect = 2; |
| 125 | |
| 126 | static const int kDumpLockRetries = 50; |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 127 | static const int kDumpLockSleepUs = 20000; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 128 | |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 129 | // don't warn about blocked writes or record buffer overflows more often than this |
| 130 | static const nsecs_t kWarningThrottleNs = seconds(5); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 131 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 132 | // RecordThread loop sleep time upon application overrun or audio HAL read error |
| 133 | static const int kRecordThreadSleepUs = 5000; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 134 | |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 135 | // maximum time to wait for setParameters to complete |
| 136 | static const nsecs_t kSetParametersTimeoutNs = seconds(2); |
Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 137 | |
Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 138 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun |
| 139 | static const uint32_t kMinThreadSleepTimeUs = 5000; |
| 140 | // maximum divider applied to the active sleep time in the mixer thread loop |
| 141 | static const uint32_t kMaxThreadSleepTimeShift = 2; |
| 142 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 143 | // minimum normal mix buffer size, expressed in milliseconds rather than frames |
| 144 | static const uint32_t kMinNormalMixBufferSizeMs = 20; |
Glenn Kasten | 4adcede | 2012-05-14 12:26:02 -0700 | [diff] [blame] | 145 | // maximum normal mix buffer size |
| 146 | static const uint32_t kMaxNormalMixBufferSizeMs = 24; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 147 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 148 | nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs; |
Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 149 | |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 150 | // Whether to use fast mixer |
| 151 | static const enum { |
| 152 | FastMixer_Never, // never initialize or use: for debugging only |
| 153 | FastMixer_Always, // always initialize and use, even if not needed: for debugging only |
| 154 | // normal mixer multiplier is 1 |
| 155 | FastMixer_Static, // initialize if needed, then use all the time if initialized, |
Glenn Kasten | 4adcede | 2012-05-14 12:26:02 -0700 | [diff] [blame] | 156 | // multiplier is calculated based on min & max normal mixer buffer size |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 157 | FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load, |
Glenn Kasten | 4adcede | 2012-05-14 12:26:02 -0700 | [diff] [blame] | 158 | // multiplier is calculated based on min & max normal mixer buffer size |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 159 | // FIXME for FastMixer_Dynamic: |
| 160 | // Supporting this option will require fixing HALs that can't handle large writes. |
| 161 | // For example, one HAL implementation returns an error from a large write, |
| 162 | // and another HAL implementation corrupts memory, possibly in the sample rate converter. |
| 163 | // We could either fix the HAL implementations, or provide a wrapper that breaks |
| 164 | // up large writes into smaller ones, and the wrapper would need to deal with scheduler. |
| 165 | } kUseFastMixer = FastMixer_Static; |
| 166 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 167 | // ---------------------------------------------------------------------------- |
| 168 | |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 169 | #ifdef ADD_BATTERY_DATA |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 170 | // To collect the amplifier usage |
| 171 | static void addBatteryData(uint32_t params) { |
Glenn Kasten | 25b248e | 2012-01-03 15:28:29 -0800 | [diff] [blame] | 172 | sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); |
| 173 | if (service == NULL) { |
| 174 | // it already logged |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 175 | return; |
| 176 | } |
| 177 | |
| 178 | service->addBatteryData(params); |
| 179 | } |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 180 | #endif |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 181 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 182 | static int load_audio_interface(const char *if_name, audio_hw_device_t **dev) |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 183 | { |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 184 | const hw_module_t *mod; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 185 | int rc; |
| 186 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 187 | rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod); |
| 188 | ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__, |
| 189 | AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); |
| 190 | if (rc) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 191 | goto out; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 192 | } |
| 193 | rc = audio_hw_device_open(mod, dev); |
| 194 | ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__, |
| 195 | AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); |
| 196 | if (rc) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 197 | goto out; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 198 | } |
| 199 | if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) { |
| 200 | ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version); |
| 201 | rc = BAD_VALUE; |
| 202 | goto out; |
| 203 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 204 | return 0; |
| 205 | |
| 206 | out: |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 207 | *dev = NULL; |
| 208 | return rc; |
| 209 | } |
| 210 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 211 | // ---------------------------------------------------------------------------- |
| 212 | |
| 213 | AudioFlinger::AudioFlinger() |
| 214 | : BnAudioFlinger(), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 215 | mPrimaryHardwareDev(NULL), |
| 216 | mHardwareStatus(AUDIO_HW_IDLE), // see also onFirstRef() |
| 217 | mMasterVolume(1.0f), |
| 218 | mMasterVolumeSupportLvl(MVS_NONE), |
| 219 | mMasterMute(false), |
| 220 | mNextUniqueId(1), |
| 221 | mMode(AUDIO_MODE_INVALID), |
| 222 | mBtNrecIsOff(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 223 | { |
Dima Zavin | 5a61d2f | 2011-04-19 19:04:32 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void AudioFlinger::onFirstRef() |
| 227 | { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 228 | int rc = 0; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 229 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 230 | Mutex::Autolock _l(mLock); |
| 231 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 232 | /* TODO: move all this work into an Init() function */ |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 233 | char val_str[PROPERTY_VALUE_MAX] = { 0 }; |
| 234 | if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) { |
| 235 | uint32_t int_val; |
| 236 | if (1 == sscanf(val_str, "%u", &int_val)) { |
| 237 | mStandbyTimeInNsecs = milliseconds(int_val); |
| 238 | ALOGI("Using %u mSec as standby time.", int_val); |
| 239 | } else { |
| 240 | mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs; |
| 241 | ALOGI("Using default %u mSec as standby time.", |
| 242 | (uint32_t)(mStandbyTimeInNsecs / 1000000)); |
| 243 | } |
| 244 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 245 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 246 | mMode = AUDIO_MODE_NORMAL; |
| 247 | mMasterVolumeSW = 1.0; |
| 248 | mMasterVolume = 1.0; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 249 | mHardwareStatus = AUDIO_HW_IDLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | AudioFlinger::~AudioFlinger() |
| 253 | { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 254 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 255 | while (!mRecordThreads.isEmpty()) { |
| 256 | // closeInput() will remove first entry from mRecordThreads |
| 257 | closeInput(mRecordThreads.keyAt(0)); |
| 258 | } |
| 259 | while (!mPlaybackThreads.isEmpty()) { |
| 260 | // closeOutput() will remove first entry from mPlaybackThreads |
| 261 | closeOutput(mPlaybackThreads.keyAt(0)); |
| 262 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 263 | |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 264 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 265 | // no mHardwareLock needed, as there are no other references to this |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 266 | audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice()); |
| 267 | delete mAudioHwDevs.valueAt(i); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 271 | static const char * const audio_interfaces[] = { |
| 272 | AUDIO_HARDWARE_MODULE_ID_PRIMARY, |
| 273 | AUDIO_HARDWARE_MODULE_ID_A2DP, |
| 274 | AUDIO_HARDWARE_MODULE_ID_USB, |
| 275 | }; |
| 276 | #define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0]))) |
| 277 | |
| 278 | audio_hw_device_t* AudioFlinger::findSuitableHwDev_l(audio_module_handle_t module, uint32_t devices) |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 279 | { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 280 | // if module is 0, the request comes from an old policy manager and we should load |
| 281 | // well known modules |
| 282 | if (module == 0) { |
| 283 | ALOGW("findSuitableHwDev_l() loading well know audio hw modules"); |
| 284 | for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) { |
| 285 | loadHwModule_l(audio_interfaces[i]); |
| 286 | } |
| 287 | } else { |
| 288 | // check a match for the requested module handle |
| 289 | AudioHwDevice *audioHwdevice = mAudioHwDevs.valueFor(module); |
| 290 | if (audioHwdevice != NULL) { |
| 291 | return audioHwdevice->hwDevice(); |
| 292 | } |
| 293 | } |
| 294 | // then try to find a module supporting the requested device. |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 295 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 296 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 297 | if ((dev->get_supported_devices(dev) & devices) == devices) |
| 298 | return dev; |
| 299 | } |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 300 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 301 | return NULL; |
| 302 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 303 | |
| 304 | status_t AudioFlinger::dumpClients(int fd, const Vector<String16>& args) |
| 305 | { |
| 306 | const size_t SIZE = 256; |
| 307 | char buffer[SIZE]; |
| 308 | String8 result; |
| 309 | |
| 310 | result.append("Clients:\n"); |
| 311 | for (size_t i = 0; i < mClients.size(); ++i) { |
Glenn Kasten | 77c1119 | 2012-01-25 14:27:41 -0800 | [diff] [blame] | 312 | sp<Client> client = mClients.valueAt(i).promote(); |
| 313 | if (client != 0) { |
| 314 | snprintf(buffer, SIZE, " pid: %d\n", client->pid()); |
| 315 | result.append(buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 316 | } |
| 317 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 318 | |
| 319 | result.append("Global session refs:\n"); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 320 | result.append(" session pid count\n"); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 321 | for (size_t i = 0; i < mAudioSessionRefs.size(); i++) { |
| 322 | AudioSessionRef *r = mAudioSessionRefs[i]; |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 323 | snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 324 | result.append(buffer); |
| 325 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 326 | write(fd, result.string(), result.size()); |
| 327 | return NO_ERROR; |
| 328 | } |
| 329 | |
| 330 | |
| 331 | status_t AudioFlinger::dumpInternals(int fd, const Vector<String16>& args) |
| 332 | { |
| 333 | const size_t SIZE = 256; |
| 334 | char buffer[SIZE]; |
| 335 | String8 result; |
Glenn Kasten | a4454b4 | 2012-01-04 11:02:33 -0800 | [diff] [blame] | 336 | hardware_call_state hardwareStatus = mHardwareStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 337 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 338 | snprintf(buffer, SIZE, "Hardware status: %d\n" |
| 339 | "Standby Time mSec: %u\n", |
| 340 | hardwareStatus, |
| 341 | (uint32_t)(mStandbyTimeInNsecs / 1000000)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 342 | result.append(buffer); |
| 343 | write(fd, result.string(), result.size()); |
| 344 | return NO_ERROR; |
| 345 | } |
| 346 | |
| 347 | status_t AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args) |
| 348 | { |
| 349 | const size_t SIZE = 256; |
| 350 | char buffer[SIZE]; |
| 351 | String8 result; |
| 352 | snprintf(buffer, SIZE, "Permission Denial: " |
| 353 | "can't dump AudioFlinger from pid=%d, uid=%d\n", |
| 354 | IPCThreadState::self()->getCallingPid(), |
| 355 | IPCThreadState::self()->getCallingUid()); |
| 356 | result.append(buffer); |
| 357 | write(fd, result.string(), result.size()); |
| 358 | return NO_ERROR; |
| 359 | } |
| 360 | |
| 361 | static bool tryLock(Mutex& mutex) |
| 362 | { |
| 363 | bool locked = false; |
| 364 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 365 | if (mutex.tryLock() == NO_ERROR) { |
| 366 | locked = true; |
| 367 | break; |
| 368 | } |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 369 | usleep(kDumpLockSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 370 | } |
| 371 | return locked; |
| 372 | } |
| 373 | |
| 374 | status_t AudioFlinger::dump(int fd, const Vector<String16>& args) |
| 375 | { |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 376 | if (!dumpAllowed()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 377 | dumpPermissionDenial(fd, args); |
| 378 | } else { |
| 379 | // get state of hardware lock |
| 380 | bool hardwareLocked = tryLock(mHardwareLock); |
| 381 | if (!hardwareLocked) { |
| 382 | String8 result(kHardwareLockedString); |
| 383 | write(fd, result.string(), result.size()); |
| 384 | } else { |
| 385 | mHardwareLock.unlock(); |
| 386 | } |
| 387 | |
| 388 | bool locked = tryLock(mLock); |
| 389 | |
| 390 | // failed to lock - AudioFlinger is probably deadlocked |
| 391 | if (!locked) { |
| 392 | String8 result(kDeadlockedString); |
| 393 | write(fd, result.string(), result.size()); |
| 394 | } |
| 395 | |
| 396 | dumpClients(fd, args); |
| 397 | dumpInternals(fd, args); |
| 398 | |
| 399 | // dump playback threads |
| 400 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 401 | mPlaybackThreads.valueAt(i)->dump(fd, args); |
| 402 | } |
| 403 | |
| 404 | // dump record threads |
| 405 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 406 | mRecordThreads.valueAt(i)->dump(fd, args); |
| 407 | } |
| 408 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 409 | // dump all hardware devs |
| 410 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 411 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 412 | dev->dump(dev, fd); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 413 | } |
| 414 | if (locked) mLock.unlock(); |
| 415 | } |
| 416 | return NO_ERROR; |
| 417 | } |
| 418 | |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 419 | sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid) |
| 420 | { |
| 421 | // If pid is already in the mClients wp<> map, then use that entry |
| 422 | // (for which promote() is always != 0), otherwise create a new entry and Client. |
| 423 | sp<Client> client = mClients.valueFor(pid).promote(); |
| 424 | if (client == 0) { |
| 425 | client = new Client(this, pid); |
| 426 | mClients.add(pid, client); |
| 427 | } |
| 428 | |
| 429 | return client; |
| 430 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 431 | |
| 432 | // IAudioFlinger interface |
| 433 | |
| 434 | |
| 435 | sp<IAudioTrack> AudioFlinger::createTrack( |
| 436 | pid_t pid, |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 437 | audio_stream_type_t streamType, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 438 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 439 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 440 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 441 | int frameCount, |
Glenn Kasten | a075db4 | 2012-03-06 11:22:44 -0800 | [diff] [blame] | 442 | IAudioFlinger::track_flags_t flags, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 443 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 444 | audio_io_handle_t output, |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 445 | pid_t tid, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 446 | int *sessionId, |
| 447 | status_t *status) |
| 448 | { |
| 449 | sp<PlaybackThread::Track> track; |
| 450 | sp<TrackHandle> trackHandle; |
| 451 | sp<Client> client; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 452 | status_t lStatus; |
| 453 | int lSessionId; |
| 454 | |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 455 | // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC, |
| 456 | // but if someone uses binder directly they could bypass that and cause us to crash |
| 457 | if (uint32_t(streamType) >= AUDIO_STREAM_CNT) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 458 | ALOGE("createTrack() invalid stream type %d", streamType); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 459 | lStatus = BAD_VALUE; |
| 460 | goto Exit; |
| 461 | } |
| 462 | |
| 463 | { |
| 464 | Mutex::Autolock _l(mLock); |
| 465 | PlaybackThread *thread = checkPlaybackThread_l(output); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 466 | PlaybackThread *effectThread = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 467 | if (thread == NULL) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 468 | ALOGE("unknown output thread"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 469 | lStatus = BAD_VALUE; |
| 470 | goto Exit; |
| 471 | } |
| 472 | |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 473 | client = registerPid_l(pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 474 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 475 | ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 476 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | f436fdc | 2012-05-24 11:07:14 -0700 | [diff] [blame] | 477 | // check if an effect chain with the same session ID is present on another |
| 478 | // output thread and move it here. |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 479 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 480 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 481 | if (mPlaybackThreads.keyAt(i) != output) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 482 | uint32_t sessions = t->hasAudioSession(*sessionId); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 483 | if (sessions & PlaybackThread::EFFECT_SESSION) { |
| 484 | effectThread = t.get(); |
Eric Laurent | f436fdc | 2012-05-24 11:07:14 -0700 | [diff] [blame] | 485 | break; |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 486 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 487 | } |
| 488 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 489 | lSessionId = *sessionId; |
| 490 | } else { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 491 | // if no audio session id is provided, create one here |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 492 | lSessionId = nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 493 | if (sessionId != NULL) { |
| 494 | *sessionId = lSessionId; |
| 495 | } |
| 496 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 497 | ALOGV("createTrack() lSessionId: %d", lSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 498 | |
| 499 | track = thread->createTrack_l(client, streamType, sampleRate, format, |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 500 | channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, &lStatus); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 501 | |
| 502 | // move effect chain to this output thread if an effect on same session was waiting |
| 503 | // for a track to be created |
| 504 | if (lStatus == NO_ERROR && effectThread != NULL) { |
| 505 | Mutex::Autolock _dl(thread->mLock); |
| 506 | Mutex::Autolock _sl(effectThread->mLock); |
| 507 | moveEffectChain_l(lSessionId, effectThread, thread, true); |
| 508 | } |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 509 | |
| 510 | // Look for sync events awaiting for a session to be used. |
| 511 | for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) { |
| 512 | if (mPendingSyncEvents[i]->triggerSession() == lSessionId) { |
| 513 | if (thread->isValidSyncEvent(mPendingSyncEvents[i])) { |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 514 | if (lStatus == NO_ERROR) { |
| 515 | track->setSyncEvent(mPendingSyncEvents[i]); |
| 516 | } else { |
| 517 | mPendingSyncEvents[i]->cancel(); |
| 518 | } |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 519 | mPendingSyncEvents.removeAt(i); |
| 520 | i--; |
| 521 | } |
| 522 | } |
| 523 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 524 | } |
| 525 | if (lStatus == NO_ERROR) { |
| 526 | trackHandle = new TrackHandle(track); |
| 527 | } else { |
| 528 | // remove local strong reference to Client before deleting the Track so that the Client |
| 529 | // destructor is called by the TrackBase destructor with mLock held |
| 530 | client.clear(); |
| 531 | track.clear(); |
| 532 | } |
| 533 | |
| 534 | Exit: |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 535 | if (status != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 536 | *status = lStatus; |
| 537 | } |
| 538 | return trackHandle; |
| 539 | } |
| 540 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 541 | uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 542 | { |
| 543 | Mutex::Autolock _l(mLock); |
| 544 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 545 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 546 | ALOGW("sampleRate() unknown thread %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 547 | return 0; |
| 548 | } |
| 549 | return thread->sampleRate(); |
| 550 | } |
| 551 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 552 | int AudioFlinger::channelCount(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 553 | { |
| 554 | Mutex::Autolock _l(mLock); |
| 555 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 556 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 557 | ALOGW("channelCount() unknown thread %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 558 | return 0; |
| 559 | } |
| 560 | return thread->channelCount(); |
| 561 | } |
| 562 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 563 | audio_format_t AudioFlinger::format(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 564 | { |
| 565 | Mutex::Autolock _l(mLock); |
| 566 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 567 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 568 | ALOGW("format() unknown thread %d", output); |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 569 | return AUDIO_FORMAT_INVALID; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 570 | } |
| 571 | return thread->format(); |
| 572 | } |
| 573 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 574 | size_t AudioFlinger::frameCount(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 575 | { |
| 576 | Mutex::Autolock _l(mLock); |
| 577 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 578 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 579 | ALOGW("frameCount() unknown thread %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 580 | return 0; |
| 581 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 582 | // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers; |
| 583 | // should examine all callers and fix them to handle smaller counts |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 584 | return thread->frameCount(); |
| 585 | } |
| 586 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 587 | uint32_t AudioFlinger::latency(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 588 | { |
| 589 | Mutex::Autolock _l(mLock); |
| 590 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 591 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 592 | ALOGW("latency() unknown thread %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 593 | return 0; |
| 594 | } |
| 595 | return thread->latency(); |
| 596 | } |
| 597 | |
| 598 | status_t AudioFlinger::setMasterVolume(float value) |
| 599 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 600 | status_t ret = initCheck(); |
| 601 | if (ret != NO_ERROR) { |
| 602 | return ret; |
| 603 | } |
| 604 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 605 | // check calling permissions |
| 606 | if (!settingsAllowed()) { |
| 607 | return PERMISSION_DENIED; |
| 608 | } |
| 609 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 610 | float swmv = value; |
| 611 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 612 | Mutex::Autolock _l(mLock); |
| 613 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 614 | // when hw supports master volume, don't scale in sw mixer |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 615 | if (MVS_NONE != mMasterVolumeSupportLvl) { |
| 616 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 617 | AutoMutex lock(mHardwareLock); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 618 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 619 | |
| 620 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
| 621 | if (NULL != dev->set_master_volume) { |
| 622 | dev->set_master_volume(dev, value); |
| 623 | } |
| 624 | mHardwareStatus = AUDIO_HW_IDLE; |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 625 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 626 | |
| 627 | swmv = 1.0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 628 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 629 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 630 | mMasterVolume = value; |
| 631 | mMasterVolumeSW = swmv; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 632 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 633 | mPlaybackThreads.valueAt(i)->setMasterVolume(swmv); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 634 | |
| 635 | return NO_ERROR; |
| 636 | } |
| 637 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 638 | status_t AudioFlinger::setMode(audio_mode_t mode) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 639 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 640 | status_t ret = initCheck(); |
| 641 | if (ret != NO_ERROR) { |
| 642 | return ret; |
| 643 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 644 | |
| 645 | // check calling permissions |
| 646 | if (!settingsAllowed()) { |
| 647 | return PERMISSION_DENIED; |
| 648 | } |
Glenn Kasten | 930f4ca | 2012-01-06 16:47:31 -0800 | [diff] [blame] | 649 | if (uint32_t(mode) >= AUDIO_MODE_CNT) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 650 | ALOGW("Illegal value: setMode(%d)", mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 651 | return BAD_VALUE; |
| 652 | } |
| 653 | |
| 654 | { // scope for the lock |
| 655 | AutoMutex lock(mHardwareLock); |
| 656 | mHardwareStatus = AUDIO_HW_SET_MODE; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 657 | ret = mPrimaryHardwareDev->set_mode(mPrimaryHardwareDev, mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 658 | mHardwareStatus = AUDIO_HW_IDLE; |
| 659 | } |
| 660 | |
| 661 | if (NO_ERROR == ret) { |
| 662 | Mutex::Autolock _l(mLock); |
| 663 | mMode = mode; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 664 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 665 | mPlaybackThreads.valueAt(i)->setMode(mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | return ret; |
| 669 | } |
| 670 | |
| 671 | status_t AudioFlinger::setMicMute(bool state) |
| 672 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 673 | status_t ret = initCheck(); |
| 674 | if (ret != NO_ERROR) { |
| 675 | return ret; |
| 676 | } |
| 677 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 678 | // check calling permissions |
| 679 | if (!settingsAllowed()) { |
| 680 | return PERMISSION_DENIED; |
| 681 | } |
| 682 | |
| 683 | AutoMutex lock(mHardwareLock); |
| 684 | mHardwareStatus = AUDIO_HW_SET_MIC_MUTE; |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 685 | ret = mPrimaryHardwareDev->set_mic_mute(mPrimaryHardwareDev, state); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 686 | mHardwareStatus = AUDIO_HW_IDLE; |
| 687 | return ret; |
| 688 | } |
| 689 | |
| 690 | bool AudioFlinger::getMicMute() const |
| 691 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 692 | status_t ret = initCheck(); |
| 693 | if (ret != NO_ERROR) { |
| 694 | return false; |
| 695 | } |
| 696 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 697 | bool state = AUDIO_MODE_INVALID; |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 698 | AutoMutex lock(mHardwareLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 699 | mHardwareStatus = AUDIO_HW_GET_MIC_MUTE; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 700 | mPrimaryHardwareDev->get_mic_mute(mPrimaryHardwareDev, &state); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 701 | mHardwareStatus = AUDIO_HW_IDLE; |
| 702 | return state; |
| 703 | } |
| 704 | |
| 705 | status_t AudioFlinger::setMasterMute(bool muted) |
| 706 | { |
| 707 | // check calling permissions |
| 708 | if (!settingsAllowed()) { |
| 709 | return PERMISSION_DENIED; |
| 710 | } |
| 711 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 712 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 713 | // This is an optimization, so PlaybackThread doesn't have to look at the one from AudioFlinger |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 714 | mMasterMute = muted; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 715 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 716 | mPlaybackThreads.valueAt(i)->setMasterMute(muted); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 717 | |
| 718 | return NO_ERROR; |
| 719 | } |
| 720 | |
| 721 | float AudioFlinger::masterVolume() const |
| 722 | { |
Glenn Kasten | 9806710 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 723 | Mutex::Autolock _l(mLock); |
| 724 | return masterVolume_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 725 | } |
| 726 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 727 | float AudioFlinger::masterVolumeSW() const |
| 728 | { |
| 729 | Mutex::Autolock _l(mLock); |
| 730 | return masterVolumeSW_l(); |
| 731 | } |
| 732 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 733 | bool AudioFlinger::masterMute() const |
| 734 | { |
Glenn Kasten | 9806710 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 735 | Mutex::Autolock _l(mLock); |
| 736 | return masterMute_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 737 | } |
| 738 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 739 | float AudioFlinger::masterVolume_l() const |
| 740 | { |
| 741 | if (MVS_FULL == mMasterVolumeSupportLvl) { |
| 742 | float ret_val; |
| 743 | AutoMutex lock(mHardwareLock); |
| 744 | |
| 745 | mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME; |
Glenn Kasten | 5798d4e | 2012-03-08 12:18:35 -0800 | [diff] [blame] | 746 | ALOG_ASSERT((NULL != mPrimaryHardwareDev) && |
| 747 | (NULL != mPrimaryHardwareDev->get_master_volume), |
| 748 | "can't get master volume"); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 749 | |
| 750 | mPrimaryHardwareDev->get_master_volume(mPrimaryHardwareDev, &ret_val); |
| 751 | mHardwareStatus = AUDIO_HW_IDLE; |
| 752 | return ret_val; |
| 753 | } |
| 754 | |
| 755 | return mMasterVolume; |
| 756 | } |
| 757 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 758 | status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value, |
| 759 | audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 760 | { |
| 761 | // check calling permissions |
| 762 | if (!settingsAllowed()) { |
| 763 | return PERMISSION_DENIED; |
| 764 | } |
| 765 | |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 766 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 767 | ALOGE("setStreamVolume() invalid stream %d", stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 768 | return BAD_VALUE; |
| 769 | } |
| 770 | |
| 771 | AutoMutex lock(mLock); |
| 772 | PlaybackThread *thread = NULL; |
| 773 | if (output) { |
| 774 | thread = checkPlaybackThread_l(output); |
| 775 | if (thread == NULL) { |
| 776 | return BAD_VALUE; |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | mStreamTypes[stream].volume = value; |
| 781 | |
| 782 | if (thread == NULL) { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 783 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 784 | mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 785 | } |
| 786 | } else { |
| 787 | thread->setStreamVolume(stream, value); |
| 788 | } |
| 789 | |
| 790 | return NO_ERROR; |
| 791 | } |
| 792 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 793 | status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 794 | { |
| 795 | // check calling permissions |
| 796 | if (!settingsAllowed()) { |
| 797 | return PERMISSION_DENIED; |
| 798 | } |
| 799 | |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 800 | if (uint32_t(stream) >= AUDIO_STREAM_CNT || |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 801 | uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 802 | ALOGE("setStreamMute() invalid stream %d", stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 803 | return BAD_VALUE; |
| 804 | } |
| 805 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 806 | AutoMutex lock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 807 | mStreamTypes[stream].mute = muted; |
| 808 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 809 | mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 810 | |
| 811 | return NO_ERROR; |
| 812 | } |
| 813 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 814 | float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 815 | { |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 816 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 817 | return 0.0f; |
| 818 | } |
| 819 | |
| 820 | AutoMutex lock(mLock); |
| 821 | float volume; |
| 822 | if (output) { |
| 823 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 824 | if (thread == NULL) { |
| 825 | return 0.0f; |
| 826 | } |
| 827 | volume = thread->streamVolume(stream); |
| 828 | } else { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 829 | volume = streamVolume_l(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | return volume; |
| 833 | } |
| 834 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 835 | bool AudioFlinger::streamMute(audio_stream_type_t stream) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 836 | { |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 837 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 838 | return true; |
| 839 | } |
| 840 | |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 841 | AutoMutex lock(mLock); |
| 842 | return streamMute_l(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 843 | } |
| 844 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 845 | status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 846 | { |
Glenn Kasten | 23d82a9 | 2012-02-03 11:10:00 -0800 | [diff] [blame] | 847 | ALOGV("setParameters(): io %d, keyvalue %s, tid %d, calling pid %d", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 848 | ioHandle, keyValuePairs.string(), gettid(), IPCThreadState::self()->getCallingPid()); |
| 849 | // check calling permissions |
| 850 | if (!settingsAllowed()) { |
| 851 | return PERMISSION_DENIED; |
| 852 | } |
| 853 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 854 | // ioHandle == 0 means the parameters are global to the audio hardware interface |
| 855 | if (ioHandle == 0) { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 856 | Mutex::Autolock _l(mLock); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 857 | status_t final_result = NO_ERROR; |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 858 | { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 859 | AutoMutex lock(mHardwareLock); |
| 860 | mHardwareStatus = AUDIO_HW_SET_PARAMETER; |
| 861 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 862 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
| 863 | status_t result = dev->set_parameters(dev, keyValuePairs.string()); |
| 864 | final_result = result ?: final_result; |
| 865 | } |
| 866 | mHardwareStatus = AUDIO_HW_IDLE; |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 867 | } |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 868 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 869 | AudioParameter param = AudioParameter(keyValuePairs); |
| 870 | String8 value; |
| 871 | if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) { |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 872 | bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF); |
| 873 | if (mBtNrecIsOff != btNrecIsOff) { |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 874 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 875 | sp<RecordThread> thread = mRecordThreads.valueAt(i); |
| 876 | RecordThread::RecordTrack *track = thread->track(); |
| 877 | if (track != NULL) { |
| 878 | audio_devices_t device = (audio_devices_t)( |
| 879 | thread->device() & AUDIO_DEVICE_IN_ALL); |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 880 | bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff; |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 881 | thread->setEffectSuspended(FX_IID_AEC, |
| 882 | suspend, |
| 883 | track->sessionId()); |
| 884 | thread->setEffectSuspended(FX_IID_NS, |
| 885 | suspend, |
| 886 | track->sessionId()); |
| 887 | } |
| 888 | } |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 889 | mBtNrecIsOff = btNrecIsOff; |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 890 | } |
| 891 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 892 | return final_result; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | // hold a strong ref on thread in case closeOutput() or closeInput() is called |
| 896 | // and the thread is exited once the lock is released |
| 897 | sp<ThreadBase> thread; |
| 898 | { |
| 899 | Mutex::Autolock _l(mLock); |
| 900 | thread = checkPlaybackThread_l(ioHandle); |
| 901 | if (thread == NULL) { |
| 902 | thread = checkRecordThread_l(ioHandle); |
Glenn Kasten | 7fc9a6f | 2012-01-10 10:46:34 -0800 | [diff] [blame] | 903 | } else if (thread == primaryPlaybackThread_l()) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 904 | // indicate output device change to all input threads for pre processing |
| 905 | AudioParameter param = AudioParameter(keyValuePairs); |
| 906 | int value; |
Eric Laurent | 89d94e7 | 2012-03-16 20:37:59 -0700 | [diff] [blame] | 907 | if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) && |
| 908 | (value != 0)) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 909 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 910 | mRecordThreads.valueAt(i)->setParameters(keyValuePairs); |
| 911 | } |
| 912 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 913 | } |
| 914 | } |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 915 | if (thread != 0) { |
| 916 | return thread->setParameters(keyValuePairs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 917 | } |
| 918 | return BAD_VALUE; |
| 919 | } |
| 920 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 921 | String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 922 | { |
Glenn Kasten | 23d82a9 | 2012-02-03 11:10:00 -0800 | [diff] [blame] | 923 | // ALOGV("getParameters() io %d, keys %s, tid %d, calling pid %d", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 924 | // ioHandle, keys.string(), gettid(), IPCThreadState::self()->getCallingPid()); |
| 925 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 926 | Mutex::Autolock _l(mLock); |
| 927 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 928 | if (ioHandle == 0) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 929 | String8 out_s8; |
| 930 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 931 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 932 | char *s; |
| 933 | { |
| 934 | AutoMutex lock(mHardwareLock); |
| 935 | mHardwareStatus = AUDIO_HW_GET_PARAMETER; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 936 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 937 | s = dev->get_parameters(dev, keys.string()); |
| 938 | mHardwareStatus = AUDIO_HW_IDLE; |
| 939 | } |
John Grossman | ef7740b | 2012-02-09 11:28:36 -0800 | [diff] [blame] | 940 | out_s8 += String8(s ? s : ""); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 941 | free(s); |
| 942 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 943 | return out_s8; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 944 | } |
| 945 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 946 | PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle); |
| 947 | if (playbackThread != NULL) { |
| 948 | return playbackThread->getParameters(keys); |
| 949 | } |
| 950 | RecordThread *recordThread = checkRecordThread_l(ioHandle); |
| 951 | if (recordThread != NULL) { |
| 952 | return recordThread->getParameters(keys); |
| 953 | } |
| 954 | return String8(""); |
| 955 | } |
| 956 | |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 957 | size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format, int channelCount) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 958 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 959 | status_t ret = initCheck(); |
| 960 | if (ret != NO_ERROR) { |
| 961 | return 0; |
| 962 | } |
| 963 | |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 964 | AutoMutex lock(mHardwareLock); |
| 965 | mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 966 | struct audio_config config = { |
| 967 | sample_rate: sampleRate, |
| 968 | channel_mask: audio_channel_in_mask_from_count(channelCount), |
| 969 | format: format, |
| 970 | }; |
| 971 | size_t size = mPrimaryHardwareDev->get_input_buffer_size(mPrimaryHardwareDev, &config); |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 972 | mHardwareStatus = AUDIO_HW_IDLE; |
| 973 | return size; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 974 | } |
| 975 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 976 | unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 977 | { |
| 978 | if (ioHandle == 0) { |
| 979 | return 0; |
| 980 | } |
| 981 | |
| 982 | Mutex::Autolock _l(mLock); |
| 983 | |
| 984 | RecordThread *recordThread = checkRecordThread_l(ioHandle); |
| 985 | if (recordThread != NULL) { |
| 986 | return recordThread->getInputFramesLost(); |
| 987 | } |
| 988 | return 0; |
| 989 | } |
| 990 | |
| 991 | status_t AudioFlinger::setVoiceVolume(float value) |
| 992 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 993 | status_t ret = initCheck(); |
| 994 | if (ret != NO_ERROR) { |
| 995 | return ret; |
| 996 | } |
| 997 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 998 | // check calling permissions |
| 999 | if (!settingsAllowed()) { |
| 1000 | return PERMISSION_DENIED; |
| 1001 | } |
| 1002 | |
| 1003 | AutoMutex lock(mHardwareLock); |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1004 | mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME; |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1005 | ret = mPrimaryHardwareDev->set_voice_volume(mPrimaryHardwareDev, value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1006 | mHardwareStatus = AUDIO_HW_IDLE; |
| 1007 | |
| 1008 | return ret; |
| 1009 | } |
| 1010 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1011 | status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, |
| 1012 | audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1013 | { |
| 1014 | status_t status; |
| 1015 | |
| 1016 | Mutex::Autolock _l(mLock); |
| 1017 | |
| 1018 | PlaybackThread *playbackThread = checkPlaybackThread_l(output); |
| 1019 | if (playbackThread != NULL) { |
| 1020 | return playbackThread->getRenderPosition(halFrames, dspFrames); |
| 1021 | } |
| 1022 | |
| 1023 | return BAD_VALUE; |
| 1024 | } |
| 1025 | |
| 1026 | void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client) |
| 1027 | { |
| 1028 | |
| 1029 | Mutex::Autolock _l(mLock); |
| 1030 | |
Glenn Kasten | bb00192 | 2012-02-03 11:10:26 -0800 | [diff] [blame] | 1031 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1032 | if (mNotificationClients.indexOfKey(pid) < 0) { |
| 1033 | sp<NotificationClient> notificationClient = new NotificationClient(this, |
| 1034 | client, |
| 1035 | pid); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1036 | ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1037 | |
| 1038 | mNotificationClients.add(pid, notificationClient); |
| 1039 | |
| 1040 | sp<IBinder> binder = client->asBinder(); |
| 1041 | binder->linkToDeath(notificationClient); |
| 1042 | |
| 1043 | // the config change is always sent from playback or record threads to avoid deadlock |
| 1044 | // with AudioSystem::gLock |
| 1045 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 1046 | mPlaybackThreads.valueAt(i)->sendConfigEvent(AudioSystem::OUTPUT_OPENED); |
| 1047 | } |
| 1048 | |
| 1049 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 1050 | mRecordThreads.valueAt(i)->sendConfigEvent(AudioSystem::INPUT_OPENED); |
| 1051 | } |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | void AudioFlinger::removeNotificationClient(pid_t pid) |
| 1056 | { |
| 1057 | Mutex::Autolock _l(mLock); |
| 1058 | |
Glenn Kasten | a3b0925 | 2012-01-20 09:19:01 -0800 | [diff] [blame] | 1059 | mNotificationClients.removeItem(pid); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1060 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1061 | ALOGV("%d died, releasing its sessions", pid); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1062 | size_t num = mAudioSessionRefs.size(); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1063 | bool removed = false; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1064 | for (size_t i = 0; i< num; ) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1065 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 1066 | ALOGV(" pid %d @ %d", ref->mPid, i); |
| 1067 | if (ref->mPid == pid) { |
| 1068 | ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1069 | mAudioSessionRefs.removeAt(i); |
| 1070 | delete ref; |
| 1071 | removed = true; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1072 | num--; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1073 | } else { |
| 1074 | i++; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | if (removed) { |
| 1078 | purgeStaleEffects_l(); |
| 1079 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | // audioConfigChanged_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | b81cc8c | 2012-03-01 09:14:51 -0800 | [diff] [blame] | 1083 | void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1084 | { |
| 1085 | size_t size = mNotificationClients.size(); |
| 1086 | for (size_t i = 0; i < size; i++) { |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 1087 | mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle, |
| 1088 | param2); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | // removeClient_l() must be called with AudioFlinger::mLock held |
| 1093 | void AudioFlinger::removeClient_l(pid_t pid) |
| 1094 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1095 | ALOGV("removeClient_l() pid %d, tid %d, calling tid %d", pid, gettid(), IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1096 | mClients.removeItem(pid); |
| 1097 | } |
| 1098 | |
| 1099 | |
| 1100 | // ---------------------------------------------------------------------------- |
| 1101 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1102 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
| 1103 | uint32_t device, type_t type) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1104 | : Thread(false), |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 1105 | mType(type), |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1106 | mAudioFlinger(audioFlinger), mSampleRate(0), mFrameCount(0), mNormalFrameCount(0), |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 1107 | // mChannelMask |
| 1108 | mChannelCount(0), |
| 1109 | mFrameSize(1), mFormat(AUDIO_FORMAT_INVALID), |
| 1110 | mParamStatus(NO_ERROR), |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 1111 | mStandby(false), mId(id), |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 1112 | mDevice(device), |
| 1113 | mDeathRecipient(new PMDeathRecipient(this)) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1114 | { |
| 1115 | } |
| 1116 | |
| 1117 | AudioFlinger::ThreadBase::~ThreadBase() |
| 1118 | { |
| 1119 | mParamCond.broadcast(); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1120 | // do not lock the mutex in destructor |
| 1121 | releaseWakeLock_l(); |
Eric Laurent | 9d18ec5 | 2011-09-27 12:07:15 -0700 | [diff] [blame] | 1122 | if (mPowerManager != 0) { |
| 1123 | sp<IBinder> binder = mPowerManager->asBinder(); |
| 1124 | binder->unlinkToDeath(mDeathRecipient); |
| 1125 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | void AudioFlinger::ThreadBase::exit() |
| 1129 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1130 | ALOGV("ThreadBase::exit"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1131 | { |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 1132 | // This lock prevents the following race in thread (uniprocessor for illustration): |
| 1133 | // if (!exitPending()) { |
| 1134 | // // context switch from here to exit() |
| 1135 | // // exit() calls requestExit(), what exitPending() observes |
| 1136 | // // exit() calls signal(), which is dropped since no waiters |
| 1137 | // // context switch back from exit() to here |
| 1138 | // mWaitWorkCV.wait(...); |
| 1139 | // // now thread is hung |
| 1140 | // } |
Glenn Kasten | a7d8d6f | 2012-01-05 15:41:56 -0800 | [diff] [blame] | 1141 | AutoMutex lock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1142 | requestExit(); |
| 1143 | mWaitWorkCV.signal(); |
| 1144 | } |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 1145 | // When Thread::requestExitAndWait is made virtual and this method is renamed to |
| 1146 | // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1147 | requestExitAndWait(); |
| 1148 | } |
| 1149 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1150 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) |
| 1151 | { |
| 1152 | status_t status; |
| 1153 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1154 | ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1155 | Mutex::Autolock _l(mLock); |
| 1156 | |
| 1157 | mNewParameters.add(keyValuePairs); |
| 1158 | mWaitWorkCV.signal(); |
| 1159 | // wait condition with timeout in case the thread loop has exited |
| 1160 | // before the request could be processed |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 1161 | if (mParamCond.waitRelative(mLock, kSetParametersTimeoutNs) == NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1162 | status = mParamStatus; |
| 1163 | mWaitWorkCV.signal(); |
| 1164 | } else { |
| 1165 | status = TIMED_OUT; |
| 1166 | } |
| 1167 | return status; |
| 1168 | } |
| 1169 | |
| 1170 | void AudioFlinger::ThreadBase::sendConfigEvent(int event, int param) |
| 1171 | { |
| 1172 | Mutex::Autolock _l(mLock); |
| 1173 | sendConfigEvent_l(event, param); |
| 1174 | } |
| 1175 | |
| 1176 | // sendConfigEvent_l() must be called with ThreadBase::mLock held |
| 1177 | void AudioFlinger::ThreadBase::sendConfigEvent_l(int event, int param) |
| 1178 | { |
Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1179 | ConfigEvent configEvent; |
| 1180 | configEvent.mEvent = event; |
| 1181 | configEvent.mParam = param; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1182 | mConfigEvents.add(configEvent); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1183 | ALOGV("sendConfigEvent() num events %d event %d, param %d", mConfigEvents.size(), event, param); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1184 | mWaitWorkCV.signal(); |
| 1185 | } |
| 1186 | |
| 1187 | void AudioFlinger::ThreadBase::processConfigEvents() |
| 1188 | { |
| 1189 | mLock.lock(); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1190 | while (!mConfigEvents.isEmpty()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1191 | ALOGV("processConfigEvents() remaining events %d", mConfigEvents.size()); |
Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1192 | ConfigEvent configEvent = mConfigEvents[0]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1193 | mConfigEvents.removeAt(0); |
| 1194 | // release mLock before locking AudioFlinger mLock: lock order is always |
| 1195 | // AudioFlinger then ThreadBase to avoid cross deadlock |
| 1196 | mLock.unlock(); |
| 1197 | mAudioFlinger->mLock.lock(); |
Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1198 | audioConfigChanged_l(configEvent.mEvent, configEvent.mParam); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1199 | mAudioFlinger->mLock.unlock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1200 | mLock.lock(); |
| 1201 | } |
| 1202 | mLock.unlock(); |
| 1203 | } |
| 1204 | |
| 1205 | status_t AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args) |
| 1206 | { |
| 1207 | const size_t SIZE = 256; |
| 1208 | char buffer[SIZE]; |
| 1209 | String8 result; |
| 1210 | |
| 1211 | bool locked = tryLock(mLock); |
| 1212 | if (!locked) { |
| 1213 | snprintf(buffer, SIZE, "thread %p maybe dead locked\n", this); |
| 1214 | write(fd, buffer, strlen(buffer)); |
| 1215 | } |
| 1216 | |
Eric Laurent | 612bbb5 | 2012-03-14 15:03:26 -0700 | [diff] [blame] | 1217 | snprintf(buffer, SIZE, "io handle: %d\n", mId); |
| 1218 | result.append(buffer); |
| 1219 | snprintf(buffer, SIZE, "TID: %d\n", getTid()); |
| 1220 | result.append(buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1221 | snprintf(buffer, SIZE, "standby: %d\n", mStandby); |
| 1222 | result.append(buffer); |
| 1223 | snprintf(buffer, SIZE, "Sample rate: %d\n", mSampleRate); |
| 1224 | result.append(buffer); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1225 | snprintf(buffer, SIZE, "HAL frame count: %d\n", mFrameCount); |
| 1226 | result.append(buffer); |
| 1227 | snprintf(buffer, SIZE, "Normal frame count: %d\n", mNormalFrameCount); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1228 | result.append(buffer); |
| 1229 | snprintf(buffer, SIZE, "Channel Count: %d\n", mChannelCount); |
| 1230 | result.append(buffer); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1231 | snprintf(buffer, SIZE, "Channel Mask: 0x%08x\n", mChannelMask); |
| 1232 | result.append(buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1233 | snprintf(buffer, SIZE, "Format: %d\n", mFormat); |
| 1234 | result.append(buffer); |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 1235 | snprintf(buffer, SIZE, "Frame size: %u\n", mFrameSize); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1236 | result.append(buffer); |
| 1237 | |
| 1238 | snprintf(buffer, SIZE, "\nPending setParameters commands: \n"); |
| 1239 | result.append(buffer); |
| 1240 | result.append(" Index Command"); |
| 1241 | for (size_t i = 0; i < mNewParameters.size(); ++i) { |
| 1242 | snprintf(buffer, SIZE, "\n %02d ", i); |
| 1243 | result.append(buffer); |
| 1244 | result.append(mNewParameters[i]); |
| 1245 | } |
| 1246 | |
| 1247 | snprintf(buffer, SIZE, "\n\nPending config events: \n"); |
| 1248 | result.append(buffer); |
| 1249 | snprintf(buffer, SIZE, " Index event param\n"); |
| 1250 | result.append(buffer); |
| 1251 | for (size_t i = 0; i < mConfigEvents.size(); i++) { |
Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1252 | snprintf(buffer, SIZE, " %02d %02d %d\n", i, mConfigEvents[i].mEvent, mConfigEvents[i].mParam); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1253 | result.append(buffer); |
| 1254 | } |
| 1255 | result.append("\n"); |
| 1256 | |
| 1257 | write(fd, result.string(), result.size()); |
| 1258 | |
| 1259 | if (locked) { |
| 1260 | mLock.unlock(); |
| 1261 | } |
| 1262 | return NO_ERROR; |
| 1263 | } |
| 1264 | |
Eric Laurent | 1d2bff0 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 1265 | status_t AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) |
| 1266 | { |
| 1267 | const size_t SIZE = 256; |
| 1268 | char buffer[SIZE]; |
| 1269 | String8 result; |
| 1270 | |
| 1271 | snprintf(buffer, SIZE, "\n- %d Effect Chains:\n", mEffectChains.size()); |
| 1272 | write(fd, buffer, strlen(buffer)); |
| 1273 | |
| 1274 | for (size_t i = 0; i < mEffectChains.size(); ++i) { |
| 1275 | sp<EffectChain> chain = mEffectChains[i]; |
| 1276 | if (chain != 0) { |
| 1277 | chain->dump(fd, args); |
| 1278 | } |
| 1279 | } |
| 1280 | return NO_ERROR; |
| 1281 | } |
| 1282 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1283 | void AudioFlinger::ThreadBase::acquireWakeLock() |
| 1284 | { |
| 1285 | Mutex::Autolock _l(mLock); |
| 1286 | acquireWakeLock_l(); |
| 1287 | } |
| 1288 | |
| 1289 | void AudioFlinger::ThreadBase::acquireWakeLock_l() |
| 1290 | { |
| 1291 | if (mPowerManager == 0) { |
| 1292 | // use checkService() to avoid blocking if power service is not up yet |
| 1293 | sp<IBinder> binder = |
| 1294 | defaultServiceManager()->checkService(String16("power")); |
| 1295 | if (binder == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1296 | ALOGW("Thread %s cannot connect to the power manager service", mName); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1297 | } else { |
| 1298 | mPowerManager = interface_cast<IPowerManager>(binder); |
| 1299 | binder->linkToDeath(mDeathRecipient); |
| 1300 | } |
| 1301 | } |
| 1302 | if (mPowerManager != 0) { |
| 1303 | sp<IBinder> binder = new BBinder(); |
| 1304 | status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, |
| 1305 | binder, |
| 1306 | String16(mName)); |
| 1307 | if (status == NO_ERROR) { |
| 1308 | mWakeLockToken = binder; |
| 1309 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1310 | ALOGV("acquireWakeLock_l() %s status %d", mName, status); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | void AudioFlinger::ThreadBase::releaseWakeLock() |
| 1315 | { |
| 1316 | Mutex::Autolock _l(mLock); |
Eric Laurent | 6dbe883 | 2011-07-28 13:59:02 -0700 | [diff] [blame] | 1317 | releaseWakeLock_l(); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | void AudioFlinger::ThreadBase::releaseWakeLock_l() |
| 1321 | { |
| 1322 | if (mWakeLockToken != 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1323 | ALOGV("releaseWakeLock_l() %s", mName); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1324 | if (mPowerManager != 0) { |
| 1325 | mPowerManager->releaseWakeLock(mWakeLockToken, 0); |
| 1326 | } |
| 1327 | mWakeLockToken.clear(); |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | void AudioFlinger::ThreadBase::clearPowerManager() |
| 1332 | { |
| 1333 | Mutex::Autolock _l(mLock); |
| 1334 | releaseWakeLock_l(); |
| 1335 | mPowerManager.clear(); |
| 1336 | } |
| 1337 | |
| 1338 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who) |
| 1339 | { |
| 1340 | sp<ThreadBase> thread = mThread.promote(); |
| 1341 | if (thread != 0) { |
| 1342 | thread->clearPowerManager(); |
| 1343 | } |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1344 | ALOGW("power manager service died !!!"); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1345 | } |
Eric Laurent | 1d2bff0 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 1346 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1347 | void AudioFlinger::ThreadBase::setEffectSuspended( |
| 1348 | const effect_uuid_t *type, bool suspend, int sessionId) |
| 1349 | { |
| 1350 | Mutex::Autolock _l(mLock); |
| 1351 | setEffectSuspended_l(type, suspend, sessionId); |
| 1352 | } |
| 1353 | |
| 1354 | void AudioFlinger::ThreadBase::setEffectSuspended_l( |
| 1355 | const effect_uuid_t *type, bool suspend, int sessionId) |
| 1356 | { |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 1357 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1358 | if (chain != 0) { |
| 1359 | if (type != NULL) { |
| 1360 | chain->setEffectSuspended_l(type, suspend); |
| 1361 | } else { |
| 1362 | chain->setEffectSuspendedAll_l(suspend); |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | updateSuspendedSessions_l(type, suspend, sessionId); |
| 1367 | } |
| 1368 | |
| 1369 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) |
| 1370 | { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1371 | ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1372 | if (index < 0) { |
| 1373 | return; |
| 1374 | } |
| 1375 | |
| 1376 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects = |
| 1377 | mSuspendedSessions.editValueAt(index); |
| 1378 | |
| 1379 | for (size_t i = 0; i < sessionEffects.size(); i++) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1380 | sp<SuspendedSessionDesc> desc = sessionEffects.valueAt(i); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1381 | for (int j = 0; j < desc->mRefCount; j++) { |
| 1382 | if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { |
| 1383 | chain->setEffectSuspendedAll_l(true); |
| 1384 | } else { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1385 | ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1386 | desc->mType.timeLow); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1387 | chain->setEffectSuspended_l(&desc->mType, true); |
| 1388 | } |
| 1389 | } |
| 1390 | } |
| 1391 | } |
| 1392 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1393 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, |
| 1394 | bool suspend, |
| 1395 | int sessionId) |
| 1396 | { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1397 | ssize_t index = mSuspendedSessions.indexOfKey(sessionId); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1398 | |
| 1399 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; |
| 1400 | |
| 1401 | if (suspend) { |
| 1402 | if (index >= 0) { |
| 1403 | sessionEffects = mSuspendedSessions.editValueAt(index); |
| 1404 | } else { |
| 1405 | mSuspendedSessions.add(sessionId, sessionEffects); |
| 1406 | } |
| 1407 | } else { |
| 1408 | if (index < 0) { |
| 1409 | return; |
| 1410 | } |
| 1411 | sessionEffects = mSuspendedSessions.editValueAt(index); |
| 1412 | } |
| 1413 | |
| 1414 | |
| 1415 | int key = EffectChain::kKeyForSuspendAll; |
| 1416 | if (type != NULL) { |
| 1417 | key = type->timeLow; |
| 1418 | } |
| 1419 | index = sessionEffects.indexOfKey(key); |
| 1420 | |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1421 | sp<SuspendedSessionDesc> desc; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1422 | if (suspend) { |
| 1423 | if (index >= 0) { |
| 1424 | desc = sessionEffects.valueAt(index); |
| 1425 | } else { |
| 1426 | desc = new SuspendedSessionDesc(); |
| 1427 | if (type != NULL) { |
| 1428 | memcpy(&desc->mType, type, sizeof(effect_uuid_t)); |
| 1429 | } |
| 1430 | sessionEffects.add(key, desc); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1431 | ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1432 | } |
| 1433 | desc->mRefCount++; |
| 1434 | } else { |
| 1435 | if (index < 0) { |
| 1436 | return; |
| 1437 | } |
| 1438 | desc = sessionEffects.valueAt(index); |
| 1439 | if (--desc->mRefCount == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1440 | ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1441 | sessionEffects.removeItemsAt(index); |
| 1442 | if (sessionEffects.isEmpty()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1443 | ALOGV("updateSuspendedSessions_l() restore removing session %d", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1444 | sessionId); |
| 1445 | mSuspendedSessions.removeItem(sessionId); |
| 1446 | } |
| 1447 | } |
| 1448 | } |
| 1449 | if (!sessionEffects.isEmpty()) { |
| 1450 | mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 1455 | bool enabled, |
| 1456 | int sessionId) |
| 1457 | { |
| 1458 | Mutex::Autolock _l(mLock); |
Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 1459 | checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); |
| 1460 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1461 | |
Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 1462 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, |
| 1463 | bool enabled, |
| 1464 | int sessionId) |
| 1465 | { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 1466 | if (mType != RECORD) { |
| 1467 | // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on |
| 1468 | // another session. This gives the priority to well behaved effect control panels |
| 1469 | // and applications not using global effects. |
Eric Laurent | 808e7d1 | 2012-05-11 19:44:09 -0700 | [diff] [blame] | 1470 | // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect |
| 1471 | // global effects |
| 1472 | if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 1473 | setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); |
| 1474 | } |
| 1475 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1476 | |
| 1477 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1478 | if (chain != 0) { |
| 1479 | chain->checkSuspendOnEffectEnabled(effect, enabled); |
| 1480 | } |
| 1481 | } |
| 1482 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1483 | // ---------------------------------------------------------------------------- |
| 1484 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1485 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, |
| 1486 | AudioStreamOut* output, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1487 | audio_io_handle_t id, |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 1488 | uint32_t device, |
| 1489 | type_t type) |
| 1490 | : ThreadBase(audioFlinger, id, device, type), |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 1491 | mMixBuffer(NULL), mSuspended(0), mBytesWritten(0), |
| 1492 | // Assumes constructor is called by AudioFlinger with it's mLock held, |
| 1493 | // but it would be safer to explicitly pass initial masterMute as parameter |
| 1494 | mMasterMute(audioFlinger->masterMute_l()), |
| 1495 | // mStreamTypes[] initialized in constructor body |
| 1496 | mOutput(output), |
| 1497 | // Assumes constructor is called by AudioFlinger with it's mLock held, |
| 1498 | // but it would be safer to explicitly pass initial masterVolume as parameter |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1499 | mMasterVolume(audioFlinger->masterVolumeSW_l()), |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 1500 | mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), |
Glenn Kasten | aa4397f | 2012-03-12 18:13:59 -0700 | [diff] [blame] | 1501 | mMixerStatus(MIXER_IDLE), |
Glenn Kasten | 8102804 | 2012-04-30 18:15:12 -0700 | [diff] [blame] | 1502 | mMixerStatusIgnoringFastTracks(MIXER_IDLE), |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1503 | standbyDelay(AudioFlinger::mStandbyTimeInNsecs), |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 1504 | // index 0 is reserved for normal mixer's submix |
| 1505 | mFastTrackAvailMask(((1 << FastMixerState::kMaxFastTracks) - 1) & ~1) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1506 | { |
Glenn Kasten | 480b468 | 2012-02-28 12:30:08 -0800 | [diff] [blame] | 1507 | snprintf(mName, kNameLength, "AudioOut_%X", id); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1508 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1509 | readOutputParameters(); |
| 1510 | |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 1511 | // mStreamTypes[AUDIO_STREAM_CNT] is initialized by stream_type_t default constructor |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1512 | // There is no AUDIO_STREAM_MIN, and ++ operator does not compile |
| 1513 | for (audio_stream_type_t stream = (audio_stream_type_t) 0; stream < AUDIO_STREAM_CNT; |
| 1514 | stream = (audio_stream_type_t) (stream + 1)) { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1515 | mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream); |
| 1516 | mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1517 | } |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1518 | // mStreamTypes[AUDIO_STREAM_CNT] exists but isn't explicitly initialized here, |
| 1519 | // because mAudioFlinger doesn't have one to copy from |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | AudioFlinger::PlaybackThread::~PlaybackThread() |
| 1523 | { |
| 1524 | delete [] mMixBuffer; |
| 1525 | } |
| 1526 | |
| 1527 | status_t AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) |
| 1528 | { |
| 1529 | dumpInternals(fd, args); |
| 1530 | dumpTracks(fd, args); |
| 1531 | dumpEffectChains(fd, args); |
| 1532 | return NO_ERROR; |
| 1533 | } |
| 1534 | |
| 1535 | status_t AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args) |
| 1536 | { |
| 1537 | const size_t SIZE = 256; |
| 1538 | char buffer[SIZE]; |
| 1539 | String8 result; |
| 1540 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1541 | result.appendFormat("Output thread %p stream volumes in dB:\n ", this); |
| 1542 | for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { |
| 1543 | const stream_type_t *st = &mStreamTypes[i]; |
| 1544 | if (i > 0) { |
| 1545 | result.appendFormat(", "); |
| 1546 | } |
| 1547 | result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); |
| 1548 | if (st->mute) { |
| 1549 | result.append("M"); |
| 1550 | } |
| 1551 | } |
| 1552 | result.append("\n"); |
| 1553 | write(fd, result.string(), result.length()); |
| 1554 | result.clear(); |
| 1555 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1556 | snprintf(buffer, SIZE, "Output thread %p tracks\n", this); |
| 1557 | result.append(buffer); |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 1558 | Track::appendDumpHeader(result); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1559 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1560 | sp<Track> track = mTracks[i]; |
| 1561 | if (track != 0) { |
| 1562 | track->dump(buffer, SIZE); |
| 1563 | result.append(buffer); |
| 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | snprintf(buffer, SIZE, "Output thread %p active tracks\n", this); |
| 1568 | result.append(buffer); |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 1569 | Track::appendDumpHeader(result); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1570 | for (size_t i = 0; i < mActiveTracks.size(); ++i) { |
Glenn Kasten | 77c1119 | 2012-01-25 14:27:41 -0800 | [diff] [blame] | 1571 | sp<Track> track = mActiveTracks[i].promote(); |
| 1572 | if (track != 0) { |
| 1573 | track->dump(buffer, SIZE); |
| 1574 | result.append(buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1575 | } |
| 1576 | } |
| 1577 | write(fd, result.string(), result.size()); |
Glenn Kasten | 88cbea8 | 2012-05-15 07:39:27 -0700 | [diff] [blame] | 1578 | |
| 1579 | // These values are "raw"; they will wrap around. See prepareTracks_l() for a better way. |
| 1580 | FastTrackUnderruns underruns = getFastTrackUnderruns(0); |
| 1581 | fdprintf(fd, "Normal mixer raw underrun counters: partial=%u empty=%u\n", |
| 1582 | underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); |
| 1583 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1584 | return NO_ERROR; |
| 1585 | } |
| 1586 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1587 | status_t AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) |
| 1588 | { |
| 1589 | const size_t SIZE = 256; |
| 1590 | char buffer[SIZE]; |
| 1591 | String8 result; |
| 1592 | |
| 1593 | snprintf(buffer, SIZE, "\nOutput thread %p internals\n", this); |
| 1594 | result.append(buffer); |
| 1595 | snprintf(buffer, SIZE, "last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime)); |
| 1596 | result.append(buffer); |
| 1597 | snprintf(buffer, SIZE, "total writes: %d\n", mNumWrites); |
| 1598 | result.append(buffer); |
| 1599 | snprintf(buffer, SIZE, "delayed writes: %d\n", mNumDelayedWrites); |
| 1600 | result.append(buffer); |
| 1601 | snprintf(buffer, SIZE, "blocked in write: %d\n", mInWrite); |
| 1602 | result.append(buffer); |
| 1603 | snprintf(buffer, SIZE, "suspend count: %d\n", mSuspended); |
| 1604 | result.append(buffer); |
| 1605 | snprintf(buffer, SIZE, "mix buffer : %p\n", mMixBuffer); |
| 1606 | result.append(buffer); |
| 1607 | write(fd, result.string(), result.size()); |
| 1608 | |
| 1609 | dumpBase(fd, args); |
| 1610 | |
| 1611 | return NO_ERROR; |
| 1612 | } |
| 1613 | |
| 1614 | // Thread virtuals |
| 1615 | status_t AudioFlinger::PlaybackThread::readyToRun() |
| 1616 | { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1617 | status_t status = initCheck(); |
| 1618 | if (status == NO_ERROR) { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1619 | ALOGI("AudioFlinger's thread %p ready to run", this); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1620 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1621 | ALOGE("No working audio driver found."); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1622 | } |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1623 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | void AudioFlinger::PlaybackThread::onFirstRef() |
| 1627 | { |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1628 | run(mName, ANDROID_PRIORITY_URGENT_AUDIO); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1629 | } |
| 1630 | |
| 1631 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 1632 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1633 | const sp<AudioFlinger::Client>& client, |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1634 | audio_stream_type_t streamType, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1635 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 1636 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1637 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1638 | int frameCount, |
| 1639 | const sp<IMemory>& sharedBuffer, |
| 1640 | int sessionId, |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1641 | IAudioFlinger::track_flags_t flags, |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1642 | pid_t tid, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1643 | status_t *status) |
| 1644 | { |
| 1645 | sp<Track> track; |
| 1646 | status_t lStatus; |
| 1647 | |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1648 | bool isTimed = (flags & IAudioFlinger::TRACK_TIMED) != 0; |
| 1649 | |
| 1650 | // client expresses a preference for FAST, but we get the final say |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1651 | if (flags & IAudioFlinger::TRACK_FAST) { |
| 1652 | if ( |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1653 | // not timed |
| 1654 | (!isTimed) && |
| 1655 | // either of these use cases: |
| 1656 | ( |
| 1657 | // use case 1: shared buffer with any frame count |
| 1658 | ( |
| 1659 | (sharedBuffer != 0) |
| 1660 | ) || |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1661 | // use case 2: callback handler and frame count is default or at least as large as HAL |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1662 | ( |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1663 | (tid != -1) && |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1664 | ((frameCount == 0) || |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 1665 | (frameCount >= (int) (mFrameCount * 2))) // * 2 is due to SRC jitter, see below |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1666 | ) |
| 1667 | ) && |
| 1668 | // PCM data |
| 1669 | audio_is_linear_pcm(format) && |
| 1670 | // mono or stereo |
| 1671 | ( (channelMask == AUDIO_CHANNEL_OUT_MONO) || |
| 1672 | (channelMask == AUDIO_CHANNEL_OUT_STEREO) ) && |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1673 | #ifndef FAST_TRACKS_AT_NON_NATIVE_SAMPLE_RATE |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1674 | // hardware sample rate |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1675 | (sampleRate == mSampleRate) && |
| 1676 | #endif |
| 1677 | // normal mixer has an associated fast mixer |
| 1678 | hasFastMixer() && |
| 1679 | // there are sufficient fast track slots available |
| 1680 | (mFastTrackAvailMask != 0) |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1681 | // FIXME test that MixerThread for this fast track has a capable output HAL |
| 1682 | // FIXME add a permission test also? |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1683 | ) { |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1684 | // if frameCount not specified, then it defaults to fast mixer (HAL) frame count |
| 1685 | if (frameCount == 0) { |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 1686 | frameCount = mFrameCount * 2; // FIXME * 2 is due to SRC jitter, should be computed |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1687 | } |
Glenn Kasten | 31dfd1d | 2012-05-01 11:07:08 -0700 | [diff] [blame] | 1688 | ALOGV("AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%d mFrameCount=%d", |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 1689 | frameCount, mFrameCount); |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1690 | } else { |
| 1691 | ALOGW("AUDIO_OUTPUT_FLAG_FAST denied: isTimed=%d sharedBuffer=%p frameCount=%d " |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1692 | "mFrameCount=%d format=%d isLinear=%d channelMask=%d sampleRate=%d mSampleRate=%d " |
| 1693 | "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", |
| 1694 | isTimed, sharedBuffer.get(), frameCount, mFrameCount, format, |
| 1695 | audio_is_linear_pcm(format), |
| 1696 | channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1697 | flags &= ~IAudioFlinger::TRACK_FAST; |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1698 | // For compatibility with AudioTrack calculation, buffer depth is forced |
| 1699 | // to be at least 2 x the normal mixer frame count and cover audio hardware latency. |
| 1700 | // This is probably too conservative, but legacy application code may depend on it. |
| 1701 | // If you change this calculation, also review the start threshold which is related. |
| 1702 | uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream); |
| 1703 | uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate); |
| 1704 | if (minBufCount < 2) { |
| 1705 | minBufCount = 2; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1706 | } |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1707 | int minFrameCount = mNormalFrameCount * minBufCount; |
| 1708 | if (frameCount < minFrameCount) { |
| 1709 | frameCount = minFrameCount; |
| 1710 | } |
| 1711 | } |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1712 | } |
| 1713 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1714 | if (mType == DIRECT) { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1715 | if ((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM) { |
| 1716 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1717 | ALOGE("createTrack_l() Bad parameter: sampleRate %d format %d, channelMask 0x%08x \"" |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1718 | "for output %p with format %d", |
| 1719 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1720 | lStatus = BAD_VALUE; |
| 1721 | goto Exit; |
| 1722 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1723 | } |
| 1724 | } else { |
| 1725 | // Resampler implementation limits input sampling rate to 2 x output sampling rate. |
| 1726 | if (sampleRate > mSampleRate*2) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1727 | ALOGE("Sample rate out of range: %d mSampleRate %d", sampleRate, mSampleRate); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1728 | lStatus = BAD_VALUE; |
| 1729 | goto Exit; |
| 1730 | } |
| 1731 | } |
| 1732 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1733 | lStatus = initCheck(); |
| 1734 | if (lStatus != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1735 | ALOGE("Audio driver not initialized."); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1736 | goto Exit; |
| 1737 | } |
| 1738 | |
| 1739 | { // scope for mLock |
| 1740 | Mutex::Autolock _l(mLock); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1741 | |
| 1742 | // all tracks in same audio session must share the same routing strategy otherwise |
| 1743 | // conflicts will happen when tracks are moved from one output to another by audio policy |
| 1744 | // manager |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 1745 | uint32_t strategy = AudioSystem::getStrategyForStream(streamType); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1746 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1747 | sp<Track> t = mTracks[i]; |
Glenn Kasten | 639dbee | 2012-03-07 12:26:34 -0800 | [diff] [blame] | 1748 | if (t != 0 && !t->isOutputTrack()) { |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 1749 | uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); |
Glenn Kasten | d879601 | 2011-10-28 10:31:42 -0700 | [diff] [blame] | 1750 | if (sessionId == t->sessionId() && strategy != actual) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1751 | ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", |
Glenn Kasten | d879601 | 2011-10-28 10:31:42 -0700 | [diff] [blame] | 1752 | strategy, actual); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1753 | lStatus = BAD_VALUE; |
| 1754 | goto Exit; |
| 1755 | } |
| 1756 | } |
| 1757 | } |
| 1758 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1759 | if (!isTimed) { |
| 1760 | track = new Track(this, client, streamType, sampleRate, format, |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1761 | channelMask, frameCount, sharedBuffer, sessionId, flags); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1762 | } else { |
| 1763 | track = TimedTrack::create(this, client, streamType, sampleRate, format, |
| 1764 | channelMask, frameCount, sharedBuffer, sessionId); |
| 1765 | } |
| 1766 | if (track == NULL || track->getCblk() == NULL || track->name() < 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1767 | lStatus = NO_MEMORY; |
| 1768 | goto Exit; |
| 1769 | } |
| 1770 | mTracks.add(track); |
| 1771 | |
| 1772 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1773 | if (chain != 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1774 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1775 | track->setMainBuffer(chain->inBuffer()); |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 1776 | chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1777 | chain->incTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1778 | } |
| 1779 | } |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1780 | |
| 1781 | #ifdef HAVE_REQUEST_PRIORITY |
| 1782 | if ((flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { |
| 1783 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 1784 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 1785 | // so ask activity manager to do this on our behalf |
| 1786 | int err = requestPriority(callingPid, tid, 1); |
| 1787 | if (err != 0) { |
| 1788 | ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", |
| 1789 | 1, callingPid, tid, err); |
| 1790 | } |
| 1791 | } |
| 1792 | #endif |
| 1793 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1794 | lStatus = NO_ERROR; |
| 1795 | |
| 1796 | Exit: |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1797 | if (status) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1798 | *status = lStatus; |
| 1799 | } |
| 1800 | return track; |
| 1801 | } |
| 1802 | |
Eric Laurent | e737cda | 2012-05-22 18:55:44 -0700 | [diff] [blame] | 1803 | uint32_t AudioFlinger::MixerThread::correctLatency(uint32_t latency) const |
| 1804 | { |
| 1805 | if (mFastMixer != NULL) { |
| 1806 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 1807 | latency += (pipe->getAvgFrames() * 1000) / mSampleRate; |
| 1808 | } |
| 1809 | return latency; |
| 1810 | } |
| 1811 | |
| 1812 | uint32_t AudioFlinger::PlaybackThread::correctLatency(uint32_t latency) const |
| 1813 | { |
| 1814 | return latency; |
| 1815 | } |
| 1816 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1817 | uint32_t AudioFlinger::PlaybackThread::latency() const |
| 1818 | { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1819 | Mutex::Autolock _l(mLock); |
| 1820 | if (initCheck() == NO_ERROR) { |
Eric Laurent | e737cda | 2012-05-22 18:55:44 -0700 | [diff] [blame] | 1821 | return correctLatency(mOutput->stream->get_latency(mOutput->stream)); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1822 | } else { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1823 | return 0; |
| 1824 | } |
| 1825 | } |
| 1826 | |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1827 | void AudioFlinger::PlaybackThread::setMasterVolume(float value) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1828 | { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1829 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1830 | mMasterVolume = value; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1831 | } |
| 1832 | |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1833 | void AudioFlinger::PlaybackThread::setMasterMute(bool muted) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1834 | { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1835 | Mutex::Autolock _l(mLock); |
| 1836 | setMasterMute_l(muted); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1837 | } |
| 1838 | |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1839 | void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1840 | { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1841 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1842 | mStreamTypes[stream].volume = value; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1843 | } |
| 1844 | |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1845 | void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1846 | { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1847 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1848 | mStreamTypes[stream].mute = muted; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1849 | } |
| 1850 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1851 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1852 | { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1853 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1854 | return mStreamTypes[stream].volume; |
| 1855 | } |
| 1856 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1857 | // addTrack_l() must be called with ThreadBase::mLock held |
| 1858 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) |
| 1859 | { |
| 1860 | status_t status = ALREADY_EXISTS; |
| 1861 | |
| 1862 | // set retry count for buffer fill |
| 1863 | track->mRetryCount = kMaxTrackStartupRetries; |
| 1864 | if (mActiveTracks.indexOf(track) < 0) { |
| 1865 | // the track is newly added, make sure it fills up all its |
| 1866 | // buffers before playing. This is to ensure the client will |
| 1867 | // effectively get the latency it requested. |
| 1868 | track->mFillingUpStatus = Track::FS_FILLING; |
| 1869 | track->mResetDone = false; |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 1870 | track->mPresentationCompleteFrames = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1871 | mActiveTracks.add(track); |
| 1872 | if (track->mainBuffer() != mMixBuffer) { |
| 1873 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 1874 | if (chain != 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1875 | ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), track->sessionId()); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1876 | chain->incActiveTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1877 | } |
| 1878 | } |
| 1879 | |
| 1880 | status = NO_ERROR; |
| 1881 | } |
| 1882 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1883 | ALOGV("mWaitWorkCV.broadcast"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1884 | mWaitWorkCV.broadcast(); |
| 1885 | |
| 1886 | return status; |
| 1887 | } |
| 1888 | |
| 1889 | // destroyTrack_l() must be called with ThreadBase::mLock held |
| 1890 | void AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) |
| 1891 | { |
| 1892 | track->mState = TrackBase::TERMINATED; |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 1893 | // active tracks are removed by threadLoop() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1894 | if (mActiveTracks.indexOf(track) < 0) { |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1895 | removeTrack_l(track); |
| 1896 | } |
| 1897 | } |
| 1898 | |
| 1899 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) |
| 1900 | { |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 1901 | track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1902 | mTracks.remove(track); |
| 1903 | deleteTrackName_l(track->name()); |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 1904 | // redundant as track is about to be destroyed, for dumpsys only |
| 1905 | track->mName = -1; |
| 1906 | if (track->isFastTrack()) { |
| 1907 | int index = track->mFastIndex; |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 1908 | ALOG_ASSERT(0 < index && index < (int)FastMixerState::kMaxFastTracks); |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 1909 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); |
| 1910 | mFastTrackAvailMask |= 1 << index; |
| 1911 | // redundant as track is about to be destroyed, for dumpsys only |
| 1912 | track->mFastIndex = -1; |
| 1913 | } |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1914 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 1915 | if (chain != 0) { |
| 1916 | chain->decTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1917 | } |
| 1918 | } |
| 1919 | |
| 1920 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) |
| 1921 | { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1922 | String8 out_s8 = String8(""); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1923 | char *s; |
| 1924 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1925 | Mutex::Autolock _l(mLock); |
| 1926 | if (initCheck() != NO_ERROR) { |
| 1927 | return out_s8; |
| 1928 | } |
| 1929 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1930 | s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1931 | out_s8 = String8(s); |
| 1932 | free(s); |
| 1933 | return out_s8; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1934 | } |
| 1935 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1936 | // audioConfigChanged_l() must be called with AudioFlinger::mLock held |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1937 | void AudioFlinger::PlaybackThread::audioConfigChanged_l(int event, int param) { |
| 1938 | AudioSystem::OutputDescriptor desc; |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 1939 | void *param2 = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1940 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1941 | ALOGV("PlaybackThread::audioConfigChanged_l, thread %p, event %d, param %d", this, event, param); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1942 | |
| 1943 | switch (event) { |
| 1944 | case AudioSystem::OUTPUT_OPENED: |
| 1945 | case AudioSystem::OUTPUT_CONFIG_CHANGED: |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1946 | desc.channels = mChannelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1947 | desc.samplingRate = mSampleRate; |
| 1948 | desc.format = mFormat; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1949 | desc.frameCount = mNormalFrameCount; // FIXME see AudioFlinger::frameCount(audio_io_handle_t) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1950 | desc.latency = latency(); |
| 1951 | param2 = &desc; |
| 1952 | break; |
| 1953 | |
| 1954 | case AudioSystem::STREAM_CONFIG_CHANGED: |
| 1955 | param2 = ¶m; |
| 1956 | case AudioSystem::OUTPUT_CLOSED: |
| 1957 | default: |
| 1958 | break; |
| 1959 | } |
| 1960 | mAudioFlinger->audioConfigChanged_l(event, mId, param2); |
| 1961 | } |
| 1962 | |
| 1963 | void AudioFlinger::PlaybackThread::readOutputParameters() |
| 1964 | { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1965 | mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1966 | mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common); |
| 1967 | mChannelCount = (uint16_t)popcount(mChannelMask); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1968 | mFormat = mOutput->stream->common.get_format(&mOutput->stream->common); |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 1969 | mFrameSize = audio_stream_frame_size(&mOutput->stream->common); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1970 | mFrameCount = mOutput->stream->common.get_buffer_size(&mOutput->stream->common) / mFrameSize; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1971 | if (mFrameCount & 15) { |
| 1972 | ALOGW("HAL output buffer size is %u frames but AudioMixer requires multiples of 16 frames", |
| 1973 | mFrameCount); |
| 1974 | } |
| 1975 | |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 1976 | // Calculate size of normal mix buffer relative to the HAL output buffer size |
Glenn Kasten | 4adcede | 2012-05-14 12:26:02 -0700 | [diff] [blame] | 1977 | double multiplier = 1.0; |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 1978 | if (mType == MIXER && (kUseFastMixer == FastMixer_Static || kUseFastMixer == FastMixer_Dynamic)) { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1979 | size_t minNormalFrameCount = (kMinNormalMixBufferSizeMs * mSampleRate) / 1000; |
Glenn Kasten | 4adcede | 2012-05-14 12:26:02 -0700 | [diff] [blame] | 1980 | size_t maxNormalFrameCount = (kMaxNormalMixBufferSizeMs * mSampleRate) / 1000; |
| 1981 | // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer |
| 1982 | minNormalFrameCount = (minNormalFrameCount + 15) & ~15; |
| 1983 | maxNormalFrameCount = maxNormalFrameCount & ~15; |
| 1984 | if (maxNormalFrameCount < minNormalFrameCount) { |
| 1985 | maxNormalFrameCount = minNormalFrameCount; |
| 1986 | } |
| 1987 | multiplier = (double) minNormalFrameCount / (double) mFrameCount; |
| 1988 | if (multiplier <= 1.0) { |
| 1989 | multiplier = 1.0; |
| 1990 | } else if (multiplier <= 2.0) { |
| 1991 | if (2 * mFrameCount <= maxNormalFrameCount) { |
| 1992 | multiplier = 2.0; |
| 1993 | } else { |
| 1994 | multiplier = (double) maxNormalFrameCount / (double) mFrameCount; |
| 1995 | } |
| 1996 | } else { |
| 1997 | // prefer an even multiplier, for compatibility with doubling of fast tracks due to HAL SRC |
| 1998 | // (it would be unusual for the normal mix buffer size to not be a multiple of fast |
| 1999 | // track, but we sometimes have to do this to satisfy the maximum frame count constraint) |
| 2000 | // FIXME this rounding up should not be done if no HAL SRC |
| 2001 | uint32_t truncMult = (uint32_t) multiplier; |
| 2002 | if ((truncMult & 1)) { |
| 2003 | if ((truncMult + 1) * mFrameCount <= maxNormalFrameCount) { |
| 2004 | ++truncMult; |
| 2005 | } |
| 2006 | } |
| 2007 | multiplier = (double) truncMult; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2008 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2009 | } |
Glenn Kasten | 4adcede | 2012-05-14 12:26:02 -0700 | [diff] [blame] | 2010 | mNormalFrameCount = multiplier * mFrameCount; |
| 2011 | // round up to nearest 16 frames to satisfy AudioMixer |
| 2012 | mNormalFrameCount = (mNormalFrameCount + 15) & ~15; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2013 | ALOGI("HAL output buffer size %u frames, normal mix buffer size %u frames", mFrameCount, mNormalFrameCount); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2014 | |
| 2015 | // FIXME - Current mixer implementation only supports stereo output: Always |
| 2016 | // Allocate a stereo buffer even if HW output is mono. |
Glenn Kasten | e9dd017 | 2012-01-27 18:08:45 -0800 | [diff] [blame] | 2017 | delete[] mMixBuffer; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2018 | mMixBuffer = new int16_t[mNormalFrameCount * 2]; |
| 2019 | memset(mMixBuffer, 0, mNormalFrameCount * 2 * sizeof(int16_t)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2020 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2021 | // force reconfiguration of effect chains and engines to take new buffer size and audio |
| 2022 | // parameters into account |
| 2023 | // Note that mLock is not held when readOutputParameters() is called from the constructor |
| 2024 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't |
| 2025 | // matter. |
| 2026 | // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains |
| 2027 | Vector< sp<EffectChain> > effectChains = mEffectChains; |
| 2028 | for (size_t i = 0; i < effectChains.size(); i ++) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2029 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2030 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2031 | } |
| 2032 | |
Eric Laurent | e737cda | 2012-05-22 18:55:44 -0700 | [diff] [blame] | 2033 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2034 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) |
| 2035 | { |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 2036 | if (halFrames == NULL || dspFrames == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2037 | return BAD_VALUE; |
| 2038 | } |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2039 | Mutex::Autolock _l(mLock); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2040 | if (initCheck() != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2041 | return INVALID_OPERATION; |
| 2042 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2043 | *halFrames = mBytesWritten / audio_stream_frame_size(&mOutput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2044 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2045 | return mOutput->stream->get_render_position(mOutput->stream, dspFrames); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2046 | } |
| 2047 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2048 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2049 | { |
| 2050 | Mutex::Autolock _l(mLock); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2051 | uint32_t result = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2052 | if (getEffectChain_l(sessionId) != 0) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2053 | result = EFFECT_SESSION; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2057 | sp<Track> track = mTracks[i]; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2058 | if (sessionId == track->sessionId() && |
| 2059 | !(track->mCblk->flags & CBLK_INVALID_MSK)) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2060 | result |= TRACK_SESSION; |
| 2061 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2062 | } |
| 2063 | } |
| 2064 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2065 | return result; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2066 | } |
| 2067 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2068 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId) |
| 2069 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2070 | // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2071 | // it is moved to correct output by audio policy manager when A2DP is connected or disconnected |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2072 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 2073 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2074 | } |
| 2075 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 2076 | sp<Track> track = mTracks[i]; |
| 2077 | if (sessionId == track->sessionId() && |
| 2078 | !(track->mCblk->flags & CBLK_INVALID_MSK)) { |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 2079 | return AudioSystem::getStrategyForStream(track->streamType()); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2080 | } |
| 2081 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2082 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2083 | } |
| 2084 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2085 | |
Glenn Kasten | aed850d | 2012-01-26 09:46:34 -0800 | [diff] [blame] | 2086 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2087 | { |
| 2088 | Mutex::Autolock _l(mLock); |
| 2089 | return mOutput; |
| 2090 | } |
| 2091 | |
| 2092 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() |
| 2093 | { |
| 2094 | Mutex::Autolock _l(mLock); |
| 2095 | AudioStreamOut *output = mOutput; |
| 2096 | mOutput = NULL; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2097 | // FIXME FastMixer might also have a raw ptr to mOutputSink; |
| 2098 | // must push a NULL and wait for ack |
| 2099 | mOutputSink.clear(); |
| 2100 | mPipeSink.clear(); |
| 2101 | mNormalSink.clear(); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2102 | return output; |
| 2103 | } |
| 2104 | |
| 2105 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 2106 | audio_stream_t* AudioFlinger::PlaybackThread::stream() const |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2107 | { |
| 2108 | if (mOutput == NULL) { |
| 2109 | return NULL; |
| 2110 | } |
| 2111 | return &mOutput->stream->common; |
| 2112 | } |
| 2113 | |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 2114 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const |
Eric Laurent | 162b40b | 2011-12-05 09:47:19 -0800 | [diff] [blame] | 2115 | { |
| 2116 | // A2DP output latency is not due only to buffering capacity. It also reflects encoding, |
| 2117 | // decoding and transfer time. So sleeping for half of the latency would likely cause |
| 2118 | // underruns |
| 2119 | if (audio_is_a2dp_device((audio_devices_t)mDevice)) { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2120 | return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
Eric Laurent | 162b40b | 2011-12-05 09:47:19 -0800 | [diff] [blame] | 2121 | } else { |
| 2122 | return (uint32_t)(mOutput->stream->get_latency(mOutput->stream) * 1000) / 2; |
| 2123 | } |
| 2124 | } |
| 2125 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 2126 | status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) |
| 2127 | { |
| 2128 | if (!isValidSyncEvent(event)) { |
| 2129 | return BAD_VALUE; |
| 2130 | } |
| 2131 | |
| 2132 | Mutex::Autolock _l(mLock); |
| 2133 | |
| 2134 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2135 | sp<Track> track = mTracks[i]; |
| 2136 | if (event->triggerSession() == track->sessionId()) { |
| 2137 | track->setSyncEvent(event); |
| 2138 | return NO_ERROR; |
| 2139 | } |
| 2140 | } |
| 2141 | |
| 2142 | return NAME_NOT_FOUND; |
| 2143 | } |
| 2144 | |
| 2145 | bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) |
| 2146 | { |
| 2147 | switch (event->type()) { |
| 2148 | case AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE: |
| 2149 | return true; |
| 2150 | default: |
| 2151 | break; |
| 2152 | } |
| 2153 | return false; |
| 2154 | } |
| 2155 | |
Eric Laurent | 44a957f | 2012-05-15 15:26:05 -0700 | [diff] [blame] | 2156 | void AudioFlinger::PlaybackThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) |
| 2157 | { |
| 2158 | size_t count = tracksToRemove.size(); |
| 2159 | if (CC_UNLIKELY(count)) { |
| 2160 | for (size_t i = 0 ; i < count ; i++) { |
| 2161 | const sp<Track>& track = tracksToRemove.itemAt(i); |
| 2162 | if ((track->sharedBuffer() != 0) && |
| 2163 | (track->mState == TrackBase::ACTIVE || track->mState == TrackBase::RESUMING)) { |
| 2164 | AudioSystem::stopOutput(mId, track->streamType(), track->sessionId()); |
| 2165 | } |
| 2166 | } |
| 2167 | } |
| 2168 | |
| 2169 | } |
| 2170 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2171 | // ---------------------------------------------------------------------------- |
| 2172 | |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 2173 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2174 | audio_io_handle_t id, uint32_t device, type_t type) |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2175 | : PlaybackThread(audioFlinger, output, id, device, type), |
| 2176 | // mAudioMixer below |
| 2177 | #ifdef SOAKER |
| 2178 | mSoaker(NULL), |
| 2179 | #endif |
| 2180 | // mFastMixer below |
| 2181 | mFastMixerFutex(0) |
| 2182 | // mOutputSink below |
| 2183 | // mPipeSink below |
| 2184 | // mNormalSink below |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2185 | { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2186 | ALOGV("MixerThread() id=%d device=%d type=%d", id, device, type); |
| 2187 | ALOGV("mSampleRate=%d, mChannelMask=%d, mChannelCount=%d, mFormat=%d, mFrameSize=%d, " |
| 2188 | "mFrameCount=%d, mNormalFrameCount=%d", |
| 2189 | mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, |
| 2190 | mNormalFrameCount); |
| 2191 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 2192 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2193 | // FIXME - Current mixer implementation only supports stereo output |
| 2194 | if (mChannelCount == 1) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 2195 | ALOGE("Invalid audio hardware channel count"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2196 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2197 | |
| 2198 | // create an NBAIO sink for the HAL output stream, and negotiate |
| 2199 | mOutputSink = new AudioStreamOutSink(output->stream); |
| 2200 | size_t numCounterOffers = 0; |
| 2201 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount)}; |
| 2202 | ssize_t index = mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); |
| 2203 | ALOG_ASSERT(index == 0); |
| 2204 | |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 2205 | // initialize fast mixer depending on configuration |
| 2206 | bool initFastMixer; |
| 2207 | switch (kUseFastMixer) { |
| 2208 | case FastMixer_Never: |
| 2209 | initFastMixer = false; |
| 2210 | break; |
| 2211 | case FastMixer_Always: |
| 2212 | initFastMixer = true; |
| 2213 | break; |
| 2214 | case FastMixer_Static: |
| 2215 | case FastMixer_Dynamic: |
| 2216 | initFastMixer = mFrameCount < mNormalFrameCount; |
| 2217 | break; |
| 2218 | } |
| 2219 | if (initFastMixer) { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2220 | |
| 2221 | // create a MonoPipe to connect our submix to FastMixer |
| 2222 | NBAIO_Format format = mOutputSink->format(); |
Glenn Kasten | 9017e5e | 2012-05-15 07:39:52 -0700 | [diff] [blame] | 2223 | // This pipe depth compensates for scheduling latency of the normal mixer thread. |
| 2224 | // When it wakes up after a maximum latency, it runs a few cycles quickly before |
| 2225 | // finally blocking. Note the pipe implementation rounds up the request to a power of 2. |
| 2226 | MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2227 | const NBAIO_Format offers[1] = {format}; |
| 2228 | size_t numCounterOffers = 0; |
| 2229 | ssize_t index = monoPipe->negotiate(offers, 1, NULL, numCounterOffers); |
| 2230 | ALOG_ASSERT(index == 0); |
| 2231 | mPipeSink = monoPipe; |
| 2232 | |
Glenn Kasten | fbae5da | 2012-05-21 09:17:20 -0700 | [diff] [blame] | 2233 | #ifdef TEE_SINK_FRAMES |
| 2234 | // create a Pipe to archive a copy of FastMixer's output for dumpsys |
| 2235 | Pipe *teeSink = new Pipe(TEE_SINK_FRAMES, format); |
| 2236 | numCounterOffers = 0; |
| 2237 | index = teeSink->negotiate(offers, 1, NULL, numCounterOffers); |
| 2238 | ALOG_ASSERT(index == 0); |
| 2239 | mTeeSink = teeSink; |
| 2240 | PipeReader *teeSource = new PipeReader(*teeSink); |
| 2241 | numCounterOffers = 0; |
| 2242 | index = teeSource->negotiate(offers, 1, NULL, numCounterOffers); |
| 2243 | ALOG_ASSERT(index == 0); |
| 2244 | mTeeSource = teeSource; |
| 2245 | #endif |
| 2246 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2247 | #ifdef SOAKER |
| 2248 | // create a soaker as workaround for governor issues |
| 2249 | mSoaker = new Soaker(); |
| 2250 | // FIXME Soaker should only run when needed, i.e. when FastMixer is not in COLD_IDLE |
| 2251 | mSoaker->run("Soaker", PRIORITY_LOWEST); |
| 2252 | #endif |
| 2253 | |
| 2254 | // create fast mixer and configure it initially with just one fast track for our submix |
| 2255 | mFastMixer = new FastMixer(); |
| 2256 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 2257 | FastMixerState *state = sq->begin(); |
| 2258 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 2259 | // wrap the source side of the MonoPipe to make it an AudioBufferProvider |
| 2260 | fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); |
| 2261 | fastTrack->mVolumeProvider = NULL; |
| 2262 | fastTrack->mGeneration++; |
| 2263 | state->mFastTracksGen++; |
| 2264 | state->mTrackMask = 1; |
| 2265 | // fast mixer will use the HAL output sink |
| 2266 | state->mOutputSink = mOutputSink.get(); |
| 2267 | state->mOutputSinkGen++; |
| 2268 | state->mFrameCount = mFrameCount; |
| 2269 | state->mCommand = FastMixerState::COLD_IDLE; |
| 2270 | // already done in constructor initialization list |
| 2271 | //mFastMixerFutex = 0; |
| 2272 | state->mColdFutexAddr = &mFastMixerFutex; |
| 2273 | state->mColdGen++; |
| 2274 | state->mDumpState = &mFastMixerDumpState; |
Glenn Kasten | fbae5da | 2012-05-21 09:17:20 -0700 | [diff] [blame] | 2275 | state->mTeeSink = mTeeSink.get(); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2276 | sq->end(); |
| 2277 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 2278 | |
| 2279 | // start the fast mixer |
| 2280 | mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); |
| 2281 | #ifdef HAVE_REQUEST_PRIORITY |
| 2282 | pid_t tid = mFastMixer->getTid(); |
| 2283 | int err = requestPriority(getpid_cached, tid, 2); |
| 2284 | if (err != 0) { |
| 2285 | ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", |
| 2286 | 2, getpid_cached, tid, err); |
| 2287 | } |
| 2288 | #endif |
| 2289 | |
| 2290 | } else { |
| 2291 | mFastMixer = NULL; |
| 2292 | } |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 2293 | |
| 2294 | switch (kUseFastMixer) { |
| 2295 | case FastMixer_Never: |
| 2296 | case FastMixer_Dynamic: |
| 2297 | mNormalSink = mOutputSink; |
| 2298 | break; |
| 2299 | case FastMixer_Always: |
| 2300 | mNormalSink = mPipeSink; |
| 2301 | break; |
| 2302 | case FastMixer_Static: |
| 2303 | mNormalSink = initFastMixer ? mPipeSink : mOutputSink; |
| 2304 | break; |
| 2305 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2306 | } |
| 2307 | |
| 2308 | AudioFlinger::MixerThread::~MixerThread() |
| 2309 | { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2310 | if (mFastMixer != NULL) { |
| 2311 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 2312 | FastMixerState *state = sq->begin(); |
| 2313 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
| 2314 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 2315 | if (old == -1) { |
| 2316 | __futex_syscall3(&mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
| 2317 | } |
| 2318 | } |
| 2319 | state->mCommand = FastMixerState::EXIT; |
| 2320 | sq->end(); |
| 2321 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 2322 | mFastMixer->join(); |
| 2323 | // Though the fast mixer thread has exited, it's state queue is still valid. |
| 2324 | // We'll use that extract the final state which contains one remaining fast track |
| 2325 | // corresponding to our sub-mix. |
| 2326 | state = sq->begin(); |
| 2327 | ALOG_ASSERT(state->mTrackMask == 1); |
| 2328 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 2329 | ALOG_ASSERT(fastTrack->mBufferProvider != NULL); |
| 2330 | delete fastTrack->mBufferProvider; |
| 2331 | sq->end(false /*didModify*/); |
| 2332 | delete mFastMixer; |
| 2333 | #ifdef SOAKER |
| 2334 | if (mSoaker != NULL) { |
| 2335 | mSoaker->requestExitAndWait(); |
| 2336 | } |
| 2337 | delete mSoaker; |
| 2338 | #endif |
| 2339 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2340 | delete mAudioMixer; |
| 2341 | } |
| 2342 | |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2343 | class CpuStats { |
| 2344 | public: |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2345 | CpuStats(); |
| 2346 | void sample(const String8 &title); |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2347 | #ifdef DEBUG_CPU_USAGE |
| 2348 | private: |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2349 | ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns |
| 2350 | CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns |
| 2351 | |
| 2352 | CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles |
| 2353 | |
| 2354 | int mCpuNum; // thread's current CPU number |
| 2355 | int mCpukHz; // frequency of thread's current CPU in kHz |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2356 | #endif |
| 2357 | }; |
| 2358 | |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2359 | CpuStats::CpuStats() |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2360 | #ifdef DEBUG_CPU_USAGE |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2361 | : mCpuNum(-1), mCpukHz(-1) |
| 2362 | #endif |
| 2363 | { |
| 2364 | } |
| 2365 | |
| 2366 | void CpuStats::sample(const String8 &title) { |
| 2367 | #ifdef DEBUG_CPU_USAGE |
| 2368 | // get current thread's delta CPU time in wall clock ns |
| 2369 | double wcNs; |
| 2370 | bool valid = mCpuUsage.sampleAndEnable(wcNs); |
| 2371 | |
| 2372 | // record sample for wall clock statistics |
| 2373 | if (valid) { |
| 2374 | mWcStats.sample(wcNs); |
| 2375 | } |
| 2376 | |
| 2377 | // get the current CPU number |
| 2378 | int cpuNum = sched_getcpu(); |
| 2379 | |
| 2380 | // get the current CPU frequency in kHz |
| 2381 | int cpukHz = mCpuUsage.getCpukHz(cpuNum); |
| 2382 | |
| 2383 | // check if either CPU number or frequency changed |
| 2384 | if (cpuNum != mCpuNum || cpukHz != mCpukHz) { |
| 2385 | mCpuNum = cpuNum; |
| 2386 | mCpukHz = cpukHz; |
| 2387 | // ignore sample for purposes of cycles |
| 2388 | valid = false; |
| 2389 | } |
| 2390 | |
| 2391 | // if no change in CPU number or frequency, then record sample for cycle statistics |
| 2392 | if (valid && mCpukHz > 0) { |
| 2393 | double cycles = wcNs * cpukHz * 0.000001; |
| 2394 | mHzStats.sample(cycles); |
| 2395 | } |
| 2396 | |
| 2397 | unsigned n = mWcStats.n(); |
| 2398 | // mCpuUsage.elapsed() is expensive, so don't call it every loop |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2399 | if ((n & 127) == 1) { |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2400 | long long elapsed = mCpuUsage.elapsed(); |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2401 | if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { |
| 2402 | double perLoop = elapsed / (double) n; |
| 2403 | double perLoop100 = perLoop * 0.01; |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2404 | double perLoop1k = perLoop * 0.001; |
| 2405 | double mean = mWcStats.mean(); |
| 2406 | double stddev = mWcStats.stddev(); |
| 2407 | double minimum = mWcStats.minimum(); |
| 2408 | double maximum = mWcStats.maximum(); |
| 2409 | double meanCycles = mHzStats.mean(); |
| 2410 | double stddevCycles = mHzStats.stddev(); |
| 2411 | double minCycles = mHzStats.minimum(); |
| 2412 | double maxCycles = mHzStats.maximum(); |
| 2413 | mCpuUsage.resetElapsed(); |
| 2414 | mWcStats.reset(); |
| 2415 | mHzStats.reset(); |
| 2416 | ALOGD("CPU usage for %s over past %.1f secs\n" |
| 2417 | " (%u mixer loops at %.1f mean ms per loop):\n" |
| 2418 | " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" |
| 2419 | " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" |
| 2420 | " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", |
| 2421 | title.string(), |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2422 | elapsed * .000000001, n, perLoop * .000001, |
| 2423 | mean * .001, |
| 2424 | stddev * .001, |
| 2425 | minimum * .001, |
| 2426 | maximum * .001, |
| 2427 | mean / perLoop100, |
| 2428 | stddev / perLoop100, |
| 2429 | minimum / perLoop100, |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2430 | maximum / perLoop100, |
| 2431 | meanCycles / perLoop1k, |
| 2432 | stddevCycles / perLoop1k, |
| 2433 | minCycles / perLoop1k, |
| 2434 | maxCycles / perLoop1k); |
| 2435 | |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2436 | } |
| 2437 | } |
| 2438 | #endif |
| 2439 | }; |
| 2440 | |
Glenn Kasten | 37d825e | 2012-02-24 07:21:48 -0800 | [diff] [blame] | 2441 | void AudioFlinger::PlaybackThread::checkSilentMode_l() |
| 2442 | { |
| 2443 | if (!mMasterMute) { |
| 2444 | char value[PROPERTY_VALUE_MAX]; |
| 2445 | if (property_get("ro.audio.silent", value, "0") > 0) { |
| 2446 | char *endptr; |
| 2447 | unsigned long ul = strtoul(value, &endptr, 0); |
| 2448 | if (*endptr == '\0' && ul != 0) { |
| 2449 | ALOGD("Silence is golden"); |
| 2450 | // The setprop command will not allow a property to be changed after |
| 2451 | // the first time it is set, so we don't have to worry about un-muting. |
| 2452 | setMasterMute_l(true); |
| 2453 | } |
| 2454 | } |
| 2455 | } |
| 2456 | } |
| 2457 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2458 | bool AudioFlinger::PlaybackThread::threadLoop() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2459 | { |
| 2460 | Vector< sp<Track> > tracksToRemove; |
Glenn Kasten | 688a640 | 2012-02-29 07:57:06 -0800 | [diff] [blame] | 2461 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2462 | standbyTime = systemTime(); |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2463 | |
| 2464 | // MIXER |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2465 | nsecs_t lastWarning = 0; |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2466 | if (mType == MIXER) { |
| 2467 | longStandbyExit = false; |
| 2468 | } |
Glenn Kasten | 688a640 | 2012-02-29 07:57:06 -0800 | [diff] [blame] | 2469 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2470 | // DUPLICATING |
| 2471 | // FIXME could this be made local to while loop? |
| 2472 | writeFrames = 0; |
Glenn Kasten | 688a640 | 2012-02-29 07:57:06 -0800 | [diff] [blame] | 2473 | |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 2474 | cacheParameters_l(); |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2475 | sleepTime = idleSleepTime; |
| 2476 | |
| 2477 | if (mType == MIXER) { |
| 2478 | sleepTimeShift = 0; |
| 2479 | } |
| 2480 | |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2481 | CpuStats cpuStats; |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2482 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2483 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2484 | acquireWakeLock(); |
| 2485 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2486 | while (!exitPending()) |
| 2487 | { |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2488 | cpuStats.sample(myName); |
Glenn Kasten | 688a640 | 2012-02-29 07:57:06 -0800 | [diff] [blame] | 2489 | |
Glenn Kasten | 73ca0f5 | 2012-02-29 07:56:15 -0800 | [diff] [blame] | 2490 | Vector< sp<EffectChain> > effectChains; |
| 2491 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2492 | processConfigEvents(); |
| 2493 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2494 | { // scope for mLock |
| 2495 | |
| 2496 | Mutex::Autolock _l(mLock); |
| 2497 | |
| 2498 | if (checkForNewParameters_l()) { |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 2499 | cacheParameters_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2500 | } |
| 2501 | |
Glenn Kasten | fa26a85 | 2012-03-06 11:28:04 -0800 | [diff] [blame] | 2502 | saveOutputTracks(); |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2503 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2504 | // put audio hardware into standby after short delay |
Glenn Kasten | 3e07470 | 2012-02-28 18:40:35 -0800 | [diff] [blame] | 2505 | if (CC_UNLIKELY((!mActiveTracks.size() && systemTime() > standbyTime) || |
Glenn Kasten | c455fe9 | 2012-02-29 07:07:30 -0800 | [diff] [blame] | 2506 | mSuspended > 0)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2507 | if (!mStandby) { |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2508 | |
| 2509 | threadLoop_standby(); |
| 2510 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2511 | mStandby = true; |
| 2512 | mBytesWritten = 0; |
| 2513 | } |
| 2514 | |
Glenn Kasten | 3e07470 | 2012-02-28 18:40:35 -0800 | [diff] [blame] | 2515 | if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2516 | // we're about to wait, flush the binder command buffer |
| 2517 | IPCThreadState::self()->flushCommands(); |
| 2518 | |
Glenn Kasten | fa26a85 | 2012-03-06 11:28:04 -0800 | [diff] [blame] | 2519 | clearOutputTracks(); |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2520 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2521 | if (exitPending()) break; |
| 2522 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2523 | releaseWakeLock_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2524 | // wait until we have something to do... |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2525 | ALOGV("%s going to sleep", myName.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2526 | mWaitWorkCV.wait(mLock); |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2527 | ALOGV("%s waking up", myName.string()); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2528 | acquireWakeLock_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2529 | |
Eric Laurent | da74744 | 2012-04-25 18:53:13 -0700 | [diff] [blame] | 2530 | mMixerStatus = MIXER_IDLE; |
Glenn Kasten | 8102804 | 2012-04-30 18:15:12 -0700 | [diff] [blame] | 2531 | mMixerStatusIgnoringFastTracks = MIXER_IDLE; |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2532 | |
Glenn Kasten | 37d825e | 2012-02-24 07:21:48 -0800 | [diff] [blame] | 2533 | checkSilentMode_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2534 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2535 | standbyTime = systemTime() + standbyDelay; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2536 | sleepTime = idleSleepTime; |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 2537 | if (mType == MIXER) { |
| 2538 | sleepTimeShift = 0; |
| 2539 | } |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2540 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2541 | continue; |
| 2542 | } |
| 2543 | } |
| 2544 | |
Glenn Kasten | 8102804 | 2012-04-30 18:15:12 -0700 | [diff] [blame] | 2545 | // mMixerStatusIgnoringFastTracks is also updated internally |
Eric Laurent | da74744 | 2012-04-25 18:53:13 -0700 | [diff] [blame] | 2546 | mMixerStatus = prepareTracks_l(&tracksToRemove); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2547 | |
| 2548 | // prevent any changes in effect chain list and in each effect chain |
| 2549 | // during mixing and effect process as the audio buffers could be deleted |
| 2550 | // or modified if an effect is created or deleted |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2551 | lockEffectChains_l(effectChains); |
Glenn Kasten | c5ac4cb | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 2552 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2553 | |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 2554 | if (CC_LIKELY(mMixerStatus == MIXER_TRACKS_READY)) { |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2555 | threadLoop_mix(); |
| 2556 | } else { |
| 2557 | threadLoop_sleepTime(); |
| 2558 | } |
| 2559 | |
| 2560 | if (mSuspended > 0) { |
| 2561 | sleepTime = suspendSleepTimeUs(); |
| 2562 | } |
| 2563 | |
| 2564 | // only process effects if we're going to write |
| 2565 | if (sleepTime == 0) { |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2566 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2567 | effectChains[i]->process_l(); |
| 2568 | } |
| 2569 | } |
| 2570 | |
| 2571 | // enable changes in effect chain |
| 2572 | unlockEffectChains(effectChains); |
| 2573 | |
| 2574 | // sleepTime == 0 means we must write to audio hardware |
| 2575 | if (sleepTime == 0) { |
| 2576 | |
| 2577 | threadLoop_write(); |
| 2578 | |
| 2579 | if (mType == MIXER) { |
| 2580 | // write blocked detection |
| 2581 | nsecs_t now = systemTime(); |
| 2582 | nsecs_t delta = now - mLastWriteTime; |
| 2583 | if (!mStandby && delta > maxPeriod) { |
| 2584 | mNumDelayedWrites++; |
| 2585 | if ((now - lastWarning) > kWarningThrottleNs) { |
Glenn Kasten | 99c99d0 | 2012-05-14 16:37:13 -0700 | [diff] [blame] | 2586 | #if defined(ATRACE_TAG) && (ATRACE_TAG != ATRACE_TAG_NEVER) |
Glenn Kasten | d8e6fd3 | 2012-05-07 11:07:57 -0700 | [diff] [blame] | 2587 | ScopedTrace st(ATRACE_TAG, "underrun"); |
Glenn Kasten | 99c99d0 | 2012-05-14 16:37:13 -0700 | [diff] [blame] | 2588 | #endif |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2589 | ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", |
| 2590 | ns2ms(delta), mNumDelayedWrites, this); |
| 2591 | lastWarning = now; |
| 2592 | } |
| 2593 | // FIXME this is broken: longStandbyExit should be handled out of the if() and with |
| 2594 | // a different threshold. Or completely removed for what it is worth anyway... |
| 2595 | if (mStandby) { |
| 2596 | longStandbyExit = true; |
| 2597 | } |
| 2598 | } |
| 2599 | } |
| 2600 | |
| 2601 | mStandby = false; |
| 2602 | } else { |
| 2603 | usleep(sleepTime); |
| 2604 | } |
| 2605 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2606 | // Finally let go of removed track(s), without the lock held |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2607 | // since we can't guarantee the destructors won't acquire that |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2608 | // same lock. This will also mutate and push a new fast mixer state. |
| 2609 | threadLoop_removeTracks(tracksToRemove); |
Glenn Kasten | 1465f0c | 2012-03-06 11:23:32 -0800 | [diff] [blame] | 2610 | tracksToRemove.clear(); |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2611 | |
Glenn Kasten | fa26a85 | 2012-03-06 11:28:04 -0800 | [diff] [blame] | 2612 | // FIXME I don't understand the need for this here; |
| 2613 | // it was in the original code but maybe the |
| 2614 | // assignment in saveOutputTracks() makes this unnecessary? |
| 2615 | clearOutputTracks(); |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2616 | |
| 2617 | // Effect chains will be actually deleted here if they were removed from |
| 2618 | // mEffectChains list during mixing or effects processing |
| 2619 | effectChains.clear(); |
| 2620 | |
| 2621 | // FIXME Note that the above .clear() is no longer necessary since effectChains |
| 2622 | // is now local to this block, but will keep it for now (at least until merge done). |
| 2623 | } |
| 2624 | |
| 2625 | if (mType == MIXER || mType == DIRECT) { |
| 2626 | // put output stream into standby mode |
| 2627 | if (!mStandby) { |
| 2628 | mOutput->stream->common.standby(&mOutput->stream->common); |
| 2629 | } |
| 2630 | } |
| 2631 | if (mType == DUPLICATING) { |
| 2632 | // for DuplicatingThread, standby mode is handled by the outputTracks |
| 2633 | } |
| 2634 | |
| 2635 | releaseWakeLock(); |
| 2636 | |
| 2637 | ALOGV("Thread %p type %d exiting", this, mType); |
| 2638 | return false; |
| 2639 | } |
| 2640 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2641 | void AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) |
| 2642 | { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2643 | PlaybackThread::threadLoop_removeTracks(tracksToRemove); |
| 2644 | } |
| 2645 | |
| 2646 | void AudioFlinger::MixerThread::threadLoop_write() |
| 2647 | { |
| 2648 | // FIXME we should only do one push per cycle; confirm this is true |
| 2649 | // Start the fast mixer if it's not already running |
| 2650 | if (mFastMixer != NULL) { |
| 2651 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 2652 | FastMixerState *state = sq->begin(); |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 2653 | if (state->mCommand != FastMixerState::MIX_WRITE && |
| 2654 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2655 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
| 2656 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 2657 | if (old == -1) { |
| 2658 | __futex_syscall3(&mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
| 2659 | } |
| 2660 | } |
| 2661 | state->mCommand = FastMixerState::MIX_WRITE; |
| 2662 | sq->end(); |
| 2663 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 2664 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 2665 | mNormalSink = mPipeSink; |
| 2666 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2667 | } else { |
| 2668 | sq->end(false /*didModify*/); |
| 2669 | } |
| 2670 | } |
| 2671 | PlaybackThread::threadLoop_write(); |
| 2672 | } |
| 2673 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2674 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
| 2675 | void AudioFlinger::PlaybackThread::threadLoop_write() |
| 2676 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2677 | // FIXME rewrite to reduce number of system calls |
| 2678 | mLastWriteTime = systemTime(); |
| 2679 | mInWrite = true; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2680 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2681 | #define mBitShift 2 // FIXME |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 2682 | size_t count = mixBufferSize >> mBitShift; |
Glenn Kasten | 99c99d0 | 2012-05-14 16:37:13 -0700 | [diff] [blame] | 2683 | #if defined(ATRACE_TAG) && (ATRACE_TAG != ATRACE_TAG_NEVER) |
Glenn Kasten | d8e6fd3 | 2012-05-07 11:07:57 -0700 | [diff] [blame] | 2684 | Tracer::traceBegin(ATRACE_TAG, "write"); |
Glenn Kasten | 99c99d0 | 2012-05-14 16:37:13 -0700 | [diff] [blame] | 2685 | #endif |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 2686 | ssize_t framesWritten = mNormalSink->write(mMixBuffer, count); |
Glenn Kasten | 99c99d0 | 2012-05-14 16:37:13 -0700 | [diff] [blame] | 2687 | #if defined(ATRACE_TAG) && (ATRACE_TAG != ATRACE_TAG_NEVER) |
Glenn Kasten | d8e6fd3 | 2012-05-07 11:07:57 -0700 | [diff] [blame] | 2688 | Tracer::traceEnd(ATRACE_TAG); |
Glenn Kasten | 99c99d0 | 2012-05-14 16:37:13 -0700 | [diff] [blame] | 2689 | #endif |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 2690 | if (framesWritten > 0) { |
| 2691 | size_t bytesWritten = framesWritten << mBitShift; |
| 2692 | mBytesWritten += bytesWritten; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2693 | } |
| 2694 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2695 | mNumWrites++; |
| 2696 | mInWrite = false; |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2697 | } |
| 2698 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2699 | void AudioFlinger::MixerThread::threadLoop_standby() |
| 2700 | { |
| 2701 | // Idle the fast mixer if it's currently running |
| 2702 | if (mFastMixer != NULL) { |
| 2703 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 2704 | FastMixerState *state = sq->begin(); |
| 2705 | if (!(state->mCommand & FastMixerState::IDLE)) { |
| 2706 | state->mCommand = FastMixerState::COLD_IDLE; |
| 2707 | state->mColdFutexAddr = &mFastMixerFutex; |
| 2708 | state->mColdGen++; |
| 2709 | mFastMixerFutex = 0; |
| 2710 | sq->end(); |
| 2711 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 2712 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 2713 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 2714 | mNormalSink = mOutputSink; |
| 2715 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2716 | } else { |
| 2717 | sq->end(false /*didModify*/); |
| 2718 | } |
| 2719 | } |
| 2720 | PlaybackThread::threadLoop_standby(); |
| 2721 | } |
| 2722 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2723 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
| 2724 | void AudioFlinger::PlaybackThread::threadLoop_standby() |
| 2725 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2726 | ALOGV("Audio hardware entering standby, mixer %p, suspend count %u", this, mSuspended); |
| 2727 | mOutput->stream->common.standby(&mOutput->stream->common); |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2728 | } |
| 2729 | |
| 2730 | void AudioFlinger::MixerThread::threadLoop_mix() |
| 2731 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2732 | // obtain the presentation timestamp of the next output buffer |
| 2733 | int64_t pts; |
| 2734 | status_t status = INVALID_OPERATION; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 2735 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2736 | if (NULL != mOutput->stream->get_next_write_timestamp) { |
| 2737 | status = mOutput->stream->get_next_write_timestamp( |
| 2738 | mOutput->stream, &pts); |
| 2739 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 2740 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2741 | if (status != NO_ERROR) { |
| 2742 | pts = AudioBufferProvider::kInvalidPTS; |
| 2743 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 2744 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2745 | // mix buffers... |
| 2746 | mAudioMixer->process(pts); |
| 2747 | // increase sleep time progressively when application underrun condition clears. |
| 2748 | // Only increase sleep time if the mixer is ready for two consecutive times to avoid |
| 2749 | // that a steady state of alternating ready/not ready conditions keeps the sleep time |
| 2750 | // such that we would underrun the audio HAL. |
| 2751 | if ((sleepTime == 0) && (sleepTimeShift > 0)) { |
| 2752 | sleepTimeShift--; |
| 2753 | } |
| 2754 | sleepTime = 0; |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 2755 | standbyTime = systemTime() + standbyDelay; |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2756 | //TODO: delay standby when effects have a tail |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2757 | } |
| 2758 | |
| 2759 | void AudioFlinger::MixerThread::threadLoop_sleepTime() |
| 2760 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2761 | // If no tracks are ready, sleep once for the duration of an output |
| 2762 | // buffer size, then write 0s to the output |
| 2763 | if (sleepTime == 0) { |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 2764 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2765 | sleepTime = activeSleepTime >> sleepTimeShift; |
| 2766 | if (sleepTime < kMinThreadSleepTimeUs) { |
| 2767 | sleepTime = kMinThreadSleepTimeUs; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2768 | } |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2769 | // reduce sleep time in case of consecutive application underruns to avoid |
| 2770 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer |
| 2771 | // duration we would end up writing less data than needed by the audio HAL if |
| 2772 | // the condition persists. |
| 2773 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { |
| 2774 | sleepTimeShift++; |
| 2775 | } |
| 2776 | } else { |
| 2777 | sleepTime = idleSleepTime; |
| 2778 | } |
| 2779 | } else if (mBytesWritten != 0 || |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 2780 | (mMixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)) { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2781 | memset (mMixBuffer, 0, mixBufferSize); |
| 2782 | sleepTime = 0; |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 2783 | ALOGV_IF((mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)), "anticipated start"); |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2784 | } |
| 2785 | // TODO add standby time extension fct of effect tail |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2786 | } |
| 2787 | |
| 2788 | // prepareTracks_l() must be called with ThreadBase::mLock held |
Glenn Kasten | 29c23c3 | 2012-01-26 13:37:52 -0800 | [diff] [blame] | 2789 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( |
Glenn Kasten | 3e07470 | 2012-02-28 18:40:35 -0800 | [diff] [blame] | 2790 | Vector< sp<Track> > *tracksToRemove) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2791 | { |
| 2792 | |
Glenn Kasten | 29c23c3 | 2012-01-26 13:37:52 -0800 | [diff] [blame] | 2793 | mixer_state mixerStatus = MIXER_IDLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2794 | // find out which tracks need to be processed |
Glenn Kasten | 3e07470 | 2012-02-28 18:40:35 -0800 | [diff] [blame] | 2795 | size_t count = mActiveTracks.size(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2796 | size_t mixedTracks = 0; |
| 2797 | size_t tracksWithEffect = 0; |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 2798 | // counts only _active_ fast tracks |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2799 | size_t fastTracks = 0; |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 2800 | uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2801 | |
| 2802 | float masterVolume = mMasterVolume; |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 2803 | bool masterMute = mMasterMute; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2804 | |
Eric Laurent | 571d49c | 2010-08-11 05:20:11 -0700 | [diff] [blame] | 2805 | if (masterMute) { |
| 2806 | masterVolume = 0; |
| 2807 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2808 | // Delegate master volume control to effect in output mix effect chain if needed |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2809 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2810 | if (chain != 0) { |
Eric Laurent | 571d49c | 2010-08-11 05:20:11 -0700 | [diff] [blame] | 2811 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2812 | chain->setVolume_l(&v, &v); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2813 | masterVolume = (float)((v + (1 << 23)) >> 24); |
| 2814 | chain.clear(); |
| 2815 | } |
| 2816 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 2817 | // prepare a new state to push |
| 2818 | FastMixerStateQueue *sq = NULL; |
| 2819 | FastMixerState *state = NULL; |
| 2820 | bool didModify = false; |
| 2821 | FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; |
| 2822 | if (mFastMixer != NULL) { |
| 2823 | sq = mFastMixer->sq(); |
| 2824 | state = sq->begin(); |
| 2825 | } |
| 2826 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2827 | for (size_t i=0 ; i<count ; i++) { |
Glenn Kasten | 3e07470 | 2012-02-28 18:40:35 -0800 | [diff] [blame] | 2828 | sp<Track> t = mActiveTracks[i].promote(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2829 | if (t == 0) continue; |
| 2830 | |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2831 | // this const just means the local variable doesn't change |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2832 | Track* const track = t.get(); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2833 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 2834 | // process fast tracks |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2835 | if (track->isFastTrack()) { |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 2836 | |
| 2837 | // It's theoretically possible (though unlikely) for a fast track to be created |
| 2838 | // and then removed within the same normal mix cycle. This is not a problem, as |
| 2839 | // the track never becomes active so it's fast mixer slot is never touched. |
| 2840 | // The converse, of removing an (active) track and then creating a new track |
| 2841 | // at the identical fast mixer slot within the same normal mix cycle, |
| 2842 | // is impossible because the slot isn't marked available until the end of each cycle. |
| 2843 | int j = track->mFastIndex; |
| 2844 | FastTrack *fastTrack = &state->mFastTracks[j]; |
| 2845 | |
| 2846 | // Determine whether the track is currently in underrun condition, |
| 2847 | // and whether it had a recent underrun. |
Glenn Kasten | 09474df | 2012-05-10 14:48:07 -0700 | [diff] [blame] | 2848 | FastTrackUnderruns underruns = mFastMixerDumpState.mTracks[j].mUnderruns; |
| 2849 | uint32_t recentFull = (underruns.mBitFields.mFull - |
| 2850 | track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK; |
| 2851 | uint32_t recentPartial = (underruns.mBitFields.mPartial - |
| 2852 | track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK; |
| 2853 | uint32_t recentEmpty = (underruns.mBitFields.mEmpty - |
| 2854 | track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK; |
| 2855 | uint32_t recentUnderruns = recentPartial + recentEmpty; |
| 2856 | track->mObservedUnderruns = underruns; |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 2857 | // don't count underruns that occur while stopping or pausing |
Glenn Kasten | d08f48c | 2012-05-01 18:14:02 -0700 | [diff] [blame] | 2858 | // or stopped which can occur when flush() is called while active |
| 2859 | if (!(track->isStopping() || track->isPausing() || track->isStopped())) { |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 2860 | track->mUnderrunCount += recentUnderruns; |
| 2861 | } |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 2862 | |
Glenn Kasten | d08f48c | 2012-05-01 18:14:02 -0700 | [diff] [blame] | 2863 | // This is similar to the state machine for normal tracks, |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 2864 | // with a few modifications for fast tracks. |
Glenn Kasten | d08f48c | 2012-05-01 18:14:02 -0700 | [diff] [blame] | 2865 | bool isActive = true; |
| 2866 | switch (track->mState) { |
| 2867 | case TrackBase::STOPPING_1: |
| 2868 | // track stays active in STOPPING_1 state until first underrun |
| 2869 | if (recentUnderruns > 0) { |
| 2870 | track->mState = TrackBase::STOPPING_2; |
| 2871 | } |
| 2872 | break; |
| 2873 | case TrackBase::PAUSING: |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 2874 | // ramp down is not yet implemented |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 2875 | track->setPaused(); |
Glenn Kasten | d08f48c | 2012-05-01 18:14:02 -0700 | [diff] [blame] | 2876 | break; |
| 2877 | case TrackBase::RESUMING: |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 2878 | // ramp up is not yet implemented |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 2879 | track->mState = TrackBase::ACTIVE; |
Glenn Kasten | d08f48c | 2012-05-01 18:14:02 -0700 | [diff] [blame] | 2880 | break; |
| 2881 | case TrackBase::ACTIVE: |
Glenn Kasten | 09474df | 2012-05-10 14:48:07 -0700 | [diff] [blame] | 2882 | if (recentFull > 0 || recentPartial > 0) { |
| 2883 | // track has provided at least some frames recently: reset retry count |
| 2884 | track->mRetryCount = kMaxTrackRetries; |
| 2885 | } |
| 2886 | if (recentUnderruns == 0) { |
| 2887 | // no recent underruns: stay active |
| 2888 | break; |
| 2889 | } |
| 2890 | // there has recently been an underrun of some kind |
| 2891 | if (track->sharedBuffer() == 0) { |
| 2892 | // were any of the recent underruns "empty" (no frames available)? |
| 2893 | if (recentEmpty == 0) { |
| 2894 | // no, then ignore the partial underruns as they are allowed indefinitely |
| 2895 | break; |
| 2896 | } |
| 2897 | // there has recently been an "empty" underrun: decrement the retry counter |
| 2898 | if (--(track->mRetryCount) > 0) { |
| 2899 | break; |
| 2900 | } |
| 2901 | // indicate to client process that the track was disabled because of underrun; |
| 2902 | // it will then automatically call start() when data is available |
| 2903 | android_atomic_or(CBLK_DISABLED_ON, &track->mCblk->flags); |
| 2904 | // remove from active list, but state remains ACTIVE [confusing but true] |
| 2905 | isActive = false; |
Glenn Kasten | d08f48c | 2012-05-01 18:14:02 -0700 | [diff] [blame] | 2906 | break; |
| 2907 | } |
| 2908 | // fall through |
| 2909 | case TrackBase::STOPPING_2: |
| 2910 | case TrackBase::PAUSED: |
| 2911 | case TrackBase::TERMINATED: |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 2912 | case TrackBase::STOPPED: |
| 2913 | case TrackBase::FLUSHED: // flush() while active |
Glenn Kasten | d08f48c | 2012-05-01 18:14:02 -0700 | [diff] [blame] | 2914 | // Check for presentation complete if track is inactive |
| 2915 | // We have consumed all the buffers of this track. |
| 2916 | // This would be incomplete if we auto-paused on underrun |
| 2917 | { |
| 2918 | size_t audioHALFrames = |
| 2919 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
| 2920 | size_t framesWritten = |
| 2921 | mBytesWritten / audio_stream_frame_size(&mOutput->stream->common); |
| 2922 | if (!track->presentationComplete(framesWritten, audioHALFrames)) { |
| 2923 | // track stays in active list until presentation is complete |
| 2924 | break; |
| 2925 | } |
| 2926 | } |
| 2927 | if (track->isStopping_2()) { |
| 2928 | track->mState = TrackBase::STOPPED; |
| 2929 | } |
| 2930 | if (track->isStopped()) { |
| 2931 | // Can't reset directly, as fast mixer is still polling this track |
| 2932 | // track->reset(); |
| 2933 | // So instead mark this track as needing to be reset after push with ack |
| 2934 | resetMask |= 1 << i; |
| 2935 | } |
| 2936 | isActive = false; |
| 2937 | break; |
| 2938 | case TrackBase::IDLE: |
| 2939 | default: |
| 2940 | LOG_FATAL("unexpected track state %d", track->mState); |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 2941 | } |
| 2942 | |
| 2943 | if (isActive) { |
| 2944 | // was it previously inactive? |
| 2945 | if (!(state->mTrackMask & (1 << j))) { |
| 2946 | ExtendedAudioBufferProvider *eabp = track; |
| 2947 | VolumeProvider *vp = track; |
| 2948 | fastTrack->mBufferProvider = eabp; |
| 2949 | fastTrack->mVolumeProvider = vp; |
| 2950 | fastTrack->mSampleRate = track->mSampleRate; |
| 2951 | fastTrack->mChannelMask = track->mChannelMask; |
| 2952 | fastTrack->mGeneration++; |
| 2953 | state->mTrackMask |= 1 << j; |
| 2954 | didModify = true; |
| 2955 | // no acknowledgement required for newly active tracks |
| 2956 | } |
| 2957 | // cache the combined master volume and stream type volume for fast mixer; this |
| 2958 | // lacks any synchronization or barrier so VolumeProvider may read a stale value |
| 2959 | track->mCachedVolume = track->isMuted() ? |
| 2960 | 0 : masterVolume * mStreamTypes[track->streamType()].volume; |
| 2961 | ++fastTracks; |
| 2962 | } else { |
| 2963 | // was it previously active? |
| 2964 | if (state->mTrackMask & (1 << j)) { |
| 2965 | fastTrack->mBufferProvider = NULL; |
| 2966 | fastTrack->mGeneration++; |
| 2967 | state->mTrackMask &= ~(1 << j); |
| 2968 | didModify = true; |
| 2969 | // If any fast tracks were removed, we must wait for acknowledgement |
| 2970 | // because we're about to decrement the last sp<> on those tracks. |
| 2971 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
Glenn Kasten | d08f48c | 2012-05-01 18:14:02 -0700 | [diff] [blame] | 2972 | } else { |
| 2973 | LOG_FATAL("fast track %d should have been active", j); |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 2974 | } |
Glenn Kasten | d08f48c | 2012-05-01 18:14:02 -0700 | [diff] [blame] | 2975 | tracksToRemove->add(track); |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 2976 | // Avoids a misleading display in dumpsys |
Glenn Kasten | 09474df | 2012-05-10 14:48:07 -0700 | [diff] [blame] | 2977 | track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2978 | } |
| 2979 | continue; |
| 2980 | } |
| 2981 | |
| 2982 | { // local variable scope to avoid goto warning |
| 2983 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2984 | audio_track_cblk_t* cblk = track->cblk(); |
| 2985 | |
| 2986 | // The first time a track is added we wait |
| 2987 | // for all its buffers to be filled before processing it |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2988 | int name = track->name(); |
Eric Laurent | a47b69c | 2011-11-08 18:10:16 -0800 | [diff] [blame] | 2989 | // make sure that we have enough frames to mix one full buffer. |
| 2990 | // enforce this condition only once to enable draining the buffer in case the client |
| 2991 | // app does not call stop() and relies on underrun to stop: |
Eric Laurent | da74744 | 2012-04-25 18:53:13 -0700 | [diff] [blame] | 2992 | // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed |
Eric Laurent | a47b69c | 2011-11-08 18:10:16 -0800 | [diff] [blame] | 2993 | // during last round |
Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2994 | uint32_t minFrames = 1; |
Eric Laurent | 83faee0 | 2012-04-27 18:24:29 -0700 | [diff] [blame] | 2995 | if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && |
Glenn Kasten | 8102804 | 2012-04-30 18:15:12 -0700 | [diff] [blame] | 2996 | (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { |
Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2997 | if (t->sampleRate() == (int)mSampleRate) { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2998 | minFrames = mNormalFrameCount; |
Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2999 | } else { |
Eric Laurent | 071ccd5 | 2011-12-22 16:08:41 -0800 | [diff] [blame] | 3000 | // +1 for rounding and +1 for additional sample needed for interpolation |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3001 | minFrames = (mNormalFrameCount * t->sampleRate()) / mSampleRate + 1 + 1; |
Eric Laurent | 071ccd5 | 2011-12-22 16:08:41 -0800 | [diff] [blame] | 3002 | // add frames already consumed but not yet released by the resampler |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 3003 | // because cblk->framesReady() will include these frames |
Eric Laurent | 071ccd5 | 2011-12-22 16:08:41 -0800 | [diff] [blame] | 3004 | minFrames += mAudioMixer->getUnreleasedFrames(track->name()); |
| 3005 | // the minimum track buffer size is normally twice the number of frames necessary |
| 3006 | // to fill one buffer and the resampler should not leave more than one buffer worth |
| 3007 | // of unreleased frames after each pass, but just in case... |
Steve Block | c1dc1cb | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3008 | ALOG_ASSERT(minFrames <= cblk->frameCount); |
Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 3009 | } |
| 3010 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 3011 | if ((track->framesReady() >= minFrames) && track->isReady() && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3012 | !track->isPaused() && !track->isTerminated()) |
| 3013 | { |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 3014 | //ALOGV("track %d u=%08x, s=%08x [OK] on thread %p", name, cblk->user, cblk->server, this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3015 | |
| 3016 | mixedTracks++; |
| 3017 | |
| 3018 | // track->mainBuffer() != mMixBuffer means there is an effect chain |
| 3019 | // connected to the track |
| 3020 | chain.clear(); |
| 3021 | if (track->mainBuffer() != mMixBuffer) { |
| 3022 | chain = getEffectChain_l(track->sessionId()); |
| 3023 | // Delegate volume control to effect in track effect chain if needed |
| 3024 | if (chain != 0) { |
| 3025 | tracksWithEffect++; |
| 3026 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3027 | ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on session %d", |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 3028 | name, track->sessionId()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3029 | } |
| 3030 | } |
| 3031 | |
| 3032 | |
| 3033 | int param = AudioMixer::VOLUME; |
| 3034 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 3035 | // no ramp for the first volume setting |
| 3036 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 3037 | if (track->mState == TrackBase::RESUMING) { |
| 3038 | track->mState = TrackBase::ACTIVE; |
| 3039 | param = AudioMixer::RAMP_VOLUME; |
| 3040 | } |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 3041 | mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3042 | } else if (cblk->server != 0) { |
| 3043 | // If the track is stopped before the first frame was mixed, |
| 3044 | // do not apply ramp |
| 3045 | param = AudioMixer::RAMP_VOLUME; |
| 3046 | } |
| 3047 | |
| 3048 | // compute volume for this track |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 3049 | uint32_t vl, vr, va; |
Eric Laurent | 8569f0d | 2010-07-29 23:43:43 -0700 | [diff] [blame] | 3050 | if (track->isMuted() || track->isPausing() || |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 3051 | mStreamTypes[track->streamType()].mute) { |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 3052 | vl = vr = va = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3053 | if (track->isPausing()) { |
| 3054 | track->setPaused(); |
| 3055 | } |
| 3056 | } else { |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 3057 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3058 | // read original volumes with volume control |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 3059 | float typeVolume = mStreamTypes[track->streamType()].volume; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3060 | float v = masterVolume * typeVolume; |
Glenn Kasten | 83d8653 | 2012-01-17 14:39:34 -0800 | [diff] [blame] | 3061 | uint32_t vlr = cblk->getVolumeLR(); |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 3062 | vl = vlr & 0xFFFF; |
| 3063 | vr = vlr >> 16; |
| 3064 | // track volumes come from shared memory, so can't be trusted and must be clamped |
| 3065 | if (vl > MAX_GAIN_INT) { |
| 3066 | ALOGV("Track left volume out of range: %04X", vl); |
| 3067 | vl = MAX_GAIN_INT; |
| 3068 | } |
| 3069 | if (vr > MAX_GAIN_INT) { |
| 3070 | ALOGV("Track right volume out of range: %04X", vr); |
| 3071 | vr = MAX_GAIN_INT; |
| 3072 | } |
| 3073 | // now apply the master volume and stream type volume |
| 3074 | vl = (uint32_t)(v * vl) << 12; |
| 3075 | vr = (uint32_t)(v * vr) << 12; |
| 3076 | // assuming master volume and stream type volume each go up to 1.0, |
| 3077 | // vl and vr are now in 8.24 format |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3078 | |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 3079 | uint16_t sendLevel = cblk->getSendLevel_U4_12(); |
| 3080 | // send level comes from shared memory and so may be corrupt |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 3081 | if (sendLevel > MAX_GAIN_INT) { |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 3082 | ALOGV("Track send level out of range: %04X", sendLevel); |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 3083 | sendLevel = MAX_GAIN_INT; |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 3084 | } |
| 3085 | va = (uint32_t)(v * sendLevel); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3086 | } |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 3087 | // Delegate volume control to effect in track effect chain if needed |
| 3088 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { |
| 3089 | // Do not ramp volume if volume is controlled by effect |
| 3090 | param = AudioMixer::VOLUME; |
| 3091 | track->mHasVolumeController = true; |
| 3092 | } else { |
| 3093 | // force no volume ramp when volume controller was just disabled or removed |
| 3094 | // from effect chain to avoid volume spike |
| 3095 | if (track->mHasVolumeController) { |
| 3096 | param = AudioMixer::VOLUME; |
| 3097 | } |
| 3098 | track->mHasVolumeController = false; |
| 3099 | } |
| 3100 | |
| 3101 | // Convert volumes from 8.24 to 4.12 format |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 3102 | // This additional clamping is needed in case chain->setVolume_l() overshot |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 3103 | vl = (vl + (1 << 11)) >> 12; |
| 3104 | if (vl > MAX_GAIN_INT) vl = MAX_GAIN_INT; |
| 3105 | vr = (vr + (1 << 11)) >> 12; |
| 3106 | if (vr > MAX_GAIN_INT) vr = MAX_GAIN_INT; |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 3107 | |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 3108 | if (va > MAX_GAIN_INT) va = MAX_GAIN_INT; // va is uint32_t, so no need to check for - |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3109 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3110 | // XXX: these things DON'T need to be done each time |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 3111 | mAudioMixer->setBufferProvider(name, track); |
| 3112 | mAudioMixer->enable(name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3113 | |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 3114 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, (void *)vl); |
| 3115 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, (void *)vr); |
| 3116 | mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, (void *)va); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3117 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 3118 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3119 | AudioMixer::TRACK, |
| 3120 | AudioMixer::FORMAT, (void *)track->format()); |
| 3121 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 3122 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3123 | AudioMixer::TRACK, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3124 | AudioMixer::CHANNEL_MASK, (void *)track->channelMask()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3125 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 3126 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3127 | AudioMixer::RESAMPLE, |
| 3128 | AudioMixer::SAMPLE_RATE, |
| 3129 | (void *)(cblk->sampleRate)); |
| 3130 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 3131 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3132 | AudioMixer::TRACK, |
| 3133 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); |
| 3134 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 3135 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3136 | AudioMixer::TRACK, |
| 3137 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); |
| 3138 | |
| 3139 | // reset retry count |
| 3140 | track->mRetryCount = kMaxTrackRetries; |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 3141 | |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 3142 | // If one track is ready, set the mixer ready if: |
| 3143 | // - the mixer was not ready during previous round OR |
| 3144 | // - no other track is not ready |
Glenn Kasten | 8102804 | 2012-04-30 18:15:12 -0700 | [diff] [blame] | 3145 | if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 3146 | mixerStatus != MIXER_TRACKS_ENABLED) { |
| 3147 | mixerStatus = MIXER_TRACKS_READY; |
| 3148 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3149 | } else { |
Eric Laurent | 91b14c4 | 2012-05-30 12:30:29 -0700 | [diff] [blame^] | 3150 | // clear effect chain input buffer if an active track underruns to avoid sending |
| 3151 | // previous audio buffer again to effects |
| 3152 | chain = getEffectChain_l(track->sessionId()); |
| 3153 | if (chain != 0) { |
| 3154 | chain->clearInputBuffer(); |
| 3155 | } |
| 3156 | |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 3157 | //ALOGV("track %d u=%08x, s=%08x [NOT READY] on thread %p", name, cblk->user, cblk->server, this); |
Eric Laurent | 83faee0 | 2012-04-27 18:24:29 -0700 | [diff] [blame] | 3158 | if ((track->sharedBuffer() != 0) || track->isTerminated() || |
| 3159 | track->isStopped() || track->isPaused()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3160 | // We have consumed all the buffers of this track. |
| 3161 | // Remove it from the list of active tracks. |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 3162 | // TODO: use actual buffer filling status instead of latency when available from |
| 3163 | // audio HAL |
| 3164 | size_t audioHALFrames = |
| 3165 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
| 3166 | size_t framesWritten = |
| 3167 | mBytesWritten / audio_stream_frame_size(&mOutput->stream->common); |
| 3168 | if (track->presentationComplete(framesWritten, audioHALFrames)) { |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 3169 | if (track->isStopped()) { |
| 3170 | track->reset(); |
| 3171 | } |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 3172 | tracksToRemove->add(track); |
| 3173 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3174 | } else { |
| 3175 | // No buffers for this track. Give it a few chances to |
| 3176 | // fill a buffer, then remove it from active list. |
| 3177 | if (--(track->mRetryCount) <= 0) { |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 3178 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list on thread %p", name, this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3179 | tracksToRemove->add(track); |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 3180 | // indicate to client process that the track was disabled because of underrun; |
| 3181 | // it will then automatically call start() when data is available |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3182 | android_atomic_or(CBLK_DISABLED_ON, &cblk->flags); |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 3183 | // If one track is not ready, mark the mixer also not ready if: |
| 3184 | // - the mixer was ready during previous round OR |
| 3185 | // - no other track is ready |
Glenn Kasten | 8102804 | 2012-04-30 18:15:12 -0700 | [diff] [blame] | 3186 | } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 3187 | mixerStatus != MIXER_TRACKS_READY) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3188 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 3189 | } |
| 3190 | } |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 3191 | mAudioMixer->disable(name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3192 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3193 | |
| 3194 | } // local variable scope to avoid goto warning |
| 3195 | track_is_ready: ; |
| 3196 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3197 | } |
| 3198 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 3199 | // Push the new FastMixer state if necessary |
| 3200 | if (didModify) { |
| 3201 | state->mFastTracksGen++; |
| 3202 | // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle |
| 3203 | if (kUseFastMixer == FastMixer_Dynamic && |
| 3204 | state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { |
| 3205 | state->mCommand = FastMixerState::COLD_IDLE; |
| 3206 | state->mColdFutexAddr = &mFastMixerFutex; |
| 3207 | state->mColdGen++; |
| 3208 | mFastMixerFutex = 0; |
| 3209 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 3210 | mNormalSink = mOutputSink; |
| 3211 | } |
| 3212 | // If we go into cold idle, need to wait for acknowledgement |
| 3213 | // so that fast mixer stops doing I/O. |
| 3214 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 3215 | } |
| 3216 | sq->end(); |
| 3217 | } |
| 3218 | if (sq != NULL) { |
| 3219 | sq->end(didModify); |
| 3220 | sq->push(block); |
| 3221 | } |
| 3222 | |
| 3223 | // Now perform the deferred reset on fast tracks that have stopped |
| 3224 | while (resetMask != 0) { |
| 3225 | size_t i = __builtin_ctz(resetMask); |
| 3226 | ALOG_ASSERT(i < count); |
| 3227 | resetMask &= ~(1 << i); |
| 3228 | sp<Track> t = mActiveTracks[i].promote(); |
| 3229 | if (t == 0) continue; |
| 3230 | Track* track = t.get(); |
| 3231 | ALOG_ASSERT(track->isFastTrack() && track->isStopped()); |
| 3232 | track->reset(); |
| 3233 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3234 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3235 | // remove all the tracks that need to be... |
| 3236 | count = tracksToRemove->size(); |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 3237 | if (CC_UNLIKELY(count)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3238 | for (size_t i=0 ; i<count ; i++) { |
| 3239 | const sp<Track>& track = tracksToRemove->itemAt(i); |
| 3240 | mActiveTracks.remove(track); |
| 3241 | if (track->mainBuffer() != mMixBuffer) { |
| 3242 | chain = getEffectChain_l(track->sessionId()); |
| 3243 | if (chain != 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3244 | ALOGV("stopping track on chain %p for session Id: %d", chain.get(), track->sessionId()); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 3245 | chain->decActiveTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3246 | } |
| 3247 | } |
| 3248 | if (track->isTerminated()) { |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 3249 | removeTrack_l(track); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3250 | } |
| 3251 | } |
| 3252 | } |
| 3253 | |
| 3254 | // mix buffer must be cleared if all tracks are connected to an |
| 3255 | // effect chain as in this case the mixer will not write to |
| 3256 | // mix buffer and track effects will accumulate into it |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3257 | if ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || (mixedTracks == 0 && fastTracks > 0)) { |
| 3258 | // FIXME as a performance optimization, should remember previous zero status |
| 3259 | memset(mMixBuffer, 0, mNormalFrameCount * mChannelCount * sizeof(int16_t)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3260 | } |
| 3261 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3262 | // if any fast tracks, then status is ready |
Glenn Kasten | 8102804 | 2012-04-30 18:15:12 -0700 | [diff] [blame] | 3263 | mMixerStatusIgnoringFastTracks = mixerStatus; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3264 | if (fastTracks > 0) { |
| 3265 | mixerStatus = MIXER_TRACKS_READY; |
| 3266 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3267 | return mixerStatus; |
| 3268 | } |
| 3269 | |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 3270 | /* |
| 3271 | The derived values that are cached: |
| 3272 | - mixBufferSize from frame count * frame size |
| 3273 | - activeSleepTime from activeSleepTimeUs() |
| 3274 | - idleSleepTime from idleSleepTimeUs() |
| 3275 | - standbyDelay from mActiveSleepTimeUs (DIRECT only) |
| 3276 | - maxPeriod from frame count and sample rate (MIXER only) |
| 3277 | |
| 3278 | The parameters that affect these derived values are: |
| 3279 | - frame count |
| 3280 | - frame size |
| 3281 | - sample rate |
| 3282 | - device type: A2DP or not |
| 3283 | - device latency |
| 3284 | - format: PCM or not |
| 3285 | - active sleep time |
| 3286 | - idle sleep time |
| 3287 | */ |
| 3288 | |
| 3289 | void AudioFlinger::PlaybackThread::cacheParameters_l() |
| 3290 | { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3291 | mixBufferSize = mNormalFrameCount * mFrameSize; |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 3292 | activeSleepTime = activeSleepTimeUs(); |
| 3293 | idleSleepTime = idleSleepTimeUs(); |
| 3294 | } |
| 3295 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 3296 | void AudioFlinger::MixerThread::invalidateTracks(audio_stream_type_t streamType) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3297 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3298 | ALOGV ("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3299 | this, streamType, mTracks.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3300 | Mutex::Autolock _l(mLock); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3301 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3302 | size_t size = mTracks.size(); |
| 3303 | for (size_t i = 0; i < size; i++) { |
| 3304 | sp<Track> t = mTracks[i]; |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 3305 | if (t->streamType() == streamType) { |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3306 | android_atomic_or(CBLK_INVALID_ON, &t->mCblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3307 | t->mCblk->cv.signal(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3308 | } |
| 3309 | } |
| 3310 | } |
| 3311 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3312 | // getTrackName_l() must be called with ThreadBase::mLock held |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 3313 | int AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3314 | { |
Jean-Michel Trivi | 9bd2322 | 2012-04-16 13:43:48 -0700 | [diff] [blame] | 3315 | return mAudioMixer->getTrackName(channelMask); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3316 | } |
| 3317 | |
| 3318 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 3319 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) |
| 3320 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3321 | ALOGV("remove track (%d) and delete from mixer", name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3322 | mAudioMixer->deleteTrackName(name); |
| 3323 | } |
| 3324 | |
| 3325 | // checkForNewParameters_l() must be called with ThreadBase::mLock held |
| 3326 | bool AudioFlinger::MixerThread::checkForNewParameters_l() |
| 3327 | { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3328 | // if !&IDLE, holds the FastMixer state to restore after new parameters processed |
| 3329 | FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3330 | bool reconfig = false; |
| 3331 | |
| 3332 | while (!mNewParameters.isEmpty()) { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3333 | |
| 3334 | if (mFastMixer != NULL) { |
| 3335 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3336 | FastMixerState *state = sq->begin(); |
| 3337 | if (!(state->mCommand & FastMixerState::IDLE)) { |
| 3338 | previousCommand = state->mCommand; |
| 3339 | state->mCommand = FastMixerState::HOT_IDLE; |
| 3340 | sq->end(); |
| 3341 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 3342 | } else { |
| 3343 | sq->end(false /*didModify*/); |
| 3344 | } |
| 3345 | } |
| 3346 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3347 | status_t status = NO_ERROR; |
| 3348 | String8 keyValuePair = mNewParameters[0]; |
| 3349 | AudioParameter param = AudioParameter(keyValuePair); |
| 3350 | int value; |
| 3351 | |
| 3352 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 3353 | reconfig = true; |
| 3354 | } |
| 3355 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 3356 | if ((audio_format_t) value != AUDIO_FORMAT_PCM_16_BIT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3357 | status = BAD_VALUE; |
| 3358 | } else { |
| 3359 | reconfig = true; |
| 3360 | } |
| 3361 | } |
| 3362 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3363 | if (value != AUDIO_CHANNEL_OUT_STEREO) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3364 | status = BAD_VALUE; |
| 3365 | } else { |
| 3366 | reconfig = true; |
| 3367 | } |
| 3368 | } |
| 3369 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 3370 | // do not accept frame count changes if tracks are open as the track buffer |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 3371 | // size depends on frame count and correct behavior would not be guaranteed |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3372 | // if frame count is changed after track creation |
| 3373 | if (!mTracks.isEmpty()) { |
| 3374 | status = INVALID_OPERATION; |
| 3375 | } else { |
| 3376 | reconfig = true; |
| 3377 | } |
| 3378 | } |
| 3379 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 3380 | #ifdef ADD_BATTERY_DATA |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3381 | // when changing the audio output device, call addBatteryData to notify |
| 3382 | // the change |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 3383 | if ((int)mDevice != value) { |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3384 | uint32_t params = 0; |
| 3385 | // check whether speaker is on |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3386 | if (value & AUDIO_DEVICE_OUT_SPEAKER) { |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3387 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
| 3388 | } |
| 3389 | |
| 3390 | int deviceWithoutSpeaker |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3391 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3392 | // check if any other device (except speaker) is on |
| 3393 | if (value & deviceWithoutSpeaker ) { |
| 3394 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 3395 | } |
| 3396 | |
| 3397 | if (params != 0) { |
| 3398 | addBatteryData(params); |
| 3399 | } |
| 3400 | } |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 3401 | #endif |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3402 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3403 | // forward device change to effects that have requested to be |
| 3404 | // aware of attached audio device. |
| 3405 | mDevice = (uint32_t)value; |
| 3406 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 3407 | mEffectChains[i]->setDevice_l(mDevice); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3408 | } |
| 3409 | } |
| 3410 | |
| 3411 | if (status == NO_ERROR) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 3412 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3413 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3414 | if (!mStandby && status == INVALID_OPERATION) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 3415 | mOutput->stream->common.standby(&mOutput->stream->common); |
| 3416 | mStandby = true; |
| 3417 | mBytesWritten = 0; |
| 3418 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3419 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3420 | } |
| 3421 | if (status == NO_ERROR && reconfig) { |
| 3422 | delete mAudioMixer; |
Glenn Kasten | e9dd017 | 2012-01-27 18:08:45 -0800 | [diff] [blame] | 3423 | // for safety in case readOutputParameters() accesses mAudioMixer (it doesn't) |
| 3424 | mAudioMixer = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3425 | readOutputParameters(); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3426 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3427 | for (size_t i = 0; i < mTracks.size() ; i++) { |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 3428 | int name = getTrackName_l((audio_channel_mask_t)mTracks[i]->mChannelMask); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3429 | if (name < 0) break; |
| 3430 | mTracks[i]->mName = name; |
| 3431 | // limit track sample rate to 2 x new output sample rate |
| 3432 | if (mTracks[i]->mCblk->sampleRate > 2 * sampleRate()) { |
| 3433 | mTracks[i]->mCblk->sampleRate = 2 * sampleRate(); |
| 3434 | } |
| 3435 | } |
| 3436 | sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); |
| 3437 | } |
| 3438 | } |
| 3439 | |
| 3440 | mNewParameters.removeAt(0); |
| 3441 | |
| 3442 | mParamStatus = status; |
| 3443 | mParamCond.signal(); |
Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 3444 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() |
| 3445 | // already timed out waiting for the status and will never signal the condition. |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 3446 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3447 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3448 | |
| 3449 | if (!(previousCommand & FastMixerState::IDLE)) { |
| 3450 | ALOG_ASSERT(mFastMixer != NULL); |
| 3451 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3452 | FastMixerState *state = sq->begin(); |
| 3453 | ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); |
| 3454 | state->mCommand = previousCommand; |
| 3455 | sq->end(); |
| 3456 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3457 | } |
| 3458 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3459 | return reconfig; |
| 3460 | } |
| 3461 | |
| 3462 | status_t AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) |
| 3463 | { |
| 3464 | const size_t SIZE = 256; |
| 3465 | char buffer[SIZE]; |
| 3466 | String8 result; |
| 3467 | |
| 3468 | PlaybackThread::dumpInternals(fd, args); |
| 3469 | |
| 3470 | snprintf(buffer, SIZE, "AudioMixer tracks: %08x\n", mAudioMixer->trackNames()); |
| 3471 | result.append(buffer); |
| 3472 | write(fd, result.string(), result.size()); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3473 | |
| 3474 | // Make a non-atomic copy of fast mixer dump state so it won't change underneath us |
| 3475 | FastMixerDumpState copy = mFastMixerDumpState; |
| 3476 | copy.dump(fd); |
| 3477 | |
Glenn Kasten | fbae5da | 2012-05-21 09:17:20 -0700 | [diff] [blame] | 3478 | // Write the tee output to a .wav file |
| 3479 | NBAIO_Source *teeSource = mTeeSource.get(); |
| 3480 | if (teeSource != NULL) { |
| 3481 | char teePath[64]; |
| 3482 | struct timeval tv; |
| 3483 | gettimeofday(&tv, NULL); |
| 3484 | struct tm tm; |
| 3485 | localtime_r(&tv.tv_sec, &tm); |
| 3486 | strftime(teePath, sizeof(teePath), "/data/misc/media/%T.wav", &tm); |
| 3487 | int teeFd = open(teePath, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); |
| 3488 | if (teeFd >= 0) { |
| 3489 | char wavHeader[44]; |
| 3490 | memcpy(wavHeader, |
| 3491 | "RIFF\0\0\0\0WAVEfmt \20\0\0\0\1\0\2\0\104\254\0\0\0\0\0\0\4\0\20\0data\0\0\0\0", |
| 3492 | sizeof(wavHeader)); |
| 3493 | NBAIO_Format format = teeSource->format(); |
| 3494 | unsigned channelCount = Format_channelCount(format); |
| 3495 | ALOG_ASSERT(channelCount <= FCC_2); |
| 3496 | unsigned sampleRate = Format_sampleRate(format); |
| 3497 | wavHeader[22] = channelCount; // number of channels |
| 3498 | wavHeader[24] = sampleRate; // sample rate |
| 3499 | wavHeader[25] = sampleRate >> 8; |
| 3500 | wavHeader[32] = channelCount * 2; // block alignment |
| 3501 | write(teeFd, wavHeader, sizeof(wavHeader)); |
| 3502 | size_t total = 0; |
| 3503 | bool firstRead = true; |
| 3504 | for (;;) { |
| 3505 | #define TEE_SINK_READ 1024 |
| 3506 | short buffer[TEE_SINK_READ * FCC_2]; |
| 3507 | size_t count = TEE_SINK_READ; |
| 3508 | ssize_t actual = teeSource->read(buffer, count); |
| 3509 | bool wasFirstRead = firstRead; |
| 3510 | firstRead = false; |
| 3511 | if (actual <= 0) { |
| 3512 | if (actual == (ssize_t) OVERRUN && wasFirstRead) { |
| 3513 | continue; |
| 3514 | } |
| 3515 | break; |
| 3516 | } |
| 3517 | ALOG_ASSERT(actual <= count); |
| 3518 | write(teeFd, buffer, actual * channelCount * sizeof(short)); |
| 3519 | total += actual; |
| 3520 | } |
| 3521 | lseek(teeFd, (off_t) 4, SEEK_SET); |
| 3522 | uint32_t temp = 44 + total * channelCount * sizeof(short) - 8; |
| 3523 | write(teeFd, &temp, sizeof(temp)); |
| 3524 | lseek(teeFd, (off_t) 40, SEEK_SET); |
| 3525 | temp = total * channelCount * sizeof(short); |
| 3526 | write(teeFd, &temp, sizeof(temp)); |
| 3527 | close(teeFd); |
| 3528 | fdprintf(fd, "FastMixer tee copied to %s\n", teePath); |
| 3529 | } else { |
| 3530 | fdprintf(fd, "FastMixer unable to create tee %s: \n", strerror(errno)); |
| 3531 | } |
| 3532 | } |
| 3533 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3534 | return NO_ERROR; |
| 3535 | } |
| 3536 | |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 3537 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3538 | { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3539 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3540 | } |
| 3541 | |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 3542 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 3543 | { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3544 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 3545 | } |
| 3546 | |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 3547 | void AudioFlinger::MixerThread::cacheParameters_l() |
| 3548 | { |
| 3549 | PlaybackThread::cacheParameters_l(); |
| 3550 | |
| 3551 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 3552 | // Should be reduced to 2x after the vendor fixes the driver issue |
| 3553 | // increase threshold again due to low power audio mode. The way this warning |
| 3554 | // threshold is calculated and its usefulness should be reconsidered anyway. |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3555 | maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 3556 | } |
| 3557 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3558 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 3559 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
| 3560 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device) |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 3561 | : PlaybackThread(audioFlinger, output, id, device, DIRECT) |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 3562 | // mLeftVolFloat, mRightVolFloat |
| 3563 | // mLeftVolShort, mRightVolShort |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3564 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3565 | } |
| 3566 | |
| 3567 | AudioFlinger::DirectOutputThread::~DirectOutputThread() |
| 3568 | { |
| 3569 | } |
| 3570 | |
Glenn Kasten | 1465f0c | 2012-03-06 11:23:32 -0800 | [diff] [blame] | 3571 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( |
| 3572 | Vector< sp<Track> > *tracksToRemove |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3573 | ) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3574 | { |
Glenn Kasten | 1465f0c | 2012-03-06 11:23:32 -0800 | [diff] [blame] | 3575 | sp<Track> trackToRemove; |
| 3576 | |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 3577 | mixer_state mixerStatus = MIXER_IDLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3578 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3579 | // find out which tracks need to be processed |
| 3580 | if (mActiveTracks.size() != 0) { |
| 3581 | sp<Track> t = mActiveTracks[0].promote(); |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 3582 | // The track died recently |
| 3583 | if (t == 0) return MIXER_IDLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3584 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3585 | Track* const track = t.get(); |
| 3586 | audio_track_cblk_t* cblk = track->cblk(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3587 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3588 | // The first time a track is added we wait |
| 3589 | // for all its buffers to be filled before processing it |
| 3590 | if (cblk->framesReady() && track->isReady() && |
| 3591 | !track->isPaused() && !track->isTerminated()) |
| 3592 | { |
| 3593 | //ALOGV("track %d u=%08x, s=%08x [OK]", track->name(), cblk->user, cblk->server); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3594 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3595 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 3596 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 3597 | mLeftVolFloat = mRightVolFloat = 0; |
| 3598 | mLeftVolShort = mRightVolShort = 0; |
| 3599 | if (track->mState == TrackBase::RESUMING) { |
| 3600 | track->mState = TrackBase::ACTIVE; |
| 3601 | rampVolume = true; |
| 3602 | } |
| 3603 | } else if (cblk->server != 0) { |
| 3604 | // If the track is stopped before the first frame was mixed, |
| 3605 | // do not apply ramp |
| 3606 | rampVolume = true; |
| 3607 | } |
| 3608 | // compute volume for this track |
| 3609 | float left, right; |
| 3610 | if (track->isMuted() || mMasterMute || track->isPausing() || |
| 3611 | mStreamTypes[track->streamType()].mute) { |
| 3612 | left = right = 0; |
| 3613 | if (track->isPausing()) { |
| 3614 | track->setPaused(); |
| 3615 | } |
| 3616 | } else { |
| 3617 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 3618 | float v = mMasterVolume * typeVolume; |
| 3619 | uint32_t vlr = cblk->getVolumeLR(); |
| 3620 | float v_clamped = v * (vlr & 0xFFFF); |
| 3621 | if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN; |
| 3622 | left = v_clamped/MAX_GAIN; |
| 3623 | v_clamped = v * (vlr >> 16); |
| 3624 | if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN; |
| 3625 | right = v_clamped/MAX_GAIN; |
| 3626 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3627 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3628 | if (left != mLeftVolFloat || right != mRightVolFloat) { |
| 3629 | mLeftVolFloat = left; |
| 3630 | mRightVolFloat = right; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3631 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3632 | // If audio HAL implements volume control, |
| 3633 | // force software volume to nominal value |
| 3634 | if (mOutput->stream->set_volume(mOutput->stream, left, right) == NO_ERROR) { |
| 3635 | left = 1.0f; |
| 3636 | right = 1.0f; |
| 3637 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3638 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3639 | // Convert volumes from float to 8.24 |
| 3640 | uint32_t vl = (uint32_t)(left * (1 << 24)); |
| 3641 | uint32_t vr = (uint32_t)(right * (1 << 24)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3642 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3643 | // Delegate volume control to effect in track effect chain if needed |
| 3644 | // only one effect chain can be present on DirectOutputThread, so if |
| 3645 | // there is one, the track is connected to it |
| 3646 | if (!mEffectChains.isEmpty()) { |
| 3647 | // Do not ramp volume if volume is controlled by effect |
| 3648 | if (mEffectChains[0]->setVolume_l(&vl, &vr)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3649 | rampVolume = false; |
| 3650 | } |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3651 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3652 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3653 | // Convert volumes from 8.24 to 4.12 format |
| 3654 | uint32_t v_clamped = (vl + (1 << 11)) >> 12; |
| 3655 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 3656 | leftVol = (uint16_t)v_clamped; |
| 3657 | v_clamped = (vr + (1 << 11)) >> 12; |
| 3658 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 3659 | rightVol = (uint16_t)v_clamped; |
| 3660 | } else { |
| 3661 | leftVol = mLeftVolShort; |
| 3662 | rightVol = mRightVolShort; |
| 3663 | rampVolume = false; |
| 3664 | } |
| 3665 | |
| 3666 | // reset retry count |
| 3667 | track->mRetryCount = kMaxTrackRetriesDirect; |
Glenn Kasten | b071e9b | 2012-03-07 17:05:59 -0800 | [diff] [blame] | 3668 | mActiveTrack = t; |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 3669 | mixerStatus = MIXER_TRACKS_READY; |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3670 | } else { |
Eric Laurent | 91b14c4 | 2012-05-30 12:30:29 -0700 | [diff] [blame^] | 3671 | // clear effect chain input buffer if an active track underruns to avoid sending |
| 3672 | // previous audio buffer again to effects |
| 3673 | if (!mEffectChains.isEmpty()) { |
| 3674 | mEffectChains[0]->clearInputBuffer(); |
| 3675 | } |
| 3676 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3677 | //ALOGV("track %d u=%08x, s=%08x [NOT READY]", track->name(), cblk->user, cblk->server); |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3678 | if (track->isTerminated() || track->isStopped() || track->isPaused()) { |
| 3679 | // We have consumed all the buffers of this track. |
| 3680 | // Remove it from the list of active tracks. |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 3681 | // TODO: implement behavior for compressed audio |
| 3682 | size_t audioHALFrames = |
| 3683 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
| 3684 | size_t framesWritten = |
| 3685 | mBytesWritten / audio_stream_frame_size(&mOutput->stream->common); |
| 3686 | if (track->presentationComplete(framesWritten, audioHALFrames)) { |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 3687 | if (track->isStopped()) { |
| 3688 | track->reset(); |
| 3689 | } |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 3690 | trackToRemove = track; |
| 3691 | } |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3692 | } else { |
| 3693 | // No buffers for this track. Give it a few chances to |
| 3694 | // fill a buffer, then remove it from active list. |
| 3695 | if (--(track->mRetryCount) <= 0) { |
| 3696 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); |
| 3697 | trackToRemove = track; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3698 | } else { |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 3699 | mixerStatus = MIXER_TRACKS_ENABLED; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3700 | } |
| 3701 | } |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3702 | } |
| 3703 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3704 | |
Glenn Kasten | 1465f0c | 2012-03-06 11:23:32 -0800 | [diff] [blame] | 3705 | // FIXME merge this with similar code for removing multiple tracks |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3706 | // remove all the tracks that need to be... |
| 3707 | if (CC_UNLIKELY(trackToRemove != 0)) { |
Glenn Kasten | 1465f0c | 2012-03-06 11:23:32 -0800 | [diff] [blame] | 3708 | tracksToRemove->add(trackToRemove); |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3709 | mActiveTracks.remove(trackToRemove); |
| 3710 | if (!mEffectChains.isEmpty()) { |
Glenn Kasten | 5798d4e | 2012-03-08 12:18:35 -0800 | [diff] [blame] | 3711 | ALOGV("stopping track on chain %p for session Id: %d", mEffectChains[0].get(), |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3712 | trackToRemove->sessionId()); |
| 3713 | mEffectChains[0]->decActiveTrackCnt(); |
| 3714 | } |
| 3715 | if (trackToRemove->isTerminated()) { |
| 3716 | removeTrack_l(trackToRemove); |
| 3717 | } |
| 3718 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3719 | |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 3720 | return mixerStatus; |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3721 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3722 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3723 | void AudioFlinger::DirectOutputThread::threadLoop_mix() |
| 3724 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3725 | AudioBufferProvider::Buffer buffer; |
| 3726 | size_t frameCount = mFrameCount; |
| 3727 | int8_t *curBuf = (int8_t *)mMixBuffer; |
| 3728 | // output audio to hardware |
| 3729 | while (frameCount) { |
| 3730 | buffer.frameCount = frameCount; |
Glenn Kasten | b071e9b | 2012-03-07 17:05:59 -0800 | [diff] [blame] | 3731 | mActiveTrack->getNextBuffer(&buffer); |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3732 | if (CC_UNLIKELY(buffer.raw == NULL)) { |
| 3733 | memset(curBuf, 0, frameCount * mFrameSize); |
| 3734 | break; |
| 3735 | } |
| 3736 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); |
| 3737 | frameCount -= buffer.frameCount; |
| 3738 | curBuf += buffer.frameCount * mFrameSize; |
Glenn Kasten | b071e9b | 2012-03-07 17:05:59 -0800 | [diff] [blame] | 3739 | mActiveTrack->releaseBuffer(&buffer); |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3740 | } |
| 3741 | sleepTime = 0; |
| 3742 | standbyTime = systemTime() + standbyDelay; |
Glenn Kasten | b071e9b | 2012-03-07 17:05:59 -0800 | [diff] [blame] | 3743 | mActiveTrack.clear(); |
Glenn Kasten | 73f4bc3 | 2012-03-09 12:08:48 -0800 | [diff] [blame] | 3744 | |
| 3745 | // apply volume |
| 3746 | |
| 3747 | // Do not apply volume on compressed audio |
| 3748 | if (!audio_is_linear_pcm(mFormat)) { |
| 3749 | return; |
| 3750 | } |
| 3751 | |
| 3752 | // convert to signed 16 bit before volume calculation |
| 3753 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT) { |
| 3754 | size_t count = mFrameCount * mChannelCount; |
| 3755 | uint8_t *src = (uint8_t *)mMixBuffer + count-1; |
| 3756 | int16_t *dst = mMixBuffer + count-1; |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 3757 | while (count--) { |
Glenn Kasten | 73f4bc3 | 2012-03-09 12:08:48 -0800 | [diff] [blame] | 3758 | *dst-- = (int16_t)(*src--^0x80) << 8; |
| 3759 | } |
| 3760 | } |
| 3761 | |
| 3762 | frameCount = mFrameCount; |
| 3763 | int16_t *out = mMixBuffer; |
| 3764 | if (rampVolume) { |
| 3765 | if (mChannelCount == 1) { |
| 3766 | int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16; |
| 3767 | int32_t vlInc = d / (int32_t)frameCount; |
| 3768 | int32_t vl = ((int32_t)mLeftVolShort << 16); |
| 3769 | do { |
| 3770 | out[0] = clamp16(mul(out[0], vl >> 16) >> 12); |
| 3771 | out++; |
| 3772 | vl += vlInc; |
| 3773 | } while (--frameCount); |
| 3774 | |
| 3775 | } else { |
| 3776 | int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16; |
| 3777 | int32_t vlInc = d / (int32_t)frameCount; |
| 3778 | d = ((int32_t)rightVol - (int32_t)mRightVolShort) << 16; |
| 3779 | int32_t vrInc = d / (int32_t)frameCount; |
| 3780 | int32_t vl = ((int32_t)mLeftVolShort << 16); |
| 3781 | int32_t vr = ((int32_t)mRightVolShort << 16); |
| 3782 | do { |
| 3783 | out[0] = clamp16(mul(out[0], vl >> 16) >> 12); |
| 3784 | out[1] = clamp16(mul(out[1], vr >> 16) >> 12); |
| 3785 | out += 2; |
| 3786 | vl += vlInc; |
| 3787 | vr += vrInc; |
| 3788 | } while (--frameCount); |
| 3789 | } |
| 3790 | } else { |
| 3791 | if (mChannelCount == 1) { |
| 3792 | do { |
| 3793 | out[0] = clamp16(mul(out[0], leftVol) >> 12); |
| 3794 | out++; |
| 3795 | } while (--frameCount); |
| 3796 | } else { |
| 3797 | do { |
| 3798 | out[0] = clamp16(mul(out[0], leftVol) >> 12); |
| 3799 | out[1] = clamp16(mul(out[1], rightVol) >> 12); |
| 3800 | out += 2; |
| 3801 | } while (--frameCount); |
| 3802 | } |
| 3803 | } |
| 3804 | |
| 3805 | // convert back to unsigned 8 bit after volume calculation |
| 3806 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT) { |
| 3807 | size_t count = mFrameCount * mChannelCount; |
| 3808 | int16_t *src = mMixBuffer; |
| 3809 | uint8_t *dst = (uint8_t *)mMixBuffer; |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 3810 | while (count--) { |
Glenn Kasten | 73f4bc3 | 2012-03-09 12:08:48 -0800 | [diff] [blame] | 3811 | *dst++ = (uint8_t)(((int32_t)*src++ + (1<<7)) >> 8)^0x80; |
| 3812 | } |
| 3813 | } |
| 3814 | |
| 3815 | mLeftVolShort = leftVol; |
| 3816 | mRightVolShort = rightVol; |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3817 | } |
| 3818 | |
| 3819 | void AudioFlinger::DirectOutputThread::threadLoop_sleepTime() |
| 3820 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3821 | if (sleepTime == 0) { |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 3822 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3823 | sleepTime = activeSleepTime; |
| 3824 | } else { |
| 3825 | sleepTime = idleSleepTime; |
| 3826 | } |
| 3827 | } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3828 | memset(mMixBuffer, 0, mFrameCount * mFrameSize); |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3829 | sleepTime = 0; |
| 3830 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3831 | } |
| 3832 | |
| 3833 | // getTrackName_l() must be called with ThreadBase::mLock held |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 3834 | int AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3835 | { |
| 3836 | return 0; |
| 3837 | } |
| 3838 | |
| 3839 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 3840 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name) |
| 3841 | { |
| 3842 | } |
| 3843 | |
| 3844 | // checkForNewParameters_l() must be called with ThreadBase::mLock held |
| 3845 | bool AudioFlinger::DirectOutputThread::checkForNewParameters_l() |
| 3846 | { |
| 3847 | bool reconfig = false; |
| 3848 | |
| 3849 | while (!mNewParameters.isEmpty()) { |
| 3850 | status_t status = NO_ERROR; |
| 3851 | String8 keyValuePair = mNewParameters[0]; |
| 3852 | AudioParameter param = AudioParameter(keyValuePair); |
| 3853 | int value; |
| 3854 | |
| 3855 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 3856 | // do not accept frame count changes if tracks are open as the track buffer |
| 3857 | // size depends on frame count and correct behavior would not be garantied |
| 3858 | // if frame count is changed after track creation |
| 3859 | if (!mTracks.isEmpty()) { |
| 3860 | status = INVALID_OPERATION; |
| 3861 | } else { |
| 3862 | reconfig = true; |
| 3863 | } |
| 3864 | } |
| 3865 | if (status == NO_ERROR) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 3866 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3867 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3868 | if (!mStandby && status == INVALID_OPERATION) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 3869 | mOutput->stream->common.standby(&mOutput->stream->common); |
| 3870 | mStandby = true; |
| 3871 | mBytesWritten = 0; |
| 3872 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3873 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3874 | } |
| 3875 | if (status == NO_ERROR && reconfig) { |
| 3876 | readOutputParameters(); |
| 3877 | sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); |
| 3878 | } |
| 3879 | } |
| 3880 | |
| 3881 | mNewParameters.removeAt(0); |
| 3882 | |
| 3883 | mParamStatus = status; |
| 3884 | mParamCond.signal(); |
Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 3885 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() |
| 3886 | // already timed out waiting for the status and will never signal the condition. |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 3887 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3888 | } |
| 3889 | return reconfig; |
| 3890 | } |
| 3891 | |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 3892 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3893 | { |
| 3894 | uint32_t time; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3895 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 162b40b | 2011-12-05 09:47:19 -0800 | [diff] [blame] | 3896 | time = PlaybackThread::activeSleepTimeUs(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3897 | } else { |
| 3898 | time = 10000; |
| 3899 | } |
| 3900 | return time; |
| 3901 | } |
| 3902 | |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 3903 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3904 | { |
| 3905 | uint32_t time; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3906 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 60e1824 | 2010-07-29 06:50:24 -0700 | [diff] [blame] | 3907 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3908 | } else { |
| 3909 | time = 10000; |
| 3910 | } |
| 3911 | return time; |
| 3912 | } |
| 3913 | |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 3914 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 3915 | { |
| 3916 | uint32_t time; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3917 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 3918 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 3919 | } else { |
| 3920 | time = 10000; |
| 3921 | } |
| 3922 | return time; |
| 3923 | } |
| 3924 | |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 3925 | void AudioFlinger::DirectOutputThread::cacheParameters_l() |
| 3926 | { |
| 3927 | PlaybackThread::cacheParameters_l(); |
| 3928 | |
| 3929 | // use shorter standby delay as on normal output to release |
| 3930 | // hardware resources as soon as possible |
| 3931 | standbyDelay = microseconds(activeSleepTime*2); |
| 3932 | } |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 3933 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3934 | // ---------------------------------------------------------------------------- |
| 3935 | |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 3936 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 3937 | AudioFlinger::MixerThread* mainThread, audio_io_handle_t id) |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 3938 | : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->device(), DUPLICATING), |
| 3939 | mWaitTimeMs(UINT_MAX) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3940 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3941 | addOutputTrack(mainThread); |
| 3942 | } |
| 3943 | |
| 3944 | AudioFlinger::DuplicatingThread::~DuplicatingThread() |
| 3945 | { |
| 3946 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 3947 | mOutputTracks[i]->destroy(); |
| 3948 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3949 | } |
| 3950 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3951 | void AudioFlinger::DuplicatingThread::threadLoop_mix() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3952 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3953 | // mix buffers... |
| 3954 | if (outputsReady(outputTracks)) { |
| 3955 | mAudioMixer->process(AudioBufferProvider::kInvalidPTS); |
| 3956 | } else { |
| 3957 | memset(mMixBuffer, 0, mixBufferSize); |
| 3958 | } |
| 3959 | sleepTime = 0; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3960 | writeFrames = mNormalFrameCount; |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | void AudioFlinger::DuplicatingThread::threadLoop_sleepTime() |
| 3964 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3965 | if (sleepTime == 0) { |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 3966 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3967 | sleepTime = activeSleepTime; |
| 3968 | } else { |
| 3969 | sleepTime = idleSleepTime; |
| 3970 | } |
| 3971 | } else if (mBytesWritten != 0) { |
| 3972 | // flush remaining overflow buffers in output tracks |
| 3973 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 3974 | if (outputTracks[i]->isActive()) { |
| 3975 | sleepTime = 0; |
| 3976 | writeFrames = 0; |
| 3977 | memset(mMixBuffer, 0, mixBufferSize); |
| 3978 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3979 | } |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3980 | } |
| 3981 | } |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3982 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3983 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3984 | void AudioFlinger::DuplicatingThread::threadLoop_write() |
| 3985 | { |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 3986 | standbyTime = systemTime() + standbyDelay; |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3987 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 3988 | outputTracks[i]->write(mMixBuffer, writeFrames); |
| 3989 | } |
| 3990 | mBytesWritten += mixBufferSize; |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3991 | } |
Glenn Kasten | 688a640 | 2012-02-29 07:57:06 -0800 | [diff] [blame] | 3992 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3993 | void AudioFlinger::DuplicatingThread::threadLoop_standby() |
| 3994 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3995 | // DuplicatingThread implements standby by stopping all tracks |
| 3996 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 3997 | outputTracks[i]->stop(); |
| 3998 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3999 | } |
| 4000 | |
Glenn Kasten | fa26a85 | 2012-03-06 11:28:04 -0800 | [diff] [blame] | 4001 | void AudioFlinger::DuplicatingThread::saveOutputTracks() |
| 4002 | { |
| 4003 | outputTracks = mOutputTracks; |
| 4004 | } |
| 4005 | |
| 4006 | void AudioFlinger::DuplicatingThread::clearOutputTracks() |
| 4007 | { |
| 4008 | outputTracks.clear(); |
| 4009 | } |
| 4010 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4011 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) |
| 4012 | { |
Glenn Kasten | b6b7406 | 2012-02-24 14:12:20 -0800 | [diff] [blame] | 4013 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 4014 | // FIXME explain this formula |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4015 | int frameCount = (3 * mNormalFrameCount * mSampleRate) / thread->sampleRate(); |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 4016 | OutputTrack *outputTrack = new OutputTrack(thread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4017 | this, |
| 4018 | mSampleRate, |
| 4019 | mFormat, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4020 | mChannelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4021 | frameCount); |
| 4022 | if (outputTrack->cblk() != NULL) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4023 | thread->setStreamVolume(AUDIO_STREAM_CNT, 1.0f); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4024 | mOutputTracks.add(outputTrack); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4025 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread); |
Glenn Kasten | 438b036 | 2012-03-06 11:24:48 -0800 | [diff] [blame] | 4026 | updateWaitTime_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4027 | } |
| 4028 | } |
| 4029 | |
| 4030 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) |
| 4031 | { |
| 4032 | Mutex::Autolock _l(mLock); |
| 4033 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 4034 | if (mOutputTracks[i]->thread() == thread) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4035 | mOutputTracks[i]->destroy(); |
| 4036 | mOutputTracks.removeAt(i); |
Glenn Kasten | 438b036 | 2012-03-06 11:24:48 -0800 | [diff] [blame] | 4037 | updateWaitTime_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4038 | return; |
| 4039 | } |
| 4040 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4041 | ALOGV("removeOutputTrack(): unkonwn thread: %p", thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4042 | } |
| 4043 | |
Glenn Kasten | 438b036 | 2012-03-06 11:24:48 -0800 | [diff] [blame] | 4044 | // caller must hold mLock |
| 4045 | void AudioFlinger::DuplicatingThread::updateWaitTime_l() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4046 | { |
| 4047 | mWaitTimeMs = UINT_MAX; |
| 4048 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 4049 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 4050 | if (strong != 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4051 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); |
| 4052 | if (waitTimeMs < mWaitTimeMs) { |
| 4053 | mWaitTimeMs = waitTimeMs; |
| 4054 | } |
| 4055 | } |
| 4056 | } |
| 4057 | } |
| 4058 | |
| 4059 | |
Glenn Kasten | 02fe1bf | 2012-02-24 15:42:17 -0800 | [diff] [blame] | 4060 | bool AudioFlinger::DuplicatingThread::outputsReady(const SortedVector< sp<OutputTrack> > &outputTracks) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4061 | { |
| 4062 | for (size_t i = 0; i < outputTracks.size(); i++) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4063 | sp<ThreadBase> thread = outputTracks[i]->thread().promote(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4064 | if (thread == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4065 | ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", outputTracks[i].get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4066 | return false; |
| 4067 | } |
| 4068 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 4069 | if (playbackThread->standby() && !playbackThread->isSuspended()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4070 | ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), thread.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4071 | return false; |
| 4072 | } |
| 4073 | } |
| 4074 | return true; |
| 4075 | } |
| 4076 | |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 4077 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4078 | { |
| 4079 | return (mWaitTimeMs * 1000) / 2; |
| 4080 | } |
| 4081 | |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 4082 | void AudioFlinger::DuplicatingThread::cacheParameters_l() |
| 4083 | { |
| 4084 | // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first |
| 4085 | updateWaitTime_l(); |
| 4086 | |
| 4087 | MixerThread::cacheParameters_l(); |
| 4088 | } |
| 4089 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4090 | // ---------------------------------------------------------------------------- |
| 4091 | |
| 4092 | // TrackBase constructor must be called with AudioFlinger::mLock held |
| 4093 | AudioFlinger::ThreadBase::TrackBase::TrackBase( |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 4094 | ThreadBase *thread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4095 | const sp<Client>& client, |
| 4096 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 4097 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4098 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4099 | int frameCount, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4100 | const sp<IMemory>& sharedBuffer, |
| 4101 | int sessionId) |
| 4102 | : RefBase(), |
| 4103 | mThread(thread), |
| 4104 | mClient(client), |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 4105 | mCblk(NULL), |
| 4106 | // mBuffer |
| 4107 | // mBufferEnd |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4108 | mFrameCount(0), |
| 4109 | mState(IDLE), |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4110 | mSampleRate(sampleRate), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4111 | mFormat(format), |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame] | 4112 | mStepServerFailed(false), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4113 | mSessionId(sessionId) |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 4114 | // mChannelCount |
| 4115 | // mChannelMask |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4116 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4117 | ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4118 | |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4119 | // ALOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4120 | size_t size = sizeof(audio_track_cblk_t); |
| 4121 | uint8_t channelCount = popcount(channelMask); |
| 4122 | size_t bufferSize = frameCount*channelCount*sizeof(int16_t); |
| 4123 | if (sharedBuffer == 0) { |
| 4124 | size += bufferSize; |
| 4125 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4126 | |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4127 | if (client != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4128 | mCblkMemory = client->heap()->allocate(size); |
| 4129 | if (mCblkMemory != 0) { |
| 4130 | mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer()); |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 4131 | if (mCblk != NULL) { // construct the shared structure in-place. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4132 | new(mCblk) audio_track_cblk_t(); |
| 4133 | // clear all buffers |
| 4134 | mCblk->frameCount = frameCount; |
| 4135 | mCblk->sampleRate = sampleRate; |
Marco Nelissen | a1472d9 | 2012-03-30 14:36:54 -0700 | [diff] [blame] | 4136 | // uncomment the following lines to quickly test 32-bit wraparound |
| 4137 | // mCblk->user = 0xffff0000; |
| 4138 | // mCblk->server = 0xffff0000; |
| 4139 | // mCblk->userBase = 0xffff0000; |
| 4140 | // mCblk->serverBase = 0xffff0000; |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4141 | mChannelCount = channelCount; |
| 4142 | mChannelMask = channelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4143 | if (sharedBuffer == 0) { |
| 4144 | mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 4145 | memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t)); |
| 4146 | // Force underrun condition to avoid false underrun callback until first data is |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 4147 | // written to buffer (other flags are cleared) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4148 | mCblk->flags = CBLK_UNDERRUN_ON; |
| 4149 | } else { |
| 4150 | mBuffer = sharedBuffer->pointer(); |
| 4151 | } |
| 4152 | mBufferEnd = (uint8_t *)mBuffer + bufferSize; |
| 4153 | } |
| 4154 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 4155 | ALOGE("not enough memory for AudioTrack size=%u", size); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4156 | client->heap()->dump("AudioTrack"); |
| 4157 | return; |
| 4158 | } |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4159 | } else { |
| 4160 | mCblk = (audio_track_cblk_t *)(new uint8_t[size]); |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 4161 | // construct the shared structure in-place. |
| 4162 | new(mCblk) audio_track_cblk_t(); |
| 4163 | // clear all buffers |
| 4164 | mCblk->frameCount = frameCount; |
| 4165 | mCblk->sampleRate = sampleRate; |
Marco Nelissen | a1472d9 | 2012-03-30 14:36:54 -0700 | [diff] [blame] | 4166 | // uncomment the following lines to quickly test 32-bit wraparound |
| 4167 | // mCblk->user = 0xffff0000; |
| 4168 | // mCblk->server = 0xffff0000; |
| 4169 | // mCblk->userBase = 0xffff0000; |
| 4170 | // mCblk->serverBase = 0xffff0000; |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 4171 | mChannelCount = channelCount; |
| 4172 | mChannelMask = channelMask; |
| 4173 | mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 4174 | memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t)); |
| 4175 | // Force underrun condition to avoid false underrun callback until first data is |
| 4176 | // written to buffer (other flags are cleared) |
| 4177 | mCblk->flags = CBLK_UNDERRUN_ON; |
| 4178 | mBufferEnd = (uint8_t *)mBuffer + bufferSize; |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4179 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4180 | } |
| 4181 | |
| 4182 | AudioFlinger::ThreadBase::TrackBase::~TrackBase() |
| 4183 | { |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 4184 | if (mCblk != NULL) { |
Glenn Kasten | 1a0ae5b | 2012-02-03 10:24:48 -0800 | [diff] [blame] | 4185 | if (mClient == 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4186 | delete mCblk; |
Glenn Kasten | 1a0ae5b | 2012-02-03 10:24:48 -0800 | [diff] [blame] | 4187 | } else { |
| 4188 | mCblk->~audio_track_cblk_t(); // destroy our shared-structure. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4189 | } |
| 4190 | } |
Glenn Kasten | dbfafaf | 2012-01-25 15:27:15 -0800 | [diff] [blame] | 4191 | mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 4192 | if (mClient != 0) { |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 4193 | // Client destructor must run with AudioFlinger mutex locked |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4194 | Mutex::Autolock _l(mClient->audioFlinger()->mLock); |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 4195 | // If the client's reference count drops to zero, the associated destructor |
| 4196 | // must run with AudioFlinger lock held. Thus the explicit clear() rather than |
| 4197 | // relying on the automatic clear() at end of scope. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4198 | mClient.clear(); |
| 4199 | } |
| 4200 | } |
| 4201 | |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 4202 | // AudioBufferProvider interface |
| 4203 | // getNextBuffer() = 0; |
| 4204 | // This implementation of releaseBuffer() is used by Track and RecordTrack, but not TimedTrack |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4205 | void AudioFlinger::ThreadBase::TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
| 4206 | { |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4207 | buffer->raw = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4208 | mFrameCount = buffer->frameCount; |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4209 | // FIXME See note at getNextBuffer() |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 4210 | (void) step(); // ignore return value of step() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4211 | buffer->frameCount = 0; |
| 4212 | } |
| 4213 | |
| 4214 | bool AudioFlinger::ThreadBase::TrackBase::step() { |
| 4215 | bool result; |
| 4216 | audio_track_cblk_t* cblk = this->cblk(); |
| 4217 | |
| 4218 | result = cblk->stepServer(mFrameCount); |
| 4219 | if (!result) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4220 | ALOGV("stepServer failed acquiring cblk mutex"); |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame] | 4221 | mStepServerFailed = true; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4222 | } |
| 4223 | return result; |
| 4224 | } |
| 4225 | |
| 4226 | void AudioFlinger::ThreadBase::TrackBase::reset() { |
| 4227 | audio_track_cblk_t* cblk = this->cblk(); |
| 4228 | |
| 4229 | cblk->user = 0; |
| 4230 | cblk->server = 0; |
| 4231 | cblk->userBase = 0; |
| 4232 | cblk->serverBase = 0; |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame] | 4233 | mStepServerFailed = false; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4234 | ALOGV("TrackBase::reset"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4235 | } |
| 4236 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4237 | int AudioFlinger::ThreadBase::TrackBase::sampleRate() const { |
| 4238 | return (int)mCblk->sampleRate; |
| 4239 | } |
| 4240 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4241 | void* AudioFlinger::ThreadBase::TrackBase::getBuffer(uint32_t offset, uint32_t frames) const { |
| 4242 | audio_track_cblk_t* cblk = this->cblk(); |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 4243 | size_t frameSize = cblk->frameSize; |
| 4244 | int8_t *bufferStart = (int8_t *)mBuffer + (offset-cblk->serverBase)*frameSize; |
| 4245 | int8_t *bufferEnd = bufferStart + frames * frameSize; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4246 | |
| 4247 | // Check validity of returned pointer in case the track control block would have been corrupted. |
Jean-Michel Trivi | acb86cc | 2012-04-16 12:43:57 -0700 | [diff] [blame] | 4248 | ALOG_ASSERT(!(bufferStart < mBuffer || bufferStart > bufferEnd || bufferEnd > mBufferEnd), |
| 4249 | "TrackBase::getBuffer buffer out of range:\n" |
| 4250 | " start: %p, end %p , mBuffer %p mBufferEnd %p\n" |
| 4251 | " server %u, serverBase %u, user %u, userBase %u, frameSize %d", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4252 | bufferStart, bufferEnd, mBuffer, mBufferEnd, |
Jean-Michel Trivi | acb86cc | 2012-04-16 12:43:57 -0700 | [diff] [blame] | 4253 | cblk->server, cblk->serverBase, cblk->user, cblk->userBase, frameSize); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4254 | |
| 4255 | return bufferStart; |
| 4256 | } |
| 4257 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 4258 | status_t AudioFlinger::ThreadBase::TrackBase::setSyncEvent(const sp<SyncEvent>& event) |
| 4259 | { |
| 4260 | mSyncEvents.add(event); |
| 4261 | return NO_ERROR; |
| 4262 | } |
| 4263 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4264 | // ---------------------------------------------------------------------------- |
| 4265 | |
| 4266 | // Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held |
| 4267 | AudioFlinger::PlaybackThread::Track::Track( |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 4268 | PlaybackThread *thread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4269 | const sp<Client>& client, |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 4270 | audio_stream_type_t streamType, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4271 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 4272 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4273 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4274 | int frameCount, |
| 4275 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 4276 | int sessionId, |
| 4277 | IAudioFlinger::track_flags_t flags) |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame] | 4278 | : TrackBase(thread, client, sampleRate, format, channelMask, frameCount, sharedBuffer, sessionId), |
Glenn Kasten | f995901 | 2012-03-19 11:14:37 -0700 | [diff] [blame] | 4279 | mMute(false), |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4280 | mFillingUpStatus(FS_INVALID), |
Glenn Kasten | f995901 | 2012-03-19 11:14:37 -0700 | [diff] [blame] | 4281 | // mRetryCount initialized later when needed |
| 4282 | mSharedBuffer(sharedBuffer), |
| 4283 | mStreamType(streamType), |
| 4284 | mName(-1), // see note below |
| 4285 | mMainBuffer(thread->mixBuffer()), |
| 4286 | mAuxBuffer(NULL), |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 4287 | mAuxEffectId(0), mHasVolumeController(false), |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 4288 | mPresentationCompleteFrames(0), |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4289 | mFlags(flags), |
| 4290 | mFastIndex(-1), |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4291 | mUnderrunCount(0), |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4292 | mCachedVolume(1.0) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4293 | { |
| 4294 | if (mCblk != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4295 | // NOTE: audio_track_cblk_t::frameSize for 8 bit PCM data is based on a sample size of |
| 4296 | // 16 bit because data is converted to 16 bit before being stored in buffer by AudioTrack |
Eric Laurent | edc15ad | 2011-07-21 19:35:01 -0700 | [diff] [blame] | 4297 | mCblk->frameSize = audio_is_linear_pcm(format) ? mChannelCount * sizeof(int16_t) : sizeof(uint8_t); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4298 | if (flags & IAudioFlinger::TRACK_FAST) { |
| 4299 | mCblk->flags |= CBLK_FAST; // atomic op not needed yet |
| 4300 | ALOG_ASSERT(thread->mFastTrackAvailMask != 0); |
| 4301 | int i = __builtin_ctz(thread->mFastTrackAvailMask); |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 4302 | ALOG_ASSERT(0 < i && i < (int)FastMixerState::kMaxFastTracks); |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4303 | // FIXME This is too eager. We allocate a fast track index before the |
| 4304 | // fast track becomes active. Since fast tracks are a scarce resource, |
| 4305 | // this means we are potentially denying other more important fast tracks from |
| 4306 | // being created. It would be better to allocate the index dynamically. |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4307 | mFastIndex = i; |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4308 | // Read the initial underruns because this field is never cleared by the fast mixer |
Glenn Kasten | 09474df | 2012-05-10 14:48:07 -0700 | [diff] [blame] | 4309 | mObservedUnderruns = thread->getFastTrackUnderruns(i); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4310 | thread->mFastTrackAvailMask &= ~(1 << i); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4311 | } |
Glenn Kasten | f995901 | 2012-03-19 11:14:37 -0700 | [diff] [blame] | 4312 | // to avoid leaking a track name, do not allocate one unless there is an mCblk |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 4313 | mName = thread->getTrackName_l((audio_channel_mask_t)channelMask); |
Glenn Kasten | f995901 | 2012-03-19 11:14:37 -0700 | [diff] [blame] | 4314 | if (mName < 0) { |
| 4315 | ALOGE("no more track names available"); |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4316 | // FIXME bug - if sufficient fast track indices, but insufficient normal mixer names, |
| 4317 | // then we leak a fast track index. Should swap these two sections, or better yet |
| 4318 | // only allocate a normal mixer name for normal tracks. |
Glenn Kasten | f995901 | 2012-03-19 11:14:37 -0700 | [diff] [blame] | 4319 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4320 | } |
Glenn Kasten | f995901 | 2012-03-19 11:14:37 -0700 | [diff] [blame] | 4321 | ALOGV("Track constructor name %d, calling pid %d", mName, IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4322 | } |
| 4323 | |
| 4324 | AudioFlinger::PlaybackThread::Track::~Track() |
| 4325 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4326 | ALOGV("PlaybackThread::Track destructor"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4327 | sp<ThreadBase> thread = mThread.promote(); |
| 4328 | if (thread != 0) { |
| 4329 | Mutex::Autolock _l(thread->mLock); |
| 4330 | mState = TERMINATED; |
| 4331 | } |
| 4332 | } |
| 4333 | |
| 4334 | void AudioFlinger::PlaybackThread::Track::destroy() |
| 4335 | { |
| 4336 | // NOTE: destroyTrack_l() can remove a strong reference to this Track |
| 4337 | // by removing it from mTracks vector, so there is a risk that this Tracks's |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 4338 | // destructor is called. As the destructor needs to lock mLock, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4339 | // we must acquire a strong reference on this Track before locking mLock |
| 4340 | // here so that the destructor is called only when exiting this function. |
| 4341 | // On the other hand, as long as Track::destroy() is only called by |
| 4342 | // TrackHandle destructor, the TrackHandle still holds a strong ref on |
| 4343 | // this Track with its member mTrack. |
| 4344 | sp<Track> keep(this); |
| 4345 | { // scope for mLock |
| 4346 | sp<ThreadBase> thread = mThread.promote(); |
| 4347 | if (thread != 0) { |
| 4348 | if (!isOutputTrack()) { |
| 4349 | if (mState == ACTIVE || mState == RESUMING) { |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 4350 | AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 4351 | |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 4352 | #ifdef ADD_BATTERY_DATA |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 4353 | // to track the speaker usage |
| 4354 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 4355 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4356 | } |
| 4357 | AudioSystem::releaseOutput(thread->id()); |
| 4358 | } |
| 4359 | Mutex::Autolock _l(thread->mLock); |
| 4360 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 4361 | playbackThread->destroyTrack_l(this); |
| 4362 | } |
| 4363 | } |
| 4364 | } |
| 4365 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4366 | /*static*/ void AudioFlinger::PlaybackThread::Track::appendDumpHeader(String8& result) |
| 4367 | { |
Glenn Kasten | e213c86 | 2012-04-25 13:46:15 -0700 | [diff] [blame] | 4368 | result.append(" Name Client Type Fmt Chn mask Session mFrCnt fCount S M F SRate L dB R dB " |
| 4369 | " Server User Main buf Aux Buf Flags FastUnder\n"); |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4370 | } |
| 4371 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4372 | void AudioFlinger::PlaybackThread::Track::dump(char* buffer, size_t size) |
| 4373 | { |
Glenn Kasten | 83d8653 | 2012-01-17 14:39:34 -0800 | [diff] [blame] | 4374 | uint32_t vlr = mCblk->getVolumeLR(); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4375 | if (isFastTrack()) { |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4376 | sprintf(buffer, " F %2d", mFastIndex); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4377 | } else { |
| 4378 | sprintf(buffer, " %4d", mName - AudioMixer::TRACK0); |
| 4379 | } |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4380 | track_state state = mState; |
| 4381 | char stateChar; |
| 4382 | switch (state) { |
| 4383 | case IDLE: |
| 4384 | stateChar = 'I'; |
| 4385 | break; |
| 4386 | case TERMINATED: |
| 4387 | stateChar = 'T'; |
| 4388 | break; |
Glenn Kasten | d08f48c | 2012-05-01 18:14:02 -0700 | [diff] [blame] | 4389 | case STOPPING_1: |
| 4390 | stateChar = 's'; |
| 4391 | break; |
| 4392 | case STOPPING_2: |
| 4393 | stateChar = '5'; |
| 4394 | break; |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4395 | case STOPPED: |
| 4396 | stateChar = 'S'; |
| 4397 | break; |
| 4398 | case RESUMING: |
| 4399 | stateChar = 'R'; |
| 4400 | break; |
| 4401 | case ACTIVE: |
| 4402 | stateChar = 'A'; |
| 4403 | break; |
| 4404 | case PAUSING: |
| 4405 | stateChar = 'p'; |
| 4406 | break; |
| 4407 | case PAUSED: |
| 4408 | stateChar = 'P'; |
| 4409 | break; |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 4410 | case FLUSHED: |
| 4411 | stateChar = 'F'; |
| 4412 | break; |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4413 | default: |
| 4414 | stateChar = '?'; |
| 4415 | break; |
| 4416 | } |
Glenn Kasten | 09474df | 2012-05-10 14:48:07 -0700 | [diff] [blame] | 4417 | char nowInUnderrun; |
| 4418 | switch (mObservedUnderruns.mBitFields.mMostRecent) { |
| 4419 | case UNDERRUN_FULL: |
| 4420 | nowInUnderrun = ' '; |
| 4421 | break; |
| 4422 | case UNDERRUN_PARTIAL: |
| 4423 | nowInUnderrun = '<'; |
| 4424 | break; |
| 4425 | case UNDERRUN_EMPTY: |
| 4426 | nowInUnderrun = '*'; |
| 4427 | break; |
| 4428 | default: |
| 4429 | nowInUnderrun = '?'; |
| 4430 | break; |
| 4431 | } |
Glenn Kasten | e213c86 | 2012-04-25 13:46:15 -0700 | [diff] [blame] | 4432 | snprintf(&buffer[7], size-7, " %6d %4u %3u 0x%08x %7u %6u %6u %1c %1d %1d %5u %5.2g %5.2g " |
| 4433 | "0x%08x 0x%08x 0x%08x 0x%08x %#5x %9u%c\n", |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 4434 | (mClient == 0) ? getpid_cached : mClient->pid(), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4435 | mStreamType, |
| 4436 | mFormat, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4437 | mChannelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4438 | mSessionId, |
| 4439 | mFrameCount, |
Glenn Kasten | e213c86 | 2012-04-25 13:46:15 -0700 | [diff] [blame] | 4440 | mCblk->frameCount, |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4441 | stateChar, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4442 | mMute, |
| 4443 | mFillingUpStatus, |
| 4444 | mCblk->sampleRate, |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4445 | 20.0 * log10((vlr & 0xFFFF) / 4096.0), |
| 4446 | 20.0 * log10((vlr >> 16) / 4096.0), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4447 | mCblk->server, |
| 4448 | mCblk->user, |
| 4449 | (int)mMainBuffer, |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4450 | (int)mAuxBuffer, |
Glenn Kasten | e213c86 | 2012-04-25 13:46:15 -0700 | [diff] [blame] | 4451 | mCblk->flags, |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4452 | mUnderrunCount, |
Glenn Kasten | 09474df | 2012-05-10 14:48:07 -0700 | [diff] [blame] | 4453 | nowInUnderrun); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4454 | } |
| 4455 | |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 4456 | // AudioBufferProvider interface |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4457 | status_t AudioFlinger::PlaybackThread::Track::getNextBuffer( |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4458 | AudioBufferProvider::Buffer* buffer, int64_t pts) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4459 | { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4460 | audio_track_cblk_t* cblk = this->cblk(); |
| 4461 | uint32_t framesReady; |
| 4462 | uint32_t framesReq = buffer->frameCount; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4463 | |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4464 | // Check if last stepServer failed, try to step now |
| 4465 | if (mStepServerFailed) { |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4466 | // FIXME When called by fast mixer, this takes a mutex with tryLock(). |
| 4467 | // Since the fast mixer is higher priority than client callback thread, |
| 4468 | // it does not result in priority inversion for client. |
| 4469 | // But a non-blocking solution would be preferable to avoid |
| 4470 | // fast mixer being unable to tryLock(), and |
| 4471 | // to avoid the extra context switches if the client wakes up, |
| 4472 | // discovers the mutex is locked, then has to wait for fast mixer to unlock. |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4473 | if (!step()) goto getNextBuffer_exit; |
| 4474 | ALOGV("stepServer recovered"); |
| 4475 | mStepServerFailed = false; |
| 4476 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4477 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4478 | // FIXME Same as above |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4479 | framesReady = cblk->framesReady(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4480 | |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4481 | if (CC_LIKELY(framesReady)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4482 | uint32_t s = cblk->server; |
| 4483 | uint32_t bufferEnd = cblk->serverBase + cblk->frameCount; |
| 4484 | |
| 4485 | bufferEnd = (cblk->loopEnd < bufferEnd) ? cblk->loopEnd : bufferEnd; |
| 4486 | if (framesReq > framesReady) { |
| 4487 | framesReq = framesReady; |
| 4488 | } |
Marco Nelissen | a1472d9 | 2012-03-30 14:36:54 -0700 | [diff] [blame] | 4489 | if (framesReq > bufferEnd - s) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4490 | framesReq = bufferEnd - s; |
| 4491 | } |
| 4492 | |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4493 | buffer->raw = getBuffer(s, framesReq); |
| 4494 | if (buffer->raw == NULL) goto getNextBuffer_exit; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4495 | |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4496 | buffer->frameCount = framesReq; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4497 | return NO_ERROR; |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4498 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4499 | |
| 4500 | getNextBuffer_exit: |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4501 | buffer->raw = NULL; |
| 4502 | buffer->frameCount = 0; |
| 4503 | ALOGV("getNextBuffer() no more data for track %d on thread %p", mName, mThread.unsafe_get()); |
| 4504 | return NOT_ENOUGH_DATA; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4505 | } |
| 4506 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4507 | // Note that framesReady() takes a mutex on the control block using tryLock(). |
| 4508 | // This could result in priority inversion if framesReady() is called by the normal mixer, |
| 4509 | // as the normal mixer thread runs at lower |
| 4510 | // priority than the client's callback thread: there is a short window within framesReady() |
| 4511 | // during which the normal mixer could be preempted, and the client callback would block. |
| 4512 | // Another problem can occur if framesReady() is called by the fast mixer: |
| 4513 | // the tryLock() could block for up to 1 ms, and a sequence of these could delay fast mixer. |
| 4514 | // FIXME Replace AudioTrackShared control block implementation by a non-blocking FIFO queue. |
| 4515 | size_t AudioFlinger::PlaybackThread::Track::framesReady() const { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4516 | return mCblk->framesReady(); |
| 4517 | } |
| 4518 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4519 | // Don't call for fast tracks; the framesReady() could result in priority inversion |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4520 | bool AudioFlinger::PlaybackThread::Track::isReady() const { |
Eric Laurent | af59ce2 | 2010-10-05 14:41:42 -0700 | [diff] [blame] | 4521 | if (mFillingUpStatus != FS_FILLING || isStopped() || isPausing()) return true; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4522 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4523 | if (framesReady() >= mCblk->frameCount || |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4524 | (mCblk->flags & CBLK_FORCEREADY_MSK)) { |
| 4525 | mFillingUpStatus = FS_FILLED; |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 4526 | android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4527 | return true; |
| 4528 | } |
| 4529 | return false; |
| 4530 | } |
| 4531 | |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 4532 | status_t AudioFlinger::PlaybackThread::Track::start(AudioSystem::sync_event_t event, |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 4533 | int triggerSession) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4534 | { |
| 4535 | status_t status = NO_ERROR; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4536 | ALOGV("start(%d), calling pid %d session %d", |
| 4537 | mName, IPCThreadState::self()->getCallingPid(), mSessionId); |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 4538 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4539 | sp<ThreadBase> thread = mThread.promote(); |
| 4540 | if (thread != 0) { |
| 4541 | Mutex::Autolock _l(thread->mLock); |
Glenn Kasten | b853e98 | 2012-01-26 13:39:18 -0800 | [diff] [blame] | 4542 | track_state state = mState; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4543 | // here the track could be either new, or restarted |
| 4544 | // in both cases "unstop" the track |
| 4545 | if (mState == PAUSED) { |
| 4546 | mState = TrackBase::RESUMING; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4547 | ALOGV("PAUSED => RESUMING (%d) on thread %p", mName, this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4548 | } else { |
| 4549 | mState = TrackBase::ACTIVE; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4550 | ALOGV("? => ACTIVE (%d) on thread %p", mName, this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4551 | } |
| 4552 | |
| 4553 | if (!isOutputTrack() && state != ACTIVE && state != RESUMING) { |
| 4554 | thread->mLock.unlock(); |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 4555 | status = AudioSystem::startOutput(thread->id(), mStreamType, mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4556 | thread->mLock.lock(); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 4557 | |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 4558 | #ifdef ADD_BATTERY_DATA |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 4559 | // to track the speaker usage |
| 4560 | if (status == NO_ERROR) { |
| 4561 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); |
| 4562 | } |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 4563 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4564 | } |
| 4565 | if (status == NO_ERROR) { |
| 4566 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 4567 | playbackThread->addTrack_l(this); |
| 4568 | } else { |
| 4569 | mState = state; |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 4570 | triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4571 | } |
| 4572 | } else { |
| 4573 | status = BAD_VALUE; |
| 4574 | } |
| 4575 | return status; |
| 4576 | } |
| 4577 | |
| 4578 | void AudioFlinger::PlaybackThread::Track::stop() |
| 4579 | { |
Glenn Kasten | 23d82a9 | 2012-02-03 11:10:00 -0800 | [diff] [blame] | 4580 | ALOGV("stop(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4581 | sp<ThreadBase> thread = mThread.promote(); |
| 4582 | if (thread != 0) { |
| 4583 | Mutex::Autolock _l(thread->mLock); |
Glenn Kasten | b853e98 | 2012-01-26 13:39:18 -0800 | [diff] [blame] | 4584 | track_state state = mState; |
Glenn Kasten | d08f48c | 2012-05-01 18:14:02 -0700 | [diff] [blame] | 4585 | if (state == RESUMING || state == ACTIVE || state == PAUSING || state == PAUSED) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4586 | // If the track is not active (PAUSED and buffers full), flush buffers |
| 4587 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 4588 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { |
| 4589 | reset(); |
Glenn Kasten | d08f48c | 2012-05-01 18:14:02 -0700 | [diff] [blame] | 4590 | mState = STOPPED; |
| 4591 | } else if (!isFastTrack()) { |
| 4592 | mState = STOPPED; |
| 4593 | } else { |
| 4594 | // prepareTracks_l() will set state to STOPPING_2 after next underrun, |
| 4595 | // and then to STOPPED and reset() when presentation is complete |
| 4596 | mState = STOPPING_1; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4597 | } |
Glenn Kasten | d08f48c | 2012-05-01 18:14:02 -0700 | [diff] [blame] | 4598 | ALOGV("not stopping/stopped => stopping/stopped (%d) on thread %p", mName, playbackThread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4599 | } |
| 4600 | if (!isOutputTrack() && (state == ACTIVE || state == RESUMING)) { |
| 4601 | thread->mLock.unlock(); |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 4602 | AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4603 | thread->mLock.lock(); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 4604 | |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 4605 | #ifdef ADD_BATTERY_DATA |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 4606 | // to track the speaker usage |
| 4607 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 4608 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4609 | } |
| 4610 | } |
| 4611 | } |
| 4612 | |
| 4613 | void AudioFlinger::PlaybackThread::Track::pause() |
| 4614 | { |
Glenn Kasten | 23d82a9 | 2012-02-03 11:10:00 -0800 | [diff] [blame] | 4615 | ALOGV("pause(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4616 | sp<ThreadBase> thread = mThread.promote(); |
| 4617 | if (thread != 0) { |
| 4618 | Mutex::Autolock _l(thread->mLock); |
| 4619 | if (mState == ACTIVE || mState == RESUMING) { |
| 4620 | mState = PAUSING; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4621 | ALOGV("ACTIVE/RESUMING => PAUSING (%d) on thread %p", mName, thread.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4622 | if (!isOutputTrack()) { |
| 4623 | thread->mLock.unlock(); |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 4624 | AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4625 | thread->mLock.lock(); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 4626 | |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 4627 | #ifdef ADD_BATTERY_DATA |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 4628 | // to track the speaker usage |
| 4629 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 4630 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4631 | } |
| 4632 | } |
| 4633 | } |
| 4634 | } |
| 4635 | |
| 4636 | void AudioFlinger::PlaybackThread::Track::flush() |
| 4637 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4638 | ALOGV("flush(%d)", mName); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4639 | sp<ThreadBase> thread = mThread.promote(); |
| 4640 | if (thread != 0) { |
| 4641 | Mutex::Autolock _l(thread->mLock); |
Glenn Kasten | d08f48c | 2012-05-01 18:14:02 -0700 | [diff] [blame] | 4642 | if (mState != STOPPING_1 && mState != STOPPING_2 && mState != STOPPED && mState != PAUSED && |
| 4643 | mState != PAUSING) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4644 | return; |
| 4645 | } |
| 4646 | // No point remaining in PAUSED state after a flush => go to |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 4647 | // FLUSHED state |
| 4648 | mState = FLUSHED; |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 4649 | // do not reset the track if it is still in the process of being stopped or paused. |
| 4650 | // this will be done by prepareTracks_l() when the track is stopped. |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 4651 | // prepareTracks_l() will see mState == FLUSHED, then |
Glenn Kasten | d08f48c | 2012-05-01 18:14:02 -0700 | [diff] [blame] | 4652 | // remove from active track list, reset(), and trigger presentation complete |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 4653 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 4654 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { |
| 4655 | reset(); |
| 4656 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4657 | } |
| 4658 | } |
| 4659 | |
| 4660 | void AudioFlinger::PlaybackThread::Track::reset() |
| 4661 | { |
| 4662 | // Do not reset twice to avoid discarding data written just after a flush and before |
| 4663 | // the audioflinger thread detects the track is stopped. |
| 4664 | if (!mResetDone) { |
| 4665 | TrackBase::reset(); |
| 4666 | // Force underrun condition to avoid false underrun callback until first data is |
| 4667 | // written to buffer |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 4668 | android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags); |
| 4669 | android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4670 | mFillingUpStatus = FS_FILLING; |
| 4671 | mResetDone = true; |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 4672 | if (mState == FLUSHED) { |
| 4673 | mState = IDLE; |
| 4674 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4675 | } |
| 4676 | } |
| 4677 | |
| 4678 | void AudioFlinger::PlaybackThread::Track::mute(bool muted) |
| 4679 | { |
| 4680 | mMute = muted; |
| 4681 | } |
| 4682 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4683 | status_t AudioFlinger::PlaybackThread::Track::attachAuxEffect(int EffectId) |
| 4684 | { |
| 4685 | status_t status = DEAD_OBJECT; |
| 4686 | sp<ThreadBase> thread = mThread.promote(); |
| 4687 | if (thread != 0) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4688 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 4689 | status = playbackThread->attachAuxEffect(this, EffectId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4690 | } |
| 4691 | return status; |
| 4692 | } |
| 4693 | |
| 4694 | void AudioFlinger::PlaybackThread::Track::setAuxBuffer(int EffectId, int32_t *buffer) |
| 4695 | { |
| 4696 | mAuxEffectId = EffectId; |
| 4697 | mAuxBuffer = buffer; |
| 4698 | } |
| 4699 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 4700 | bool AudioFlinger::PlaybackThread::Track::presentationComplete(size_t framesWritten, |
| 4701 | size_t audioHalFrames) |
| 4702 | { |
| 4703 | // a track is considered presented when the total number of frames written to audio HAL |
| 4704 | // corresponds to the number of frames written when presentationComplete() is called for the |
| 4705 | // first time (mPresentationCompleteFrames == 0) plus the buffer filling status at that time. |
| 4706 | if (mPresentationCompleteFrames == 0) { |
| 4707 | mPresentationCompleteFrames = framesWritten + audioHalFrames; |
| 4708 | ALOGV("presentationComplete() reset: mPresentationCompleteFrames %d audioHalFrames %d", |
| 4709 | mPresentationCompleteFrames, audioHalFrames); |
| 4710 | } |
| 4711 | if (framesWritten >= mPresentationCompleteFrames) { |
| 4712 | ALOGV("presentationComplete() session %d complete: framesWritten %d", |
| 4713 | mSessionId, framesWritten); |
| 4714 | triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 4715 | return true; |
| 4716 | } |
| 4717 | return false; |
| 4718 | } |
| 4719 | |
| 4720 | void AudioFlinger::PlaybackThread::Track::triggerEvents(AudioSystem::sync_event_t type) |
| 4721 | { |
| 4722 | for (int i = 0; i < (int)mSyncEvents.size(); i++) { |
| 4723 | if (mSyncEvents[i]->type() == type) { |
| 4724 | mSyncEvents[i]->trigger(); |
| 4725 | mSyncEvents.removeAt(i); |
| 4726 | i--; |
| 4727 | } |
| 4728 | } |
| 4729 | } |
| 4730 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4731 | // implement VolumeBufferProvider interface |
| 4732 | |
| 4733 | uint32_t AudioFlinger::PlaybackThread::Track::getVolumeLR() |
| 4734 | { |
| 4735 | // called by FastMixer, so not allowed to take any locks, block, or do I/O including logs |
| 4736 | ALOG_ASSERT(isFastTrack() && (mCblk != NULL)); |
| 4737 | uint32_t vlr = mCblk->getVolumeLR(); |
| 4738 | uint32_t vl = vlr & 0xFFFF; |
| 4739 | uint32_t vr = vlr >> 16; |
| 4740 | // track volumes come from shared memory, so can't be trusted and must be clamped |
| 4741 | if (vl > MAX_GAIN_INT) { |
| 4742 | vl = MAX_GAIN_INT; |
| 4743 | } |
| 4744 | if (vr > MAX_GAIN_INT) { |
| 4745 | vr = MAX_GAIN_INT; |
| 4746 | } |
| 4747 | // now apply the cached master volume and stream type volume; |
| 4748 | // this is trusted but lacks any synchronization or barrier so may be stale |
| 4749 | float v = mCachedVolume; |
| 4750 | vl *= v; |
| 4751 | vr *= v; |
| 4752 | // re-combine into U4.16 |
| 4753 | vlr = (vr << 16) | (vl & 0xFFFF); |
| 4754 | // FIXME look at mute, pause, and stop flags |
| 4755 | return vlr; |
| 4756 | } |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 4757 | |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 4758 | status_t AudioFlinger::PlaybackThread::Track::setSyncEvent(const sp<SyncEvent>& event) |
| 4759 | { |
| 4760 | if (mState == TERMINATED || mState == PAUSED || |
| 4761 | ((framesReady() == 0) && ((mSharedBuffer != 0) || |
| 4762 | (mState == STOPPED)))) { |
| 4763 | ALOGW("Track::setSyncEvent() in invalid state %d on session %d %s mode, framesReady %d ", |
| 4764 | mState, mSessionId, (mSharedBuffer != 0) ? "static" : "stream", framesReady()); |
| 4765 | event->cancel(); |
| 4766 | return INVALID_OPERATION; |
| 4767 | } |
| 4768 | TrackBase::setSyncEvent(event); |
| 4769 | return NO_ERROR; |
| 4770 | } |
| 4771 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4772 | // timed audio tracks |
| 4773 | |
| 4774 | sp<AudioFlinger::PlaybackThread::TimedTrack> |
| 4775 | AudioFlinger::PlaybackThread::TimedTrack::create( |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 4776 | PlaybackThread *thread, |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4777 | const sp<Client>& client, |
| 4778 | audio_stream_type_t streamType, |
| 4779 | uint32_t sampleRate, |
| 4780 | audio_format_t format, |
| 4781 | uint32_t channelMask, |
| 4782 | int frameCount, |
| 4783 | const sp<IMemory>& sharedBuffer, |
| 4784 | int sessionId) { |
| 4785 | if (!client->reserveTimedTrack()) |
| 4786 | return NULL; |
| 4787 | |
Glenn Kasten | a035676 | 2012-03-19 10:38:51 -0700 | [diff] [blame] | 4788 | return new TimedTrack( |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4789 | thread, client, streamType, sampleRate, format, channelMask, frameCount, |
| 4790 | sharedBuffer, sessionId); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4791 | } |
| 4792 | |
| 4793 | AudioFlinger::PlaybackThread::TimedTrack::TimedTrack( |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 4794 | PlaybackThread *thread, |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4795 | const sp<Client>& client, |
| 4796 | audio_stream_type_t streamType, |
| 4797 | uint32_t sampleRate, |
| 4798 | audio_format_t format, |
| 4799 | uint32_t channelMask, |
| 4800 | int frameCount, |
| 4801 | const sp<IMemory>& sharedBuffer, |
| 4802 | int sessionId) |
| 4803 | : Track(thread, client, streamType, sampleRate, format, channelMask, |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 4804 | frameCount, sharedBuffer, sessionId, IAudioFlinger::TRACK_TIMED), |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4805 | mQueueHeadInFlight(false), |
| 4806 | mTrimQueueHeadOnRelease(false), |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4807 | mFramesPendingInQueue(0), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4808 | mTimedSilenceBuffer(NULL), |
| 4809 | mTimedSilenceBufferSize(0), |
| 4810 | mTimedAudioOutputOnTime(false), |
| 4811 | mMediaTimeTransformValid(false) |
| 4812 | { |
| 4813 | LocalClock lc; |
| 4814 | mLocalTimeFreq = lc.getLocalFreq(); |
| 4815 | |
| 4816 | mLocalTimeToSampleTransform.a_zero = 0; |
| 4817 | mLocalTimeToSampleTransform.b_zero = 0; |
| 4818 | mLocalTimeToSampleTransform.a_to_b_numer = sampleRate; |
| 4819 | mLocalTimeToSampleTransform.a_to_b_denom = mLocalTimeFreq; |
| 4820 | LinearTransform::reduce(&mLocalTimeToSampleTransform.a_to_b_numer, |
| 4821 | &mLocalTimeToSampleTransform.a_to_b_denom); |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4822 | |
| 4823 | mMediaTimeToSampleTransform.a_zero = 0; |
| 4824 | mMediaTimeToSampleTransform.b_zero = 0; |
| 4825 | mMediaTimeToSampleTransform.a_to_b_numer = sampleRate; |
| 4826 | mMediaTimeToSampleTransform.a_to_b_denom = 1000000; |
| 4827 | LinearTransform::reduce(&mMediaTimeToSampleTransform.a_to_b_numer, |
| 4828 | &mMediaTimeToSampleTransform.a_to_b_denom); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4829 | } |
| 4830 | |
| 4831 | AudioFlinger::PlaybackThread::TimedTrack::~TimedTrack() { |
| 4832 | mClient->releaseTimedTrack(); |
| 4833 | delete [] mTimedSilenceBuffer; |
| 4834 | } |
| 4835 | |
| 4836 | status_t AudioFlinger::PlaybackThread::TimedTrack::allocateTimedBuffer( |
| 4837 | size_t size, sp<IMemory>* buffer) { |
| 4838 | |
| 4839 | Mutex::Autolock _l(mTimedBufferQueueLock); |
| 4840 | |
| 4841 | trimTimedBufferQueue_l(); |
| 4842 | |
| 4843 | // lazily initialize the shared memory heap for timed buffers |
| 4844 | if (mTimedMemoryDealer == NULL) { |
| 4845 | const int kTimedBufferHeapSize = 512 << 10; |
| 4846 | |
| 4847 | mTimedMemoryDealer = new MemoryDealer(kTimedBufferHeapSize, |
| 4848 | "AudioFlingerTimed"); |
| 4849 | if (mTimedMemoryDealer == NULL) |
| 4850 | return NO_MEMORY; |
| 4851 | } |
| 4852 | |
| 4853 | sp<IMemory> newBuffer = mTimedMemoryDealer->allocate(size); |
| 4854 | if (newBuffer == NULL) { |
| 4855 | newBuffer = mTimedMemoryDealer->allocate(size); |
| 4856 | if (newBuffer == NULL) |
| 4857 | return NO_MEMORY; |
| 4858 | } |
| 4859 | |
| 4860 | *buffer = newBuffer; |
| 4861 | return NO_ERROR; |
| 4862 | } |
| 4863 | |
| 4864 | // caller must hold mTimedBufferQueueLock |
| 4865 | void AudioFlinger::PlaybackThread::TimedTrack::trimTimedBufferQueue_l() { |
| 4866 | int64_t mediaTimeNow; |
| 4867 | { |
| 4868 | Mutex::Autolock mttLock(mMediaTimeTransformLock); |
| 4869 | if (!mMediaTimeTransformValid) |
| 4870 | return; |
| 4871 | |
| 4872 | int64_t targetTimeNow; |
| 4873 | status_t res = (mMediaTimeTransformTarget == TimedAudioTrack::COMMON_TIME) |
| 4874 | ? mCCHelper.getCommonTime(&targetTimeNow) |
| 4875 | : mCCHelper.getLocalTime(&targetTimeNow); |
| 4876 | |
| 4877 | if (OK != res) |
| 4878 | return; |
| 4879 | |
| 4880 | if (!mMediaTimeTransform.doReverseTransform(targetTimeNow, |
| 4881 | &mediaTimeNow)) { |
| 4882 | return; |
| 4883 | } |
| 4884 | } |
| 4885 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4886 | size_t trimEnd; |
| 4887 | for (trimEnd = 0; trimEnd < mTimedBufferQueue.size(); trimEnd++) { |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4888 | int64_t bufEnd; |
| 4889 | |
John Grossman | c95cfbb | 2012-04-12 11:53:11 -0700 | [diff] [blame] | 4890 | if ((trimEnd + 1) < mTimedBufferQueue.size()) { |
| 4891 | // We have a next buffer. Just use its PTS as the PTS of the frame |
| 4892 | // following the last frame in this buffer. If the stream is sparse |
| 4893 | // (ie, there are deliberate gaps left in the stream which should be |
| 4894 | // filled with silence by the TimedAudioTrack), then this can result |
| 4895 | // in one extra buffer being left un-trimmed when it could have |
| 4896 | // been. In general, this is not typical, and we would rather |
| 4897 | // optimized away the TS calculation below for the more common case |
| 4898 | // where PTSes are contiguous. |
| 4899 | bufEnd = mTimedBufferQueue[trimEnd + 1].pts(); |
| 4900 | } else { |
| 4901 | // We have no next buffer. Compute the PTS of the frame following |
| 4902 | // the last frame in this buffer by computing the duration of of |
| 4903 | // this frame in media time units and adding it to the PTS of the |
| 4904 | // buffer. |
| 4905 | int64_t frameCount = mTimedBufferQueue[trimEnd].buffer()->size() |
| 4906 | / mCblk->frameSize; |
| 4907 | |
| 4908 | if (!mMediaTimeToSampleTransform.doReverseTransform(frameCount, |
| 4909 | &bufEnd)) { |
| 4910 | ALOGE("Failed to convert frame count of %lld to media time" |
| 4911 | " duration" " (scale factor %d/%u) in %s", |
| 4912 | frameCount, |
| 4913 | mMediaTimeToSampleTransform.a_to_b_numer, |
| 4914 | mMediaTimeToSampleTransform.a_to_b_denom, |
| 4915 | __PRETTY_FUNCTION__); |
| 4916 | break; |
| 4917 | } |
| 4918 | bufEnd += mTimedBufferQueue[trimEnd].pts(); |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4919 | } |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4920 | |
| 4921 | if (bufEnd > mediaTimeNow) |
| 4922 | break; |
| 4923 | |
| 4924 | // Is the buffer we want to use in the middle of a mix operation right |
| 4925 | // now? If so, don't actually trim it. Just wait for the releaseBuffer |
| 4926 | // from the mixer which should be coming back shortly. |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4927 | if (!trimEnd && mQueueHeadInFlight) { |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4928 | mTrimQueueHeadOnRelease = true; |
| 4929 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4930 | } |
| 4931 | |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4932 | size_t trimStart = mTrimQueueHeadOnRelease ? 1 : 0; |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4933 | if (trimStart < trimEnd) { |
| 4934 | // Update the bookkeeping for framesReady() |
| 4935 | for (size_t i = trimStart; i < trimEnd; ++i) { |
| 4936 | updateFramesPendingAfterTrim_l(mTimedBufferQueue[i], "trim"); |
| 4937 | } |
| 4938 | |
| 4939 | // Now actually remove the buffers from the queue. |
| 4940 | mTimedBufferQueue.removeItemsAt(trimStart, trimEnd); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4941 | } |
| 4942 | } |
| 4943 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4944 | void AudioFlinger::PlaybackThread::TimedTrack::trimTimedBufferQueueHead_l( |
| 4945 | const char* logTag) { |
John Grossman | d3030da | 2012-04-12 11:56:36 -0700 | [diff] [blame] | 4946 | ALOG_ASSERT(mTimedBufferQueue.size() > 0, |
| 4947 | "%s called (reason \"%s\"), but timed buffer queue has no" |
| 4948 | " elements to trim.", __FUNCTION__, logTag); |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4949 | |
| 4950 | updateFramesPendingAfterTrim_l(mTimedBufferQueue[0], logTag); |
| 4951 | mTimedBufferQueue.removeAt(0); |
| 4952 | } |
| 4953 | |
| 4954 | void AudioFlinger::PlaybackThread::TimedTrack::updateFramesPendingAfterTrim_l( |
| 4955 | const TimedBuffer& buf, |
| 4956 | const char* logTag) { |
| 4957 | uint32_t bufBytes = buf.buffer()->size(); |
| 4958 | uint32_t consumedAlready = buf.position(); |
| 4959 | |
Eric Laurent | b388e53 | 2012-04-14 13:32:48 -0700 | [diff] [blame] | 4960 | ALOG_ASSERT(consumedAlready <= bufBytes, |
John Grossman | d3030da | 2012-04-12 11:56:36 -0700 | [diff] [blame] | 4961 | "Bad bookkeeping while updating frames pending. Timed buffer is" |
| 4962 | " only %u bytes long, but claims to have consumed %u" |
| 4963 | " bytes. (update reason: \"%s\")", |
Eric Laurent | b388e53 | 2012-04-14 13:32:48 -0700 | [diff] [blame] | 4964 | bufBytes, consumedAlready, logTag); |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4965 | |
| 4966 | uint32_t bufFrames = (bufBytes - consumedAlready) / mCblk->frameSize; |
John Grossman | d3030da | 2012-04-12 11:56:36 -0700 | [diff] [blame] | 4967 | ALOG_ASSERT(mFramesPendingInQueue >= bufFrames, |
| 4968 | "Bad bookkeeping while updating frames pending. Should have at" |
| 4969 | " least %u queued frames, but we think we have only %u. (update" |
| 4970 | " reason: \"%s\")", |
| 4971 | bufFrames, mFramesPendingInQueue, logTag); |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4972 | |
| 4973 | mFramesPendingInQueue -= bufFrames; |
| 4974 | } |
| 4975 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4976 | status_t AudioFlinger::PlaybackThread::TimedTrack::queueTimedBuffer( |
| 4977 | const sp<IMemory>& buffer, int64_t pts) { |
| 4978 | |
| 4979 | { |
| 4980 | Mutex::Autolock mttLock(mMediaTimeTransformLock); |
| 4981 | if (!mMediaTimeTransformValid) |
| 4982 | return INVALID_OPERATION; |
| 4983 | } |
| 4984 | |
| 4985 | Mutex::Autolock _l(mTimedBufferQueueLock); |
| 4986 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4987 | uint32_t bufFrames = buffer->size() / mCblk->frameSize; |
| 4988 | mFramesPendingInQueue += bufFrames; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4989 | mTimedBufferQueue.add(TimedBuffer(buffer, pts)); |
| 4990 | |
| 4991 | return NO_ERROR; |
| 4992 | } |
| 4993 | |
| 4994 | status_t AudioFlinger::PlaybackThread::TimedTrack::setMediaTimeTransform( |
| 4995 | const LinearTransform& xform, TimedAudioTrack::TargetTimeline target) { |
| 4996 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4997 | ALOGVV("setMediaTimeTransform az=%lld bz=%lld n=%d d=%u tgt=%d", |
| 4998 | xform.a_zero, xform.b_zero, xform.a_to_b_numer, xform.a_to_b_denom, |
| 4999 | target); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5000 | |
| 5001 | if (!(target == TimedAudioTrack::LOCAL_TIME || |
| 5002 | target == TimedAudioTrack::COMMON_TIME)) { |
| 5003 | return BAD_VALUE; |
| 5004 | } |
| 5005 | |
| 5006 | Mutex::Autolock lock(mMediaTimeTransformLock); |
| 5007 | mMediaTimeTransform = xform; |
| 5008 | mMediaTimeTransformTarget = target; |
| 5009 | mMediaTimeTransformValid = true; |
| 5010 | |
| 5011 | return NO_ERROR; |
| 5012 | } |
| 5013 | |
| 5014 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
| 5015 | |
| 5016 | // implementation of getNextBuffer for tracks whose buffers have timestamps |
| 5017 | status_t AudioFlinger::PlaybackThread::TimedTrack::getNextBuffer( |
| 5018 | AudioBufferProvider::Buffer* buffer, int64_t pts) |
| 5019 | { |
| 5020 | if (pts == AudioBufferProvider::kInvalidPTS) { |
| 5021 | buffer->raw = 0; |
| 5022 | buffer->frameCount = 0; |
John Grossman | 8d314b7 | 2012-04-19 12:08:17 -0700 | [diff] [blame] | 5023 | mTimedAudioOutputOnTime = false; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5024 | return INVALID_OPERATION; |
| 5025 | } |
| 5026 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5027 | Mutex::Autolock _l(mTimedBufferQueueLock); |
| 5028 | |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 5029 | ALOG_ASSERT(!mQueueHeadInFlight, |
| 5030 | "getNextBuffer called without releaseBuffer!"); |
| 5031 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5032 | while (true) { |
| 5033 | |
| 5034 | // if we have no timed buffers, then fail |
| 5035 | if (mTimedBufferQueue.isEmpty()) { |
| 5036 | buffer->raw = 0; |
| 5037 | buffer->frameCount = 0; |
| 5038 | return NOT_ENOUGH_DATA; |
| 5039 | } |
| 5040 | |
| 5041 | TimedBuffer& head = mTimedBufferQueue.editItemAt(0); |
| 5042 | |
| 5043 | // calculate the PTS of the head of the timed buffer queue expressed in |
| 5044 | // local time |
| 5045 | int64_t headLocalPTS; |
| 5046 | { |
| 5047 | Mutex::Autolock mttLock(mMediaTimeTransformLock); |
| 5048 | |
Glenn Kasten | 5798d4e | 2012-03-08 12:18:35 -0800 | [diff] [blame] | 5049 | ALOG_ASSERT(mMediaTimeTransformValid, "media time transform invalid"); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5050 | |
| 5051 | if (mMediaTimeTransform.a_to_b_denom == 0) { |
| 5052 | // the transform represents a pause, so yield silence |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 5053 | timedYieldSilence_l(buffer->frameCount, buffer); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5054 | return NO_ERROR; |
| 5055 | } |
| 5056 | |
| 5057 | int64_t transformedPTS; |
| 5058 | if (!mMediaTimeTransform.doForwardTransform(head.pts(), |
| 5059 | &transformedPTS)) { |
| 5060 | // the transform failed. this shouldn't happen, but if it does |
| 5061 | // then just drop this buffer |
| 5062 | ALOGW("timedGetNextBuffer transform failed"); |
| 5063 | buffer->raw = 0; |
| 5064 | buffer->frameCount = 0; |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 5065 | trimTimedBufferQueueHead_l("getNextBuffer; no transform"); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5066 | return NO_ERROR; |
| 5067 | } |
| 5068 | |
| 5069 | if (mMediaTimeTransformTarget == TimedAudioTrack::COMMON_TIME) { |
| 5070 | if (OK != mCCHelper.commonTimeToLocalTime(transformedPTS, |
| 5071 | &headLocalPTS)) { |
| 5072 | buffer->raw = 0; |
| 5073 | buffer->frameCount = 0; |
| 5074 | return INVALID_OPERATION; |
| 5075 | } |
| 5076 | } else { |
| 5077 | headLocalPTS = transformedPTS; |
| 5078 | } |
| 5079 | } |
| 5080 | |
| 5081 | // adjust the head buffer's PTS to reflect the portion of the head buffer |
| 5082 | // that has already been consumed |
| 5083 | int64_t effectivePTS = headLocalPTS + |
| 5084 | ((head.position() / mCblk->frameSize) * mLocalTimeFreq / sampleRate()); |
| 5085 | |
| 5086 | // Calculate the delta in samples between the head of the input buffer |
| 5087 | // queue and the start of the next output buffer that will be written. |
| 5088 | // If the transformation fails because of over or underflow, it means |
| 5089 | // that the sample's position in the output stream is so far out of |
| 5090 | // whack that it should just be dropped. |
| 5091 | int64_t sampleDelta; |
| 5092 | if (llabs(effectivePTS - pts) >= (static_cast<int64_t>(1) << 31)) { |
| 5093 | ALOGV("*** head buffer is too far from PTS: dropped buffer"); |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 5094 | trimTimedBufferQueueHead_l("getNextBuffer, buf pts too far from" |
| 5095 | " mix"); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5096 | continue; |
| 5097 | } |
| 5098 | if (!mLocalTimeToSampleTransform.doForwardTransform( |
| 5099 | (effectivePTS - pts) << 32, &sampleDelta)) { |
John Grossman | d3030da | 2012-04-12 11:56:36 -0700 | [diff] [blame] | 5100 | ALOGV("*** too late during sample rate transform: dropped buffer"); |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 5101 | trimTimedBufferQueueHead_l("getNextBuffer, bad local to sample"); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5102 | continue; |
| 5103 | } |
| 5104 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 5105 | ALOGVV("*** getNextBuffer head.pts=%lld head.pos=%d pts=%lld" |
| 5106 | " sampleDelta=[%d.%08x]", |
| 5107 | head.pts(), head.position(), pts, |
| 5108 | static_cast<int32_t>((sampleDelta >= 0 ? 0 : 1) |
| 5109 | + (sampleDelta >> 32)), |
| 5110 | static_cast<uint32_t>(sampleDelta & 0xFFFFFFFF)); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5111 | |
| 5112 | // if the delta between the ideal placement for the next input sample and |
| 5113 | // the current output position is within this threshold, then we will |
| 5114 | // concatenate the next input samples to the previous output |
| 5115 | const int64_t kSampleContinuityThreshold = |
John Grossman | 8d314b7 | 2012-04-19 12:08:17 -0700 | [diff] [blame] | 5116 | (static_cast<int64_t>(sampleRate()) << 32) / 250; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5117 | |
| 5118 | // if this is the first buffer of audio that we're emitting from this track |
| 5119 | // then it should be almost exactly on time. |
| 5120 | const int64_t kSampleStartupThreshold = 1LL << 32; |
| 5121 | |
| 5122 | if ((mTimedAudioOutputOnTime && llabs(sampleDelta) <= kSampleContinuityThreshold) || |
John Grossman | 8d314b7 | 2012-04-19 12:08:17 -0700 | [diff] [blame] | 5123 | (!mTimedAudioOutputOnTime && llabs(sampleDelta) <= kSampleStartupThreshold)) { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5124 | // the next input is close enough to being on time, so concatenate it |
| 5125 | // with the last output |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 5126 | timedYieldSamples_l(buffer); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5127 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 5128 | ALOGVV("*** on time: head.pos=%d frameCount=%u", |
| 5129 | head.position(), buffer->frameCount); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5130 | return NO_ERROR; |
John Grossman | 8d314b7 | 2012-04-19 12:08:17 -0700 | [diff] [blame] | 5131 | } |
| 5132 | |
| 5133 | // Looks like our output is not on time. Reset our on timed status. |
| 5134 | // Next time we mix samples from our input queue, then should be within |
| 5135 | // the StartupThreshold. |
| 5136 | mTimedAudioOutputOnTime = false; |
| 5137 | if (sampleDelta > 0) { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5138 | // the gap between the current output position and the proper start of |
| 5139 | // the next input sample is too big, so fill it with silence |
| 5140 | uint32_t framesUntilNextInput = (sampleDelta + 0x80000000) >> 32; |
| 5141 | |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 5142 | timedYieldSilence_l(framesUntilNextInput, buffer); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5143 | ALOGV("*** silence: frameCount=%u", buffer->frameCount); |
| 5144 | return NO_ERROR; |
| 5145 | } else { |
| 5146 | // the next input sample is late |
| 5147 | uint32_t lateFrames = static_cast<uint32_t>(-((sampleDelta + 0x80000000) >> 32)); |
| 5148 | size_t onTimeSamplePosition = |
| 5149 | head.position() + lateFrames * mCblk->frameSize; |
| 5150 | |
| 5151 | if (onTimeSamplePosition > head.buffer()->size()) { |
| 5152 | // all the remaining samples in the head are too late, so |
| 5153 | // drop it and move on |
| 5154 | ALOGV("*** too late: dropped buffer"); |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 5155 | trimTimedBufferQueueHead_l("getNextBuffer, dropped late buffer"); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5156 | continue; |
| 5157 | } else { |
| 5158 | // skip over the late samples |
| 5159 | head.setPosition(onTimeSamplePosition); |
| 5160 | |
| 5161 | // yield the available samples |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 5162 | timedYieldSamples_l(buffer); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5163 | |
| 5164 | ALOGV("*** late: head.pos=%d frameCount=%u", head.position(), buffer->frameCount); |
| 5165 | return NO_ERROR; |
| 5166 | } |
| 5167 | } |
| 5168 | } |
| 5169 | } |
| 5170 | |
| 5171 | // Yield samples from the timed buffer queue head up to the given output |
| 5172 | // buffer's capacity. |
| 5173 | // |
| 5174 | // Caller must hold mTimedBufferQueueLock |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 5175 | void AudioFlinger::PlaybackThread::TimedTrack::timedYieldSamples_l( |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5176 | AudioBufferProvider::Buffer* buffer) { |
| 5177 | |
| 5178 | const TimedBuffer& head = mTimedBufferQueue[0]; |
| 5179 | |
| 5180 | buffer->raw = (static_cast<uint8_t*>(head.buffer()->pointer()) + |
| 5181 | head.position()); |
| 5182 | |
| 5183 | uint32_t framesLeftInHead = ((head.buffer()->size() - head.position()) / |
| 5184 | mCblk->frameSize); |
| 5185 | size_t framesRequested = buffer->frameCount; |
| 5186 | buffer->frameCount = min(framesLeftInHead, framesRequested); |
| 5187 | |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 5188 | mQueueHeadInFlight = true; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5189 | mTimedAudioOutputOnTime = true; |
| 5190 | } |
| 5191 | |
| 5192 | // Yield samples of silence up to the given output buffer's capacity |
| 5193 | // |
| 5194 | // Caller must hold mTimedBufferQueueLock |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 5195 | void AudioFlinger::PlaybackThread::TimedTrack::timedYieldSilence_l( |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5196 | uint32_t numFrames, AudioBufferProvider::Buffer* buffer) { |
| 5197 | |
| 5198 | // lazily allocate a buffer filled with silence |
| 5199 | if (mTimedSilenceBufferSize < numFrames * mCblk->frameSize) { |
| 5200 | delete [] mTimedSilenceBuffer; |
| 5201 | mTimedSilenceBufferSize = numFrames * mCblk->frameSize; |
| 5202 | mTimedSilenceBuffer = new uint8_t[mTimedSilenceBufferSize]; |
| 5203 | memset(mTimedSilenceBuffer, 0, mTimedSilenceBufferSize); |
| 5204 | } |
| 5205 | |
| 5206 | buffer->raw = mTimedSilenceBuffer; |
| 5207 | size_t framesRequested = buffer->frameCount; |
| 5208 | buffer->frameCount = min(numFrames, framesRequested); |
| 5209 | |
| 5210 | mTimedAudioOutputOnTime = false; |
| 5211 | } |
| 5212 | |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 5213 | // AudioBufferProvider interface |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5214 | void AudioFlinger::PlaybackThread::TimedTrack::releaseBuffer( |
| 5215 | AudioBufferProvider::Buffer* buffer) { |
| 5216 | |
| 5217 | Mutex::Autolock _l(mTimedBufferQueueLock); |
| 5218 | |
John Grossman | fe5b3ba | 2012-02-12 17:51:21 -0800 | [diff] [blame] | 5219 | // If the buffer which was just released is part of the buffer at the head |
| 5220 | // of the queue, be sure to update the amt of the buffer which has been |
| 5221 | // consumed. If the buffer being returned is not part of the head of the |
| 5222 | // queue, its either because the buffer is part of the silence buffer, or |
| 5223 | // because the head of the timed queue was trimmed after the mixer called |
| 5224 | // getNextBuffer but before the mixer called releaseBuffer. |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 5225 | if (buffer->raw == mTimedSilenceBuffer) { |
| 5226 | ALOG_ASSERT(!mQueueHeadInFlight, |
| 5227 | "Queue head in flight during release of silence buffer!"); |
| 5228 | goto done; |
| 5229 | } |
| 5230 | |
| 5231 | ALOG_ASSERT(mQueueHeadInFlight, |
| 5232 | "TimedTrack::releaseBuffer of non-silence buffer, but no queue" |
| 5233 | " head in flight."); |
| 5234 | |
| 5235 | if (mTimedBufferQueue.size()) { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5236 | TimedBuffer& head = mTimedBufferQueue.editItemAt(0); |
John Grossman | fe5b3ba | 2012-02-12 17:51:21 -0800 | [diff] [blame] | 5237 | |
| 5238 | void* start = head.buffer()->pointer(); |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 5239 | void* end = reinterpret_cast<void*>( |
| 5240 | reinterpret_cast<uint8_t*>(head.buffer()->pointer()) |
| 5241 | + head.buffer()->size()); |
John Grossman | fe5b3ba | 2012-02-12 17:51:21 -0800 | [diff] [blame] | 5242 | |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 5243 | ALOG_ASSERT((buffer->raw >= start) && (buffer->raw < end), |
| 5244 | "released buffer not within the head of the timed buffer" |
| 5245 | " queue; qHead = [%p, %p], released buffer = %p", |
| 5246 | start, end, buffer->raw); |
| 5247 | |
| 5248 | head.setPosition(head.position() + |
| 5249 | (buffer->frameCount * mCblk->frameSize)); |
| 5250 | mQueueHeadInFlight = false; |
| 5251 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 5252 | ALOG_ASSERT(mFramesPendingInQueue >= buffer->frameCount, |
| 5253 | "Bad bookkeeping during releaseBuffer! Should have at" |
| 5254 | " least %u queued frames, but we think we have only %u", |
| 5255 | buffer->frameCount, mFramesPendingInQueue); |
| 5256 | |
| 5257 | mFramesPendingInQueue -= buffer->frameCount; |
| 5258 | |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 5259 | if ((static_cast<size_t>(head.position()) >= head.buffer()->size()) |
| 5260 | || mTrimQueueHeadOnRelease) { |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 5261 | trimTimedBufferQueueHead_l("releaseBuffer"); |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 5262 | mTrimQueueHeadOnRelease = false; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5263 | } |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 5264 | } else { |
| 5265 | LOG_FATAL("TimedTrack::releaseBuffer of non-silence buffer with no" |
| 5266 | " buffers in the timed buffer queue"); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5267 | } |
| 5268 | |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 5269 | done: |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5270 | buffer->raw = 0; |
| 5271 | buffer->frameCount = 0; |
| 5272 | } |
| 5273 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 5274 | size_t AudioFlinger::PlaybackThread::TimedTrack::framesReady() const { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5275 | Mutex::Autolock _l(mTimedBufferQueueLock); |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 5276 | return mFramesPendingInQueue; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5277 | } |
| 5278 | |
| 5279 | AudioFlinger::PlaybackThread::TimedTrack::TimedBuffer::TimedBuffer() |
| 5280 | : mPTS(0), mPosition(0) {} |
| 5281 | |
| 5282 | AudioFlinger::PlaybackThread::TimedTrack::TimedBuffer::TimedBuffer( |
| 5283 | const sp<IMemory>& buffer, int64_t pts) |
| 5284 | : mBuffer(buffer), mPTS(pts), mPosition(0) {} |
| 5285 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5286 | // ---------------------------------------------------------------------------- |
| 5287 | |
| 5288 | // RecordTrack constructor must be called with AudioFlinger::mLock held |
| 5289 | AudioFlinger::RecordThread::RecordTrack::RecordTrack( |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 5290 | RecordThread *thread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5291 | const sp<Client>& client, |
| 5292 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5293 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 5294 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5295 | int frameCount, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5296 | int sessionId) |
| 5297 | : TrackBase(thread, client, sampleRate, format, |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame] | 5298 | channelMask, frameCount, 0 /*sharedBuffer*/, sessionId), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5299 | mOverflow(false) |
| 5300 | { |
| 5301 | if (mCblk != NULL) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 5302 | ALOGV("RecordTrack constructor, size %d", (int)mBufferEnd - (int)mBuffer); |
| 5303 | if (format == AUDIO_FORMAT_PCM_16_BIT) { |
| 5304 | mCblk->frameSize = mChannelCount * sizeof(int16_t); |
| 5305 | } else if (format == AUDIO_FORMAT_PCM_8_BIT) { |
| 5306 | mCblk->frameSize = mChannelCount * sizeof(int8_t); |
| 5307 | } else { |
| 5308 | mCblk->frameSize = sizeof(int8_t); |
| 5309 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5310 | } |
| 5311 | } |
| 5312 | |
| 5313 | AudioFlinger::RecordThread::RecordTrack::~RecordTrack() |
| 5314 | { |
| 5315 | sp<ThreadBase> thread = mThread.promote(); |
| 5316 | if (thread != 0) { |
| 5317 | AudioSystem::releaseInput(thread->id()); |
| 5318 | } |
| 5319 | } |
| 5320 | |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 5321 | // AudioBufferProvider interface |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5322 | status_t AudioFlinger::RecordThread::RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5323 | { |
| 5324 | audio_track_cblk_t* cblk = this->cblk(); |
| 5325 | uint32_t framesAvail; |
| 5326 | uint32_t framesReq = buffer->frameCount; |
| 5327 | |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 5328 | // Check if last stepServer failed, try to step now |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame] | 5329 | if (mStepServerFailed) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5330 | if (!step()) goto getNextBuffer_exit; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5331 | ALOGV("stepServer recovered"); |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame] | 5332 | mStepServerFailed = false; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5333 | } |
| 5334 | |
| 5335 | framesAvail = cblk->framesAvailable_l(); |
| 5336 | |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 5337 | if (CC_LIKELY(framesAvail)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5338 | uint32_t s = cblk->server; |
| 5339 | uint32_t bufferEnd = cblk->serverBase + cblk->frameCount; |
| 5340 | |
| 5341 | if (framesReq > framesAvail) { |
| 5342 | framesReq = framesAvail; |
| 5343 | } |
Marco Nelissen | a1472d9 | 2012-03-30 14:36:54 -0700 | [diff] [blame] | 5344 | if (framesReq > bufferEnd - s) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5345 | framesReq = bufferEnd - s; |
| 5346 | } |
| 5347 | |
| 5348 | buffer->raw = getBuffer(s, framesReq); |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 5349 | if (buffer->raw == NULL) goto getNextBuffer_exit; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5350 | |
| 5351 | buffer->frameCount = framesReq; |
| 5352 | return NO_ERROR; |
| 5353 | } |
| 5354 | |
| 5355 | getNextBuffer_exit: |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 5356 | buffer->raw = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5357 | buffer->frameCount = 0; |
| 5358 | return NOT_ENOUGH_DATA; |
| 5359 | } |
| 5360 | |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5361 | status_t AudioFlinger::RecordThread::RecordTrack::start(AudioSystem::sync_event_t event, |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 5362 | int triggerSession) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5363 | { |
| 5364 | sp<ThreadBase> thread = mThread.promote(); |
| 5365 | if (thread != 0) { |
| 5366 | RecordThread *recordThread = (RecordThread *)thread.get(); |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5367 | return recordThread->start(this, event, triggerSession); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5368 | } else { |
| 5369 | return BAD_VALUE; |
| 5370 | } |
| 5371 | } |
| 5372 | |
| 5373 | void AudioFlinger::RecordThread::RecordTrack::stop() |
| 5374 | { |
| 5375 | sp<ThreadBase> thread = mThread.promote(); |
| 5376 | if (thread != 0) { |
| 5377 | RecordThread *recordThread = (RecordThread *)thread.get(); |
| 5378 | recordThread->stop(this); |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 5379 | TrackBase::reset(); |
Glenn Kasten | 17a736c | 2012-02-14 08:52:15 -0800 | [diff] [blame] | 5380 | // Force overrun condition to avoid false overrun callback until first data is |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 5381 | // read from buffer |
| 5382 | android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5383 | } |
| 5384 | } |
| 5385 | |
| 5386 | void AudioFlinger::RecordThread::RecordTrack::dump(char* buffer, size_t size) |
| 5387 | { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 5388 | snprintf(buffer, size, " %05d %03u 0x%08x %05d %04u %01d %05u %08x %08x\n", |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 5389 | (mClient == 0) ? getpid_cached : mClient->pid(), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5390 | mFormat, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 5391 | mChannelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5392 | mSessionId, |
| 5393 | mFrameCount, |
| 5394 | mState, |
| 5395 | mCblk->sampleRate, |
| 5396 | mCblk->server, |
| 5397 | mCblk->user); |
| 5398 | } |
| 5399 | |
| 5400 | |
| 5401 | // ---------------------------------------------------------------------------- |
| 5402 | |
| 5403 | AudioFlinger::PlaybackThread::OutputTrack::OutputTrack( |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 5404 | PlaybackThread *playbackThread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5405 | DuplicatingThread *sourceThread, |
| 5406 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5407 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 5408 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5409 | int frameCount) |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 5410 | : Track(playbackThread, NULL, AUDIO_STREAM_CNT, sampleRate, format, channelMask, frameCount, |
| 5411 | NULL, 0, IAudioFlinger::TRACK_DEFAULT), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5412 | mActive(false), mSourceThread(sourceThread) |
| 5413 | { |
| 5414 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5415 | if (mCblk != NULL) { |
| 5416 | mCblk->flags |= CBLK_DIRECTION_OUT; |
| 5417 | mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5418 | mOutBuffer.frameCount = 0; |
| 5419 | playbackThread->mTracks.add(this); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5420 | ALOGV("OutputTrack constructor mCblk %p, mBuffer %p, mCblk->buffers %p, " \ |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 5421 | "mCblk->frameCount %d, mCblk->sampleRate %d, mChannelMask 0x%08x mBufferEnd %p", |
| 5422 | mCblk, mBuffer, mCblk->buffers, |
| 5423 | mCblk->frameCount, mCblk->sampleRate, mChannelMask, mBufferEnd); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5424 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5425 | ALOGW("Error creating output track on thread %p", playbackThread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5426 | } |
| 5427 | } |
| 5428 | |
| 5429 | AudioFlinger::PlaybackThread::OutputTrack::~OutputTrack() |
| 5430 | { |
| 5431 | clearBufferQueue(); |
| 5432 | } |
| 5433 | |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5434 | status_t AudioFlinger::PlaybackThread::OutputTrack::start(AudioSystem::sync_event_t event, |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 5435 | int triggerSession) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5436 | { |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5437 | status_t status = Track::start(event, triggerSession); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5438 | if (status != NO_ERROR) { |
| 5439 | return status; |
| 5440 | } |
| 5441 | |
| 5442 | mActive = true; |
| 5443 | mRetryCount = 127; |
| 5444 | return status; |
| 5445 | } |
| 5446 | |
| 5447 | void AudioFlinger::PlaybackThread::OutputTrack::stop() |
| 5448 | { |
| 5449 | Track::stop(); |
| 5450 | clearBufferQueue(); |
| 5451 | mOutBuffer.frameCount = 0; |
| 5452 | mActive = false; |
| 5453 | } |
| 5454 | |
| 5455 | bool AudioFlinger::PlaybackThread::OutputTrack::write(int16_t* data, uint32_t frames) |
| 5456 | { |
| 5457 | Buffer *pInBuffer; |
| 5458 | Buffer inBuffer; |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 5459 | uint32_t channelCount = mChannelCount; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5460 | bool outputBufferFull = false; |
| 5461 | inBuffer.frameCount = frames; |
| 5462 | inBuffer.i16 = data; |
| 5463 | |
| 5464 | uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs(); |
| 5465 | |
| 5466 | if (!mActive && frames != 0) { |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5467 | start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5468 | sp<ThreadBase> thread = mThread.promote(); |
| 5469 | if (thread != 0) { |
| 5470 | MixerThread *mixerThread = (MixerThread *)thread.get(); |
| 5471 | if (mCblk->frameCount > frames){ |
| 5472 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { |
| 5473 | uint32_t startFrames = (mCblk->frameCount - frames); |
| 5474 | pInBuffer = new Buffer; |
| 5475 | pInBuffer->mBuffer = new int16_t[startFrames * channelCount]; |
| 5476 | pInBuffer->frameCount = startFrames; |
| 5477 | pInBuffer->i16 = pInBuffer->mBuffer; |
| 5478 | memset(pInBuffer->raw, 0, startFrames * channelCount * sizeof(int16_t)); |
| 5479 | mBufferQueue.add(pInBuffer); |
| 5480 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5481 | ALOGW ("OutputTrack::write() %p no more buffers in queue", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5482 | } |
| 5483 | } |
| 5484 | } |
| 5485 | } |
| 5486 | |
| 5487 | while (waitTimeLeftMs) { |
| 5488 | // First write pending buffers, then new data |
| 5489 | if (mBufferQueue.size()) { |
| 5490 | pInBuffer = mBufferQueue.itemAt(0); |
| 5491 | } else { |
| 5492 | pInBuffer = &inBuffer; |
| 5493 | } |
| 5494 | |
| 5495 | if (pInBuffer->frameCount == 0) { |
| 5496 | break; |
| 5497 | } |
| 5498 | |
| 5499 | if (mOutBuffer.frameCount == 0) { |
| 5500 | mOutBuffer.frameCount = pInBuffer->frameCount; |
| 5501 | nsecs_t startTime = systemTime(); |
Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 5502 | if (obtainBuffer(&mOutBuffer, waitTimeLeftMs) == (status_t)NO_MORE_BUFFERS) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5503 | ALOGV ("OutputTrack::write() %p thread %p no more output buffers", this, mThread.unsafe_get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5504 | outputBufferFull = true; |
| 5505 | break; |
| 5506 | } |
| 5507 | uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime); |
| 5508 | if (waitTimeLeftMs >= waitTimeMs) { |
| 5509 | waitTimeLeftMs -= waitTimeMs; |
| 5510 | } else { |
| 5511 | waitTimeLeftMs = 0; |
| 5512 | } |
| 5513 | } |
| 5514 | |
| 5515 | uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount : pInBuffer->frameCount; |
| 5516 | memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * channelCount * sizeof(int16_t)); |
| 5517 | mCblk->stepUser(outFrames); |
| 5518 | pInBuffer->frameCount -= outFrames; |
| 5519 | pInBuffer->i16 += outFrames * channelCount; |
| 5520 | mOutBuffer.frameCount -= outFrames; |
| 5521 | mOutBuffer.i16 += outFrames * channelCount; |
| 5522 | |
| 5523 | if (pInBuffer->frameCount == 0) { |
| 5524 | if (mBufferQueue.size()) { |
| 5525 | mBufferQueue.removeAt(0); |
| 5526 | delete [] pInBuffer->mBuffer; |
| 5527 | delete pInBuffer; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5528 | ALOGV("OutputTrack::write() %p thread %p released overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5529 | } else { |
| 5530 | break; |
| 5531 | } |
| 5532 | } |
| 5533 | } |
| 5534 | |
| 5535 | // If we could not write all frames, allocate a buffer and queue it for next time. |
| 5536 | if (inBuffer.frameCount) { |
| 5537 | sp<ThreadBase> thread = mThread.promote(); |
| 5538 | if (thread != 0 && !thread->standby()) { |
| 5539 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { |
| 5540 | pInBuffer = new Buffer; |
| 5541 | pInBuffer->mBuffer = new int16_t[inBuffer.frameCount * channelCount]; |
| 5542 | pInBuffer->frameCount = inBuffer.frameCount; |
| 5543 | pInBuffer->i16 = pInBuffer->mBuffer; |
| 5544 | memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * channelCount * sizeof(int16_t)); |
| 5545 | mBufferQueue.add(pInBuffer); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5546 | ALOGV("OutputTrack::write() %p thread %p adding overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5547 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5548 | ALOGW("OutputTrack::write() %p thread %p no more overflow buffers", mThread.unsafe_get(), this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5549 | } |
| 5550 | } |
| 5551 | } |
| 5552 | |
| 5553 | // Calling write() with a 0 length buffer, means that no more data will be written: |
| 5554 | // If no more buffers are pending, fill output track buffer to make sure it is started |
| 5555 | // by output mixer. |
| 5556 | if (frames == 0 && mBufferQueue.size() == 0) { |
| 5557 | if (mCblk->user < mCblk->frameCount) { |
| 5558 | frames = mCblk->frameCount - mCblk->user; |
| 5559 | pInBuffer = new Buffer; |
| 5560 | pInBuffer->mBuffer = new int16_t[frames * channelCount]; |
| 5561 | pInBuffer->frameCount = frames; |
| 5562 | pInBuffer->i16 = pInBuffer->mBuffer; |
| 5563 | memset(pInBuffer->raw, 0, frames * channelCount * sizeof(int16_t)); |
| 5564 | mBufferQueue.add(pInBuffer); |
| 5565 | } else if (mActive) { |
| 5566 | stop(); |
| 5567 | } |
| 5568 | } |
| 5569 | |
| 5570 | return outputBufferFull; |
| 5571 | } |
| 5572 | |
| 5573 | status_t AudioFlinger::PlaybackThread::OutputTrack::obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs) |
| 5574 | { |
| 5575 | int active; |
| 5576 | status_t result; |
| 5577 | audio_track_cblk_t* cblk = mCblk; |
| 5578 | uint32_t framesReq = buffer->frameCount; |
| 5579 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5580 | // ALOGV("OutputTrack::obtainBuffer user %d, server %d", cblk->user, cblk->server); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5581 | buffer->frameCount = 0; |
| 5582 | |
| 5583 | uint32_t framesAvail = cblk->framesAvailable(); |
| 5584 | |
| 5585 | |
| 5586 | if (framesAvail == 0) { |
| 5587 | Mutex::Autolock _l(cblk->lock); |
| 5588 | goto start_loop_here; |
| 5589 | while (framesAvail == 0) { |
| 5590 | active = mActive; |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 5591 | if (CC_UNLIKELY(!active)) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5592 | ALOGV("Not active and NO_MORE_BUFFERS"); |
Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 5593 | return NO_MORE_BUFFERS; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5594 | } |
| 5595 | result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs)); |
| 5596 | if (result != NO_ERROR) { |
Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 5597 | return NO_MORE_BUFFERS; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5598 | } |
| 5599 | // read the server count again |
| 5600 | start_loop_here: |
| 5601 | framesAvail = cblk->framesAvailable_l(); |
| 5602 | } |
| 5603 | } |
| 5604 | |
| 5605 | // if (framesAvail < framesReq) { |
Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 5606 | // return NO_MORE_BUFFERS; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5607 | // } |
| 5608 | |
| 5609 | if (framesReq > framesAvail) { |
| 5610 | framesReq = framesAvail; |
| 5611 | } |
| 5612 | |
| 5613 | uint32_t u = cblk->user; |
| 5614 | uint32_t bufferEnd = cblk->userBase + cblk->frameCount; |
| 5615 | |
Marco Nelissen | a1472d9 | 2012-03-30 14:36:54 -0700 | [diff] [blame] | 5616 | if (framesReq > bufferEnd - u) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5617 | framesReq = bufferEnd - u; |
| 5618 | } |
| 5619 | |
| 5620 | buffer->frameCount = framesReq; |
| 5621 | buffer->raw = (void *)cblk->buffer(u); |
| 5622 | return NO_ERROR; |
| 5623 | } |
| 5624 | |
| 5625 | |
| 5626 | void AudioFlinger::PlaybackThread::OutputTrack::clearBufferQueue() |
| 5627 | { |
| 5628 | size_t size = mBufferQueue.size(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5629 | |
| 5630 | for (size_t i = 0; i < size; i++) { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 5631 | Buffer *pBuffer = mBufferQueue.itemAt(i); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5632 | delete [] pBuffer->mBuffer; |
| 5633 | delete pBuffer; |
| 5634 | } |
| 5635 | mBufferQueue.clear(); |
| 5636 | } |
| 5637 | |
| 5638 | // ---------------------------------------------------------------------------- |
| 5639 | |
| 5640 | AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid) |
| 5641 | : RefBase(), |
| 5642 | mAudioFlinger(audioFlinger), |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 5643 | // FIXME should be a "k" constant not hard-coded, in .h or ro. property, see 4 lines below |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5644 | mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5645 | mPid(pid), |
| 5646 | mTimedTrackCount(0) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5647 | { |
| 5648 | // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer |
| 5649 | } |
| 5650 | |
| 5651 | // Client destructor must be called with AudioFlinger::mLock held |
| 5652 | AudioFlinger::Client::~Client() |
| 5653 | { |
| 5654 | mAudioFlinger->removeClient_l(mPid); |
| 5655 | } |
| 5656 | |
Glenn Kasten | 435dbe6 | 2012-01-30 10:15:48 -0800 | [diff] [blame] | 5657 | sp<MemoryDealer> AudioFlinger::Client::heap() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5658 | { |
| 5659 | return mMemoryDealer; |
| 5660 | } |
| 5661 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5662 | // Reserve one of the limited slots for a timed audio track associated |
| 5663 | // with this client |
| 5664 | bool AudioFlinger::Client::reserveTimedTrack() |
| 5665 | { |
| 5666 | const int kMaxTimedTracksPerClient = 4; |
| 5667 | |
| 5668 | Mutex::Autolock _l(mTimedTrackLock); |
| 5669 | |
| 5670 | if (mTimedTrackCount >= kMaxTimedTracksPerClient) { |
| 5671 | ALOGW("can not create timed track - pid %d has exceeded the limit", |
| 5672 | mPid); |
| 5673 | return false; |
| 5674 | } |
| 5675 | |
| 5676 | mTimedTrackCount++; |
| 5677 | return true; |
| 5678 | } |
| 5679 | |
| 5680 | // Release a slot for a timed audio track |
| 5681 | void AudioFlinger::Client::releaseTimedTrack() |
| 5682 | { |
| 5683 | Mutex::Autolock _l(mTimedTrackLock); |
| 5684 | mTimedTrackCount--; |
| 5685 | } |
| 5686 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5687 | // ---------------------------------------------------------------------------- |
| 5688 | |
| 5689 | AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger, |
| 5690 | const sp<IAudioFlingerClient>& client, |
| 5691 | pid_t pid) |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 5692 | : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5693 | { |
| 5694 | } |
| 5695 | |
| 5696 | AudioFlinger::NotificationClient::~NotificationClient() |
| 5697 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5698 | } |
| 5699 | |
| 5700 | void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who) |
| 5701 | { |
| 5702 | sp<NotificationClient> keep(this); |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 5703 | mAudioFlinger->removeNotificationClient(mPid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5704 | } |
| 5705 | |
| 5706 | // ---------------------------------------------------------------------------- |
| 5707 | |
| 5708 | AudioFlinger::TrackHandle::TrackHandle(const sp<AudioFlinger::PlaybackThread::Track>& track) |
| 5709 | : BnAudioTrack(), |
| 5710 | mTrack(track) |
| 5711 | { |
| 5712 | } |
| 5713 | |
| 5714 | AudioFlinger::TrackHandle::~TrackHandle() { |
| 5715 | // just stop the track on deletion, associated resources |
| 5716 | // will be freed from the main thread once all pending buffers have |
| 5717 | // been played. Unless it's not in the active track list, in which |
| 5718 | // case we free everything now... |
| 5719 | mTrack->destroy(); |
| 5720 | } |
| 5721 | |
Glenn Kasten | 90716c5 | 2012-01-26 13:40:12 -0800 | [diff] [blame] | 5722 | sp<IMemory> AudioFlinger::TrackHandle::getCblk() const { |
| 5723 | return mTrack->getCblk(); |
| 5724 | } |
| 5725 | |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5726 | status_t AudioFlinger::TrackHandle::start() { |
| 5727 | return mTrack->start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5728 | } |
| 5729 | |
| 5730 | void AudioFlinger::TrackHandle::stop() { |
| 5731 | mTrack->stop(); |
| 5732 | } |
| 5733 | |
| 5734 | void AudioFlinger::TrackHandle::flush() { |
| 5735 | mTrack->flush(); |
| 5736 | } |
| 5737 | |
| 5738 | void AudioFlinger::TrackHandle::mute(bool e) { |
| 5739 | mTrack->mute(e); |
| 5740 | } |
| 5741 | |
| 5742 | void AudioFlinger::TrackHandle::pause() { |
| 5743 | mTrack->pause(); |
| 5744 | } |
| 5745 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5746 | status_t AudioFlinger::TrackHandle::attachAuxEffect(int EffectId) |
| 5747 | { |
| 5748 | return mTrack->attachAuxEffect(EffectId); |
| 5749 | } |
| 5750 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5751 | status_t AudioFlinger::TrackHandle::allocateTimedBuffer(size_t size, |
| 5752 | sp<IMemory>* buffer) { |
| 5753 | if (!mTrack->isTimedTrack()) |
| 5754 | return INVALID_OPERATION; |
| 5755 | |
| 5756 | PlaybackThread::TimedTrack* tt = |
| 5757 | reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get()); |
| 5758 | return tt->allocateTimedBuffer(size, buffer); |
| 5759 | } |
| 5760 | |
| 5761 | status_t AudioFlinger::TrackHandle::queueTimedBuffer(const sp<IMemory>& buffer, |
| 5762 | int64_t pts) { |
| 5763 | if (!mTrack->isTimedTrack()) |
| 5764 | return INVALID_OPERATION; |
| 5765 | |
| 5766 | PlaybackThread::TimedTrack* tt = |
| 5767 | reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get()); |
| 5768 | return tt->queueTimedBuffer(buffer, pts); |
| 5769 | } |
| 5770 | |
| 5771 | status_t AudioFlinger::TrackHandle::setMediaTimeTransform( |
| 5772 | const LinearTransform& xform, int target) { |
| 5773 | |
| 5774 | if (!mTrack->isTimedTrack()) |
| 5775 | return INVALID_OPERATION; |
| 5776 | |
| 5777 | PlaybackThread::TimedTrack* tt = |
| 5778 | reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get()); |
| 5779 | return tt->setMediaTimeTransform( |
| 5780 | xform, static_cast<TimedAudioTrack::TargetTimeline>(target)); |
| 5781 | } |
| 5782 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5783 | status_t AudioFlinger::TrackHandle::onTransact( |
| 5784 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 5785 | { |
| 5786 | return BnAudioTrack::onTransact(code, data, reply, flags); |
| 5787 | } |
| 5788 | |
| 5789 | // ---------------------------------------------------------------------------- |
| 5790 | |
| 5791 | sp<IAudioRecord> AudioFlinger::openRecord( |
| 5792 | pid_t pid, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 5793 | audio_io_handle_t input, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5794 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5795 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 5796 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5797 | int frameCount, |
Glenn Kasten | a075db4 | 2012-03-06 11:22:44 -0800 | [diff] [blame] | 5798 | IAudioFlinger::track_flags_t flags, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5799 | int *sessionId, |
| 5800 | status_t *status) |
| 5801 | { |
| 5802 | sp<RecordThread::RecordTrack> recordTrack; |
| 5803 | sp<RecordHandle> recordHandle; |
| 5804 | sp<Client> client; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5805 | status_t lStatus; |
| 5806 | RecordThread *thread; |
| 5807 | size_t inFrameCount; |
| 5808 | int lSessionId; |
| 5809 | |
| 5810 | // check calling permissions |
| 5811 | if (!recordingAllowed()) { |
| 5812 | lStatus = PERMISSION_DENIED; |
| 5813 | goto Exit; |
| 5814 | } |
| 5815 | |
| 5816 | // add client to list |
| 5817 | { // scope for mLock |
| 5818 | Mutex::Autolock _l(mLock); |
| 5819 | thread = checkRecordThread_l(input); |
| 5820 | if (thread == NULL) { |
| 5821 | lStatus = BAD_VALUE; |
| 5822 | goto Exit; |
| 5823 | } |
| 5824 | |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 5825 | client = registerPid_l(pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5826 | |
| 5827 | // If no audio session id is provided, create one here |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5828 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5829 | lSessionId = *sessionId; |
| 5830 | } else { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5831 | lSessionId = nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5832 | if (sessionId != NULL) { |
| 5833 | *sessionId = lSessionId; |
| 5834 | } |
| 5835 | } |
| 5836 | // create new record track. The record track uses one track in mHardwareMixerThread by convention. |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5837 | recordTrack = thread->createRecordTrack_l(client, |
| 5838 | sampleRate, |
| 5839 | format, |
| 5840 | channelMask, |
| 5841 | frameCount, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5842 | lSessionId, |
| 5843 | &lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5844 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5845 | if (lStatus != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5846 | // remove local strong reference to Client before deleting the RecordTrack so that the Client |
| 5847 | // destructor is called by the TrackBase destructor with mLock held |
| 5848 | client.clear(); |
| 5849 | recordTrack.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5850 | goto Exit; |
| 5851 | } |
| 5852 | |
| 5853 | // return to handle to client |
| 5854 | recordHandle = new RecordHandle(recordTrack); |
| 5855 | lStatus = NO_ERROR; |
| 5856 | |
| 5857 | Exit: |
| 5858 | if (status) { |
| 5859 | *status = lStatus; |
| 5860 | } |
| 5861 | return recordHandle; |
| 5862 | } |
| 5863 | |
| 5864 | // ---------------------------------------------------------------------------- |
| 5865 | |
| 5866 | AudioFlinger::RecordHandle::RecordHandle(const sp<AudioFlinger::RecordThread::RecordTrack>& recordTrack) |
| 5867 | : BnAudioRecord(), |
| 5868 | mRecordTrack(recordTrack) |
| 5869 | { |
| 5870 | } |
| 5871 | |
| 5872 | AudioFlinger::RecordHandle::~RecordHandle() { |
| 5873 | stop(); |
| 5874 | } |
| 5875 | |
Glenn Kasten | 90716c5 | 2012-01-26 13:40:12 -0800 | [diff] [blame] | 5876 | sp<IMemory> AudioFlinger::RecordHandle::getCblk() const { |
| 5877 | return mRecordTrack->getCblk(); |
| 5878 | } |
| 5879 | |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5880 | status_t AudioFlinger::RecordHandle::start(int event, int triggerSession) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5881 | ALOGV("RecordHandle::start()"); |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5882 | return mRecordTrack->start((AudioSystem::sync_event_t)event, triggerSession); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5883 | } |
| 5884 | |
| 5885 | void AudioFlinger::RecordHandle::stop() { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5886 | ALOGV("RecordHandle::stop()"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5887 | mRecordTrack->stop(); |
| 5888 | } |
| 5889 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5890 | status_t AudioFlinger::RecordHandle::onTransact( |
| 5891 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 5892 | { |
| 5893 | return BnAudioRecord::onTransact(code, data, reply, flags); |
| 5894 | } |
| 5895 | |
| 5896 | // ---------------------------------------------------------------------------- |
| 5897 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5898 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, |
| 5899 | AudioStreamIn *input, |
| 5900 | uint32_t sampleRate, |
| 5901 | uint32_t channels, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 5902 | audio_io_handle_t id, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5903 | uint32_t device) : |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 5904 | ThreadBase(audioFlinger, id, device, RECORD), |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 5905 | mInput(input), mTrack(NULL), mResampler(NULL), mRsmpOutBuffer(NULL), mRsmpInBuffer(NULL), |
| 5906 | // mRsmpInIndex and mInputBytes set by readInputParameters() |
| 5907 | mReqChannelCount(popcount(channels)), |
| 5908 | mReqSampleRate(sampleRate) |
| 5909 | // mBytesRead is only meaningful while active, and so is cleared in start() |
| 5910 | // (but might be better to also clear here for dump?) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5911 | { |
Glenn Kasten | 480b468 | 2012-02-28 12:30:08 -0800 | [diff] [blame] | 5912 | snprintf(mName, kNameLength, "AudioIn_%X", id); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 5913 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5914 | readInputParameters(); |
| 5915 | } |
| 5916 | |
| 5917 | |
| 5918 | AudioFlinger::RecordThread::~RecordThread() |
| 5919 | { |
| 5920 | delete[] mRsmpInBuffer; |
Glenn Kasten | e9dd017 | 2012-01-27 18:08:45 -0800 | [diff] [blame] | 5921 | delete mResampler; |
| 5922 | delete[] mRsmpOutBuffer; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5923 | } |
| 5924 | |
| 5925 | void AudioFlinger::RecordThread::onFirstRef() |
| 5926 | { |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 5927 | run(mName, PRIORITY_URGENT_AUDIO); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5928 | } |
| 5929 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5930 | status_t AudioFlinger::RecordThread::readyToRun() |
| 5931 | { |
| 5932 | status_t status = initCheck(); |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5933 | ALOGW_IF(status != NO_ERROR,"RecordThread %p could not initialize", this); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5934 | return status; |
| 5935 | } |
| 5936 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5937 | bool AudioFlinger::RecordThread::threadLoop() |
| 5938 | { |
| 5939 | AudioBufferProvider::Buffer buffer; |
| 5940 | sp<RecordTrack> activeTrack; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5941 | Vector< sp<EffectChain> > effectChains; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5942 | |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 5943 | nsecs_t lastWarning = 0; |
| 5944 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 5945 | acquireWakeLock(); |
| 5946 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5947 | // start recording |
| 5948 | while (!exitPending()) { |
| 5949 | |
| 5950 | processConfigEvents(); |
| 5951 | |
| 5952 | { // scope for mLock |
| 5953 | Mutex::Autolock _l(mLock); |
| 5954 | checkForNewParameters_l(); |
| 5955 | if (mActiveTrack == 0 && mConfigEvents.isEmpty()) { |
| 5956 | if (!mStandby) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5957 | mInput->stream->common.standby(&mInput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5958 | mStandby = true; |
| 5959 | } |
| 5960 | |
| 5961 | if (exitPending()) break; |
| 5962 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 5963 | releaseWakeLock_l(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5964 | ALOGV("RecordThread: loop stopping"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5965 | // go to sleep |
| 5966 | mWaitWorkCV.wait(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5967 | ALOGV("RecordThread: loop starting"); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 5968 | acquireWakeLock_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5969 | continue; |
| 5970 | } |
| 5971 | if (mActiveTrack != 0) { |
| 5972 | if (mActiveTrack->mState == TrackBase::PAUSING) { |
| 5973 | if (!mStandby) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5974 | mInput->stream->common.standby(&mInput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5975 | mStandby = true; |
| 5976 | } |
| 5977 | mActiveTrack.clear(); |
| 5978 | mStartStopCond.broadcast(); |
| 5979 | } else if (mActiveTrack->mState == TrackBase::RESUMING) { |
| 5980 | if (mReqChannelCount != mActiveTrack->channelCount()) { |
| 5981 | mActiveTrack.clear(); |
| 5982 | mStartStopCond.broadcast(); |
| 5983 | } else if (mBytesRead != 0) { |
| 5984 | // record start succeeds only if first read from audio input |
| 5985 | // succeeds |
| 5986 | if (mBytesRead > 0) { |
| 5987 | mActiveTrack->mState = TrackBase::ACTIVE; |
| 5988 | } else { |
| 5989 | mActiveTrack.clear(); |
| 5990 | } |
| 5991 | mStartStopCond.broadcast(); |
| 5992 | } |
| 5993 | mStandby = false; |
| 5994 | } |
| 5995 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5996 | lockEffectChains_l(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5997 | } |
| 5998 | |
| 5999 | if (mActiveTrack != 0) { |
| 6000 | if (mActiveTrack->mState != TrackBase::ACTIVE && |
| 6001 | mActiveTrack->mState != TrackBase::RESUMING) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6002 | unlockEffectChains(effectChains); |
| 6003 | usleep(kRecordThreadSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6004 | continue; |
| 6005 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6006 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 6007 | effectChains[i]->process_l(); |
| 6008 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6009 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6010 | buffer.frameCount = mFrameCount; |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 6011 | if (CC_LIKELY(mActiveTrack->getNextBuffer(&buffer) == NO_ERROR)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6012 | size_t framesOut = buffer.frameCount; |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 6013 | if (mResampler == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6014 | // no resampling |
| 6015 | while (framesOut) { |
| 6016 | size_t framesIn = mFrameCount - mRsmpInIndex; |
| 6017 | if (framesIn) { |
| 6018 | int8_t *src = (int8_t *)mRsmpInBuffer + mRsmpInIndex * mFrameSize; |
| 6019 | int8_t *dst = buffer.i8 + (buffer.frameCount - framesOut) * mActiveTrack->mCblk->frameSize; |
| 6020 | if (framesIn > framesOut) |
| 6021 | framesIn = framesOut; |
| 6022 | mRsmpInIndex += framesIn; |
| 6023 | framesOut -= framesIn; |
| 6024 | if ((int)mChannelCount == mReqChannelCount || |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6025 | mFormat != AUDIO_FORMAT_PCM_16_BIT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6026 | memcpy(dst, src, framesIn * mFrameSize); |
| 6027 | } else { |
| 6028 | int16_t *src16 = (int16_t *)src; |
| 6029 | int16_t *dst16 = (int16_t *)dst; |
| 6030 | if (mChannelCount == 1) { |
| 6031 | while (framesIn--) { |
| 6032 | *dst16++ = *src16; |
| 6033 | *dst16++ = *src16++; |
| 6034 | } |
| 6035 | } else { |
| 6036 | while (framesIn--) { |
| 6037 | *dst16++ = (int16_t)(((int32_t)*src16 + (int32_t)*(src16 + 1)) >> 1); |
| 6038 | src16 += 2; |
| 6039 | } |
| 6040 | } |
| 6041 | } |
| 6042 | } |
| 6043 | if (framesOut && mFrameCount == mRsmpInIndex) { |
| 6044 | if (framesOut == mFrameCount && |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6045 | ((int)mChannelCount == mReqChannelCount || mFormat != AUDIO_FORMAT_PCM_16_BIT)) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6046 | mBytesRead = mInput->stream->read(mInput->stream, buffer.raw, mInputBytes); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6047 | framesOut = 0; |
| 6048 | } else { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6049 | mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6050 | mRsmpInIndex = 0; |
| 6051 | } |
| 6052 | if (mBytesRead < 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 6053 | ALOGE("Error reading audio input"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6054 | if (mActiveTrack->mState == TrackBase::ACTIVE) { |
| 6055 | // Force input into standby so that it tries to |
| 6056 | // recover at next read attempt |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6057 | mInput->stream->common.standby(&mInput->stream->common); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6058 | usleep(kRecordThreadSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6059 | } |
| 6060 | mRsmpInIndex = mFrameCount; |
| 6061 | framesOut = 0; |
| 6062 | buffer.frameCount = 0; |
| 6063 | } |
| 6064 | } |
| 6065 | } |
| 6066 | } else { |
| 6067 | // resampling |
| 6068 | |
| 6069 | memset(mRsmpOutBuffer, 0, framesOut * 2 * sizeof(int32_t)); |
| 6070 | // alter output frame count as if we were expecting stereo samples |
| 6071 | if (mChannelCount == 1 && mReqChannelCount == 1) { |
| 6072 | framesOut >>= 1; |
| 6073 | } |
| 6074 | mResampler->resample(mRsmpOutBuffer, framesOut, this); |
| 6075 | // ditherAndClamp() works as long as all buffers returned by mActiveTrack->getNextBuffer() |
| 6076 | // are 32 bit aligned which should be always true. |
| 6077 | if (mChannelCount == 2 && mReqChannelCount == 1) { |
Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 6078 | ditherAndClamp(mRsmpOutBuffer, mRsmpOutBuffer, framesOut); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6079 | // the resampler always outputs stereo samples: do post stereo to mono conversion |
| 6080 | int16_t *src = (int16_t *)mRsmpOutBuffer; |
| 6081 | int16_t *dst = buffer.i16; |
| 6082 | while (framesOut--) { |
| 6083 | *dst++ = (int16_t)(((int32_t)*src + (int32_t)*(src + 1)) >> 1); |
| 6084 | src += 2; |
| 6085 | } |
| 6086 | } else { |
Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 6087 | ditherAndClamp((int32_t *)buffer.raw, mRsmpOutBuffer, framesOut); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6088 | } |
| 6089 | |
| 6090 | } |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 6091 | if (mFramestoDrop == 0) { |
| 6092 | mActiveTrack->releaseBuffer(&buffer); |
| 6093 | } else { |
| 6094 | if (mFramestoDrop > 0) { |
| 6095 | mFramestoDrop -= buffer.frameCount; |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 6096 | if (mFramestoDrop <= 0) { |
| 6097 | clearSyncStartEvent(); |
| 6098 | } |
| 6099 | } else { |
| 6100 | mFramestoDrop += buffer.frameCount; |
| 6101 | if (mFramestoDrop >= 0 || mSyncStartEvent == 0 || |
| 6102 | mSyncStartEvent->isCancelled()) { |
| 6103 | ALOGW("Synced record %s, session %d, trigger session %d", |
| 6104 | (mFramestoDrop >= 0) ? "timed out" : "cancelled", |
| 6105 | mActiveTrack->sessionId(), |
| 6106 | (mSyncStartEvent != 0) ? mSyncStartEvent->triggerSession() : 0); |
| 6107 | clearSyncStartEvent(); |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 6108 | } |
| 6109 | } |
| 6110 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6111 | mActiveTrack->overflow(); |
| 6112 | } |
| 6113 | // client isn't retrieving buffers fast enough |
| 6114 | else { |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 6115 | if (!mActiveTrack->setOverflow()) { |
| 6116 | nsecs_t now = systemTime(); |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 6117 | if ((now - lastWarning) > kWarningThrottleNs) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6118 | ALOGW("RecordThread: buffer overflow"); |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 6119 | lastWarning = now; |
| 6120 | } |
| 6121 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6122 | // Release the processor for a while before asking for a new buffer. |
| 6123 | // This will give the application more chance to read from the buffer and |
| 6124 | // clear the overflow. |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6125 | usleep(kRecordThreadSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6126 | } |
| 6127 | } |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6128 | // enable changes in effect chain |
| 6129 | unlockEffectChains(effectChains); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6130 | effectChains.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6131 | } |
| 6132 | |
| 6133 | if (!mStandby) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6134 | mInput->stream->common.standby(&mInput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6135 | } |
| 6136 | mActiveTrack.clear(); |
| 6137 | |
| 6138 | mStartStopCond.broadcast(); |
| 6139 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 6140 | releaseWakeLock(); |
| 6141 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6142 | ALOGV("RecordThread %p exiting", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6143 | return false; |
| 6144 | } |
| 6145 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6146 | |
| 6147 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( |
| 6148 | const sp<AudioFlinger::Client>& client, |
| 6149 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 6150 | audio_format_t format, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6151 | int channelMask, |
| 6152 | int frameCount, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6153 | int sessionId, |
| 6154 | status_t *status) |
| 6155 | { |
| 6156 | sp<RecordTrack> track; |
| 6157 | status_t lStatus; |
| 6158 | |
| 6159 | lStatus = initCheck(); |
| 6160 | if (lStatus != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 6161 | ALOGE("Audio driver not initialized."); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6162 | goto Exit; |
| 6163 | } |
| 6164 | |
| 6165 | { // scope for mLock |
| 6166 | Mutex::Autolock _l(mLock); |
| 6167 | |
| 6168 | track = new RecordTrack(this, client, sampleRate, |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame] | 6169 | format, channelMask, frameCount, sessionId); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6170 | |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 6171 | if (track->getCblk() == 0) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6172 | lStatus = NO_MEMORY; |
| 6173 | goto Exit; |
| 6174 | } |
| 6175 | |
| 6176 | mTrack = track.get(); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 6177 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 6178 | bool suspend = audio_is_bluetooth_sco_device( |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 6179 | (audio_devices_t)(mDevice & AUDIO_DEVICE_IN_ALL)) && mAudioFlinger->btNrecIsOff(); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 6180 | setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); |
| 6181 | setEffectSuspended_l(FX_IID_NS, suspend, sessionId); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6182 | } |
| 6183 | lStatus = NO_ERROR; |
| 6184 | |
| 6185 | Exit: |
| 6186 | if (status) { |
| 6187 | *status = lStatus; |
| 6188 | } |
| 6189 | return track; |
| 6190 | } |
| 6191 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 6192 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 6193 | AudioSystem::sync_event_t event, |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 6194 | int triggerSession) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6195 | { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 6196 | ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 6197 | sp<ThreadBase> strongMe = this; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6198 | status_t status = NO_ERROR; |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 6199 | |
| 6200 | if (event == AudioSystem::SYNC_EVENT_NONE) { |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 6201 | clearSyncStartEvent(); |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 6202 | } else if (event != AudioSystem::SYNC_EVENT_SAME) { |
| 6203 | mSyncStartEvent = mAudioFlinger->createSyncEvent(event, |
| 6204 | triggerSession, |
| 6205 | recordTrack->sessionId(), |
| 6206 | syncStartEventCallback, |
| 6207 | this); |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 6208 | // Sync event can be cancelled by the trigger session if the track is not in a |
| 6209 | // compatible state in which case we start record immediately |
| 6210 | if (mSyncStartEvent->isCancelled()) { |
| 6211 | clearSyncStartEvent(); |
| 6212 | } else { |
| 6213 | // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs |
| 6214 | mFramestoDrop = - ((AudioSystem::kSyncRecordStartTimeOutMs * mReqSampleRate) / 1000); |
| 6215 | } |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 6216 | } |
| 6217 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6218 | { |
Glenn Kasten | a7d8d6f | 2012-01-05 15:41:56 -0800 | [diff] [blame] | 6219 | AutoMutex lock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6220 | if (mActiveTrack != 0) { |
| 6221 | if (recordTrack != mActiveTrack.get()) { |
| 6222 | status = -EBUSY; |
| 6223 | } else if (mActiveTrack->mState == TrackBase::PAUSING) { |
| 6224 | mActiveTrack->mState = TrackBase::ACTIVE; |
| 6225 | } |
| 6226 | return status; |
| 6227 | } |
| 6228 | |
| 6229 | recordTrack->mState = TrackBase::IDLE; |
| 6230 | mActiveTrack = recordTrack; |
| 6231 | mLock.unlock(); |
| 6232 | status_t status = AudioSystem::startInput(mId); |
| 6233 | mLock.lock(); |
| 6234 | if (status != NO_ERROR) { |
| 6235 | mActiveTrack.clear(); |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 6236 | clearSyncStartEvent(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6237 | return status; |
| 6238 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6239 | mRsmpInIndex = mFrameCount; |
| 6240 | mBytesRead = 0; |
Eric Laurent | 243f5f9 | 2011-02-28 16:52:51 -0800 | [diff] [blame] | 6241 | if (mResampler != NULL) { |
| 6242 | mResampler->reset(); |
| 6243 | } |
| 6244 | mActiveTrack->mState = TrackBase::RESUMING; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6245 | // signal thread to start |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6246 | ALOGV("Signal record thread"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6247 | mWaitWorkCV.signal(); |
| 6248 | // do not wait for mStartStopCond if exiting |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 6249 | if (exitPending()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6250 | mActiveTrack.clear(); |
| 6251 | status = INVALID_OPERATION; |
| 6252 | goto startError; |
| 6253 | } |
| 6254 | mStartStopCond.wait(mLock); |
| 6255 | if (mActiveTrack == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6256 | ALOGV("Record failed to start"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6257 | status = BAD_VALUE; |
| 6258 | goto startError; |
| 6259 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6260 | ALOGV("Record started OK"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6261 | return status; |
| 6262 | } |
| 6263 | startError: |
| 6264 | AudioSystem::stopInput(mId); |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 6265 | clearSyncStartEvent(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6266 | return status; |
| 6267 | } |
| 6268 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 6269 | void AudioFlinger::RecordThread::clearSyncStartEvent() |
| 6270 | { |
| 6271 | if (mSyncStartEvent != 0) { |
| 6272 | mSyncStartEvent->cancel(); |
| 6273 | } |
| 6274 | mSyncStartEvent.clear(); |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 6275 | mFramestoDrop = 0; |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 6276 | } |
| 6277 | |
| 6278 | void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) |
| 6279 | { |
| 6280 | sp<SyncEvent> strongEvent = event.promote(); |
| 6281 | |
| 6282 | if (strongEvent != 0) { |
| 6283 | RecordThread *me = (RecordThread *)strongEvent->cookie(); |
| 6284 | me->handleSyncStartEvent(strongEvent); |
| 6285 | } |
| 6286 | } |
| 6287 | |
| 6288 | void AudioFlinger::RecordThread::handleSyncStartEvent(const sp<SyncEvent>& event) |
| 6289 | { |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 6290 | if (event == mSyncStartEvent) { |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 6291 | // TODO: use actual buffer filling status instead of 2 buffers when info is available |
| 6292 | // from audio HAL |
| 6293 | mFramestoDrop = mFrameCount * 2; |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 6294 | } |
| 6295 | } |
| 6296 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6297 | void AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6298 | ALOGV("RecordThread::stop"); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 6299 | sp<ThreadBase> strongMe = this; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6300 | { |
Glenn Kasten | a7d8d6f | 2012-01-05 15:41:56 -0800 | [diff] [blame] | 6301 | AutoMutex lock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6302 | if (mActiveTrack != 0 && recordTrack == mActiveTrack.get()) { |
| 6303 | mActiveTrack->mState = TrackBase::PAUSING; |
| 6304 | // do not wait for mStartStopCond if exiting |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 6305 | if (exitPending()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6306 | return; |
| 6307 | } |
| 6308 | mStartStopCond.wait(mLock); |
| 6309 | // if we have been restarted, recordTrack == mActiveTrack.get() here |
| 6310 | if (mActiveTrack == 0 || recordTrack != mActiveTrack.get()) { |
| 6311 | mLock.unlock(); |
| 6312 | AudioSystem::stopInput(mId); |
| 6313 | mLock.lock(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6314 | ALOGV("Record stopped OK"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6315 | } |
| 6316 | } |
| 6317 | } |
| 6318 | } |
| 6319 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 6320 | bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event) |
| 6321 | { |
| 6322 | return false; |
| 6323 | } |
| 6324 | |
| 6325 | status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event) |
| 6326 | { |
| 6327 | if (!isValidSyncEvent(event)) { |
| 6328 | return BAD_VALUE; |
| 6329 | } |
| 6330 | |
| 6331 | Mutex::Autolock _l(mLock); |
| 6332 | |
| 6333 | if (mTrack != NULL && event->triggerSession() == mTrack->sessionId()) { |
| 6334 | mTrack->setSyncEvent(event); |
| 6335 | return NO_ERROR; |
| 6336 | } |
| 6337 | return NAME_NOT_FOUND; |
| 6338 | } |
| 6339 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6340 | status_t AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) |
| 6341 | { |
| 6342 | const size_t SIZE = 256; |
| 6343 | char buffer[SIZE]; |
| 6344 | String8 result; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6345 | |
| 6346 | snprintf(buffer, SIZE, "\nInput thread %p internals\n", this); |
| 6347 | result.append(buffer); |
| 6348 | |
| 6349 | if (mActiveTrack != 0) { |
| 6350 | result.append("Active Track:\n"); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 6351 | result.append(" Clien Fmt Chn mask Session Buf S SRate Serv User\n"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6352 | mActiveTrack->dump(buffer, SIZE); |
| 6353 | result.append(buffer); |
| 6354 | |
| 6355 | snprintf(buffer, SIZE, "In index: %d\n", mRsmpInIndex); |
| 6356 | result.append(buffer); |
| 6357 | snprintf(buffer, SIZE, "In size: %d\n", mInputBytes); |
| 6358 | result.append(buffer); |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 6359 | snprintf(buffer, SIZE, "Resampling: %d\n", (mResampler != NULL)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6360 | result.append(buffer); |
| 6361 | snprintf(buffer, SIZE, "Out channel count: %d\n", mReqChannelCount); |
| 6362 | result.append(buffer); |
| 6363 | snprintf(buffer, SIZE, "Out sample rate: %d\n", mReqSampleRate); |
| 6364 | result.append(buffer); |
| 6365 | |
| 6366 | |
| 6367 | } else { |
| 6368 | result.append("No record client\n"); |
| 6369 | } |
| 6370 | write(fd, result.string(), result.size()); |
| 6371 | |
| 6372 | dumpBase(fd, args); |
Eric Laurent | 1d2bff0 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 6373 | dumpEffectChains(fd, args); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6374 | |
| 6375 | return NO_ERROR; |
| 6376 | } |
| 6377 | |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 6378 | // AudioBufferProvider interface |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 6379 | status_t AudioFlinger::RecordThread::getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6380 | { |
| 6381 | size_t framesReq = buffer->frameCount; |
| 6382 | size_t framesReady = mFrameCount - mRsmpInIndex; |
| 6383 | int channelCount; |
| 6384 | |
| 6385 | if (framesReady == 0) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6386 | mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6387 | if (mBytesRead < 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 6388 | ALOGE("RecordThread::getNextBuffer() Error reading audio input"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6389 | if (mActiveTrack->mState == TrackBase::ACTIVE) { |
| 6390 | // Force input into standby so that it tries to |
| 6391 | // recover at next read attempt |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6392 | mInput->stream->common.standby(&mInput->stream->common); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6393 | usleep(kRecordThreadSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6394 | } |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 6395 | buffer->raw = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6396 | buffer->frameCount = 0; |
| 6397 | return NOT_ENOUGH_DATA; |
| 6398 | } |
| 6399 | mRsmpInIndex = 0; |
| 6400 | framesReady = mFrameCount; |
| 6401 | } |
| 6402 | |
| 6403 | if (framesReq > framesReady) { |
| 6404 | framesReq = framesReady; |
| 6405 | } |
| 6406 | |
| 6407 | if (mChannelCount == 1 && mReqChannelCount == 2) { |
| 6408 | channelCount = 1; |
| 6409 | } else { |
| 6410 | channelCount = 2; |
| 6411 | } |
| 6412 | buffer->raw = mRsmpInBuffer + mRsmpInIndex * channelCount; |
| 6413 | buffer->frameCount = framesReq; |
| 6414 | return NO_ERROR; |
| 6415 | } |
| 6416 | |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 6417 | // AudioBufferProvider interface |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6418 | void AudioFlinger::RecordThread::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
| 6419 | { |
| 6420 | mRsmpInIndex += buffer->frameCount; |
| 6421 | buffer->frameCount = 0; |
| 6422 | } |
| 6423 | |
| 6424 | bool AudioFlinger::RecordThread::checkForNewParameters_l() |
| 6425 | { |
| 6426 | bool reconfig = false; |
| 6427 | |
| 6428 | while (!mNewParameters.isEmpty()) { |
| 6429 | status_t status = NO_ERROR; |
| 6430 | String8 keyValuePair = mNewParameters[0]; |
| 6431 | AudioParameter param = AudioParameter(keyValuePair); |
| 6432 | int value; |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 6433 | audio_format_t reqFormat = mFormat; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6434 | int reqSamplingRate = mReqSampleRate; |
| 6435 | int reqChannelCount = mReqChannelCount; |
| 6436 | |
| 6437 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 6438 | reqSamplingRate = value; |
| 6439 | reconfig = true; |
| 6440 | } |
| 6441 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 6442 | reqFormat = (audio_format_t) value; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6443 | reconfig = true; |
| 6444 | } |
| 6445 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6446 | reqChannelCount = popcount(value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6447 | reconfig = true; |
| 6448 | } |
| 6449 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 6450 | // do not accept frame count changes if tracks are open as the track buffer |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 6451 | // size depends on frame count and correct behavior would not be guaranteed |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6452 | // if frame count is changed after track creation |
| 6453 | if (mActiveTrack != 0) { |
| 6454 | status = INVALID_OPERATION; |
| 6455 | } else { |
| 6456 | reconfig = true; |
| 6457 | } |
| 6458 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6459 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 6460 | // forward device change to effects that have requested to be |
| 6461 | // aware of attached audio device. |
| 6462 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 6463 | mEffectChains[i]->setDevice_l(value); |
| 6464 | } |
| 6465 | // store input device and output device but do not forward output device to audio HAL. |
| 6466 | // Note that status is ignored by the caller for output device |
| 6467 | // (see AudioFlinger::setParameters() |
| 6468 | if (value & AUDIO_DEVICE_OUT_ALL) { |
| 6469 | mDevice &= (uint32_t)~(value & AUDIO_DEVICE_OUT_ALL); |
| 6470 | status = BAD_VALUE; |
| 6471 | } else { |
| 6472 | mDevice &= (uint32_t)~(value & AUDIO_DEVICE_IN_ALL); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 6473 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 6474 | if (mTrack != NULL) { |
| 6475 | bool suspend = audio_is_bluetooth_sco_device( |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 6476 | (audio_devices_t)value) && mAudioFlinger->btNrecIsOff(); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 6477 | setEffectSuspended_l(FX_IID_AEC, suspend, mTrack->sessionId()); |
| 6478 | setEffectSuspended_l(FX_IID_NS, suspend, mTrack->sessionId()); |
| 6479 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6480 | } |
| 6481 | mDevice |= (uint32_t)value; |
| 6482 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6483 | if (status == NO_ERROR) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6484 | status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6485 | if (status == INVALID_OPERATION) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 6486 | mInput->stream->common.standby(&mInput->stream->common); |
| 6487 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 6488 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6489 | } |
| 6490 | if (reconfig) { |
| 6491 | if (status == BAD_VALUE && |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6492 | reqFormat == mInput->stream->common.get_format(&mInput->stream->common) && |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6493 | reqFormat == AUDIO_FORMAT_PCM_16_BIT && |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6494 | ((int)mInput->stream->common.get_sample_rate(&mInput->stream->common) <= (2 * reqSamplingRate)) && |
Glenn Kasten | 53d76db | 2012-03-08 12:32:47 -0800 | [diff] [blame] | 6495 | popcount(mInput->stream->common.get_channels(&mInput->stream->common)) <= FCC_2 && |
| 6496 | (reqChannelCount <= FCC_2)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6497 | status = NO_ERROR; |
| 6498 | } |
| 6499 | if (status == NO_ERROR) { |
| 6500 | readInputParameters(); |
| 6501 | sendConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED); |
| 6502 | } |
| 6503 | } |
| 6504 | } |
| 6505 | |
| 6506 | mNewParameters.removeAt(0); |
| 6507 | |
| 6508 | mParamStatus = status; |
| 6509 | mParamCond.signal(); |
Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 6510 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() |
| 6511 | // already timed out waiting for the status and will never signal the condition. |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 6512 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6513 | } |
| 6514 | return reconfig; |
| 6515 | } |
| 6516 | |
| 6517 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) |
| 6518 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6519 | char *s; |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6520 | String8 out_s8 = String8(); |
| 6521 | |
| 6522 | Mutex::Autolock _l(mLock); |
| 6523 | if (initCheck() != NO_ERROR) { |
| 6524 | return out_s8; |
| 6525 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6526 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6527 | s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6528 | out_s8 = String8(s); |
| 6529 | free(s); |
| 6530 | return out_s8; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6531 | } |
| 6532 | |
| 6533 | void AudioFlinger::RecordThread::audioConfigChanged_l(int event, int param) { |
| 6534 | AudioSystem::OutputDescriptor desc; |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 6535 | void *param2 = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6536 | |
| 6537 | switch (event) { |
| 6538 | case AudioSystem::INPUT_OPENED: |
| 6539 | case AudioSystem::INPUT_CONFIG_CHANGED: |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 6540 | desc.channels = mChannelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6541 | desc.samplingRate = mSampleRate; |
| 6542 | desc.format = mFormat; |
| 6543 | desc.frameCount = mFrameCount; |
| 6544 | desc.latency = 0; |
| 6545 | param2 = &desc; |
| 6546 | break; |
| 6547 | |
| 6548 | case AudioSystem::INPUT_CLOSED: |
| 6549 | default: |
| 6550 | break; |
| 6551 | } |
| 6552 | mAudioFlinger->audioConfigChanged_l(event, mId, param2); |
| 6553 | } |
| 6554 | |
| 6555 | void AudioFlinger::RecordThread::readInputParameters() |
| 6556 | { |
Glenn Kasten | e9dd017 | 2012-01-27 18:08:45 -0800 | [diff] [blame] | 6557 | delete mRsmpInBuffer; |
| 6558 | // mRsmpInBuffer is always assigned a new[] below |
| 6559 | delete mRsmpOutBuffer; |
| 6560 | mRsmpOutBuffer = NULL; |
| 6561 | delete mResampler; |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 6562 | mResampler = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6563 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6564 | mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 6565 | mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); |
| 6566 | mChannelCount = (uint16_t)popcount(mChannelMask); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6567 | mFormat = mInput->stream->common.get_format(&mInput->stream->common); |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 6568 | mFrameSize = audio_stream_frame_size(&mInput->stream->common); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6569 | mInputBytes = mInput->stream->common.get_buffer_size(&mInput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6570 | mFrameCount = mInputBytes / mFrameSize; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 6571 | mNormalFrameCount = mFrameCount; // not used by record, but used by input effects |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6572 | mRsmpInBuffer = new int16_t[mFrameCount * mChannelCount]; |
| 6573 | |
Glenn Kasten | 53d76db | 2012-03-08 12:32:47 -0800 | [diff] [blame] | 6574 | if (mSampleRate != mReqSampleRate && mChannelCount <= FCC_2 && mReqChannelCount <= FCC_2) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6575 | { |
| 6576 | int channelCount; |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 6577 | // optimization: if mono to mono, use the resampler in stereo to stereo mode to avoid |
| 6578 | // stereo to mono post process as the resampler always outputs stereo. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6579 | if (mChannelCount == 1 && mReqChannelCount == 2) { |
| 6580 | channelCount = 1; |
| 6581 | } else { |
| 6582 | channelCount = 2; |
| 6583 | } |
| 6584 | mResampler = AudioResampler::create(16, channelCount, mReqSampleRate); |
| 6585 | mResampler->setSampleRate(mSampleRate); |
| 6586 | mResampler->setVolume(AudioMixer::UNITY_GAIN, AudioMixer::UNITY_GAIN); |
| 6587 | mRsmpOutBuffer = new int32_t[mFrameCount * 2]; |
| 6588 | |
| 6589 | // optmization: if mono to mono, alter input frame count as if we were inputing stereo samples |
| 6590 | if (mChannelCount == 1 && mReqChannelCount == 1) { |
| 6591 | mFrameCount >>= 1; |
| 6592 | } |
| 6593 | |
| 6594 | } |
| 6595 | mRsmpInIndex = mFrameCount; |
| 6596 | } |
| 6597 | |
| 6598 | unsigned int AudioFlinger::RecordThread::getInputFramesLost() |
| 6599 | { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6600 | Mutex::Autolock _l(mLock); |
| 6601 | if (initCheck() != NO_ERROR) { |
| 6602 | return 0; |
| 6603 | } |
| 6604 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6605 | return mInput->stream->get_input_frames_lost(mInput->stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6606 | } |
| 6607 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6608 | uint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) |
| 6609 | { |
| 6610 | Mutex::Autolock _l(mLock); |
| 6611 | uint32_t result = 0; |
| 6612 | if (getEffectChain_l(sessionId) != 0) { |
| 6613 | result = EFFECT_SESSION; |
| 6614 | } |
| 6615 | |
| 6616 | if (mTrack != NULL && sessionId == mTrack->sessionId()) { |
| 6617 | result |= TRACK_SESSION; |
| 6618 | } |
| 6619 | |
| 6620 | return result; |
| 6621 | } |
| 6622 | |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 6623 | AudioFlinger::RecordThread::RecordTrack* AudioFlinger::RecordThread::track() |
| 6624 | { |
| 6625 | Mutex::Autolock _l(mLock); |
| 6626 | return mTrack; |
| 6627 | } |
| 6628 | |
Glenn Kasten | aed850d | 2012-01-26 09:46:34 -0800 | [diff] [blame] | 6629 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::getInput() const |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6630 | { |
| 6631 | Mutex::Autolock _l(mLock); |
| 6632 | return mInput; |
| 6633 | } |
| 6634 | |
| 6635 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() |
| 6636 | { |
| 6637 | Mutex::Autolock _l(mLock); |
| 6638 | AudioStreamIn *input = mInput; |
| 6639 | mInput = NULL; |
| 6640 | return input; |
| 6641 | } |
| 6642 | |
| 6643 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 6644 | audio_stream_t* AudioFlinger::RecordThread::stream() const |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6645 | { |
| 6646 | if (mInput == NULL) { |
| 6647 | return NULL; |
| 6648 | } |
| 6649 | return &mInput->stream->common; |
| 6650 | } |
| 6651 | |
| 6652 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6653 | // ---------------------------------------------------------------------------- |
| 6654 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6655 | audio_module_handle_t AudioFlinger::loadHwModule(const char *name) |
| 6656 | { |
| 6657 | if (!settingsAllowed()) { |
| 6658 | return 0; |
| 6659 | } |
| 6660 | Mutex::Autolock _l(mLock); |
| 6661 | return loadHwModule_l(name); |
| 6662 | } |
| 6663 | |
| 6664 | // loadHwModule_l() must be called with AudioFlinger::mLock held |
| 6665 | audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name) |
| 6666 | { |
| 6667 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 6668 | if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) { |
| 6669 | ALOGW("loadHwModule() module %s already loaded", name); |
| 6670 | return mAudioHwDevs.keyAt(i); |
| 6671 | } |
| 6672 | } |
| 6673 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6674 | audio_hw_device_t *dev; |
| 6675 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6676 | int rc = load_audio_interface(name, &dev); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6677 | if (rc) { |
| 6678 | ALOGI("loadHwModule() error %d loading module %s ", rc, name); |
| 6679 | return 0; |
| 6680 | } |
| 6681 | |
| 6682 | mHardwareStatus = AUDIO_HW_INIT; |
| 6683 | rc = dev->init_check(dev); |
| 6684 | mHardwareStatus = AUDIO_HW_IDLE; |
| 6685 | if (rc) { |
| 6686 | ALOGI("loadHwModule() init check error %d for module %s ", rc, name); |
| 6687 | return 0; |
| 6688 | } |
| 6689 | |
| 6690 | if ((mMasterVolumeSupportLvl != MVS_NONE) && |
| 6691 | (NULL != dev->set_master_volume)) { |
| 6692 | AutoMutex lock(mHardwareLock); |
| 6693 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
| 6694 | dev->set_master_volume(dev, mMasterVolume); |
| 6695 | mHardwareStatus = AUDIO_HW_IDLE; |
| 6696 | } |
| 6697 | |
| 6698 | audio_module_handle_t handle = nextUniqueId(); |
| 6699 | mAudioHwDevs.add(handle, new AudioHwDevice(name, dev)); |
| 6700 | |
| 6701 | ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d", |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6702 | name, dev->common.module->name, dev->common.module->id, handle); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6703 | |
| 6704 | return handle; |
| 6705 | |
| 6706 | } |
| 6707 | |
| 6708 | audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module, |
| 6709 | audio_devices_t *pDevices, |
| 6710 | uint32_t *pSamplingRate, |
| 6711 | audio_format_t *pFormat, |
| 6712 | audio_channel_mask_t *pChannelMask, |
| 6713 | uint32_t *pLatencyMs, |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 6714 | audio_output_flags_t flags) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6715 | { |
| 6716 | status_t status; |
| 6717 | PlaybackThread *thread = NULL; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6718 | struct audio_config config = { |
| 6719 | sample_rate: pSamplingRate ? *pSamplingRate : 0, |
| 6720 | channel_mask: pChannelMask ? *pChannelMask : 0, |
| 6721 | format: pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT, |
| 6722 | }; |
| 6723 | audio_stream_out_t *outStream = NULL; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6724 | audio_hw_device_t *outHwDev; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6725 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6726 | ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %d, Channels %x, flags %x", |
| 6727 | module, |
Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 6728 | (pDevices != NULL) ? (int)*pDevices : 0, |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6729 | config.sample_rate, |
| 6730 | config.format, |
| 6731 | config.channel_mask, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6732 | flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6733 | |
| 6734 | if (pDevices == NULL || *pDevices == 0) { |
| 6735 | return 0; |
| 6736 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6737 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6738 | Mutex::Autolock _l(mLock); |
| 6739 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6740 | outHwDev = findSuitableHwDev_l(module, *pDevices); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6741 | if (outHwDev == NULL) |
| 6742 | return 0; |
| 6743 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6744 | audio_io_handle_t id = nextUniqueId(); |
| 6745 | |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 6746 | mHardwareStatus = AUDIO_HW_OUTPUT_OPEN; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6747 | |
| 6748 | status = outHwDev->open_output_stream(outHwDev, |
| 6749 | id, |
| 6750 | *pDevices, |
| 6751 | (audio_output_flags_t)flags, |
| 6752 | &config, |
| 6753 | &outStream); |
| 6754 | |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 6755 | mHardwareStatus = AUDIO_HW_IDLE; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6756 | ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, Channels %x, status %d", |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6757 | outStream, |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6758 | config.sample_rate, |
| 6759 | config.format, |
| 6760 | config.channel_mask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6761 | status); |
| 6762 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6763 | if (status == NO_ERROR && outStream != NULL) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6764 | AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6765 | |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 6766 | if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) || |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6767 | (config.format != AUDIO_FORMAT_PCM_16_BIT) || |
| 6768 | (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6769 | thread = new DirectOutputThread(this, output, id, *pDevices); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6770 | ALOGV("openOutput() created direct output: ID %d thread %p", id, thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6771 | } else { |
| 6772 | thread = new MixerThread(this, output, id, *pDevices); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6773 | ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6774 | } |
| 6775 | mPlaybackThreads.add(id, thread); |
| 6776 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6777 | if (pSamplingRate != NULL) *pSamplingRate = config.sample_rate; |
| 6778 | if (pFormat != NULL) *pFormat = config.format; |
| 6779 | if (pChannelMask != NULL) *pChannelMask = config.channel_mask; |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 6780 | if (pLatencyMs != NULL) *pLatencyMs = thread->latency(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6781 | |
| 6782 | // notify client processes of the new output creation |
| 6783 | thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6784 | |
| 6785 | // the first primary output opened designates the primary hw device |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 6786 | if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6787 | ALOGI("Using module %d has the primary audio interface", module); |
| 6788 | mPrimaryHardwareDev = outHwDev; |
| 6789 | |
| 6790 | AutoMutex lock(mHardwareLock); |
| 6791 | mHardwareStatus = AUDIO_HW_SET_MODE; |
| 6792 | outHwDev->set_mode(outHwDev, mMode); |
| 6793 | |
| 6794 | // Determine the level of master volume support the primary audio HAL has, |
| 6795 | // and set the initial master volume at the same time. |
| 6796 | float initialVolume = 1.0; |
| 6797 | mMasterVolumeSupportLvl = MVS_NONE; |
| 6798 | |
| 6799 | mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME; |
| 6800 | if ((NULL != outHwDev->get_master_volume) && |
| 6801 | (NO_ERROR == outHwDev->get_master_volume(outHwDev, &initialVolume))) { |
| 6802 | mMasterVolumeSupportLvl = MVS_FULL; |
| 6803 | } else { |
| 6804 | mMasterVolumeSupportLvl = MVS_SETONLY; |
| 6805 | initialVolume = 1.0; |
| 6806 | } |
| 6807 | |
| 6808 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
| 6809 | if ((NULL == outHwDev->set_master_volume) || |
| 6810 | (NO_ERROR != outHwDev->set_master_volume(outHwDev, initialVolume))) { |
| 6811 | mMasterVolumeSupportLvl = MVS_NONE; |
| 6812 | } |
| 6813 | // now that we have a primary device, initialize master volume on other devices |
| 6814 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 6815 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
| 6816 | |
| 6817 | if ((dev != mPrimaryHardwareDev) && |
| 6818 | (NULL != dev->set_master_volume)) { |
| 6819 | dev->set_master_volume(dev, initialVolume); |
| 6820 | } |
| 6821 | } |
| 6822 | mHardwareStatus = AUDIO_HW_IDLE; |
| 6823 | mMasterVolumeSW = (MVS_NONE == mMasterVolumeSupportLvl) |
| 6824 | ? initialVolume |
| 6825 | : 1.0; |
| 6826 | mMasterVolume = initialVolume; |
| 6827 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6828 | return id; |
| 6829 | } |
| 6830 | |
| 6831 | return 0; |
| 6832 | } |
| 6833 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6834 | audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1, |
| 6835 | audio_io_handle_t output2) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6836 | { |
| 6837 | Mutex::Autolock _l(mLock); |
| 6838 | MixerThread *thread1 = checkMixerThread_l(output1); |
| 6839 | MixerThread *thread2 = checkMixerThread_l(output2); |
| 6840 | |
| 6841 | if (thread1 == NULL || thread2 == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6842 | ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1, output2); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6843 | return 0; |
| 6844 | } |
| 6845 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6846 | audio_io_handle_t id = nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6847 | DuplicatingThread *thread = new DuplicatingThread(this, thread1, id); |
| 6848 | thread->addOutputTrack(thread2); |
| 6849 | mPlaybackThreads.add(id, thread); |
| 6850 | // notify client processes of the new output creation |
| 6851 | thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); |
| 6852 | return id; |
| 6853 | } |
| 6854 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6855 | status_t AudioFlinger::closeOutput(audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6856 | { |
| 6857 | // keep strong reference on the playback thread so that |
| 6858 | // it is not destroyed while exit() is executed |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 6859 | sp<PlaybackThread> thread; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6860 | { |
| 6861 | Mutex::Autolock _l(mLock); |
| 6862 | thread = checkPlaybackThread_l(output); |
| 6863 | if (thread == NULL) { |
| 6864 | return BAD_VALUE; |
| 6865 | } |
| 6866 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6867 | ALOGV("closeOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6868 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6869 | if (thread->type() == ThreadBase::MIXER) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6870 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6871 | if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6872 | DuplicatingThread *dupThread = (DuplicatingThread *)mPlaybackThreads.valueAt(i).get(); |
| 6873 | dupThread->removeOutputTrack((MixerThread *)thread.get()); |
| 6874 | } |
| 6875 | } |
| 6876 | } |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 6877 | audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6878 | mPlaybackThreads.removeItem(output); |
| 6879 | } |
| 6880 | thread->exit(); |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 6881 | // The thread entity (active unit of execution) is no longer running here, |
| 6882 | // but the ThreadBase container still exists. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6883 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6884 | if (thread->type() != ThreadBase::DUPLICATING) { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6885 | AudioStreamOut *out = thread->clearOutput(); |
Glenn Kasten | 5798d4e | 2012-03-08 12:18:35 -0800 | [diff] [blame] | 6886 | ALOG_ASSERT(out != NULL, "out shouldn't be NULL"); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6887 | // from now on thread->mOutput is NULL |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6888 | out->hwDev->close_output_stream(out->hwDev, out->stream); |
| 6889 | delete out; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6890 | } |
| 6891 | return NO_ERROR; |
| 6892 | } |
| 6893 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6894 | status_t AudioFlinger::suspendOutput(audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6895 | { |
| 6896 | Mutex::Autolock _l(mLock); |
| 6897 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 6898 | |
| 6899 | if (thread == NULL) { |
| 6900 | return BAD_VALUE; |
| 6901 | } |
| 6902 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6903 | ALOGV("suspendOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6904 | thread->suspend(); |
| 6905 | |
| 6906 | return NO_ERROR; |
| 6907 | } |
| 6908 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6909 | status_t AudioFlinger::restoreOutput(audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6910 | { |
| 6911 | Mutex::Autolock _l(mLock); |
| 6912 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 6913 | |
| 6914 | if (thread == NULL) { |
| 6915 | return BAD_VALUE; |
| 6916 | } |
| 6917 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6918 | ALOGV("restoreOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6919 | |
| 6920 | thread->restore(); |
| 6921 | |
| 6922 | return NO_ERROR; |
| 6923 | } |
| 6924 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6925 | audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module, |
| 6926 | audio_devices_t *pDevices, |
| 6927 | uint32_t *pSamplingRate, |
| 6928 | audio_format_t *pFormat, |
| 6929 | uint32_t *pChannelMask) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6930 | { |
| 6931 | status_t status; |
| 6932 | RecordThread *thread = NULL; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6933 | struct audio_config config = { |
| 6934 | sample_rate: pSamplingRate ? *pSamplingRate : 0, |
| 6935 | channel_mask: pChannelMask ? *pChannelMask : 0, |
| 6936 | format: pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT, |
| 6937 | }; |
| 6938 | uint32_t reqSamplingRate = config.sample_rate; |
| 6939 | audio_format_t reqFormat = config.format; |
| 6940 | audio_channel_mask_t reqChannels = config.channel_mask; |
| 6941 | audio_stream_in_t *inStream = NULL; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6942 | audio_hw_device_t *inHwDev; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6943 | |
| 6944 | if (pDevices == NULL || *pDevices == 0) { |
| 6945 | return 0; |
| 6946 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6947 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6948 | Mutex::Autolock _l(mLock); |
| 6949 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6950 | inHwDev = findSuitableHwDev_l(module, *pDevices); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6951 | if (inHwDev == NULL) |
| 6952 | return 0; |
| 6953 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6954 | audio_io_handle_t id = nextUniqueId(); |
| 6955 | |
| 6956 | status = inHwDev->open_input_stream(inHwDev, id, *pDevices, &config, |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6957 | &inStream); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6958 | ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, status %d", |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6959 | inStream, |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6960 | config.sample_rate, |
| 6961 | config.format, |
| 6962 | config.channel_mask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6963 | status); |
| 6964 | |
| 6965 | // If the input could not be opened with the requested parameters and we can handle the conversion internally, |
| 6966 | // try to open again with the proposed parameters. The AudioFlinger can resample the input and do mono to stereo |
| 6967 | // or stereo to mono conversions on 16 bit PCM inputs. |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6968 | if (status == BAD_VALUE && |
| 6969 | reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT && |
| 6970 | (config.sample_rate <= 2 * reqSamplingRate) && |
| 6971 | (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannels) <= FCC_2)) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6972 | ALOGV("openInput() reopening with proposed sampling rate and channels"); |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6973 | inStream = NULL; |
| 6974 | status = inHwDev->open_input_stream(inHwDev, id, *pDevices, &config, &inStream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6975 | } |
| 6976 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6977 | if (status == NO_ERROR && inStream != NULL) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6978 | AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream); |
| 6979 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6980 | // Start record thread |
| 6981 | // RecorThread require both input and output device indication to forward to audio |
| 6982 | // pre processing modules |
| 6983 | uint32_t device = (*pDevices) | primaryOutputDevice_l(); |
| 6984 | thread = new RecordThread(this, |
| 6985 | input, |
| 6986 | reqSamplingRate, |
| 6987 | reqChannels, |
| 6988 | id, |
| 6989 | device); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6990 | mRecordThreads.add(id, thread); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6991 | ALOGV("openInput() created record thread: ID %d thread %p", id, thread); |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 6992 | if (pSamplingRate != NULL) *pSamplingRate = reqSamplingRate; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6993 | if (pFormat != NULL) *pFormat = config.format; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6994 | if (pChannelMask != NULL) *pChannelMask = reqChannels; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6995 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6996 | input->stream->common.standby(&input->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6997 | |
| 6998 | // notify client processes of the new input creation |
| 6999 | thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED); |
| 7000 | return id; |
| 7001 | } |
| 7002 | |
| 7003 | return 0; |
| 7004 | } |
| 7005 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 7006 | status_t AudioFlinger::closeInput(audio_io_handle_t input) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7007 | { |
| 7008 | // keep strong reference on the record thread so that |
| 7009 | // it is not destroyed while exit() is executed |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 7010 | sp<RecordThread> thread; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7011 | { |
| 7012 | Mutex::Autolock _l(mLock); |
| 7013 | thread = checkRecordThread_l(input); |
| 7014 | if (thread == NULL) { |
| 7015 | return BAD_VALUE; |
| 7016 | } |
| 7017 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7018 | ALOGV("closeInput() %d", input); |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 7019 | audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7020 | mRecordThreads.removeItem(input); |
| 7021 | } |
| 7022 | thread->exit(); |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 7023 | // The thread entity (active unit of execution) is no longer running here, |
| 7024 | // but the ThreadBase container still exists. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7025 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 7026 | AudioStreamIn *in = thread->clearInput(); |
Glenn Kasten | 5798d4e | 2012-03-08 12:18:35 -0800 | [diff] [blame] | 7027 | ALOG_ASSERT(in != NULL, "in shouldn't be NULL"); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 7028 | // from now on thread->mInput is NULL |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 7029 | in->hwDev->close_input_stream(in->hwDev, in->stream); |
| 7030 | delete in; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7031 | |
| 7032 | return NO_ERROR; |
| 7033 | } |
| 7034 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 7035 | status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7036 | { |
| 7037 | Mutex::Autolock _l(mLock); |
| 7038 | MixerThread *dstThread = checkMixerThread_l(output); |
| 7039 | if (dstThread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7040 | ALOGW("setStreamOutput() bad output id %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7041 | return BAD_VALUE; |
| 7042 | } |
| 7043 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7044 | ALOGV("setStreamOutput() stream %d to output %d", stream, output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7045 | audioConfigChanged_l(AudioSystem::STREAM_CONFIG_CHANGED, output, &stream); |
| 7046 | |
| 7047 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 7048 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 7049 | if (thread != dstThread && thread->type() != ThreadBase::DIRECT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7050 | MixerThread *srcThread = (MixerThread *)thread; |
| 7051 | srcThread->invalidateTracks(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7052 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7053 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7054 | |
| 7055 | return NO_ERROR; |
| 7056 | } |
| 7057 | |
| 7058 | |
| 7059 | int AudioFlinger::newAudioSessionId() |
| 7060 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7061 | return nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7062 | } |
| 7063 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7064 | void AudioFlinger::acquireAudioSessionId(int audioSession) |
| 7065 | { |
| 7066 | Mutex::Autolock _l(mLock); |
Glenn Kasten | bb00192 | 2012-02-03 11:10:26 -0800 | [diff] [blame] | 7067 | pid_t caller = IPCThreadState::self()->getCallingPid(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7068 | ALOGV("acquiring %d from %d", audioSession, caller); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 7069 | size_t num = mAudioSessionRefs.size(); |
| 7070 | for (size_t i = 0; i< num; i++) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7071 | AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 7072 | if (ref->mSessionid == audioSession && ref->mPid == caller) { |
| 7073 | ref->mCnt++; |
| 7074 | ALOGV(" incremented refcount to %d", ref->mCnt); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7075 | return; |
| 7076 | } |
| 7077 | } |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 7078 | mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller)); |
| 7079 | ALOGV(" added new entry for %d", audioSession); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7080 | } |
| 7081 | |
| 7082 | void AudioFlinger::releaseAudioSessionId(int audioSession) |
| 7083 | { |
| 7084 | Mutex::Autolock _l(mLock); |
Glenn Kasten | bb00192 | 2012-02-03 11:10:26 -0800 | [diff] [blame] | 7085 | pid_t caller = IPCThreadState::self()->getCallingPid(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7086 | ALOGV("releasing %d from %d", audioSession, caller); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 7087 | size_t num = mAudioSessionRefs.size(); |
| 7088 | for (size_t i = 0; i< num; i++) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7089 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 7090 | if (ref->mSessionid == audioSession && ref->mPid == caller) { |
| 7091 | ref->mCnt--; |
| 7092 | ALOGV(" decremented refcount to %d", ref->mCnt); |
| 7093 | if (ref->mCnt == 0) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7094 | mAudioSessionRefs.removeAt(i); |
| 7095 | delete ref; |
| 7096 | purgeStaleEffects_l(); |
| 7097 | } |
| 7098 | return; |
| 7099 | } |
| 7100 | } |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7101 | ALOGW("session id %d not found for pid %d", audioSession, caller); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7102 | } |
| 7103 | |
| 7104 | void AudioFlinger::purgeStaleEffects_l() { |
| 7105 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7106 | ALOGV("purging stale effects"); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7107 | |
| 7108 | Vector< sp<EffectChain> > chains; |
| 7109 | |
| 7110 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 7111 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 7112 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 7113 | sp<EffectChain> ec = t->mEffectChains[j]; |
Marco Nelissen | 0270b18 | 2011-08-12 14:14:39 -0700 | [diff] [blame] | 7114 | if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) { |
| 7115 | chains.push(ec); |
| 7116 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7117 | } |
| 7118 | } |
| 7119 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 7120 | sp<RecordThread> t = mRecordThreads.valueAt(i); |
| 7121 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 7122 | sp<EffectChain> ec = t->mEffectChains[j]; |
| 7123 | chains.push(ec); |
| 7124 | } |
| 7125 | } |
| 7126 | |
| 7127 | for (size_t i = 0; i < chains.size(); i++) { |
| 7128 | sp<EffectChain> ec = chains[i]; |
| 7129 | int sessionid = ec->sessionId(); |
| 7130 | sp<ThreadBase> t = ec->mThread.promote(); |
| 7131 | if (t == 0) { |
| 7132 | continue; |
| 7133 | } |
| 7134 | size_t numsessionrefs = mAudioSessionRefs.size(); |
| 7135 | bool found = false; |
| 7136 | for (size_t k = 0; k < numsessionrefs; k++) { |
| 7137 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(k); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 7138 | if (ref->mSessionid == sessionid) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7139 | ALOGV(" session %d still exists for %d with %d refs", |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 7140 | sessionid, ref->mPid, ref->mCnt); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7141 | found = true; |
| 7142 | break; |
| 7143 | } |
| 7144 | } |
| 7145 | if (!found) { |
| 7146 | // remove all effects from the chain |
| 7147 | while (ec->mEffects.size()) { |
| 7148 | sp<EffectModule> effect = ec->mEffects[0]; |
| 7149 | effect->unPin(); |
| 7150 | Mutex::Autolock _l (t->mLock); |
| 7151 | t->removeEffect_l(effect); |
| 7152 | for (size_t j = 0; j < effect->mHandles.size(); j++) { |
| 7153 | sp<EffectHandle> handle = effect->mHandles[j].promote(); |
| 7154 | if (handle != 0) { |
| 7155 | handle->mEffect.clear(); |
Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 7156 | if (handle->mHasControl && handle->mEnabled) { |
| 7157 | t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId()); |
| 7158 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7159 | } |
| 7160 | } |
| 7161 | AudioSystem::unregisterEffect(effect->id()); |
| 7162 | } |
| 7163 | } |
| 7164 | } |
| 7165 | return; |
| 7166 | } |
| 7167 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7168 | // checkPlaybackThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 7169 | AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7170 | { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 7171 | return mPlaybackThreads.valueFor(output).get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7172 | } |
| 7173 | |
| 7174 | // checkMixerThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 7175 | AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7176 | { |
| 7177 | PlaybackThread *thread = checkPlaybackThread_l(output); |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 7178 | return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7179 | } |
| 7180 | |
| 7181 | // checkRecordThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 7182 | AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7183 | { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 7184 | return mRecordThreads.valueFor(input).get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7185 | } |
| 7186 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7187 | uint32_t AudioFlinger::nextUniqueId() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7188 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7189 | return android_atomic_inc(&mNextUniqueId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7190 | } |
| 7191 | |
Glenn Kasten | 02fe1bf | 2012-02-24 15:42:17 -0800 | [diff] [blame] | 7192 | AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7193 | { |
| 7194 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 7195 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 7196 | AudioStreamOut *output = thread->getOutput(); |
| 7197 | if (output != NULL && output->hwDev == mPrimaryHardwareDev) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7198 | return thread; |
| 7199 | } |
| 7200 | } |
| 7201 | return NULL; |
| 7202 | } |
| 7203 | |
Glenn Kasten | 02fe1bf | 2012-02-24 15:42:17 -0800 | [diff] [blame] | 7204 | uint32_t AudioFlinger::primaryOutputDevice_l() const |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7205 | { |
| 7206 | PlaybackThread *thread = primaryPlaybackThread_l(); |
| 7207 | |
| 7208 | if (thread == NULL) { |
| 7209 | return 0; |
| 7210 | } |
| 7211 | |
| 7212 | return thread->device(); |
| 7213 | } |
| 7214 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 7215 | sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type, |
| 7216 | int triggerSession, |
| 7217 | int listenerSession, |
| 7218 | sync_event_callback_t callBack, |
| 7219 | void *cookie) |
| 7220 | { |
| 7221 | Mutex::Autolock _l(mLock); |
| 7222 | |
| 7223 | sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie); |
| 7224 | status_t playStatus = NAME_NOT_FOUND; |
| 7225 | status_t recStatus = NAME_NOT_FOUND; |
| 7226 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 7227 | playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event); |
| 7228 | if (playStatus == NO_ERROR) { |
| 7229 | return event; |
| 7230 | } |
| 7231 | } |
| 7232 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 7233 | recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event); |
| 7234 | if (recStatus == NO_ERROR) { |
| 7235 | return event; |
| 7236 | } |
| 7237 | } |
| 7238 | if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) { |
| 7239 | mPendingSyncEvents.add(event); |
| 7240 | } else { |
| 7241 | ALOGV("createSyncEvent() invalid event %d", event->type()); |
| 7242 | event.clear(); |
| 7243 | } |
| 7244 | return event; |
| 7245 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7246 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7247 | // ---------------------------------------------------------------------------- |
| 7248 | // Effect management |
| 7249 | // ---------------------------------------------------------------------------- |
| 7250 | |
| 7251 | |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 7252 | status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7253 | { |
| 7254 | Mutex::Autolock _l(mLock); |
| 7255 | return EffectQueryNumberEffects(numEffects); |
| 7256 | } |
| 7257 | |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 7258 | status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7259 | { |
| 7260 | Mutex::Autolock _l(mLock); |
| 7261 | return EffectQueryEffect(index, descriptor); |
| 7262 | } |
| 7263 | |
Glenn Kasten | 5e92a78 | 2012-01-30 07:40:52 -0800 | [diff] [blame] | 7264 | status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid, |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 7265 | effect_descriptor_t *descriptor) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7266 | { |
| 7267 | Mutex::Autolock _l(mLock); |
| 7268 | return EffectGetDescriptor(pUuid, descriptor); |
| 7269 | } |
| 7270 | |
| 7271 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7272 | sp<IEffect> AudioFlinger::createEffect(pid_t pid, |
| 7273 | effect_descriptor_t *pDesc, |
| 7274 | const sp<IEffectClient>& effectClient, |
| 7275 | int32_t priority, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 7276 | audio_io_handle_t io, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7277 | int sessionId, |
| 7278 | status_t *status, |
| 7279 | int *id, |
| 7280 | int *enabled) |
| 7281 | { |
| 7282 | status_t lStatus = NO_ERROR; |
| 7283 | sp<EffectHandle> handle; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7284 | effect_descriptor_t desc; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7285 | |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 7286 | ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d", |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7287 | pid, effectClient.get(), priority, sessionId, io); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7288 | |
| 7289 | if (pDesc == NULL) { |
| 7290 | lStatus = BAD_VALUE; |
| 7291 | goto Exit; |
| 7292 | } |
| 7293 | |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7294 | // check audio settings permission for global effects |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7295 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7296 | lStatus = PERMISSION_DENIED; |
| 7297 | goto Exit; |
| 7298 | } |
| 7299 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7300 | // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7301 | // that can only be created by audio policy manager (running in same process) |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 7302 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7303 | lStatus = PERMISSION_DENIED; |
| 7304 | goto Exit; |
| 7305 | } |
| 7306 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7307 | if (io == 0) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7308 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7309 | // output must be specified by AudioPolicyManager when using session |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7310 | // AUDIO_SESSION_OUTPUT_STAGE |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7311 | lStatus = BAD_VALUE; |
| 7312 | goto Exit; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7313 | } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7314 | // if the output returned by getOutputForEffect() is removed before we lock the |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7315 | // mutex below, the call to checkPlaybackThread_l(io) below will detect it |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7316 | // and we will exit safely |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7317 | io = AudioSystem::getOutputForEffect(&desc); |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7318 | } |
| 7319 | } |
| 7320 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7321 | { |
| 7322 | Mutex::Autolock _l(mLock); |
| 7323 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7324 | |
| 7325 | if (!EffectIsNullUuid(&pDesc->uuid)) { |
| 7326 | // if uuid is specified, request effect descriptor |
| 7327 | lStatus = EffectGetDescriptor(&pDesc->uuid, &desc); |
| 7328 | if (lStatus < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7329 | ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7330 | goto Exit; |
| 7331 | } |
| 7332 | } else { |
| 7333 | // if uuid is not specified, look for an available implementation |
| 7334 | // of the required type in effect factory |
| 7335 | if (EffectIsNullUuid(&pDesc->type)) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7336 | ALOGW("createEffect() no effect type"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7337 | lStatus = BAD_VALUE; |
| 7338 | goto Exit; |
| 7339 | } |
| 7340 | uint32_t numEffects = 0; |
| 7341 | effect_descriptor_t d; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7342 | d.flags = 0; // prevent compiler warning |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7343 | bool found = false; |
| 7344 | |
| 7345 | lStatus = EffectQueryNumberEffects(&numEffects); |
| 7346 | if (lStatus < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7347 | ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7348 | goto Exit; |
| 7349 | } |
| 7350 | for (uint32_t i = 0; i < numEffects; i++) { |
| 7351 | lStatus = EffectQueryEffect(i, &desc); |
| 7352 | if (lStatus < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7353 | ALOGW("createEffect() error %d from EffectQueryEffect", lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7354 | continue; |
| 7355 | } |
| 7356 | if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) { |
| 7357 | // If matching type found save effect descriptor. If the session is |
| 7358 | // 0 and the effect is not auxiliary, continue enumeration in case |
| 7359 | // an auxiliary version of this effect type is available |
| 7360 | found = true; |
| 7361 | memcpy(&d, &desc, sizeof(effect_descriptor_t)); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7362 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX || |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7363 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7364 | break; |
| 7365 | } |
| 7366 | } |
| 7367 | } |
| 7368 | if (!found) { |
| 7369 | lStatus = BAD_VALUE; |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7370 | ALOGW("createEffect() effect not found"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7371 | goto Exit; |
| 7372 | } |
| 7373 | // For same effect type, chose auxiliary version over insert version if |
| 7374 | // connect to output mix (Compliance to OpenSL ES) |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7375 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7376 | (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7377 | memcpy(&desc, &d, sizeof(effect_descriptor_t)); |
| 7378 | } |
| 7379 | } |
| 7380 | |
| 7381 | // Do not allow auxiliary effects on a session different from 0 (output mix) |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7382 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7383 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7384 | lStatus = INVALID_OPERATION; |
| 7385 | goto Exit; |
| 7386 | } |
| 7387 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7388 | // check recording permission for visualizer |
| 7389 | if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) && |
| 7390 | !recordingAllowed()) { |
| 7391 | lStatus = PERMISSION_DENIED; |
| 7392 | goto Exit; |
| 7393 | } |
| 7394 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7395 | // return effect descriptor |
| 7396 | memcpy(pDesc, &desc, sizeof(effect_descriptor_t)); |
| 7397 | |
| 7398 | // If output is not specified try to find a matching audio session ID in one of the |
| 7399 | // output threads. |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7400 | // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX |
| 7401 | // because of code checking output when entering the function. |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7402 | // Note: io is never 0 when creating an effect on an input |
| 7403 | if (io == 0) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 7404 | // look for the thread where the specified audio session is present |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7405 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 7406 | if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7407 | io = mPlaybackThreads.keyAt(i); |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7408 | break; |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 7409 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7410 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7411 | if (io == 0) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 7412 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 7413 | if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { |
| 7414 | io = mRecordThreads.keyAt(i); |
| 7415 | break; |
| 7416 | } |
| 7417 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7418 | } |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7419 | // If no output thread contains the requested session ID, default to |
| 7420 | // first output. The effect chain will be moved to the correct output |
| 7421 | // thread when a track with the same session ID is created |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7422 | if (io == 0 && mPlaybackThreads.size()) { |
| 7423 | io = mPlaybackThreads.keyAt(0); |
| 7424 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7425 | ALOGV("createEffect() got io %d for effect %s", io, desc.name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7426 | } |
| 7427 | ThreadBase *thread = checkRecordThread_l(io); |
| 7428 | if (thread == NULL) { |
| 7429 | thread = checkPlaybackThread_l(io); |
| 7430 | if (thread == NULL) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 7431 | ALOGE("createEffect() unknown output thread"); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7432 | lStatus = BAD_VALUE; |
| 7433 | goto Exit; |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7434 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7435 | } |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7436 | |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 7437 | sp<Client> client = registerPid_l(pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7438 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7439 | // create effect on selected output thread |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7440 | handle = thread->createEffect_l(client, effectClient, priority, sessionId, |
| 7441 | &desc, enabled, &lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7442 | if (handle != 0 && id != NULL) { |
| 7443 | *id = handle->id(); |
| 7444 | } |
| 7445 | } |
| 7446 | |
| 7447 | Exit: |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 7448 | if (status != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7449 | *status = lStatus; |
| 7450 | } |
| 7451 | return handle; |
| 7452 | } |
| 7453 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 7454 | status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput, |
| 7455 | audio_io_handle_t dstOutput) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7456 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7457 | ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7458 | sessionId, srcOutput, dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7459 | Mutex::Autolock _l(mLock); |
| 7460 | if (srcOutput == dstOutput) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7461 | ALOGW("moveEffects() same dst and src outputs %d", dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7462 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7463 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7464 | PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput); |
| 7465 | if (srcThread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7466 | ALOGW("moveEffects() bad srcOutput %d", srcOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7467 | return BAD_VALUE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7468 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7469 | PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput); |
| 7470 | if (dstThread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7471 | ALOGW("moveEffects() bad dstOutput %d", dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7472 | return BAD_VALUE; |
| 7473 | } |
| 7474 | |
| 7475 | Mutex::Autolock _dl(dstThread->mLock); |
| 7476 | Mutex::Autolock _sl(srcThread->mLock); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7477 | moveEffectChain_l(sessionId, srcThread, dstThread, false); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7478 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7479 | return NO_ERROR; |
| 7480 | } |
| 7481 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7482 | // moveEffectChain_l must be called with both srcThread and dstThread mLocks held |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7483 | status_t AudioFlinger::moveEffectChain_l(int sessionId, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7484 | AudioFlinger::PlaybackThread *srcThread, |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 7485 | AudioFlinger::PlaybackThread *dstThread, |
| 7486 | bool reRegister) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7487 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7488 | ALOGV("moveEffectChain_l() session %d from thread %p to thread %p", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7489 | sessionId, srcThread, dstThread); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7490 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7491 | sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7492 | if (chain == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7493 | ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7494 | sessionId, srcThread); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7495 | return INVALID_OPERATION; |
| 7496 | } |
| 7497 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 7498 | // remove chain first. This is useful only if reconfiguring effect chain on same output thread, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7499 | // so that a new chain is created with correct parameters when first effect is added. This is |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 7500 | // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7501 | // removed. |
| 7502 | srcThread->removeEffectChain_l(chain); |
| 7503 | |
| 7504 | // transfer all effects one by one so that new effect chain is created on new thread with |
| 7505 | // correct buffer sizes and audio parameters and effect engines reconfigured accordingly |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 7506 | audio_io_handle_t dstOutput = dstThread->id(); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 7507 | sp<EffectChain> dstChain; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7508 | uint32_t strategy = 0; // prevent compiler warning |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7509 | sp<EffectModule> effect = chain->getEffectFromId_l(0); |
| 7510 | while (effect != 0) { |
| 7511 | srcThread->removeEffect_l(effect); |
| 7512 | dstThread->addEffect_l(effect); |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 7513 | // removeEffect_l() has stopped the effect if it was active so it must be restarted |
| 7514 | if (effect->state() == EffectModule::ACTIVE || |
| 7515 | effect->state() == EffectModule::STOPPING) { |
| 7516 | effect->start(); |
| 7517 | } |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 7518 | // if the move request is not received from audio policy manager, the effect must be |
| 7519 | // re-registered with the new strategy and output |
| 7520 | if (dstChain == 0) { |
| 7521 | dstChain = effect->chain().promote(); |
| 7522 | if (dstChain == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7523 | ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get()); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 7524 | srcThread->addEffect_l(effect); |
| 7525 | return NO_INIT; |
| 7526 | } |
| 7527 | strategy = dstChain->strategy(); |
| 7528 | } |
| 7529 | if (reRegister) { |
| 7530 | AudioSystem::unregisterEffect(effect->id()); |
| 7531 | AudioSystem::registerEffect(&effect->desc(), |
| 7532 | dstOutput, |
| 7533 | strategy, |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7534 | sessionId, |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 7535 | effect->id()); |
| 7536 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7537 | effect = chain->getEffectFromId_l(0); |
| 7538 | } |
| 7539 | |
| 7540 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7541 | } |
| 7542 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7543 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7544 | // PlaybackThread::createEffect_l() must be called with AudioFlinger::mLock held |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7545 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7546 | const sp<AudioFlinger::Client>& client, |
| 7547 | const sp<IEffectClient>& effectClient, |
| 7548 | int32_t priority, |
| 7549 | int sessionId, |
| 7550 | effect_descriptor_t *desc, |
| 7551 | int *enabled, |
| 7552 | status_t *status |
| 7553 | ) |
| 7554 | { |
| 7555 | sp<EffectModule> effect; |
| 7556 | sp<EffectHandle> handle; |
| 7557 | status_t lStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7558 | sp<EffectChain> chain; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7559 | bool chainCreated = false; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7560 | bool effectCreated = false; |
| 7561 | bool effectRegistered = false; |
| 7562 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7563 | lStatus = initCheck(); |
| 7564 | if (lStatus != NO_ERROR) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7565 | ALOGW("createEffect_l() Audio driver not initialized."); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7566 | goto Exit; |
| 7567 | } |
| 7568 | |
| 7569 | // Do not allow effects with session ID 0 on direct output or duplicating threads |
| 7570 | // TODO: add rule for hw accelerated effects on direct outputs with non PCM format |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7571 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && mType != MIXER) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7572 | ALOGW("createEffect_l() Cannot add auxiliary effect %s to session %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7573 | desc->name, sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7574 | lStatus = BAD_VALUE; |
| 7575 | goto Exit; |
| 7576 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7577 | // Only Pre processor effects are allowed on input threads and only on input threads |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 7578 | if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7579 | ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d", |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7580 | desc->name, desc->flags, mType); |
| 7581 | lStatus = BAD_VALUE; |
| 7582 | goto Exit; |
| 7583 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7584 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7585 | ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7586 | |
| 7587 | { // scope for mLock |
| 7588 | Mutex::Autolock _l(mLock); |
| 7589 | |
| 7590 | // check for existing effect chain with the requested audio session |
| 7591 | chain = getEffectChain_l(sessionId); |
| 7592 | if (chain == 0) { |
| 7593 | // create a new chain for this session |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7594 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7595 | chain = new EffectChain(this, sessionId); |
| 7596 | addEffectChain_l(chain); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7597 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 7598 | chainCreated = true; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7599 | } else { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7600 | effect = chain->getEffectFromDesc_l(desc); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7601 | } |
| 7602 | |
Glenn Kasten | 7fc9a6f | 2012-01-10 10:46:34 -0800 | [diff] [blame] | 7603 | ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7604 | |
| 7605 | if (effect == 0) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7606 | int id = mAudioFlinger->nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7607 | // Check CPU and memory usage |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7608 | lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7609 | if (lStatus != NO_ERROR) { |
| 7610 | goto Exit; |
| 7611 | } |
| 7612 | effectRegistered = true; |
| 7613 | // create a new effect module if none present in the chain |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7614 | effect = new EffectModule(this, chain, desc, id, sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7615 | lStatus = effect->status(); |
| 7616 | if (lStatus != NO_ERROR) { |
| 7617 | goto Exit; |
| 7618 | } |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7619 | lStatus = chain->addEffect_l(effect); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7620 | if (lStatus != NO_ERROR) { |
| 7621 | goto Exit; |
| 7622 | } |
| 7623 | effectCreated = true; |
| 7624 | |
| 7625 | effect->setDevice(mDevice); |
| 7626 | effect->setMode(mAudioFlinger->getMode()); |
| 7627 | } |
| 7628 | // create effect handle and connect it to effect module |
| 7629 | handle = new EffectHandle(effect, client, effectClient, priority); |
| 7630 | lStatus = effect->addHandle(handle); |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 7631 | if (enabled != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7632 | *enabled = (int)effect->isEnabled(); |
| 7633 | } |
| 7634 | } |
| 7635 | |
| 7636 | Exit: |
| 7637 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7638 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7639 | if (effectCreated) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7640 | chain->removeEffect_l(effect); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7641 | } |
| 7642 | if (effectRegistered) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7643 | AudioSystem::unregisterEffect(effect->id()); |
| 7644 | } |
| 7645 | if (chainCreated) { |
| 7646 | removeEffectChain_l(chain); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7647 | } |
| 7648 | handle.clear(); |
| 7649 | } |
| 7650 | |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 7651 | if (status != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7652 | *status = lStatus; |
| 7653 | } |
| 7654 | return handle; |
| 7655 | } |
| 7656 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7657 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId) |
| 7658 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7659 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 7660 | return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7661 | } |
| 7662 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7663 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and |
| 7664 | // PlaybackThread::mLock held |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7665 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7666 | { |
| 7667 | // check for existing effect chain with the requested audio session |
| 7668 | int sessionId = effect->sessionId(); |
| 7669 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 7670 | bool chainCreated = false; |
| 7671 | |
| 7672 | if (chain == 0) { |
| 7673 | // create a new chain for this session |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7674 | ALOGV("addEffect_l() new effect chain for session %d", sessionId); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7675 | chain = new EffectChain(this, sessionId); |
| 7676 | addEffectChain_l(chain); |
| 7677 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 7678 | chainCreated = true; |
| 7679 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7680 | ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7681 | |
| 7682 | if (chain->getEffectFromId_l(effect->id()) != 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7683 | ALOGW("addEffect_l() %p effect %s already present in chain %p", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7684 | this, effect->desc().name, chain.get()); |
| 7685 | return BAD_VALUE; |
| 7686 | } |
| 7687 | |
| 7688 | status_t status = chain->addEffect_l(effect); |
| 7689 | if (status != NO_ERROR) { |
| 7690 | if (chainCreated) { |
| 7691 | removeEffectChain_l(chain); |
| 7692 | } |
| 7693 | return status; |
| 7694 | } |
| 7695 | |
| 7696 | effect->setDevice(mDevice); |
| 7697 | effect->setMode(mAudioFlinger->getMode()); |
| 7698 | return NO_ERROR; |
| 7699 | } |
| 7700 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7701 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7702 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7703 | ALOGV("removeEffect_l() %p effect %p", this, effect.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7704 | effect_descriptor_t desc = effect->desc(); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7705 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7706 | detachAuxEffect_l(effect->id()); |
| 7707 | } |
| 7708 | |
| 7709 | sp<EffectChain> chain = effect->chain().promote(); |
| 7710 | if (chain != 0) { |
| 7711 | // remove effect chain if removing last effect |
| 7712 | if (chain->removeEffect_l(effect) == 0) { |
| 7713 | removeEffectChain_l(chain); |
| 7714 | } |
| 7715 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7716 | ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7717 | } |
| 7718 | } |
| 7719 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7720 | void AudioFlinger::ThreadBase::lockEffectChains_l( |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 7721 | Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7722 | { |
| 7723 | effectChains = mEffectChains; |
| 7724 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7725 | mEffectChains[i]->lock(); |
| 7726 | } |
| 7727 | } |
| 7728 | |
| 7729 | void AudioFlinger::ThreadBase::unlockEffectChains( |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 7730 | const Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7731 | { |
| 7732 | for (size_t i = 0; i < effectChains.size(); i++) { |
| 7733 | effectChains[i]->unlock(); |
| 7734 | } |
| 7735 | } |
| 7736 | |
| 7737 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId) |
| 7738 | { |
| 7739 | Mutex::Autolock _l(mLock); |
| 7740 | return getEffectChain_l(sessionId); |
| 7741 | } |
| 7742 | |
| 7743 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) |
| 7744 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7745 | size_t size = mEffectChains.size(); |
| 7746 | for (size_t i = 0; i < size; i++) { |
| 7747 | if (mEffectChains[i]->sessionId() == sessionId) { |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 7748 | return mEffectChains[i]; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7749 | } |
| 7750 | } |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 7751 | return 0; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7752 | } |
| 7753 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 7754 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7755 | { |
| 7756 | Mutex::Autolock _l(mLock); |
| 7757 | size_t size = mEffectChains.size(); |
| 7758 | for (size_t i = 0; i < size; i++) { |
| 7759 | mEffectChains[i]->setMode_l(mode); |
| 7760 | } |
| 7761 | } |
| 7762 | |
| 7763 | void AudioFlinger::ThreadBase::disconnectEffect(const sp<EffectModule>& effect, |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7764 | const wp<EffectHandle>& handle, |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 7765 | bool unpinIfLast) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7766 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7767 | Mutex::Autolock _l(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7768 | ALOGV("disconnectEffect() %p effect %p", this, effect.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7769 | // delete the effect module if removing last handle on it |
| 7770 | if (effect->removeHandle(handle) == 0) { |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 7771 | if (!effect->isPinned() || unpinIfLast) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7772 | removeEffect_l(effect); |
| 7773 | AudioSystem::unregisterEffect(effect->id()); |
| 7774 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7775 | } |
| 7776 | } |
| 7777 | |
| 7778 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 7779 | { |
| 7780 | int session = chain->sessionId(); |
| 7781 | int16_t *buffer = mMixBuffer; |
| 7782 | bool ownsBuffer = false; |
| 7783 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7784 | ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7785 | if (session > 0) { |
| 7786 | // Only one effect chain can be present in direct output thread and it uses |
| 7787 | // the mix buffer as input |
| 7788 | if (mType != DIRECT) { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 7789 | size_t numSamples = mNormalFrameCount * mChannelCount; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7790 | buffer = new int16_t[numSamples]; |
| 7791 | memset(buffer, 0, numSamples * sizeof(int16_t)); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7792 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7793 | ownsBuffer = true; |
| 7794 | } |
| 7795 | |
| 7796 | // Attach all tracks with same session ID to this chain. |
| 7797 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 7798 | sp<Track> track = mTracks[i]; |
| 7799 | if (session == track->sessionId()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7800 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7801 | track->setMainBuffer(buffer); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7802 | chain->incTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7803 | } |
| 7804 | } |
| 7805 | |
| 7806 | // indicate all active tracks in the chain |
| 7807 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 7808 | sp<Track> track = mActiveTracks[i].promote(); |
| 7809 | if (track == 0) continue; |
| 7810 | if (session == track->sessionId()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7811 | ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7812 | chain->incActiveTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7813 | } |
| 7814 | } |
| 7815 | } |
| 7816 | |
| 7817 | chain->setInBuffer(buffer, ownsBuffer); |
| 7818 | chain->setOutBuffer(mMixBuffer); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7819 | // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7820 | // chains list in order to be processed last as it contains output stage effects |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7821 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before |
| 7822 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7823 | // after track specific effects and before output stage |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7824 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and |
| 7825 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7826 | // Effect chain for other sessions are inserted at beginning of effect |
| 7827 | // chains list to be processed before output mix effects. Relative order between other |
| 7828 | // sessions is not important |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7829 | size_t size = mEffectChains.size(); |
| 7830 | size_t i = 0; |
| 7831 | for (i = 0; i < size; i++) { |
| 7832 | if (mEffectChains[i]->sessionId() < session) break; |
| 7833 | } |
| 7834 | mEffectChains.insertAt(chain, i); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7835 | checkSuspendOnAddEffectChain_l(chain); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7836 | |
| 7837 | return NO_ERROR; |
| 7838 | } |
| 7839 | |
| 7840 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 7841 | { |
| 7842 | int session = chain->sessionId(); |
| 7843 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7844 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7845 | |
| 7846 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7847 | if (chain == mEffectChains[i]) { |
| 7848 | mEffectChains.removeAt(i); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7849 | // detach all active tracks from the chain |
| 7850 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 7851 | sp<Track> track = mActiveTracks[i].promote(); |
| 7852 | if (track == 0) continue; |
| 7853 | if (session == track->sessionId()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7854 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7855 | chain.get(), session); |
| 7856 | chain->decActiveTrackCnt(); |
| 7857 | } |
| 7858 | } |
| 7859 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7860 | // detach all tracks with same session ID from this chain |
| 7861 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 7862 | sp<Track> track = mTracks[i]; |
| 7863 | if (session == track->sessionId()) { |
| 7864 | track->setMainBuffer(mMixBuffer); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7865 | chain->decTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7866 | } |
| 7867 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7868 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7869 | } |
| 7870 | } |
| 7871 | return mEffectChains.size(); |
| 7872 | } |
| 7873 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7874 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( |
| 7875 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7876 | { |
| 7877 | Mutex::Autolock _l(mLock); |
| 7878 | return attachAuxEffect_l(track, EffectId); |
| 7879 | } |
| 7880 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7881 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( |
| 7882 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7883 | { |
| 7884 | status_t status = NO_ERROR; |
| 7885 | |
| 7886 | if (EffectId == 0) { |
| 7887 | track->setAuxBuffer(0, NULL); |
| 7888 | } else { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7889 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX |
| 7890 | sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7891 | if (effect != 0) { |
| 7892 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7893 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); |
| 7894 | } else { |
| 7895 | status = INVALID_OPERATION; |
| 7896 | } |
| 7897 | } else { |
| 7898 | status = BAD_VALUE; |
| 7899 | } |
| 7900 | } |
| 7901 | return status; |
| 7902 | } |
| 7903 | |
| 7904 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) |
| 7905 | { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 7906 | for (size_t i = 0; i < mTracks.size(); ++i) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7907 | sp<Track> track = mTracks[i]; |
| 7908 | if (track->auxEffectId() == effectId) { |
| 7909 | attachAuxEffect_l(track, 0); |
| 7910 | } |
| 7911 | } |
| 7912 | } |
| 7913 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7914 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 7915 | { |
| 7916 | // only one chain per input thread |
| 7917 | if (mEffectChains.size() != 0) { |
| 7918 | return INVALID_OPERATION; |
| 7919 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7920 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7921 | |
| 7922 | chain->setInBuffer(NULL); |
| 7923 | chain->setOutBuffer(NULL); |
| 7924 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7925 | checkSuspendOnAddEffectChain_l(chain); |
| 7926 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7927 | mEffectChains.add(chain); |
| 7928 | |
| 7929 | return NO_ERROR; |
| 7930 | } |
| 7931 | |
| 7932 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 7933 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7934 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7935 | ALOGW_IF(mEffectChains.size() != 1, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7936 | "removeEffectChain_l() %p invalid chain size %d on thread %p", |
| 7937 | chain.get(), mEffectChains.size(), this); |
| 7938 | if (mEffectChains.size() == 1) { |
| 7939 | mEffectChains.removeAt(0); |
| 7940 | } |
| 7941 | return 0; |
| 7942 | } |
| 7943 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7944 | // ---------------------------------------------------------------------------- |
| 7945 | // EffectModule implementation |
| 7946 | // ---------------------------------------------------------------------------- |
| 7947 | |
| 7948 | #undef LOG_TAG |
| 7949 | #define LOG_TAG "AudioFlinger::EffectModule" |
| 7950 | |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 7951 | AudioFlinger::EffectModule::EffectModule(ThreadBase *thread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7952 | const wp<AudioFlinger::EffectChain>& chain, |
| 7953 | effect_descriptor_t *desc, |
| 7954 | int id, |
| 7955 | int sessionId) |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 7956 | : mThread(thread), mChain(chain), mId(id), mSessionId(sessionId), mEffectInterface(NULL), |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7957 | mStatus(NO_INIT), mState(IDLE), mSuspended(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7958 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7959 | ALOGV("Constructor %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7960 | int lStatus; |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 7961 | if (thread == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7962 | return; |
| 7963 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7964 | |
| 7965 | memcpy(&mDescriptor, desc, sizeof(effect_descriptor_t)); |
| 7966 | |
| 7967 | // create effect engine from effect factory |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7968 | mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7969 | |
| 7970 | if (mStatus != NO_ERROR) { |
| 7971 | return; |
| 7972 | } |
| 7973 | lStatus = init(); |
| 7974 | if (lStatus < 0) { |
| 7975 | mStatus = lStatus; |
| 7976 | goto Error; |
| 7977 | } |
| 7978 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7979 | if (mSessionId > AUDIO_SESSION_OUTPUT_MIX) { |
| 7980 | mPinned = true; |
| 7981 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7982 | ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7983 | return; |
| 7984 | Error: |
| 7985 | EffectRelease(mEffectInterface); |
| 7986 | mEffectInterface = NULL; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7987 | ALOGV("Constructor Error %d", mStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7988 | } |
| 7989 | |
| 7990 | AudioFlinger::EffectModule::~EffectModule() |
| 7991 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7992 | ALOGV("Destructor %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7993 | if (mEffectInterface != NULL) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7994 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 7995 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { |
| 7996 | sp<ThreadBase> thread = mThread.promote(); |
| 7997 | if (thread != 0) { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 7998 | audio_stream_t *stream = thread->stream(); |
| 7999 | if (stream != NULL) { |
| 8000 | stream->remove_audio_effect(stream, mEffectInterface); |
| 8001 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 8002 | } |
| 8003 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8004 | // release effect engine |
| 8005 | EffectRelease(mEffectInterface); |
| 8006 | } |
| 8007 | } |
| 8008 | |
Glenn Kasten | 435dbe6 | 2012-01-30 10:15:48 -0800 | [diff] [blame] | 8009 | status_t AudioFlinger::EffectModule::addHandle(const sp<EffectHandle>& handle) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8010 | { |
| 8011 | status_t status; |
| 8012 | |
| 8013 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8014 | int priority = handle->priority(); |
| 8015 | size_t size = mHandles.size(); |
| 8016 | sp<EffectHandle> h; |
| 8017 | size_t i; |
| 8018 | for (i = 0; i < size; i++) { |
| 8019 | h = mHandles[i].promote(); |
| 8020 | if (h == 0) continue; |
| 8021 | if (h->priority() <= priority) break; |
| 8022 | } |
| 8023 | // if inserted in first place, move effect control from previous owner to this handle |
| 8024 | if (i == 0) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8025 | bool enabled = false; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8026 | if (h != 0) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8027 | enabled = h->enabled(); |
| 8028 | h->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8029 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8030 | handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8031 | status = NO_ERROR; |
| 8032 | } else { |
| 8033 | status = ALREADY_EXISTS; |
| 8034 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8035 | ALOGV("addHandle() %p added handle %p in position %d", this, handle.get(), i); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8036 | mHandles.insertAt(handle, i); |
| 8037 | return status; |
| 8038 | } |
| 8039 | |
| 8040 | size_t AudioFlinger::EffectModule::removeHandle(const wp<EffectHandle>& handle) |
| 8041 | { |
| 8042 | Mutex::Autolock _l(mLock); |
| 8043 | size_t size = mHandles.size(); |
| 8044 | size_t i; |
| 8045 | for (i = 0; i < size; i++) { |
| 8046 | if (mHandles[i] == handle) break; |
| 8047 | } |
| 8048 | if (i == size) { |
| 8049 | return size; |
| 8050 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8051 | ALOGV("removeHandle() %p removed handle %p in position %d", this, handle.unsafe_get(), i); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8052 | |
| 8053 | bool enabled = false; |
| 8054 | EffectHandle *hdl = handle.unsafe_get(); |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 8055 | if (hdl != NULL) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8056 | ALOGV("removeHandle() unsafe_get OK"); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8057 | enabled = hdl->enabled(); |
| 8058 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8059 | mHandles.removeAt(i); |
| 8060 | size = mHandles.size(); |
| 8061 | // if removed from first place, move effect control from this handle to next in line |
| 8062 | if (i == 0 && size != 0) { |
| 8063 | sp<EffectHandle> h = mHandles[0].promote(); |
| 8064 | if (h != 0) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8065 | h->setControl(true /*hasControl*/, true /*signal*/ , enabled /*enabled*/); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8066 | } |
| 8067 | } |
| 8068 | |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 8069 | // Prevent calls to process() and other functions on effect interface from now on. |
| 8070 | // The effect engine will be released by the destructor when the last strong reference on |
| 8071 | // this object is released which can happen after next process is called. |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8072 | if (size == 0 && !mPinned) { |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 8073 | mState = DESTROYED; |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 8074 | } |
| 8075 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8076 | return size; |
| 8077 | } |
| 8078 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8079 | sp<AudioFlinger::EffectHandle> AudioFlinger::EffectModule::controlHandle() |
| 8080 | { |
| 8081 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 8082 | return mHandles.size() != 0 ? mHandles[0].promote() : 0; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8083 | } |
| 8084 | |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 8085 | void AudioFlinger::EffectModule::disconnect(const wp<EffectHandle>& handle, bool unpinIfLast) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8086 | { |
Glenn Kasten | 90bebef | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 8087 | ALOGV("disconnect() %p handle %p", this, handle.unsafe_get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8088 | // keep a strong reference on this EffectModule to avoid calling the |
| 8089 | // destructor before we exit |
| 8090 | sp<EffectModule> keep(this); |
| 8091 | { |
| 8092 | sp<ThreadBase> thread = mThread.promote(); |
| 8093 | if (thread != 0) { |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 8094 | thread->disconnectEffect(keep, handle, unpinIfLast); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8095 | } |
| 8096 | } |
| 8097 | } |
| 8098 | |
| 8099 | void AudioFlinger::EffectModule::updateState() { |
| 8100 | Mutex::Autolock _l(mLock); |
| 8101 | |
| 8102 | switch (mState) { |
| 8103 | case RESTART: |
| 8104 | reset_l(); |
| 8105 | // FALL THROUGH |
| 8106 | |
| 8107 | case STARTING: |
| 8108 | // clear auxiliary effect input buffer for next accumulation |
| 8109 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 8110 | memset(mConfig.inputCfg.buffer.raw, |
| 8111 | 0, |
| 8112 | mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); |
| 8113 | } |
| 8114 | start_l(); |
| 8115 | mState = ACTIVE; |
| 8116 | break; |
| 8117 | case STOPPING: |
| 8118 | stop_l(); |
| 8119 | mDisableWaitCnt = mMaxDisableWaitCnt; |
| 8120 | mState = STOPPED; |
| 8121 | break; |
| 8122 | case STOPPED: |
| 8123 | // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the |
| 8124 | // turn off sequence. |
| 8125 | if (--mDisableWaitCnt == 0) { |
| 8126 | reset_l(); |
| 8127 | mState = IDLE; |
| 8128 | } |
| 8129 | break; |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 8130 | default: //IDLE , ACTIVE, DESTROYED |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8131 | break; |
| 8132 | } |
| 8133 | } |
| 8134 | |
| 8135 | void AudioFlinger::EffectModule::process() |
| 8136 | { |
| 8137 | Mutex::Autolock _l(mLock); |
| 8138 | |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 8139 | if (mState == DESTROYED || mEffectInterface == NULL || |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8140 | mConfig.inputCfg.buffer.raw == NULL || |
| 8141 | mConfig.outputCfg.buffer.raw == NULL) { |
| 8142 | return; |
| 8143 | } |
| 8144 | |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 8145 | if (isProcessEnabled()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8146 | // do 32 bit to 16 bit conversion for auxiliary effect input buffer |
| 8147 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 8148 | ditherAndClamp(mConfig.inputCfg.buffer.s32, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8149 | mConfig.inputCfg.buffer.s32, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 8150 | mConfig.inputCfg.buffer.frameCount/2); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8151 | } |
| 8152 | |
| 8153 | // do the actual processing in the effect engine |
| 8154 | int ret = (*mEffectInterface)->process(mEffectInterface, |
| 8155 | &mConfig.inputCfg.buffer, |
| 8156 | &mConfig.outputCfg.buffer); |
| 8157 | |
| 8158 | // force transition to IDLE state when engine is ready |
| 8159 | if (mState == STOPPED && ret == -ENODATA) { |
| 8160 | mDisableWaitCnt = 1; |
| 8161 | } |
| 8162 | |
| 8163 | // clear auxiliary effect input buffer for next accumulation |
| 8164 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
Eric Laurent | 7333748 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 8165 | memset(mConfig.inputCfg.buffer.raw, 0, |
| 8166 | mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8167 | } |
| 8168 | } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT && |
Eric Laurent | 7333748 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 8169 | mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { |
| 8170 | // If an insert effect is idle and input buffer is different from output buffer, |
| 8171 | // accumulate input onto output |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8172 | sp<EffectChain> chain = mChain.promote(); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 8173 | if (chain != 0 && chain->activeTrackCnt() != 0) { |
Eric Laurent | 7333748 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 8174 | size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2; //always stereo here |
| 8175 | int16_t *in = mConfig.inputCfg.buffer.s16; |
| 8176 | int16_t *out = mConfig.outputCfg.buffer.s16; |
| 8177 | for (size_t i = 0; i < frameCnt; i++) { |
| 8178 | out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8179 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8180 | } |
| 8181 | } |
| 8182 | } |
| 8183 | |
| 8184 | void AudioFlinger::EffectModule::reset_l() |
| 8185 | { |
| 8186 | if (mEffectInterface == NULL) { |
| 8187 | return; |
| 8188 | } |
| 8189 | (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL); |
| 8190 | } |
| 8191 | |
| 8192 | status_t AudioFlinger::EffectModule::configure() |
| 8193 | { |
| 8194 | uint32_t channels; |
| 8195 | if (mEffectInterface == NULL) { |
| 8196 | return NO_INIT; |
| 8197 | } |
| 8198 | |
| 8199 | sp<ThreadBase> thread = mThread.promote(); |
| 8200 | if (thread == 0) { |
| 8201 | return DEAD_OBJECT; |
| 8202 | } |
| 8203 | |
| 8204 | // TODO: handle configuration of effects replacing track process |
| 8205 | if (thread->channelCount() == 1) { |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 8206 | channels = AUDIO_CHANNEL_OUT_MONO; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8207 | } else { |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 8208 | channels = AUDIO_CHANNEL_OUT_STEREO; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8209 | } |
| 8210 | |
| 8211 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 8212 | mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8213 | } else { |
| 8214 | mConfig.inputCfg.channels = channels; |
| 8215 | } |
| 8216 | mConfig.outputCfg.channels = channels; |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 8217 | mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
| 8218 | mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8219 | mConfig.inputCfg.samplingRate = thread->sampleRate(); |
| 8220 | mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate; |
| 8221 | mConfig.inputCfg.bufferProvider.cookie = NULL; |
| 8222 | mConfig.inputCfg.bufferProvider.getBuffer = NULL; |
| 8223 | mConfig.inputCfg.bufferProvider.releaseBuffer = NULL; |
| 8224 | mConfig.outputCfg.bufferProvider.cookie = NULL; |
| 8225 | mConfig.outputCfg.bufferProvider.getBuffer = NULL; |
| 8226 | mConfig.outputCfg.bufferProvider.releaseBuffer = NULL; |
| 8227 | mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; |
| 8228 | // Insert effect: |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 8229 | // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 8230 | // always overwrites output buffer: input buffer == output buffer |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8231 | // - in other sessions: |
| 8232 | // last effect in the chain accumulates in output buffer: input buffer != output buffer |
| 8233 | // other effect: overwrites output buffer: input buffer == output buffer |
| 8234 | // Auxiliary effect: |
| 8235 | // accumulates in output buffer: input buffer != output buffer |
| 8236 | // Therefore: accumulate <=> input buffer != output buffer |
| 8237 | if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { |
| 8238 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE; |
| 8239 | } else { |
| 8240 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE; |
| 8241 | } |
| 8242 | mConfig.inputCfg.mask = EFFECT_CONFIG_ALL; |
| 8243 | mConfig.outputCfg.mask = EFFECT_CONFIG_ALL; |
| 8244 | mConfig.inputCfg.buffer.frameCount = thread->frameCount(); |
| 8245 | mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount; |
| 8246 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8247 | ALOGV("configure() %p thread %p buffer %p framecount %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 8248 | this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount); |
| 8249 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8250 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8251 | uint32_t size = sizeof(int); |
| 8252 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 8253 | EFFECT_CMD_SET_CONFIG, |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8254 | sizeof(effect_config_t), |
| 8255 | &mConfig, |
| 8256 | &size, |
| 8257 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8258 | if (status == 0) { |
| 8259 | status = cmdStatus; |
| 8260 | } |
| 8261 | |
| 8262 | mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) / |
| 8263 | (1000 * mConfig.outputCfg.buffer.frameCount); |
| 8264 | |
| 8265 | return status; |
| 8266 | } |
| 8267 | |
| 8268 | status_t AudioFlinger::EffectModule::init() |
| 8269 | { |
| 8270 | Mutex::Autolock _l(mLock); |
| 8271 | if (mEffectInterface == NULL) { |
| 8272 | return NO_INIT; |
| 8273 | } |
| 8274 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8275 | uint32_t size = sizeof(status_t); |
| 8276 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 8277 | EFFECT_CMD_INIT, |
| 8278 | 0, |
| 8279 | NULL, |
| 8280 | &size, |
| 8281 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8282 | if (status == 0) { |
| 8283 | status = cmdStatus; |
| 8284 | } |
| 8285 | return status; |
| 8286 | } |
| 8287 | |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 8288 | status_t AudioFlinger::EffectModule::start() |
| 8289 | { |
| 8290 | Mutex::Autolock _l(mLock); |
| 8291 | return start_l(); |
| 8292 | } |
| 8293 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8294 | status_t AudioFlinger::EffectModule::start_l() |
| 8295 | { |
| 8296 | if (mEffectInterface == NULL) { |
| 8297 | return NO_INIT; |
| 8298 | } |
| 8299 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8300 | uint32_t size = sizeof(status_t); |
| 8301 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 8302 | EFFECT_CMD_ENABLE, |
| 8303 | 0, |
| 8304 | NULL, |
| 8305 | &size, |
| 8306 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8307 | if (status == 0) { |
| 8308 | status = cmdStatus; |
| 8309 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 8310 | if (status == 0 && |
| 8311 | ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 8312 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) { |
| 8313 | sp<ThreadBase> thread = mThread.promote(); |
| 8314 | if (thread != 0) { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 8315 | audio_stream_t *stream = thread->stream(); |
| 8316 | if (stream != NULL) { |
| 8317 | stream->add_audio_effect(stream, mEffectInterface); |
| 8318 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 8319 | } |
| 8320 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8321 | return status; |
| 8322 | } |
| 8323 | |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 8324 | status_t AudioFlinger::EffectModule::stop() |
| 8325 | { |
| 8326 | Mutex::Autolock _l(mLock); |
| 8327 | return stop_l(); |
| 8328 | } |
| 8329 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8330 | status_t AudioFlinger::EffectModule::stop_l() |
| 8331 | { |
| 8332 | if (mEffectInterface == NULL) { |
| 8333 | return NO_INIT; |
| 8334 | } |
| 8335 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8336 | uint32_t size = sizeof(status_t); |
| 8337 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 8338 | EFFECT_CMD_DISABLE, |
| 8339 | 0, |
| 8340 | NULL, |
| 8341 | &size, |
| 8342 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8343 | if (status == 0) { |
| 8344 | status = cmdStatus; |
| 8345 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 8346 | if (status == 0 && |
| 8347 | ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 8348 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) { |
| 8349 | sp<ThreadBase> thread = mThread.promote(); |
| 8350 | if (thread != 0) { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 8351 | audio_stream_t *stream = thread->stream(); |
| 8352 | if (stream != NULL) { |
| 8353 | stream->remove_audio_effect(stream, mEffectInterface); |
| 8354 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 8355 | } |
| 8356 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8357 | return status; |
| 8358 | } |
| 8359 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8360 | status_t AudioFlinger::EffectModule::command(uint32_t cmdCode, |
| 8361 | uint32_t cmdSize, |
| 8362 | void *pCmdData, |
| 8363 | uint32_t *replySize, |
| 8364 | void *pReplyData) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8365 | { |
| 8366 | Mutex::Autolock _l(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8367 | // ALOGV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8368 | |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 8369 | if (mState == DESTROYED || mEffectInterface == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8370 | return NO_INIT; |
| 8371 | } |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8372 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 8373 | cmdCode, |
| 8374 | cmdSize, |
| 8375 | pCmdData, |
| 8376 | replySize, |
| 8377 | pReplyData); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8378 | if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) { |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8379 | uint32_t size = (replySize == NULL) ? 0 : *replySize; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8380 | for (size_t i = 1; i < mHandles.size(); i++) { |
| 8381 | sp<EffectHandle> h = mHandles[i].promote(); |
| 8382 | if (h != 0) { |
| 8383 | h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData); |
| 8384 | } |
| 8385 | } |
| 8386 | } |
| 8387 | return status; |
| 8388 | } |
| 8389 | |
| 8390 | status_t AudioFlinger::EffectModule::setEnabled(bool enabled) |
| 8391 | { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 8392 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8393 | Mutex::Autolock _l(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8394 | ALOGV("setEnabled %p enabled %d", this, enabled); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8395 | |
| 8396 | if (enabled != isEnabled()) { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 8397 | status_t status = AudioSystem::setEffectEnabled(mId, enabled); |
| 8398 | if (enabled && status != NO_ERROR) { |
| 8399 | return status; |
| 8400 | } |
| 8401 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8402 | switch (mState) { |
| 8403 | // going from disabled to enabled |
| 8404 | case IDLE: |
| 8405 | mState = STARTING; |
| 8406 | break; |
| 8407 | case STOPPED: |
| 8408 | mState = RESTART; |
| 8409 | break; |
| 8410 | case STOPPING: |
| 8411 | mState = ACTIVE; |
| 8412 | break; |
| 8413 | |
| 8414 | // going from enabled to disabled |
| 8415 | case RESTART: |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 8416 | mState = STOPPED; |
| 8417 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8418 | case STARTING: |
| 8419 | mState = IDLE; |
| 8420 | break; |
| 8421 | case ACTIVE: |
| 8422 | mState = STOPPING; |
| 8423 | break; |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 8424 | case DESTROYED: |
| 8425 | return NO_ERROR; // simply ignore as we are being destroyed |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8426 | } |
| 8427 | for (size_t i = 1; i < mHandles.size(); i++) { |
| 8428 | sp<EffectHandle> h = mHandles[i].promote(); |
| 8429 | if (h != 0) { |
| 8430 | h->setEnabled(enabled); |
| 8431 | } |
| 8432 | } |
| 8433 | } |
| 8434 | return NO_ERROR; |
| 8435 | } |
| 8436 | |
Glenn Kasten | c59c004 | 2012-02-02 14:06:11 -0800 | [diff] [blame] | 8437 | bool AudioFlinger::EffectModule::isEnabled() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8438 | { |
| 8439 | switch (mState) { |
| 8440 | case RESTART: |
| 8441 | case STARTING: |
| 8442 | case ACTIVE: |
| 8443 | return true; |
| 8444 | case IDLE: |
| 8445 | case STOPPING: |
| 8446 | case STOPPED: |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 8447 | case DESTROYED: |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8448 | default: |
| 8449 | return false; |
| 8450 | } |
| 8451 | } |
| 8452 | |
Glenn Kasten | c59c004 | 2012-02-02 14:06:11 -0800 | [diff] [blame] | 8453 | bool AudioFlinger::EffectModule::isProcessEnabled() const |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 8454 | { |
| 8455 | switch (mState) { |
| 8456 | case RESTART: |
| 8457 | case ACTIVE: |
| 8458 | case STOPPING: |
| 8459 | case STOPPED: |
| 8460 | return true; |
| 8461 | case IDLE: |
| 8462 | case STARTING: |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 8463 | case DESTROYED: |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 8464 | default: |
| 8465 | return false; |
| 8466 | } |
| 8467 | } |
| 8468 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8469 | status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller) |
| 8470 | { |
| 8471 | Mutex::Autolock _l(mLock); |
| 8472 | status_t status = NO_ERROR; |
| 8473 | |
| 8474 | // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume |
| 8475 | // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set) |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 8476 | if (isProcessEnabled() && |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 8477 | ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL || |
| 8478 | (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8479 | status_t cmdStatus; |
| 8480 | uint32_t volume[2]; |
| 8481 | uint32_t *pVolume = NULL; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8482 | uint32_t size = sizeof(volume); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8483 | volume[0] = *left; |
| 8484 | volume[1] = *right; |
| 8485 | if (controller) { |
| 8486 | pVolume = volume; |
| 8487 | } |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8488 | status = (*mEffectInterface)->command(mEffectInterface, |
| 8489 | EFFECT_CMD_SET_VOLUME, |
| 8490 | size, |
| 8491 | volume, |
| 8492 | &size, |
| 8493 | pVolume); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8494 | if (controller && status == NO_ERROR && size == sizeof(volume)) { |
| 8495 | *left = volume[0]; |
| 8496 | *right = volume[1]; |
| 8497 | } |
| 8498 | } |
| 8499 | return status; |
| 8500 | } |
| 8501 | |
| 8502 | status_t AudioFlinger::EffectModule::setDevice(uint32_t device) |
| 8503 | { |
| 8504 | Mutex::Autolock _l(mLock); |
| 8505 | status_t status = NO_ERROR; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 8506 | if (device && (mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) { |
| 8507 | // audio pre processing modules on RecordThread can receive both output and |
| 8508 | // input device indication in the same call |
| 8509 | uint32_t dev = device & AUDIO_DEVICE_OUT_ALL; |
| 8510 | if (dev) { |
| 8511 | status_t cmdStatus; |
| 8512 | uint32_t size = sizeof(status_t); |
| 8513 | |
| 8514 | status = (*mEffectInterface)->command(mEffectInterface, |
| 8515 | EFFECT_CMD_SET_DEVICE, |
| 8516 | sizeof(uint32_t), |
| 8517 | &dev, |
| 8518 | &size, |
| 8519 | &cmdStatus); |
| 8520 | if (status == NO_ERROR) { |
| 8521 | status = cmdStatus; |
| 8522 | } |
| 8523 | } |
| 8524 | dev = device & AUDIO_DEVICE_IN_ALL; |
| 8525 | if (dev) { |
| 8526 | status_t cmdStatus; |
| 8527 | uint32_t size = sizeof(status_t); |
| 8528 | |
| 8529 | status_t status2 = (*mEffectInterface)->command(mEffectInterface, |
| 8530 | EFFECT_CMD_SET_INPUT_DEVICE, |
| 8531 | sizeof(uint32_t), |
| 8532 | &dev, |
| 8533 | &size, |
| 8534 | &cmdStatus); |
| 8535 | if (status2 == NO_ERROR) { |
| 8536 | status2 = cmdStatus; |
| 8537 | } |
| 8538 | if (status == NO_ERROR) { |
| 8539 | status = status2; |
| 8540 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8541 | } |
| 8542 | } |
| 8543 | return status; |
| 8544 | } |
| 8545 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 8546 | status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8547 | { |
| 8548 | Mutex::Autolock _l(mLock); |
| 8549 | status_t status = NO_ERROR; |
| 8550 | if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8551 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8552 | uint32_t size = sizeof(status_t); |
| 8553 | status = (*mEffectInterface)->command(mEffectInterface, |
| 8554 | EFFECT_CMD_SET_AUDIO_MODE, |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 8555 | sizeof(audio_mode_t), |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 8556 | &mode, |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8557 | &size, |
| 8558 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8559 | if (status == NO_ERROR) { |
| 8560 | status = cmdStatus; |
| 8561 | } |
| 8562 | } |
| 8563 | return status; |
| 8564 | } |
| 8565 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8566 | void AudioFlinger::EffectModule::setSuspended(bool suspended) |
| 8567 | { |
| 8568 | Mutex::Autolock _l(mLock); |
| 8569 | mSuspended = suspended; |
| 8570 | } |
Glenn Kasten | a3a8548 | 2012-01-04 11:01:11 -0800 | [diff] [blame] | 8571 | |
| 8572 | bool AudioFlinger::EffectModule::suspended() const |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8573 | { |
| 8574 | Mutex::Autolock _l(mLock); |
| 8575 | return mSuspended; |
| 8576 | } |
| 8577 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8578 | status_t AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args) |
| 8579 | { |
| 8580 | const size_t SIZE = 256; |
| 8581 | char buffer[SIZE]; |
| 8582 | String8 result; |
| 8583 | |
| 8584 | snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId); |
| 8585 | result.append(buffer); |
| 8586 | |
| 8587 | bool locked = tryLock(mLock); |
| 8588 | // failed to lock - AudioFlinger is probably deadlocked |
| 8589 | if (!locked) { |
| 8590 | result.append("\t\tCould not lock Fx mutex:\n"); |
| 8591 | } |
| 8592 | |
| 8593 | result.append("\t\tSession Status State Engine:\n"); |
| 8594 | snprintf(buffer, SIZE, "\t\t%05d %03d %03d 0x%08x\n", |
| 8595 | mSessionId, mStatus, mState, (uint32_t)mEffectInterface); |
| 8596 | result.append(buffer); |
| 8597 | |
| 8598 | result.append("\t\tDescriptor:\n"); |
| 8599 | snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", |
| 8600 | mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion, |
| 8601 | mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],mDescriptor.uuid.node[2], |
| 8602 | mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]); |
| 8603 | result.append(buffer); |
| 8604 | snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", |
| 8605 | mDescriptor.type.timeLow, mDescriptor.type.timeMid, mDescriptor.type.timeHiAndVersion, |
| 8606 | mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],mDescriptor.type.node[2], |
| 8607 | mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]); |
| 8608 | result.append(buffer); |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 8609 | snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X\n", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8610 | mDescriptor.apiVersion, |
| 8611 | mDescriptor.flags); |
| 8612 | result.append(buffer); |
| 8613 | snprintf(buffer, SIZE, "\t\t- name: %s\n", |
| 8614 | mDescriptor.name); |
| 8615 | result.append(buffer); |
| 8616 | snprintf(buffer, SIZE, "\t\t- implementor: %s\n", |
| 8617 | mDescriptor.implementor); |
| 8618 | result.append(buffer); |
| 8619 | |
| 8620 | result.append("\t\t- Input configuration:\n"); |
| 8621 | result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); |
| 8622 | snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n", |
| 8623 | (uint32_t)mConfig.inputCfg.buffer.raw, |
| 8624 | mConfig.inputCfg.buffer.frameCount, |
| 8625 | mConfig.inputCfg.samplingRate, |
| 8626 | mConfig.inputCfg.channels, |
| 8627 | mConfig.inputCfg.format); |
| 8628 | result.append(buffer); |
| 8629 | |
| 8630 | result.append("\t\t- Output configuration:\n"); |
| 8631 | result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); |
| 8632 | snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n", |
| 8633 | (uint32_t)mConfig.outputCfg.buffer.raw, |
| 8634 | mConfig.outputCfg.buffer.frameCount, |
| 8635 | mConfig.outputCfg.samplingRate, |
| 8636 | mConfig.outputCfg.channels, |
| 8637 | mConfig.outputCfg.format); |
| 8638 | result.append(buffer); |
| 8639 | |
| 8640 | snprintf(buffer, SIZE, "\t\t%d Clients:\n", mHandles.size()); |
| 8641 | result.append(buffer); |
| 8642 | result.append("\t\t\tPid Priority Ctrl Locked client server\n"); |
| 8643 | for (size_t i = 0; i < mHandles.size(); ++i) { |
| 8644 | sp<EffectHandle> handle = mHandles[i].promote(); |
| 8645 | if (handle != 0) { |
| 8646 | handle->dump(buffer, SIZE); |
| 8647 | result.append(buffer); |
| 8648 | } |
| 8649 | } |
| 8650 | |
| 8651 | result.append("\n"); |
| 8652 | |
| 8653 | write(fd, result.string(), result.length()); |
| 8654 | |
| 8655 | if (locked) { |
| 8656 | mLock.unlock(); |
| 8657 | } |
| 8658 | |
| 8659 | return NO_ERROR; |
| 8660 | } |
| 8661 | |
| 8662 | // ---------------------------------------------------------------------------- |
| 8663 | // EffectHandle implementation |
| 8664 | // ---------------------------------------------------------------------------- |
| 8665 | |
| 8666 | #undef LOG_TAG |
| 8667 | #define LOG_TAG "AudioFlinger::EffectHandle" |
| 8668 | |
| 8669 | AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect, |
| 8670 | const sp<AudioFlinger::Client>& client, |
| 8671 | const sp<IEffectClient>& effectClient, |
| 8672 | int32_t priority) |
| 8673 | : BnEffect(), |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8674 | mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL), |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8675 | mPriority(priority), mHasControl(false), mEnabled(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8676 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8677 | ALOGV("constructor %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8678 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8679 | if (client == 0) { |
| 8680 | return; |
| 8681 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8682 | int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int); |
| 8683 | mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset); |
| 8684 | if (mCblkMemory != 0) { |
| 8685 | mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer()); |
| 8686 | |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 8687 | if (mCblk != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8688 | new(mCblk) effect_param_cblk_t(); |
| 8689 | mBuffer = (uint8_t *)mCblk + bufOffset; |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 8690 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8691 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 8692 | ALOGE("not enough memory for Effect size=%u", EFFECT_PARAM_BUFFER_SIZE + sizeof(effect_param_cblk_t)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8693 | return; |
| 8694 | } |
| 8695 | } |
| 8696 | |
| 8697 | AudioFlinger::EffectHandle::~EffectHandle() |
| 8698 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8699 | ALOGV("Destructor %p", this); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8700 | disconnect(false); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8701 | ALOGV("Destructor DONE %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8702 | } |
| 8703 | |
| 8704 | status_t AudioFlinger::EffectHandle::enable() |
| 8705 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8706 | ALOGV("enable %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8707 | if (!mHasControl) return INVALID_OPERATION; |
| 8708 | if (mEffect == 0) return DEAD_OBJECT; |
| 8709 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 8710 | if (mEnabled) { |
| 8711 | return NO_ERROR; |
| 8712 | } |
| 8713 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8714 | mEnabled = true; |
| 8715 | |
| 8716 | sp<ThreadBase> thread = mEffect->thread().promote(); |
| 8717 | if (thread != 0) { |
| 8718 | thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId()); |
| 8719 | } |
| 8720 | |
| 8721 | // checkSuspendOnEffectEnabled() can suspend this same effect when enabled |
| 8722 | if (mEffect->suspended()) { |
| 8723 | return NO_ERROR; |
| 8724 | } |
| 8725 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 8726 | status_t status = mEffect->setEnabled(true); |
| 8727 | if (status != NO_ERROR) { |
| 8728 | if (thread != 0) { |
| 8729 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); |
| 8730 | } |
| 8731 | mEnabled = false; |
| 8732 | } |
| 8733 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8734 | } |
| 8735 | |
| 8736 | status_t AudioFlinger::EffectHandle::disable() |
| 8737 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8738 | ALOGV("disable %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8739 | if (!mHasControl) return INVALID_OPERATION; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8740 | if (mEffect == 0) return DEAD_OBJECT; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8741 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 8742 | if (!mEnabled) { |
| 8743 | return NO_ERROR; |
| 8744 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8745 | mEnabled = false; |
| 8746 | |
| 8747 | if (mEffect->suspended()) { |
| 8748 | return NO_ERROR; |
| 8749 | } |
| 8750 | |
| 8751 | status_t status = mEffect->setEnabled(false); |
| 8752 | |
| 8753 | sp<ThreadBase> thread = mEffect->thread().promote(); |
| 8754 | if (thread != 0) { |
| 8755 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); |
| 8756 | } |
| 8757 | |
| 8758 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8759 | } |
| 8760 | |
| 8761 | void AudioFlinger::EffectHandle::disconnect() |
| 8762 | { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8763 | disconnect(true); |
| 8764 | } |
| 8765 | |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 8766 | void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast) |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8767 | { |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 8768 | ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8769 | if (mEffect == 0) { |
| 8770 | return; |
| 8771 | } |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 8772 | mEffect->disconnect(this, unpinIfLast); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8773 | |
Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 8774 | if (mHasControl && mEnabled) { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 8775 | sp<ThreadBase> thread = mEffect->thread().promote(); |
| 8776 | if (thread != 0) { |
| 8777 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); |
| 8778 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8779 | } |
| 8780 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8781 | // release sp on module => module destructor can be called now |
| 8782 | mEffect.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8783 | if (mClient != 0) { |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 8784 | if (mCblk != NULL) { |
Glenn Kasten | 1a0ae5b | 2012-02-03 10:24:48 -0800 | [diff] [blame] | 8785 | // unlike ~TrackBase(), mCblk is never a local new, so don't delete |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8786 | mCblk->~effect_param_cblk_t(); // destroy our shared-structure. |
| 8787 | } |
Glenn Kasten | dbfafaf | 2012-01-25 15:27:15 -0800 | [diff] [blame] | 8788 | mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 8789 | // Client destructor must run with AudioFlinger mutex locked |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8790 | Mutex::Autolock _l(mClient->audioFlinger()->mLock); |
| 8791 | mClient.clear(); |
| 8792 | } |
| 8793 | } |
| 8794 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8795 | status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode, |
| 8796 | uint32_t cmdSize, |
| 8797 | void *pCmdData, |
| 8798 | uint32_t *replySize, |
| 8799 | void *pReplyData) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8800 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8801 | // ALOGV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p", |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8802 | // cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8803 | |
| 8804 | // only get parameter command is permitted for applications not controlling the effect |
| 8805 | if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) { |
| 8806 | return INVALID_OPERATION; |
| 8807 | } |
| 8808 | if (mEffect == 0) return DEAD_OBJECT; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8809 | if (mClient == 0) return INVALID_OPERATION; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8810 | |
| 8811 | // handle commands that are not forwarded transparently to effect engine |
| 8812 | if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) { |
| 8813 | // No need to trylock() here as this function is executed in the binder thread serving a particular client process: |
| 8814 | // no risk to block the whole media server process or mixer threads is we are stuck here |
| 8815 | Mutex::Autolock _l(mCblk->lock); |
| 8816 | if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE || |
| 8817 | mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) { |
| 8818 | mCblk->serverIndex = 0; |
| 8819 | mCblk->clientIndex = 0; |
| 8820 | return BAD_VALUE; |
| 8821 | } |
| 8822 | status_t status = NO_ERROR; |
| 8823 | while (mCblk->serverIndex < mCblk->clientIndex) { |
| 8824 | int reply; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8825 | uint32_t rsize = sizeof(int); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8826 | int *p = (int *)(mBuffer + mCblk->serverIndex); |
| 8827 | int size = *p++; |
| 8828 | if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 8829 | ALOGW("command(): invalid parameter block size"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8830 | break; |
| 8831 | } |
| 8832 | effect_param_t *param = (effect_param_t *)p; |
| 8833 | if (param->psize == 0 || param->vsize == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 8834 | ALOGW("command(): null parameter or value size"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8835 | mCblk->serverIndex += size; |
| 8836 | continue; |
| 8837 | } |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8838 | uint32_t psize = sizeof(effect_param_t) + |
| 8839 | ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + |
| 8840 | param->vsize; |
| 8841 | status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM, |
| 8842 | psize, |
| 8843 | p, |
| 8844 | &rsize, |
| 8845 | &reply); |
Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 8846 | // stop at first error encountered |
| 8847 | if (ret != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8848 | status = ret; |
Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 8849 | *(int *)pReplyData = reply; |
| 8850 | break; |
| 8851 | } else if (reply != NO_ERROR) { |
| 8852 | *(int *)pReplyData = reply; |
| 8853 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8854 | } |
| 8855 | mCblk->serverIndex += size; |
| 8856 | } |
| 8857 | mCblk->serverIndex = 0; |
| 8858 | mCblk->clientIndex = 0; |
| 8859 | return status; |
| 8860 | } else if (cmdCode == EFFECT_CMD_ENABLE) { |
Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 8861 | *(int *)pReplyData = NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8862 | return enable(); |
| 8863 | } else if (cmdCode == EFFECT_CMD_DISABLE) { |
Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 8864 | *(int *)pReplyData = NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8865 | return disable(); |
| 8866 | } |
| 8867 | |
| 8868 | return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 8869 | } |
| 8870 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8871 | void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8872 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8873 | ALOGV("setControl %p control %d", this, hasControl); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8874 | |
| 8875 | mHasControl = hasControl; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8876 | mEnabled = enabled; |
| 8877 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8878 | if (signal && mEffectClient != 0) { |
| 8879 | mEffectClient->controlStatusChanged(hasControl); |
| 8880 | } |
| 8881 | } |
| 8882 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8883 | void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode, |
| 8884 | uint32_t cmdSize, |
| 8885 | void *pCmdData, |
| 8886 | uint32_t replySize, |
| 8887 | void *pReplyData) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8888 | { |
| 8889 | if (mEffectClient != 0) { |
| 8890 | mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 8891 | } |
| 8892 | } |
| 8893 | |
| 8894 | |
| 8895 | |
| 8896 | void AudioFlinger::EffectHandle::setEnabled(bool enabled) |
| 8897 | { |
| 8898 | if (mEffectClient != 0) { |
| 8899 | mEffectClient->enableStatusChanged(enabled); |
| 8900 | } |
| 8901 | } |
| 8902 | |
| 8903 | status_t AudioFlinger::EffectHandle::onTransact( |
| 8904 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 8905 | { |
| 8906 | return BnEffect::onTransact(code, data, reply, flags); |
| 8907 | } |
| 8908 | |
| 8909 | |
| 8910 | void AudioFlinger::EffectHandle::dump(char* buffer, size_t size) |
| 8911 | { |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 8912 | bool locked = mCblk != NULL && tryLock(mCblk->lock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8913 | |
| 8914 | snprintf(buffer, size, "\t\t\t%05d %05d %01u %01u %05u %05u\n", |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 8915 | (mClient == 0) ? getpid_cached : mClient->pid(), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8916 | mPriority, |
| 8917 | mHasControl, |
| 8918 | !locked, |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8919 | mCblk ? mCblk->clientIndex : 0, |
| 8920 | mCblk ? mCblk->serverIndex : 0 |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8921 | ); |
| 8922 | |
| 8923 | if (locked) { |
| 8924 | mCblk->lock.unlock(); |
| 8925 | } |
| 8926 | } |
| 8927 | |
| 8928 | #undef LOG_TAG |
| 8929 | #define LOG_TAG "AudioFlinger::EffectChain" |
| 8930 | |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 8931 | AudioFlinger::EffectChain::EffectChain(ThreadBase *thread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8932 | int sessionId) |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 8933 | : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0), |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 8934 | mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX), |
| 8935 | mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8936 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 8937 | mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 8938 | if (thread == NULL) { |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 8939 | return; |
| 8940 | } |
| 8941 | mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) / |
| 8942 | thread->frameCount(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8943 | } |
| 8944 | |
| 8945 | AudioFlinger::EffectChain::~EffectChain() |
| 8946 | { |
| 8947 | if (mOwnInBuffer) { |
| 8948 | delete mInBuffer; |
| 8949 | } |
| 8950 | |
| 8951 | } |
| 8952 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8953 | // getEffectFromDesc_l() must be called with ThreadBase::mLock held |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8954 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(effect_descriptor_t *descriptor) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8955 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8956 | size_t size = mEffects.size(); |
| 8957 | |
| 8958 | for (size_t i = 0; i < size; i++) { |
| 8959 | if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) { |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 8960 | return mEffects[i]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8961 | } |
| 8962 | } |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 8963 | return 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8964 | } |
| 8965 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8966 | // getEffectFromId_l() must be called with ThreadBase::mLock held |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8967 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8968 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8969 | size_t size = mEffects.size(); |
| 8970 | |
| 8971 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 8972 | // by convention, return first effect if id provided is 0 (0 is never a valid id) |
| 8973 | if (id == 0 || mEffects[i]->id() == id) { |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 8974 | return mEffects[i]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8975 | } |
| 8976 | } |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 8977 | return 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8978 | } |
| 8979 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8980 | // getEffectFromType_l() must be called with ThreadBase::mLock held |
| 8981 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l( |
| 8982 | const effect_uuid_t *type) |
| 8983 | { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8984 | size_t size = mEffects.size(); |
| 8985 | |
| 8986 | for (size_t i = 0; i < size; i++) { |
| 8987 | if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) { |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 8988 | return mEffects[i]; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8989 | } |
| 8990 | } |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 8991 | return 0; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8992 | } |
| 8993 | |
Eric Laurent | 91b14c4 | 2012-05-30 12:30:29 -0700 | [diff] [blame^] | 8994 | void AudioFlinger::EffectChain::clearInputBuffer() |
| 8995 | { |
| 8996 | Mutex::Autolock _l(mLock); |
| 8997 | sp<ThreadBase> thread = mThread.promote(); |
| 8998 | if (thread == 0) { |
| 8999 | ALOGW("clearInputBuffer(): cannot promote mixer thread"); |
| 9000 | return; |
| 9001 | } |
| 9002 | clearInputBuffer_l(thread); |
| 9003 | } |
| 9004 | |
| 9005 | // Must be called with EffectChain::mLock locked |
| 9006 | void AudioFlinger::EffectChain::clearInputBuffer_l(sp<ThreadBase> thread) |
| 9007 | { |
| 9008 | size_t numSamples = thread->frameCount() * thread->channelCount(); |
| 9009 | memset(mInBuffer, 0, numSamples * sizeof(int16_t)); |
| 9010 | |
| 9011 | } |
| 9012 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9013 | // Must be called with EffectChain::mLock locked |
| 9014 | void AudioFlinger::EffectChain::process_l() |
| 9015 | { |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 9016 | sp<ThreadBase> thread = mThread.promote(); |
| 9017 | if (thread == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 9018 | ALOGW("process_l(): cannot promote mixer thread"); |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 9019 | return; |
| 9020 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 9021 | bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) || |
| 9022 | (mSessionId == AUDIO_SESSION_OUTPUT_STAGE); |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 9023 | // always process effects unless no more tracks are on the session and the effect tail |
| 9024 | // has been rendered |
| 9025 | bool doProcess = true; |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 9026 | if (!isGlobalSession) { |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 9027 | bool tracksOnSession = (trackCnt() != 0); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 9028 | |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 9029 | if (!tracksOnSession && mTailBufferCount == 0) { |
| 9030 | doProcess = false; |
| 9031 | } |
| 9032 | |
| 9033 | if (activeTrackCnt() == 0) { |
| 9034 | // if no track is active and the effect tail has not been rendered, |
| 9035 | // the input buffer must be cleared here as the mixer process will not do it |
| 9036 | if (tracksOnSession || mTailBufferCount > 0) { |
Eric Laurent | 91b14c4 | 2012-05-30 12:30:29 -0700 | [diff] [blame^] | 9037 | clearInputBuffer_l(thread); |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 9038 | if (mTailBufferCount > 0) { |
| 9039 | mTailBufferCount--; |
| 9040 | } |
| 9041 | } |
| 9042 | } |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 9043 | } |
| 9044 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9045 | size_t size = mEffects.size(); |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 9046 | if (doProcess) { |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 9047 | for (size_t i = 0; i < size; i++) { |
| 9048 | mEffects[i]->process(); |
| 9049 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9050 | } |
| 9051 | for (size_t i = 0; i < size; i++) { |
| 9052 | mEffects[i]->updateState(); |
| 9053 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9054 | } |
| 9055 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 9056 | // addEffect_l() must be called with PlaybackThread::mLock held |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 9057 | status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9058 | { |
| 9059 | effect_descriptor_t desc = effect->desc(); |
| 9060 | uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK; |
| 9061 | |
| 9062 | Mutex::Autolock _l(mLock); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 9063 | effect->setChain(this); |
| 9064 | sp<ThreadBase> thread = mThread.promote(); |
| 9065 | if (thread == 0) { |
| 9066 | return NO_INIT; |
| 9067 | } |
| 9068 | effect->setThread(thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9069 | |
| 9070 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 9071 | // Auxiliary effects are inserted at the beginning of mEffects vector as |
| 9072 | // they are processed first and accumulated in chain input buffer |
| 9073 | mEffects.insertAt(effect, 0); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 9074 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9075 | // the input buffer for auxiliary effect contains mono samples in |
| 9076 | // 32 bit format. This is to avoid saturation in AudoMixer |
| 9077 | // accumulation stage. Saturation is done in EffectModule::process() before |
| 9078 | // calling the process in effect engine |
| 9079 | size_t numSamples = thread->frameCount(); |
| 9080 | int32_t *buffer = new int32_t[numSamples]; |
| 9081 | memset(buffer, 0, numSamples * sizeof(int32_t)); |
| 9082 | effect->setInBuffer((int16_t *)buffer); |
| 9083 | // auxiliary effects output samples to chain input buffer for further processing |
| 9084 | // by insert effects |
| 9085 | effect->setOutBuffer(mInBuffer); |
| 9086 | } else { |
| 9087 | // Insert effects are inserted at the end of mEffects vector as they are processed |
| 9088 | // after track and auxiliary effects. |
| 9089 | // Insert effect order as a function of indicated preference: |
| 9090 | // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if |
| 9091 | // another effect is present |
| 9092 | // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the |
| 9093 | // last effect claiming first position |
| 9094 | // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the |
| 9095 | // first effect claiming last position |
| 9096 | // else if EFFECT_FLAG_INSERT_ANY insert after first or before last |
| 9097 | // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is |
| 9098 | // already present |
| 9099 | |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 9100 | size_t size = mEffects.size(); |
| 9101 | size_t idx_insert = size; |
| 9102 | ssize_t idx_insert_first = -1; |
| 9103 | ssize_t idx_insert_last = -1; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9104 | |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 9105 | for (size_t i = 0; i < size; i++) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9106 | effect_descriptor_t d = mEffects[i]->desc(); |
| 9107 | uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK; |
| 9108 | uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK; |
| 9109 | if (iMode == EFFECT_FLAG_TYPE_INSERT) { |
| 9110 | // check invalid effect chaining combinations |
| 9111 | if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE || |
| 9112 | iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 9113 | ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s", desc.name, d.name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9114 | return INVALID_OPERATION; |
| 9115 | } |
| 9116 | // remember position of first insert effect and by default |
| 9117 | // select this as insert position for new effect |
| 9118 | if (idx_insert == size) { |
| 9119 | idx_insert = i; |
| 9120 | } |
| 9121 | // remember position of last insert effect claiming |
| 9122 | // first position |
| 9123 | if (iPref == EFFECT_FLAG_INSERT_FIRST) { |
| 9124 | idx_insert_first = i; |
| 9125 | } |
| 9126 | // remember position of first insert effect claiming |
| 9127 | // last position |
| 9128 | if (iPref == EFFECT_FLAG_INSERT_LAST && |
| 9129 | idx_insert_last == -1) { |
| 9130 | idx_insert_last = i; |
| 9131 | } |
| 9132 | } |
| 9133 | } |
| 9134 | |
| 9135 | // modify idx_insert from first position if needed |
| 9136 | if (insertPref == EFFECT_FLAG_INSERT_LAST) { |
| 9137 | if (idx_insert_last != -1) { |
| 9138 | idx_insert = idx_insert_last; |
| 9139 | } else { |
| 9140 | idx_insert = size; |
| 9141 | } |
| 9142 | } else { |
| 9143 | if (idx_insert_first != -1) { |
| 9144 | idx_insert = idx_insert_first + 1; |
| 9145 | } |
| 9146 | } |
| 9147 | |
| 9148 | // always read samples from chain input buffer |
| 9149 | effect->setInBuffer(mInBuffer); |
| 9150 | |
| 9151 | // if last effect in the chain, output samples to chain |
| 9152 | // output buffer, otherwise to chain input buffer |
| 9153 | if (idx_insert == size) { |
| 9154 | if (idx_insert != 0) { |
| 9155 | mEffects[idx_insert-1]->setOutBuffer(mInBuffer); |
| 9156 | mEffects[idx_insert-1]->configure(); |
| 9157 | } |
| 9158 | effect->setOutBuffer(mOutBuffer); |
| 9159 | } else { |
| 9160 | effect->setOutBuffer(mInBuffer); |
| 9161 | } |
| 9162 | mEffects.insertAt(effect, idx_insert); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9163 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 9164 | ALOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this, idx_insert); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9165 | } |
| 9166 | effect->configure(); |
| 9167 | return NO_ERROR; |
| 9168 | } |
| 9169 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 9170 | // removeEffect_l() must be called with PlaybackThread::mLock held |
| 9171 | size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9172 | { |
| 9173 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 9174 | size_t size = mEffects.size(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9175 | uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK; |
| 9176 | |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 9177 | for (size_t i = 0; i < size; i++) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9178 | if (effect == mEffects[i]) { |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 9179 | // calling stop here will remove pre-processing effect from the audio HAL. |
| 9180 | // This is safe as we hold the EffectChain mutex which guarantees that we are not in |
| 9181 | // the middle of a read from audio HAL |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 9182 | if (mEffects[i]->state() == EffectModule::ACTIVE || |
| 9183 | mEffects[i]->state() == EffectModule::STOPPING) { |
| 9184 | mEffects[i]->stop(); |
| 9185 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9186 | if (type == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 9187 | delete[] effect->inBuffer(); |
| 9188 | } else { |
| 9189 | if (i == size - 1 && i != 0) { |
| 9190 | mEffects[i - 1]->setOutBuffer(mOutBuffer); |
| 9191 | mEffects[i - 1]->configure(); |
| 9192 | } |
| 9193 | } |
| 9194 | mEffects.removeAt(i); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 9195 | ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(), this, i); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9196 | break; |
| 9197 | } |
| 9198 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9199 | |
| 9200 | return mEffects.size(); |
| 9201 | } |
| 9202 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 9203 | // setDevice_l() must be called with PlaybackThread::mLock held |
| 9204 | void AudioFlinger::EffectChain::setDevice_l(uint32_t device) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9205 | { |
| 9206 | size_t size = mEffects.size(); |
| 9207 | for (size_t i = 0; i < size; i++) { |
| 9208 | mEffects[i]->setDevice(device); |
| 9209 | } |
| 9210 | } |
| 9211 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 9212 | // setMode_l() must be called with PlaybackThread::mLock held |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 9213 | void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9214 | { |
| 9215 | size_t size = mEffects.size(); |
| 9216 | for (size_t i = 0; i < size; i++) { |
| 9217 | mEffects[i]->setMode(mode); |
| 9218 | } |
| 9219 | } |
| 9220 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 9221 | // setVolume_l() must be called with PlaybackThread::mLock held |
| 9222 | bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9223 | { |
| 9224 | uint32_t newLeft = *left; |
| 9225 | uint32_t newRight = *right; |
| 9226 | bool hasControl = false; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 9227 | int ctrlIdx = -1; |
| 9228 | size_t size = mEffects.size(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9229 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 9230 | // first update volume controller |
| 9231 | for (size_t i = size; i > 0; i--) { |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 9232 | if (mEffects[i - 1]->isProcessEnabled() && |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 9233 | (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) { |
| 9234 | ctrlIdx = i - 1; |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 9235 | hasControl = true; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 9236 | break; |
| 9237 | } |
| 9238 | } |
| 9239 | |
| 9240 | if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) { |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 9241 | if (hasControl) { |
| 9242 | *left = mNewLeftVolume; |
| 9243 | *right = mNewRightVolume; |
| 9244 | } |
| 9245 | return hasControl; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 9246 | } |
| 9247 | |
| 9248 | mVolumeCtrlIdx = ctrlIdx; |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 9249 | mLeftVolume = newLeft; |
| 9250 | mRightVolume = newRight; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 9251 | |
| 9252 | // second get volume update from volume controller |
| 9253 | if (ctrlIdx >= 0) { |
| 9254 | mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true); |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 9255 | mNewLeftVolume = newLeft; |
| 9256 | mNewRightVolume = newRight; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9257 | } |
| 9258 | // then indicate volume to all other effects in chain. |
| 9259 | // Pass altered volume to effects before volume controller |
| 9260 | // and requested volume to effects after controller |
| 9261 | uint32_t lVol = newLeft; |
| 9262 | uint32_t rVol = newRight; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 9263 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9264 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 9265 | if ((int)i == ctrlIdx) continue; |
| 9266 | // this also works for ctrlIdx == -1 when there is no volume controller |
| 9267 | if ((int)i > ctrlIdx) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9268 | lVol = *left; |
| 9269 | rVol = *right; |
| 9270 | } |
| 9271 | mEffects[i]->setVolume(&lVol, &rVol, false); |
| 9272 | } |
| 9273 | *left = newLeft; |
| 9274 | *right = newRight; |
| 9275 | |
| 9276 | return hasControl; |
| 9277 | } |
| 9278 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9279 | status_t AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args) |
| 9280 | { |
| 9281 | const size_t SIZE = 256; |
| 9282 | char buffer[SIZE]; |
| 9283 | String8 result; |
| 9284 | |
| 9285 | snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId); |
| 9286 | result.append(buffer); |
| 9287 | |
| 9288 | bool locked = tryLock(mLock); |
| 9289 | // failed to lock - AudioFlinger is probably deadlocked |
| 9290 | if (!locked) { |
| 9291 | result.append("\tCould not lock mutex:\n"); |
| 9292 | } |
| 9293 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 9294 | result.append("\tNum fx In buffer Out buffer Active tracks:\n"); |
| 9295 | snprintf(buffer, SIZE, "\t%02d 0x%08x 0x%08x %d\n", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9296 | mEffects.size(), |
| 9297 | (uint32_t)mInBuffer, |
| 9298 | (uint32_t)mOutBuffer, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9299 | mActiveTrackCnt); |
| 9300 | result.append(buffer); |
| 9301 | write(fd, result.string(), result.size()); |
| 9302 | |
| 9303 | for (size_t i = 0; i < mEffects.size(); ++i) { |
| 9304 | sp<EffectModule> effect = mEffects[i]; |
| 9305 | if (effect != 0) { |
| 9306 | effect->dump(fd, args); |
| 9307 | } |
| 9308 | } |
| 9309 | |
| 9310 | if (locked) { |
| 9311 | mLock.unlock(); |
| 9312 | } |
| 9313 | |
| 9314 | return NO_ERROR; |
| 9315 | } |
| 9316 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9317 | // must be called with ThreadBase::mLock held |
| 9318 | void AudioFlinger::EffectChain::setEffectSuspended_l( |
| 9319 | const effect_uuid_t *type, bool suspend) |
| 9320 | { |
| 9321 | sp<SuspendedEffectDesc> desc; |
| 9322 | // use effect type UUID timelow as key as there is no real risk of identical |
| 9323 | // timeLow fields among effect type UUIDs. |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 9324 | ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9325 | if (suspend) { |
| 9326 | if (index >= 0) { |
| 9327 | desc = mSuspendedEffects.valueAt(index); |
| 9328 | } else { |
| 9329 | desc = new SuspendedEffectDesc(); |
| 9330 | memcpy(&desc->mType, type, sizeof(effect_uuid_t)); |
| 9331 | mSuspendedEffects.add(type->timeLow, desc); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 9332 | ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9333 | } |
| 9334 | if (desc->mRefCount++ == 0) { |
| 9335 | sp<EffectModule> effect = getEffectIfEnabled(type); |
| 9336 | if (effect != 0) { |
| 9337 | desc->mEffect = effect; |
| 9338 | effect->setSuspended(true); |
| 9339 | effect->setEnabled(false); |
| 9340 | } |
| 9341 | } |
| 9342 | } else { |
| 9343 | if (index < 0) { |
| 9344 | return; |
| 9345 | } |
| 9346 | desc = mSuspendedEffects.valueAt(index); |
| 9347 | if (desc->mRefCount <= 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 9348 | ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9349 | desc->mRefCount = 1; |
| 9350 | } |
| 9351 | if (--desc->mRefCount == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 9352 | ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9353 | if (desc->mEffect != 0) { |
| 9354 | sp<EffectModule> effect = desc->mEffect.promote(); |
| 9355 | if (effect != 0) { |
| 9356 | effect->setSuspended(false); |
| 9357 | sp<EffectHandle> handle = effect->controlHandle(); |
| 9358 | if (handle != 0) { |
| 9359 | effect->setEnabled(handle->enabled()); |
| 9360 | } |
| 9361 | } |
| 9362 | desc->mEffect.clear(); |
| 9363 | } |
| 9364 | mSuspendedEffects.removeItemsAt(index); |
| 9365 | } |
| 9366 | } |
| 9367 | } |
| 9368 | |
| 9369 | // must be called with ThreadBase::mLock held |
| 9370 | void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend) |
| 9371 | { |
| 9372 | sp<SuspendedEffectDesc> desc; |
| 9373 | |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 9374 | ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9375 | if (suspend) { |
| 9376 | if (index >= 0) { |
| 9377 | desc = mSuspendedEffects.valueAt(index); |
| 9378 | } else { |
| 9379 | desc = new SuspendedEffectDesc(); |
| 9380 | mSuspendedEffects.add((int)kKeyForSuspendAll, desc); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 9381 | ALOGV("setEffectSuspendedAll_l() add entry for 0"); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9382 | } |
| 9383 | if (desc->mRefCount++ == 0) { |
Glenn Kasten | d053971 | 2012-01-30 12:56:03 -0800 | [diff] [blame] | 9384 | Vector< sp<EffectModule> > effects; |
| 9385 | getSuspendEligibleEffects(effects); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9386 | for (size_t i = 0; i < effects.size(); i++) { |
| 9387 | setEffectSuspended_l(&effects[i]->desc().type, true); |
| 9388 | } |
| 9389 | } |
| 9390 | } else { |
| 9391 | if (index < 0) { |
| 9392 | return; |
| 9393 | } |
| 9394 | desc = mSuspendedEffects.valueAt(index); |
| 9395 | if (desc->mRefCount <= 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 9396 | ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9397 | desc->mRefCount = 1; |
| 9398 | } |
| 9399 | if (--desc->mRefCount == 0) { |
| 9400 | Vector<const effect_uuid_t *> types; |
| 9401 | for (size_t i = 0; i < mSuspendedEffects.size(); i++) { |
| 9402 | if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) { |
| 9403 | continue; |
| 9404 | } |
| 9405 | types.add(&mSuspendedEffects.valueAt(i)->mType); |
| 9406 | } |
| 9407 | for (size_t i = 0; i < types.size(); i++) { |
| 9408 | setEffectSuspended_l(types[i], false); |
| 9409 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 9410 | ALOGV("setEffectSuspendedAll_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9411 | mSuspendedEffects.removeItem((int)kKeyForSuspendAll); |
| 9412 | } |
| 9413 | } |
| 9414 | } |
| 9415 | |
Eric Laurent | 6bffdb8 | 2011-09-23 08:40:41 -0700 | [diff] [blame] | 9416 | |
| 9417 | // The volume effect is used for automated tests only |
| 9418 | #ifndef OPENSL_ES_H_ |
| 9419 | static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6, |
| 9420 | { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }; |
| 9421 | const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_; |
| 9422 | #endif //OPENSL_ES_H_ |
| 9423 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 9424 | bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc) |
| 9425 | { |
| 9426 | // auxiliary effects and visualizer are never suspended on output mix |
| 9427 | if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) && |
| 9428 | (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) || |
Eric Laurent | 6bffdb8 | 2011-09-23 08:40:41 -0700 | [diff] [blame] | 9429 | (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) || |
| 9430 | (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 9431 | return false; |
| 9432 | } |
| 9433 | return true; |
| 9434 | } |
| 9435 | |
Glenn Kasten | d053971 | 2012-01-30 12:56:03 -0800 | [diff] [blame] | 9436 | void AudioFlinger::EffectChain::getSuspendEligibleEffects(Vector< sp<AudioFlinger::EffectModule> > &effects) |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9437 | { |
Glenn Kasten | d053971 | 2012-01-30 12:56:03 -0800 | [diff] [blame] | 9438 | effects.clear(); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9439 | for (size_t i = 0; i < mEffects.size(); i++) { |
Glenn Kasten | d053971 | 2012-01-30 12:56:03 -0800 | [diff] [blame] | 9440 | if (isEffectEligibleForSuspend(mEffects[i]->desc())) { |
| 9441 | effects.add(mEffects[i]); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9442 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9443 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9444 | } |
| 9445 | |
| 9446 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled( |
| 9447 | const effect_uuid_t *type) |
| 9448 | { |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 9449 | sp<EffectModule> effect = getEffectFromType_l(type); |
| 9450 | return effect != 0 && effect->isEnabled() ? effect : 0; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9451 | } |
| 9452 | |
| 9453 | void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 9454 | bool enabled) |
| 9455 | { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 9456 | ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9457 | if (enabled) { |
| 9458 | if (index < 0) { |
| 9459 | // if the effect is not suspend check if all effects are suspended |
| 9460 | index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); |
| 9461 | if (index < 0) { |
| 9462 | return; |
| 9463 | } |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 9464 | if (!isEffectEligibleForSuspend(effect->desc())) { |
| 9465 | return; |
| 9466 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9467 | setEffectSuspended_l(&effect->desc().type, enabled); |
| 9468 | index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 9469 | if (index < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 9470 | ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!"); |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 9471 | return; |
| 9472 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9473 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 9474 | ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x", |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 9475 | effect->desc().type.timeLow); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9476 | sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); |
| 9477 | // if effect is requested to suspended but was not yet enabled, supend it now. |
| 9478 | if (desc->mEffect == 0) { |
| 9479 | desc->mEffect = effect; |
| 9480 | effect->setEnabled(false); |
| 9481 | effect->setSuspended(true); |
| 9482 | } |
| 9483 | } else { |
| 9484 | if (index < 0) { |
| 9485 | return; |
| 9486 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 9487 | ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x", |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 9488 | effect->desc().type.timeLow); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9489 | sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); |
| 9490 | desc->mEffect.clear(); |
| 9491 | effect->setSuspended(false); |
| 9492 | } |
| 9493 | } |
| 9494 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9495 | #undef LOG_TAG |
| 9496 | #define LOG_TAG "AudioFlinger" |
| 9497 | |
| 9498 | // ---------------------------------------------------------------------------- |
| 9499 | |
| 9500 | status_t AudioFlinger::onTransact( |
| 9501 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 9502 | { |
| 9503 | return BnAudioFlinger::onTransact(code, data, reply, flags); |
| 9504 | } |
| 9505 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9506 | }; // namespace android |