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> |
| 30 | #include <binder/Parcel.h> |
| 31 | #include <binder/IPCThreadState.h> |
| 32 | #include <utils/String16.h> |
| 33 | #include <utils/threads.h> |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 34 | #include <utils/Atomic.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 35 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 36 | #include <cutils/bitops.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 37 | #include <cutils/properties.h> |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 38 | #include <cutils/compiler.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 39 | |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 40 | #undef ADD_BATTERY_DATA |
| 41 | |
| 42 | #ifdef ADD_BATTERY_DATA |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 43 | #include <media/IMediaPlayerService.h> |
Glenn Kasten | 25b248e | 2012-01-03 15:28:29 -0800 | [diff] [blame] | 44 | #include <media/IMediaDeathNotifier.h> |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 45 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 46 | |
| 47 | #include <private/media/AudioTrackShared.h> |
| 48 | #include <private/media/AudioEffectShared.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 49 | |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 50 | #include <system/audio.h> |
Dima Zavin | 7394a4f | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 51 | #include <hardware/audio.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 52 | |
| 53 | #include "AudioMixer.h" |
| 54 | #include "AudioFlinger.h" |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 55 | #include "ServiceUtilities.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 56 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 57 | #include <media/EffectsFactoryApi.h> |
Eric Laurent | 6d8b694 | 2011-06-24 07:01:31 -0700 | [diff] [blame] | 58 | #include <audio_effects/effect_visualizer.h> |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 59 | #include <audio_effects/effect_ns.h> |
| 60 | #include <audio_effects/effect_aec.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 61 | |
Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 62 | #include <audio_utils/primitives.h> |
| 63 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 64 | #include <powermanager/PowerManager.h> |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 65 | |
Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 66 | // #define DEBUG_CPU_USAGE 10 // log statistics every n wall clock seconds |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 67 | #ifdef DEBUG_CPU_USAGE |
| 68 | #include <cpustats/CentralTendencyStatistics.h> |
| 69 | #include <cpustats/ThreadCpuUsage.h> |
| 70 | #endif |
Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 71 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 72 | #include <common_time/cc_helper.h> |
| 73 | #include <common_time/local_clock.h> |
| 74 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 75 | #include "FastMixer.h" |
| 76 | |
| 77 | // NBAIO implementations |
| 78 | #include "AudioStreamOutSink.h" |
| 79 | #include "MonoPipe.h" |
| 80 | #include "MonoPipeReader.h" |
| 81 | #include "SourceAudioBufferProvider.h" |
| 82 | |
Glenn Kasten | 1dc28b7 | 2012-04-24 10:01:03 -0700 | [diff] [blame] | 83 | #ifdef HAVE_REQUEST_PRIORITY |
| 84 | #include "SchedulingPolicyService.h" |
| 85 | #endif |
| 86 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 87 | #ifdef SOAKER |
| 88 | #include "Soaker.h" |
| 89 | #endif |
| 90 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 91 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 92 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 93 | // Note: the following macro is used for extremely verbose logging message. In |
| 94 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 95 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 96 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 97 | // turned on. Do not uncomment the #def below unless you really know what you |
| 98 | // are doing and want to see all of the extremely verbose messages. |
| 99 | //#define VERY_VERY_VERBOSE_LOGGING |
| 100 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 101 | #define ALOGVV ALOGV |
| 102 | #else |
| 103 | #define ALOGVV(a...) do { } while(0) |
| 104 | #endif |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 105 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 106 | namespace android { |
| 107 | |
Glenn Kasten | ec1d6b5 | 2011-12-12 09:04:45 -0800 | [diff] [blame] | 108 | static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n"; |
| 109 | static const char kHardwareLockedString[] = "Hardware lock is taken\n"; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 110 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 111 | static const float MAX_GAIN = 4096.0f; |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 112 | static const uint32_t MAX_GAIN_INT = 0x1000; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 113 | |
| 114 | // retry counts for buffer fill timeout |
| 115 | // 50 * ~20msecs = 1 second |
| 116 | static const int8_t kMaxTrackRetries = 50; |
| 117 | static const int8_t kMaxTrackStartupRetries = 50; |
| 118 | // allow less retry attempts on direct output thread. |
| 119 | // direct outputs can be a scarce resource in audio hardware and should |
| 120 | // be released as quickly as possible. |
| 121 | static const int8_t kMaxTrackRetriesDirect = 2; |
| 122 | |
| 123 | static const int kDumpLockRetries = 50; |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 124 | static const int kDumpLockSleepUs = 20000; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 125 | |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 126 | // don't warn about blocked writes or record buffer overflows more often than this |
| 127 | static const nsecs_t kWarningThrottleNs = seconds(5); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 128 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 129 | // RecordThread loop sleep time upon application overrun or audio HAL read error |
| 130 | static const int kRecordThreadSleepUs = 5000; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 131 | |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 132 | // maximum time to wait for setParameters to complete |
| 133 | static const nsecs_t kSetParametersTimeoutNs = seconds(2); |
Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 134 | |
Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 135 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun |
| 136 | static const uint32_t kMinThreadSleepTimeUs = 5000; |
| 137 | // maximum divider applied to the active sleep time in the mixer thread loop |
| 138 | static const uint32_t kMaxThreadSleepTimeShift = 2; |
| 139 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 140 | // minimum normal mix buffer size, expressed in milliseconds rather than frames |
| 141 | static const uint32_t kMinNormalMixBufferSizeMs = 20; |
| 142 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 143 | nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs; |
Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 144 | |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 145 | // Whether to use fast mixer |
| 146 | static const enum { |
| 147 | FastMixer_Never, // never initialize or use: for debugging only |
| 148 | FastMixer_Always, // always initialize and use, even if not needed: for debugging only |
| 149 | // normal mixer multiplier is 1 |
| 150 | FastMixer_Static, // initialize if needed, then use all the time if initialized, |
| 151 | // multipler is calculated based on minimum normal mixer buffer size |
| 152 | FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load, |
| 153 | // multipler is calculated based on minimum normal mixer buffer size |
| 154 | // FIXME for FastMixer_Dynamic: |
| 155 | // Supporting this option will require fixing HALs that can't handle large writes. |
| 156 | // For example, one HAL implementation returns an error from a large write, |
| 157 | // and another HAL implementation corrupts memory, possibly in the sample rate converter. |
| 158 | // We could either fix the HAL implementations, or provide a wrapper that breaks |
| 159 | // up large writes into smaller ones, and the wrapper would need to deal with scheduler. |
| 160 | } kUseFastMixer = FastMixer_Static; |
| 161 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 162 | // ---------------------------------------------------------------------------- |
| 163 | |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 164 | #ifdef ADD_BATTERY_DATA |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 165 | // To collect the amplifier usage |
| 166 | static void addBatteryData(uint32_t params) { |
Glenn Kasten | 25b248e | 2012-01-03 15:28:29 -0800 | [diff] [blame] | 167 | sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); |
| 168 | if (service == NULL) { |
| 169 | // it already logged |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 170 | return; |
| 171 | } |
| 172 | |
| 173 | service->addBatteryData(params); |
| 174 | } |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 175 | #endif |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 176 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 177 | 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] | 178 | { |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 179 | const hw_module_t *mod; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 180 | int rc; |
| 181 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 182 | rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod); |
| 183 | ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__, |
| 184 | AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); |
| 185 | if (rc) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 186 | goto out; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 187 | } |
| 188 | rc = audio_hw_device_open(mod, dev); |
| 189 | ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__, |
| 190 | AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); |
| 191 | if (rc) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 192 | goto out; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 193 | } |
| 194 | if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) { |
| 195 | ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version); |
| 196 | rc = BAD_VALUE; |
| 197 | goto out; |
| 198 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 199 | return 0; |
| 200 | |
| 201 | out: |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 202 | *dev = NULL; |
| 203 | return rc; |
| 204 | } |
| 205 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 206 | // ---------------------------------------------------------------------------- |
| 207 | |
| 208 | AudioFlinger::AudioFlinger() |
| 209 | : BnAudioFlinger(), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 210 | mPrimaryHardwareDev(NULL), |
| 211 | mHardwareStatus(AUDIO_HW_IDLE), // see also onFirstRef() |
| 212 | mMasterVolume(1.0f), |
| 213 | mMasterVolumeSupportLvl(MVS_NONE), |
| 214 | mMasterMute(false), |
| 215 | mNextUniqueId(1), |
| 216 | mMode(AUDIO_MODE_INVALID), |
| 217 | mBtNrecIsOff(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 218 | { |
Dima Zavin | 5a61d2f | 2011-04-19 19:04:32 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | void AudioFlinger::onFirstRef() |
| 222 | { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 223 | int rc = 0; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 224 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 225 | Mutex::Autolock _l(mLock); |
| 226 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 227 | /* TODO: move all this work into an Init() function */ |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 228 | char val_str[PROPERTY_VALUE_MAX] = { 0 }; |
| 229 | if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) { |
| 230 | uint32_t int_val; |
| 231 | if (1 == sscanf(val_str, "%u", &int_val)) { |
| 232 | mStandbyTimeInNsecs = milliseconds(int_val); |
| 233 | ALOGI("Using %u mSec as standby time.", int_val); |
| 234 | } else { |
| 235 | mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs; |
| 236 | ALOGI("Using default %u mSec as standby time.", |
| 237 | (uint32_t)(mStandbyTimeInNsecs / 1000000)); |
| 238 | } |
| 239 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 240 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 241 | mMode = AUDIO_MODE_NORMAL; |
| 242 | mMasterVolumeSW = 1.0; |
| 243 | mMasterVolume = 1.0; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 244 | mHardwareStatus = AUDIO_HW_IDLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | AudioFlinger::~AudioFlinger() |
| 248 | { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 249 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 250 | while (!mRecordThreads.isEmpty()) { |
| 251 | // closeInput() will remove first entry from mRecordThreads |
| 252 | closeInput(mRecordThreads.keyAt(0)); |
| 253 | } |
| 254 | while (!mPlaybackThreads.isEmpty()) { |
| 255 | // closeOutput() will remove first entry from mPlaybackThreads |
| 256 | closeOutput(mPlaybackThreads.keyAt(0)); |
| 257 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 258 | |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 259 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 260 | // no mHardwareLock needed, as there are no other references to this |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 261 | audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice()); |
| 262 | delete mAudioHwDevs.valueAt(i); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 266 | static const char * const audio_interfaces[] = { |
| 267 | AUDIO_HARDWARE_MODULE_ID_PRIMARY, |
| 268 | AUDIO_HARDWARE_MODULE_ID_A2DP, |
| 269 | AUDIO_HARDWARE_MODULE_ID_USB, |
| 270 | }; |
| 271 | #define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0]))) |
| 272 | |
| 273 | 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] | 274 | { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 275 | // if module is 0, the request comes from an old policy manager and we should load |
| 276 | // well known modules |
| 277 | if (module == 0) { |
| 278 | ALOGW("findSuitableHwDev_l() loading well know audio hw modules"); |
| 279 | for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) { |
| 280 | loadHwModule_l(audio_interfaces[i]); |
| 281 | } |
| 282 | } else { |
| 283 | // check a match for the requested module handle |
| 284 | AudioHwDevice *audioHwdevice = mAudioHwDevs.valueFor(module); |
| 285 | if (audioHwdevice != NULL) { |
| 286 | return audioHwdevice->hwDevice(); |
| 287 | } |
| 288 | } |
| 289 | // then try to find a module supporting the requested device. |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 290 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 291 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 292 | if ((dev->get_supported_devices(dev) & devices) == devices) |
| 293 | return dev; |
| 294 | } |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 295 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 296 | return NULL; |
| 297 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 298 | |
| 299 | status_t AudioFlinger::dumpClients(int fd, const Vector<String16>& args) |
| 300 | { |
| 301 | const size_t SIZE = 256; |
| 302 | char buffer[SIZE]; |
| 303 | String8 result; |
| 304 | |
| 305 | result.append("Clients:\n"); |
| 306 | for (size_t i = 0; i < mClients.size(); ++i) { |
Glenn Kasten | 77c1119 | 2012-01-25 14:27:41 -0800 | [diff] [blame] | 307 | sp<Client> client = mClients.valueAt(i).promote(); |
| 308 | if (client != 0) { |
| 309 | snprintf(buffer, SIZE, " pid: %d\n", client->pid()); |
| 310 | result.append(buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 311 | } |
| 312 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 313 | |
| 314 | result.append("Global session refs:\n"); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 315 | result.append(" session pid count\n"); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 316 | for (size_t i = 0; i < mAudioSessionRefs.size(); i++) { |
| 317 | AudioSessionRef *r = mAudioSessionRefs[i]; |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 318 | 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] | 319 | result.append(buffer); |
| 320 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 321 | write(fd, result.string(), result.size()); |
| 322 | return NO_ERROR; |
| 323 | } |
| 324 | |
| 325 | |
| 326 | status_t AudioFlinger::dumpInternals(int fd, const Vector<String16>& args) |
| 327 | { |
| 328 | const size_t SIZE = 256; |
| 329 | char buffer[SIZE]; |
| 330 | String8 result; |
Glenn Kasten | a4454b4 | 2012-01-04 11:02:33 -0800 | [diff] [blame] | 331 | hardware_call_state hardwareStatus = mHardwareStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 332 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 333 | snprintf(buffer, SIZE, "Hardware status: %d\n" |
| 334 | "Standby Time mSec: %u\n", |
| 335 | hardwareStatus, |
| 336 | (uint32_t)(mStandbyTimeInNsecs / 1000000)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 337 | result.append(buffer); |
| 338 | write(fd, result.string(), result.size()); |
| 339 | return NO_ERROR; |
| 340 | } |
| 341 | |
| 342 | status_t AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args) |
| 343 | { |
| 344 | const size_t SIZE = 256; |
| 345 | char buffer[SIZE]; |
| 346 | String8 result; |
| 347 | snprintf(buffer, SIZE, "Permission Denial: " |
| 348 | "can't dump AudioFlinger from pid=%d, uid=%d\n", |
| 349 | IPCThreadState::self()->getCallingPid(), |
| 350 | IPCThreadState::self()->getCallingUid()); |
| 351 | result.append(buffer); |
| 352 | write(fd, result.string(), result.size()); |
| 353 | return NO_ERROR; |
| 354 | } |
| 355 | |
| 356 | static bool tryLock(Mutex& mutex) |
| 357 | { |
| 358 | bool locked = false; |
| 359 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 360 | if (mutex.tryLock() == NO_ERROR) { |
| 361 | locked = true; |
| 362 | break; |
| 363 | } |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 364 | usleep(kDumpLockSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 365 | } |
| 366 | return locked; |
| 367 | } |
| 368 | |
| 369 | status_t AudioFlinger::dump(int fd, const Vector<String16>& args) |
| 370 | { |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 371 | if (!dumpAllowed()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 372 | dumpPermissionDenial(fd, args); |
| 373 | } else { |
| 374 | // get state of hardware lock |
| 375 | bool hardwareLocked = tryLock(mHardwareLock); |
| 376 | if (!hardwareLocked) { |
| 377 | String8 result(kHardwareLockedString); |
| 378 | write(fd, result.string(), result.size()); |
| 379 | } else { |
| 380 | mHardwareLock.unlock(); |
| 381 | } |
| 382 | |
| 383 | bool locked = tryLock(mLock); |
| 384 | |
| 385 | // failed to lock - AudioFlinger is probably deadlocked |
| 386 | if (!locked) { |
| 387 | String8 result(kDeadlockedString); |
| 388 | write(fd, result.string(), result.size()); |
| 389 | } |
| 390 | |
| 391 | dumpClients(fd, args); |
| 392 | dumpInternals(fd, args); |
| 393 | |
| 394 | // dump playback threads |
| 395 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 396 | mPlaybackThreads.valueAt(i)->dump(fd, args); |
| 397 | } |
| 398 | |
| 399 | // dump record threads |
| 400 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 401 | mRecordThreads.valueAt(i)->dump(fd, args); |
| 402 | } |
| 403 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 404 | // dump all hardware devs |
| 405 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 406 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 407 | dev->dump(dev, fd); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 408 | } |
| 409 | if (locked) mLock.unlock(); |
| 410 | } |
| 411 | return NO_ERROR; |
| 412 | } |
| 413 | |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 414 | sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid) |
| 415 | { |
| 416 | // If pid is already in the mClients wp<> map, then use that entry |
| 417 | // (for which promote() is always != 0), otherwise create a new entry and Client. |
| 418 | sp<Client> client = mClients.valueFor(pid).promote(); |
| 419 | if (client == 0) { |
| 420 | client = new Client(this, pid); |
| 421 | mClients.add(pid, client); |
| 422 | } |
| 423 | |
| 424 | return client; |
| 425 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 426 | |
| 427 | // IAudioFlinger interface |
| 428 | |
| 429 | |
| 430 | sp<IAudioTrack> AudioFlinger::createTrack( |
| 431 | pid_t pid, |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 432 | audio_stream_type_t streamType, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 433 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 434 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 435 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 436 | int frameCount, |
Glenn Kasten | a075db4 | 2012-03-06 11:22:44 -0800 | [diff] [blame] | 437 | IAudioFlinger::track_flags_t flags, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 438 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 439 | audio_io_handle_t output, |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 440 | pid_t tid, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 441 | int *sessionId, |
| 442 | status_t *status) |
| 443 | { |
| 444 | sp<PlaybackThread::Track> track; |
| 445 | sp<TrackHandle> trackHandle; |
| 446 | sp<Client> client; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 447 | status_t lStatus; |
| 448 | int lSessionId; |
| 449 | |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 450 | // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC, |
| 451 | // but if someone uses binder directly they could bypass that and cause us to crash |
| 452 | if (uint32_t(streamType) >= AUDIO_STREAM_CNT) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 453 | ALOGE("createTrack() invalid stream type %d", streamType); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 454 | lStatus = BAD_VALUE; |
| 455 | goto Exit; |
| 456 | } |
| 457 | |
| 458 | { |
| 459 | Mutex::Autolock _l(mLock); |
| 460 | PlaybackThread *thread = checkPlaybackThread_l(output); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 461 | PlaybackThread *effectThread = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 462 | if (thread == NULL) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 463 | ALOGE("unknown output thread"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 464 | lStatus = BAD_VALUE; |
| 465 | goto Exit; |
| 466 | } |
| 467 | |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 468 | client = registerPid_l(pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 469 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 470 | ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 471 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 472 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 473 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 474 | if (mPlaybackThreads.keyAt(i) != output) { |
| 475 | // prevent same audio session on different output threads |
| 476 | uint32_t sessions = t->hasAudioSession(*sessionId); |
| 477 | if (sessions & PlaybackThread::TRACK_SESSION) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 478 | ALOGE("createTrack() session ID %d already in use", *sessionId); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 479 | lStatus = BAD_VALUE; |
| 480 | goto Exit; |
| 481 | } |
| 482 | // check if an effect with same session ID is waiting for a track to be created |
| 483 | if (sessions & PlaybackThread::EFFECT_SESSION) { |
| 484 | effectThread = t.get(); |
| 485 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 486 | } |
| 487 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 488 | lSessionId = *sessionId; |
| 489 | } else { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 490 | // if no audio session id is provided, create one here |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 491 | lSessionId = nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 492 | if (sessionId != NULL) { |
| 493 | *sessionId = lSessionId; |
| 494 | } |
| 495 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 496 | ALOGV("createTrack() lSessionId: %d", lSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 497 | |
| 498 | track = thread->createTrack_l(client, streamType, sampleRate, format, |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 499 | channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, &lStatus); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 500 | |
| 501 | // move effect chain to this output thread if an effect on same session was waiting |
| 502 | // for a track to be created |
| 503 | if (lStatus == NO_ERROR && effectThread != NULL) { |
| 504 | Mutex::Autolock _dl(thread->mLock); |
| 505 | Mutex::Autolock _sl(effectThread->mLock); |
| 506 | moveEffectChain_l(lSessionId, effectThread, thread, true); |
| 507 | } |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 508 | |
| 509 | // Look for sync events awaiting for a session to be used. |
| 510 | for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) { |
| 511 | if (mPendingSyncEvents[i]->triggerSession() == lSessionId) { |
| 512 | if (thread->isValidSyncEvent(mPendingSyncEvents[i])) { |
| 513 | track->setSyncEvent(mPendingSyncEvents[i]); |
| 514 | mPendingSyncEvents.removeAt(i); |
| 515 | i--; |
| 516 | } |
| 517 | } |
| 518 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 519 | } |
| 520 | if (lStatus == NO_ERROR) { |
| 521 | trackHandle = new TrackHandle(track); |
| 522 | } else { |
| 523 | // remove local strong reference to Client before deleting the Track so that the Client |
| 524 | // destructor is called by the TrackBase destructor with mLock held |
| 525 | client.clear(); |
| 526 | track.clear(); |
| 527 | } |
| 528 | |
| 529 | Exit: |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 530 | if (status != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 531 | *status = lStatus; |
| 532 | } |
| 533 | return trackHandle; |
| 534 | } |
| 535 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 536 | uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 537 | { |
| 538 | Mutex::Autolock _l(mLock); |
| 539 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 540 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 541 | ALOGW("sampleRate() unknown thread %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 542 | return 0; |
| 543 | } |
| 544 | return thread->sampleRate(); |
| 545 | } |
| 546 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 547 | int AudioFlinger::channelCount(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 548 | { |
| 549 | Mutex::Autolock _l(mLock); |
| 550 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 551 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 552 | ALOGW("channelCount() unknown thread %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 553 | return 0; |
| 554 | } |
| 555 | return thread->channelCount(); |
| 556 | } |
| 557 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 558 | audio_format_t AudioFlinger::format(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 559 | { |
| 560 | Mutex::Autolock _l(mLock); |
| 561 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 562 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 563 | ALOGW("format() unknown thread %d", output); |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 564 | return AUDIO_FORMAT_INVALID; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 565 | } |
| 566 | return thread->format(); |
| 567 | } |
| 568 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 569 | size_t AudioFlinger::frameCount(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 570 | { |
| 571 | Mutex::Autolock _l(mLock); |
| 572 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 573 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 574 | ALOGW("frameCount() unknown thread %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 575 | return 0; |
| 576 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 577 | // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers; |
| 578 | // should examine all callers and fix them to handle smaller counts |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 579 | return thread->frameCount(); |
| 580 | } |
| 581 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 582 | uint32_t AudioFlinger::latency(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 583 | { |
| 584 | Mutex::Autolock _l(mLock); |
| 585 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 586 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 587 | ALOGW("latency() unknown thread %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 588 | return 0; |
| 589 | } |
| 590 | return thread->latency(); |
| 591 | } |
| 592 | |
| 593 | status_t AudioFlinger::setMasterVolume(float value) |
| 594 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 595 | status_t ret = initCheck(); |
| 596 | if (ret != NO_ERROR) { |
| 597 | return ret; |
| 598 | } |
| 599 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 600 | // check calling permissions |
| 601 | if (!settingsAllowed()) { |
| 602 | return PERMISSION_DENIED; |
| 603 | } |
| 604 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 605 | float swmv = value; |
| 606 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 607 | Mutex::Autolock _l(mLock); |
| 608 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 609 | // when hw supports master volume, don't scale in sw mixer |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 610 | if (MVS_NONE != mMasterVolumeSupportLvl) { |
| 611 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 612 | AutoMutex lock(mHardwareLock); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 613 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 614 | |
| 615 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
| 616 | if (NULL != dev->set_master_volume) { |
| 617 | dev->set_master_volume(dev, value); |
| 618 | } |
| 619 | mHardwareStatus = AUDIO_HW_IDLE; |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 620 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 621 | |
| 622 | swmv = 1.0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 623 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 624 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 625 | mMasterVolume = value; |
| 626 | mMasterVolumeSW = swmv; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 627 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 628 | mPlaybackThreads.valueAt(i)->setMasterVolume(swmv); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 629 | |
| 630 | return NO_ERROR; |
| 631 | } |
| 632 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 633 | status_t AudioFlinger::setMode(audio_mode_t mode) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 634 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 635 | status_t ret = initCheck(); |
| 636 | if (ret != NO_ERROR) { |
| 637 | return ret; |
| 638 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 639 | |
| 640 | // check calling permissions |
| 641 | if (!settingsAllowed()) { |
| 642 | return PERMISSION_DENIED; |
| 643 | } |
Glenn Kasten | 930f4ca | 2012-01-06 16:47:31 -0800 | [diff] [blame] | 644 | if (uint32_t(mode) >= AUDIO_MODE_CNT) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 645 | ALOGW("Illegal value: setMode(%d)", mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 646 | return BAD_VALUE; |
| 647 | } |
| 648 | |
| 649 | { // scope for the lock |
| 650 | AutoMutex lock(mHardwareLock); |
| 651 | mHardwareStatus = AUDIO_HW_SET_MODE; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 652 | ret = mPrimaryHardwareDev->set_mode(mPrimaryHardwareDev, mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 653 | mHardwareStatus = AUDIO_HW_IDLE; |
| 654 | } |
| 655 | |
| 656 | if (NO_ERROR == ret) { |
| 657 | Mutex::Autolock _l(mLock); |
| 658 | mMode = mode; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 659 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 660 | mPlaybackThreads.valueAt(i)->setMode(mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | return ret; |
| 664 | } |
| 665 | |
| 666 | status_t AudioFlinger::setMicMute(bool state) |
| 667 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 668 | status_t ret = initCheck(); |
| 669 | if (ret != NO_ERROR) { |
| 670 | return ret; |
| 671 | } |
| 672 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 673 | // check calling permissions |
| 674 | if (!settingsAllowed()) { |
| 675 | return PERMISSION_DENIED; |
| 676 | } |
| 677 | |
| 678 | AutoMutex lock(mHardwareLock); |
| 679 | mHardwareStatus = AUDIO_HW_SET_MIC_MUTE; |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 680 | ret = mPrimaryHardwareDev->set_mic_mute(mPrimaryHardwareDev, state); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 681 | mHardwareStatus = AUDIO_HW_IDLE; |
| 682 | return ret; |
| 683 | } |
| 684 | |
| 685 | bool AudioFlinger::getMicMute() const |
| 686 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 687 | status_t ret = initCheck(); |
| 688 | if (ret != NO_ERROR) { |
| 689 | return false; |
| 690 | } |
| 691 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 692 | bool state = AUDIO_MODE_INVALID; |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 693 | AutoMutex lock(mHardwareLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 694 | mHardwareStatus = AUDIO_HW_GET_MIC_MUTE; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 695 | mPrimaryHardwareDev->get_mic_mute(mPrimaryHardwareDev, &state); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 696 | mHardwareStatus = AUDIO_HW_IDLE; |
| 697 | return state; |
| 698 | } |
| 699 | |
| 700 | status_t AudioFlinger::setMasterMute(bool muted) |
| 701 | { |
| 702 | // check calling permissions |
| 703 | if (!settingsAllowed()) { |
| 704 | return PERMISSION_DENIED; |
| 705 | } |
| 706 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 707 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 708 | // 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] | 709 | mMasterMute = muted; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 710 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 711 | mPlaybackThreads.valueAt(i)->setMasterMute(muted); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 712 | |
| 713 | return NO_ERROR; |
| 714 | } |
| 715 | |
| 716 | float AudioFlinger::masterVolume() const |
| 717 | { |
Glenn Kasten | 9806710 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 718 | Mutex::Autolock _l(mLock); |
| 719 | return masterVolume_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 720 | } |
| 721 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 722 | float AudioFlinger::masterVolumeSW() const |
| 723 | { |
| 724 | Mutex::Autolock _l(mLock); |
| 725 | return masterVolumeSW_l(); |
| 726 | } |
| 727 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 728 | bool AudioFlinger::masterMute() const |
| 729 | { |
Glenn Kasten | 9806710 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 730 | Mutex::Autolock _l(mLock); |
| 731 | return masterMute_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 732 | } |
| 733 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 734 | float AudioFlinger::masterVolume_l() const |
| 735 | { |
| 736 | if (MVS_FULL == mMasterVolumeSupportLvl) { |
| 737 | float ret_val; |
| 738 | AutoMutex lock(mHardwareLock); |
| 739 | |
| 740 | mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME; |
Glenn Kasten | 5798d4e | 2012-03-08 12:18:35 -0800 | [diff] [blame] | 741 | ALOG_ASSERT((NULL != mPrimaryHardwareDev) && |
| 742 | (NULL != mPrimaryHardwareDev->get_master_volume), |
| 743 | "can't get master volume"); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 744 | |
| 745 | mPrimaryHardwareDev->get_master_volume(mPrimaryHardwareDev, &ret_val); |
| 746 | mHardwareStatus = AUDIO_HW_IDLE; |
| 747 | return ret_val; |
| 748 | } |
| 749 | |
| 750 | return mMasterVolume; |
| 751 | } |
| 752 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 753 | status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value, |
| 754 | audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 755 | { |
| 756 | // check calling permissions |
| 757 | if (!settingsAllowed()) { |
| 758 | return PERMISSION_DENIED; |
| 759 | } |
| 760 | |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 761 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 762 | ALOGE("setStreamVolume() invalid stream %d", stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 763 | return BAD_VALUE; |
| 764 | } |
| 765 | |
| 766 | AutoMutex lock(mLock); |
| 767 | PlaybackThread *thread = NULL; |
| 768 | if (output) { |
| 769 | thread = checkPlaybackThread_l(output); |
| 770 | if (thread == NULL) { |
| 771 | return BAD_VALUE; |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | mStreamTypes[stream].volume = value; |
| 776 | |
| 777 | if (thread == NULL) { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 778 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 779 | mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 780 | } |
| 781 | } else { |
| 782 | thread->setStreamVolume(stream, value); |
| 783 | } |
| 784 | |
| 785 | return NO_ERROR; |
| 786 | } |
| 787 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 788 | status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 789 | { |
| 790 | // check calling permissions |
| 791 | if (!settingsAllowed()) { |
| 792 | return PERMISSION_DENIED; |
| 793 | } |
| 794 | |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 795 | if (uint32_t(stream) >= AUDIO_STREAM_CNT || |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 796 | uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 797 | ALOGE("setStreamMute() invalid stream %d", stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 798 | return BAD_VALUE; |
| 799 | } |
| 800 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 801 | AutoMutex lock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 802 | mStreamTypes[stream].mute = muted; |
| 803 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 804 | mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 805 | |
| 806 | return NO_ERROR; |
| 807 | } |
| 808 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 809 | 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] | 810 | { |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 811 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 812 | return 0.0f; |
| 813 | } |
| 814 | |
| 815 | AutoMutex lock(mLock); |
| 816 | float volume; |
| 817 | if (output) { |
| 818 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 819 | if (thread == NULL) { |
| 820 | return 0.0f; |
| 821 | } |
| 822 | volume = thread->streamVolume(stream); |
| 823 | } else { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 824 | volume = streamVolume_l(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | return volume; |
| 828 | } |
| 829 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 830 | bool AudioFlinger::streamMute(audio_stream_type_t stream) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 831 | { |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 832 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 833 | return true; |
| 834 | } |
| 835 | |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 836 | AutoMutex lock(mLock); |
| 837 | return streamMute_l(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 838 | } |
| 839 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 840 | status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 841 | { |
Glenn Kasten | 23d82a9 | 2012-02-03 11:10:00 -0800 | [diff] [blame] | 842 | ALOGV("setParameters(): io %d, keyvalue %s, tid %d, calling pid %d", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 843 | ioHandle, keyValuePairs.string(), gettid(), IPCThreadState::self()->getCallingPid()); |
| 844 | // check calling permissions |
| 845 | if (!settingsAllowed()) { |
| 846 | return PERMISSION_DENIED; |
| 847 | } |
| 848 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 849 | // ioHandle == 0 means the parameters are global to the audio hardware interface |
| 850 | if (ioHandle == 0) { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 851 | Mutex::Autolock _l(mLock); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 852 | status_t final_result = NO_ERROR; |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 853 | { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 854 | AutoMutex lock(mHardwareLock); |
| 855 | mHardwareStatus = AUDIO_HW_SET_PARAMETER; |
| 856 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 857 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
| 858 | status_t result = dev->set_parameters(dev, keyValuePairs.string()); |
| 859 | final_result = result ?: final_result; |
| 860 | } |
| 861 | mHardwareStatus = AUDIO_HW_IDLE; |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 862 | } |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 863 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 864 | AudioParameter param = AudioParameter(keyValuePairs); |
| 865 | String8 value; |
| 866 | if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) { |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 867 | bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF); |
| 868 | if (mBtNrecIsOff != btNrecIsOff) { |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 869 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 870 | sp<RecordThread> thread = mRecordThreads.valueAt(i); |
| 871 | RecordThread::RecordTrack *track = thread->track(); |
| 872 | if (track != NULL) { |
| 873 | audio_devices_t device = (audio_devices_t)( |
| 874 | thread->device() & AUDIO_DEVICE_IN_ALL); |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 875 | bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff; |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 876 | thread->setEffectSuspended(FX_IID_AEC, |
| 877 | suspend, |
| 878 | track->sessionId()); |
| 879 | thread->setEffectSuspended(FX_IID_NS, |
| 880 | suspend, |
| 881 | track->sessionId()); |
| 882 | } |
| 883 | } |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 884 | mBtNrecIsOff = btNrecIsOff; |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 885 | } |
| 886 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 887 | return final_result; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | // hold a strong ref on thread in case closeOutput() or closeInput() is called |
| 891 | // and the thread is exited once the lock is released |
| 892 | sp<ThreadBase> thread; |
| 893 | { |
| 894 | Mutex::Autolock _l(mLock); |
| 895 | thread = checkPlaybackThread_l(ioHandle); |
| 896 | if (thread == NULL) { |
| 897 | thread = checkRecordThread_l(ioHandle); |
Glenn Kasten | 7fc9a6f | 2012-01-10 10:46:34 -0800 | [diff] [blame] | 898 | } else if (thread == primaryPlaybackThread_l()) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 899 | // indicate output device change to all input threads for pre processing |
| 900 | AudioParameter param = AudioParameter(keyValuePairs); |
| 901 | int value; |
Eric Laurent | 89d94e7 | 2012-03-16 20:37:59 -0700 | [diff] [blame] | 902 | if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) && |
| 903 | (value != 0)) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 904 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 905 | mRecordThreads.valueAt(i)->setParameters(keyValuePairs); |
| 906 | } |
| 907 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 908 | } |
| 909 | } |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 910 | if (thread != 0) { |
| 911 | return thread->setParameters(keyValuePairs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 912 | } |
| 913 | return BAD_VALUE; |
| 914 | } |
| 915 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 916 | String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 917 | { |
Glenn Kasten | 23d82a9 | 2012-02-03 11:10:00 -0800 | [diff] [blame] | 918 | // ALOGV("getParameters() io %d, keys %s, tid %d, calling pid %d", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 919 | // ioHandle, keys.string(), gettid(), IPCThreadState::self()->getCallingPid()); |
| 920 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 921 | Mutex::Autolock _l(mLock); |
| 922 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 923 | if (ioHandle == 0) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 924 | String8 out_s8; |
| 925 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 926 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 927 | char *s; |
| 928 | { |
| 929 | AutoMutex lock(mHardwareLock); |
| 930 | mHardwareStatus = AUDIO_HW_GET_PARAMETER; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 931 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 932 | s = dev->get_parameters(dev, keys.string()); |
| 933 | mHardwareStatus = AUDIO_HW_IDLE; |
| 934 | } |
John Grossman | ef7740b | 2012-02-09 11:28:36 -0800 | [diff] [blame] | 935 | out_s8 += String8(s ? s : ""); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 936 | free(s); |
| 937 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 938 | return out_s8; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 941 | PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle); |
| 942 | if (playbackThread != NULL) { |
| 943 | return playbackThread->getParameters(keys); |
| 944 | } |
| 945 | RecordThread *recordThread = checkRecordThread_l(ioHandle); |
| 946 | if (recordThread != NULL) { |
| 947 | return recordThread->getParameters(keys); |
| 948 | } |
| 949 | return String8(""); |
| 950 | } |
| 951 | |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 952 | 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] | 953 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 954 | status_t ret = initCheck(); |
| 955 | if (ret != NO_ERROR) { |
| 956 | return 0; |
| 957 | } |
| 958 | |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 959 | AutoMutex lock(mHardwareLock); |
| 960 | mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 961 | struct audio_config config = { |
| 962 | sample_rate: sampleRate, |
| 963 | channel_mask: audio_channel_in_mask_from_count(channelCount), |
| 964 | format: format, |
| 965 | }; |
| 966 | size_t size = mPrimaryHardwareDev->get_input_buffer_size(mPrimaryHardwareDev, &config); |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 967 | mHardwareStatus = AUDIO_HW_IDLE; |
| 968 | return size; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 971 | unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 972 | { |
| 973 | if (ioHandle == 0) { |
| 974 | return 0; |
| 975 | } |
| 976 | |
| 977 | Mutex::Autolock _l(mLock); |
| 978 | |
| 979 | RecordThread *recordThread = checkRecordThread_l(ioHandle); |
| 980 | if (recordThread != NULL) { |
| 981 | return recordThread->getInputFramesLost(); |
| 982 | } |
| 983 | return 0; |
| 984 | } |
| 985 | |
| 986 | status_t AudioFlinger::setVoiceVolume(float value) |
| 987 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 988 | status_t ret = initCheck(); |
| 989 | if (ret != NO_ERROR) { |
| 990 | return ret; |
| 991 | } |
| 992 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 993 | // check calling permissions |
| 994 | if (!settingsAllowed()) { |
| 995 | return PERMISSION_DENIED; |
| 996 | } |
| 997 | |
| 998 | AutoMutex lock(mHardwareLock); |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 999 | mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME; |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1000 | ret = mPrimaryHardwareDev->set_voice_volume(mPrimaryHardwareDev, value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1001 | mHardwareStatus = AUDIO_HW_IDLE; |
| 1002 | |
| 1003 | return ret; |
| 1004 | } |
| 1005 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1006 | status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, |
| 1007 | audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1008 | { |
| 1009 | status_t status; |
| 1010 | |
| 1011 | Mutex::Autolock _l(mLock); |
| 1012 | |
| 1013 | PlaybackThread *playbackThread = checkPlaybackThread_l(output); |
| 1014 | if (playbackThread != NULL) { |
| 1015 | return playbackThread->getRenderPosition(halFrames, dspFrames); |
| 1016 | } |
| 1017 | |
| 1018 | return BAD_VALUE; |
| 1019 | } |
| 1020 | |
| 1021 | void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client) |
| 1022 | { |
| 1023 | |
| 1024 | Mutex::Autolock _l(mLock); |
| 1025 | |
Glenn Kasten | bb00192 | 2012-02-03 11:10:26 -0800 | [diff] [blame] | 1026 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1027 | if (mNotificationClients.indexOfKey(pid) < 0) { |
| 1028 | sp<NotificationClient> notificationClient = new NotificationClient(this, |
| 1029 | client, |
| 1030 | pid); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1031 | ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1032 | |
| 1033 | mNotificationClients.add(pid, notificationClient); |
| 1034 | |
| 1035 | sp<IBinder> binder = client->asBinder(); |
| 1036 | binder->linkToDeath(notificationClient); |
| 1037 | |
| 1038 | // the config change is always sent from playback or record threads to avoid deadlock |
| 1039 | // with AudioSystem::gLock |
| 1040 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 1041 | mPlaybackThreads.valueAt(i)->sendConfigEvent(AudioSystem::OUTPUT_OPENED); |
| 1042 | } |
| 1043 | |
| 1044 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 1045 | mRecordThreads.valueAt(i)->sendConfigEvent(AudioSystem::INPUT_OPENED); |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | void AudioFlinger::removeNotificationClient(pid_t pid) |
| 1051 | { |
| 1052 | Mutex::Autolock _l(mLock); |
| 1053 | |
Glenn Kasten | a3b0925 | 2012-01-20 09:19:01 -0800 | [diff] [blame] | 1054 | mNotificationClients.removeItem(pid); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1055 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1056 | ALOGV("%d died, releasing its sessions", pid); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1057 | size_t num = mAudioSessionRefs.size(); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1058 | bool removed = false; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1059 | for (size_t i = 0; i< num; ) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1060 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 1061 | ALOGV(" pid %d @ %d", ref->mPid, i); |
| 1062 | if (ref->mPid == pid) { |
| 1063 | ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1064 | mAudioSessionRefs.removeAt(i); |
| 1065 | delete ref; |
| 1066 | removed = true; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1067 | num--; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1068 | } else { |
| 1069 | i++; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1070 | } |
| 1071 | } |
| 1072 | if (removed) { |
| 1073 | purgeStaleEffects_l(); |
| 1074 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | // audioConfigChanged_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | b81cc8c | 2012-03-01 09:14:51 -0800 | [diff] [blame] | 1078 | 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] | 1079 | { |
| 1080 | size_t size = mNotificationClients.size(); |
| 1081 | for (size_t i = 0; i < size; i++) { |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 1082 | mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle, |
| 1083 | param2); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | // removeClient_l() must be called with AudioFlinger::mLock held |
| 1088 | void AudioFlinger::removeClient_l(pid_t pid) |
| 1089 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1090 | 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] | 1091 | mClients.removeItem(pid); |
| 1092 | } |
| 1093 | |
| 1094 | |
| 1095 | // ---------------------------------------------------------------------------- |
| 1096 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1097 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
| 1098 | uint32_t device, type_t type) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1099 | : Thread(false), |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 1100 | mType(type), |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1101 | mAudioFlinger(audioFlinger), mSampleRate(0), mFrameCount(0), mNormalFrameCount(0), |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 1102 | // mChannelMask |
| 1103 | mChannelCount(0), |
| 1104 | mFrameSize(1), mFormat(AUDIO_FORMAT_INVALID), |
| 1105 | mParamStatus(NO_ERROR), |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 1106 | mStandby(false), mId(id), |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 1107 | mDevice(device), |
| 1108 | mDeathRecipient(new PMDeathRecipient(this)) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1109 | { |
| 1110 | } |
| 1111 | |
| 1112 | AudioFlinger::ThreadBase::~ThreadBase() |
| 1113 | { |
| 1114 | mParamCond.broadcast(); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1115 | // do not lock the mutex in destructor |
| 1116 | releaseWakeLock_l(); |
Eric Laurent | 9d18ec5 | 2011-09-27 12:07:15 -0700 | [diff] [blame] | 1117 | if (mPowerManager != 0) { |
| 1118 | sp<IBinder> binder = mPowerManager->asBinder(); |
| 1119 | binder->unlinkToDeath(mDeathRecipient); |
| 1120 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | void AudioFlinger::ThreadBase::exit() |
| 1124 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1125 | ALOGV("ThreadBase::exit"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1126 | { |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 1127 | // This lock prevents the following race in thread (uniprocessor for illustration): |
| 1128 | // if (!exitPending()) { |
| 1129 | // // context switch from here to exit() |
| 1130 | // // exit() calls requestExit(), what exitPending() observes |
| 1131 | // // exit() calls signal(), which is dropped since no waiters |
| 1132 | // // context switch back from exit() to here |
| 1133 | // mWaitWorkCV.wait(...); |
| 1134 | // // now thread is hung |
| 1135 | // } |
Glenn Kasten | a7d8d6f | 2012-01-05 15:41:56 -0800 | [diff] [blame] | 1136 | AutoMutex lock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1137 | requestExit(); |
| 1138 | mWaitWorkCV.signal(); |
| 1139 | } |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 1140 | // When Thread::requestExitAndWait is made virtual and this method is renamed to |
| 1141 | // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1142 | requestExitAndWait(); |
| 1143 | } |
| 1144 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1145 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) |
| 1146 | { |
| 1147 | status_t status; |
| 1148 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1149 | ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1150 | Mutex::Autolock _l(mLock); |
| 1151 | |
| 1152 | mNewParameters.add(keyValuePairs); |
| 1153 | mWaitWorkCV.signal(); |
| 1154 | // wait condition with timeout in case the thread loop has exited |
| 1155 | // before the request could be processed |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 1156 | if (mParamCond.waitRelative(mLock, kSetParametersTimeoutNs) == NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1157 | status = mParamStatus; |
| 1158 | mWaitWorkCV.signal(); |
| 1159 | } else { |
| 1160 | status = TIMED_OUT; |
| 1161 | } |
| 1162 | return status; |
| 1163 | } |
| 1164 | |
| 1165 | void AudioFlinger::ThreadBase::sendConfigEvent(int event, int param) |
| 1166 | { |
| 1167 | Mutex::Autolock _l(mLock); |
| 1168 | sendConfigEvent_l(event, param); |
| 1169 | } |
| 1170 | |
| 1171 | // sendConfigEvent_l() must be called with ThreadBase::mLock held |
| 1172 | void AudioFlinger::ThreadBase::sendConfigEvent_l(int event, int param) |
| 1173 | { |
Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1174 | ConfigEvent configEvent; |
| 1175 | configEvent.mEvent = event; |
| 1176 | configEvent.mParam = param; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1177 | mConfigEvents.add(configEvent); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1178 | 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] | 1179 | mWaitWorkCV.signal(); |
| 1180 | } |
| 1181 | |
| 1182 | void AudioFlinger::ThreadBase::processConfigEvents() |
| 1183 | { |
| 1184 | mLock.lock(); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1185 | while (!mConfigEvents.isEmpty()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1186 | ALOGV("processConfigEvents() remaining events %d", mConfigEvents.size()); |
Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1187 | ConfigEvent configEvent = mConfigEvents[0]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1188 | mConfigEvents.removeAt(0); |
| 1189 | // release mLock before locking AudioFlinger mLock: lock order is always |
| 1190 | // AudioFlinger then ThreadBase to avoid cross deadlock |
| 1191 | mLock.unlock(); |
| 1192 | mAudioFlinger->mLock.lock(); |
Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1193 | audioConfigChanged_l(configEvent.mEvent, configEvent.mParam); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1194 | mAudioFlinger->mLock.unlock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1195 | mLock.lock(); |
| 1196 | } |
| 1197 | mLock.unlock(); |
| 1198 | } |
| 1199 | |
| 1200 | status_t AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args) |
| 1201 | { |
| 1202 | const size_t SIZE = 256; |
| 1203 | char buffer[SIZE]; |
| 1204 | String8 result; |
| 1205 | |
| 1206 | bool locked = tryLock(mLock); |
| 1207 | if (!locked) { |
| 1208 | snprintf(buffer, SIZE, "thread %p maybe dead locked\n", this); |
| 1209 | write(fd, buffer, strlen(buffer)); |
| 1210 | } |
| 1211 | |
Eric Laurent | 612bbb5 | 2012-03-14 15:03:26 -0700 | [diff] [blame] | 1212 | snprintf(buffer, SIZE, "io handle: %d\n", mId); |
| 1213 | result.append(buffer); |
| 1214 | snprintf(buffer, SIZE, "TID: %d\n", getTid()); |
| 1215 | result.append(buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1216 | snprintf(buffer, SIZE, "standby: %d\n", mStandby); |
| 1217 | result.append(buffer); |
| 1218 | snprintf(buffer, SIZE, "Sample rate: %d\n", mSampleRate); |
| 1219 | result.append(buffer); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1220 | snprintf(buffer, SIZE, "HAL frame count: %d\n", mFrameCount); |
| 1221 | result.append(buffer); |
| 1222 | snprintf(buffer, SIZE, "Normal frame count: %d\n", mNormalFrameCount); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1223 | result.append(buffer); |
| 1224 | snprintf(buffer, SIZE, "Channel Count: %d\n", mChannelCount); |
| 1225 | result.append(buffer); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1226 | snprintf(buffer, SIZE, "Channel Mask: 0x%08x\n", mChannelMask); |
| 1227 | result.append(buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1228 | snprintf(buffer, SIZE, "Format: %d\n", mFormat); |
| 1229 | result.append(buffer); |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 1230 | snprintf(buffer, SIZE, "Frame size: %u\n", mFrameSize); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1231 | result.append(buffer); |
| 1232 | |
| 1233 | snprintf(buffer, SIZE, "\nPending setParameters commands: \n"); |
| 1234 | result.append(buffer); |
| 1235 | result.append(" Index Command"); |
| 1236 | for (size_t i = 0; i < mNewParameters.size(); ++i) { |
| 1237 | snprintf(buffer, SIZE, "\n %02d ", i); |
| 1238 | result.append(buffer); |
| 1239 | result.append(mNewParameters[i]); |
| 1240 | } |
| 1241 | |
| 1242 | snprintf(buffer, SIZE, "\n\nPending config events: \n"); |
| 1243 | result.append(buffer); |
| 1244 | snprintf(buffer, SIZE, " Index event param\n"); |
| 1245 | result.append(buffer); |
| 1246 | for (size_t i = 0; i < mConfigEvents.size(); i++) { |
Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1247 | 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] | 1248 | result.append(buffer); |
| 1249 | } |
| 1250 | result.append("\n"); |
| 1251 | |
| 1252 | write(fd, result.string(), result.size()); |
| 1253 | |
| 1254 | if (locked) { |
| 1255 | mLock.unlock(); |
| 1256 | } |
| 1257 | return NO_ERROR; |
| 1258 | } |
| 1259 | |
Eric Laurent | 1d2bff0 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 1260 | status_t AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) |
| 1261 | { |
| 1262 | const size_t SIZE = 256; |
| 1263 | char buffer[SIZE]; |
| 1264 | String8 result; |
| 1265 | |
| 1266 | snprintf(buffer, SIZE, "\n- %d Effect Chains:\n", mEffectChains.size()); |
| 1267 | write(fd, buffer, strlen(buffer)); |
| 1268 | |
| 1269 | for (size_t i = 0; i < mEffectChains.size(); ++i) { |
| 1270 | sp<EffectChain> chain = mEffectChains[i]; |
| 1271 | if (chain != 0) { |
| 1272 | chain->dump(fd, args); |
| 1273 | } |
| 1274 | } |
| 1275 | return NO_ERROR; |
| 1276 | } |
| 1277 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1278 | void AudioFlinger::ThreadBase::acquireWakeLock() |
| 1279 | { |
| 1280 | Mutex::Autolock _l(mLock); |
| 1281 | acquireWakeLock_l(); |
| 1282 | } |
| 1283 | |
| 1284 | void AudioFlinger::ThreadBase::acquireWakeLock_l() |
| 1285 | { |
| 1286 | if (mPowerManager == 0) { |
| 1287 | // use checkService() to avoid blocking if power service is not up yet |
| 1288 | sp<IBinder> binder = |
| 1289 | defaultServiceManager()->checkService(String16("power")); |
| 1290 | if (binder == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1291 | ALOGW("Thread %s cannot connect to the power manager service", mName); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1292 | } else { |
| 1293 | mPowerManager = interface_cast<IPowerManager>(binder); |
| 1294 | binder->linkToDeath(mDeathRecipient); |
| 1295 | } |
| 1296 | } |
| 1297 | if (mPowerManager != 0) { |
| 1298 | sp<IBinder> binder = new BBinder(); |
| 1299 | status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, |
| 1300 | binder, |
| 1301 | String16(mName)); |
| 1302 | if (status == NO_ERROR) { |
| 1303 | mWakeLockToken = binder; |
| 1304 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1305 | ALOGV("acquireWakeLock_l() %s status %d", mName, status); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1306 | } |
| 1307 | } |
| 1308 | |
| 1309 | void AudioFlinger::ThreadBase::releaseWakeLock() |
| 1310 | { |
| 1311 | Mutex::Autolock _l(mLock); |
Eric Laurent | 6dbe883 | 2011-07-28 13:59:02 -0700 | [diff] [blame] | 1312 | releaseWakeLock_l(); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | void AudioFlinger::ThreadBase::releaseWakeLock_l() |
| 1316 | { |
| 1317 | if (mWakeLockToken != 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1318 | ALOGV("releaseWakeLock_l() %s", mName); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1319 | if (mPowerManager != 0) { |
| 1320 | mPowerManager->releaseWakeLock(mWakeLockToken, 0); |
| 1321 | } |
| 1322 | mWakeLockToken.clear(); |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | void AudioFlinger::ThreadBase::clearPowerManager() |
| 1327 | { |
| 1328 | Mutex::Autolock _l(mLock); |
| 1329 | releaseWakeLock_l(); |
| 1330 | mPowerManager.clear(); |
| 1331 | } |
| 1332 | |
| 1333 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who) |
| 1334 | { |
| 1335 | sp<ThreadBase> thread = mThread.promote(); |
| 1336 | if (thread != 0) { |
| 1337 | thread->clearPowerManager(); |
| 1338 | } |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1339 | ALOGW("power manager service died !!!"); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1340 | } |
Eric Laurent | 1d2bff0 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 1341 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1342 | void AudioFlinger::ThreadBase::setEffectSuspended( |
| 1343 | const effect_uuid_t *type, bool suspend, int sessionId) |
| 1344 | { |
| 1345 | Mutex::Autolock _l(mLock); |
| 1346 | setEffectSuspended_l(type, suspend, sessionId); |
| 1347 | } |
| 1348 | |
| 1349 | void AudioFlinger::ThreadBase::setEffectSuspended_l( |
| 1350 | const effect_uuid_t *type, bool suspend, int sessionId) |
| 1351 | { |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 1352 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1353 | if (chain != 0) { |
| 1354 | if (type != NULL) { |
| 1355 | chain->setEffectSuspended_l(type, suspend); |
| 1356 | } else { |
| 1357 | chain->setEffectSuspendedAll_l(suspend); |
| 1358 | } |
| 1359 | } |
| 1360 | |
| 1361 | updateSuspendedSessions_l(type, suspend, sessionId); |
| 1362 | } |
| 1363 | |
| 1364 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) |
| 1365 | { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1366 | ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1367 | if (index < 0) { |
| 1368 | return; |
| 1369 | } |
| 1370 | |
| 1371 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects = |
| 1372 | mSuspendedSessions.editValueAt(index); |
| 1373 | |
| 1374 | for (size_t i = 0; i < sessionEffects.size(); i++) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1375 | sp<SuspendedSessionDesc> desc = sessionEffects.valueAt(i); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1376 | for (int j = 0; j < desc->mRefCount; j++) { |
| 1377 | if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { |
| 1378 | chain->setEffectSuspendedAll_l(true); |
| 1379 | } else { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1380 | ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1381 | desc->mType.timeLow); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1382 | chain->setEffectSuspended_l(&desc->mType, true); |
| 1383 | } |
| 1384 | } |
| 1385 | } |
| 1386 | } |
| 1387 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1388 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, |
| 1389 | bool suspend, |
| 1390 | int sessionId) |
| 1391 | { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1392 | ssize_t index = mSuspendedSessions.indexOfKey(sessionId); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1393 | |
| 1394 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; |
| 1395 | |
| 1396 | if (suspend) { |
| 1397 | if (index >= 0) { |
| 1398 | sessionEffects = mSuspendedSessions.editValueAt(index); |
| 1399 | } else { |
| 1400 | mSuspendedSessions.add(sessionId, sessionEffects); |
| 1401 | } |
| 1402 | } else { |
| 1403 | if (index < 0) { |
| 1404 | return; |
| 1405 | } |
| 1406 | sessionEffects = mSuspendedSessions.editValueAt(index); |
| 1407 | } |
| 1408 | |
| 1409 | |
| 1410 | int key = EffectChain::kKeyForSuspendAll; |
| 1411 | if (type != NULL) { |
| 1412 | key = type->timeLow; |
| 1413 | } |
| 1414 | index = sessionEffects.indexOfKey(key); |
| 1415 | |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1416 | sp<SuspendedSessionDesc> desc; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1417 | if (suspend) { |
| 1418 | if (index >= 0) { |
| 1419 | desc = sessionEffects.valueAt(index); |
| 1420 | } else { |
| 1421 | desc = new SuspendedSessionDesc(); |
| 1422 | if (type != NULL) { |
| 1423 | memcpy(&desc->mType, type, sizeof(effect_uuid_t)); |
| 1424 | } |
| 1425 | sessionEffects.add(key, desc); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1426 | ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1427 | } |
| 1428 | desc->mRefCount++; |
| 1429 | } else { |
| 1430 | if (index < 0) { |
| 1431 | return; |
| 1432 | } |
| 1433 | desc = sessionEffects.valueAt(index); |
| 1434 | if (--desc->mRefCount == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1435 | ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1436 | sessionEffects.removeItemsAt(index); |
| 1437 | if (sessionEffects.isEmpty()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1438 | ALOGV("updateSuspendedSessions_l() restore removing session %d", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1439 | sessionId); |
| 1440 | mSuspendedSessions.removeItem(sessionId); |
| 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | if (!sessionEffects.isEmpty()) { |
| 1445 | mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 1450 | bool enabled, |
| 1451 | int sessionId) |
| 1452 | { |
| 1453 | Mutex::Autolock _l(mLock); |
Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 1454 | checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); |
| 1455 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1456 | |
Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 1457 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, |
| 1458 | bool enabled, |
| 1459 | int sessionId) |
| 1460 | { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 1461 | if (mType != RECORD) { |
| 1462 | // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on |
| 1463 | // another session. This gives the priority to well behaved effect control panels |
| 1464 | // and applications not using global effects. |
| 1465 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX) { |
| 1466 | setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); |
| 1467 | } |
| 1468 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1469 | |
| 1470 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1471 | if (chain != 0) { |
| 1472 | chain->checkSuspendOnEffectEnabled(effect, enabled); |
| 1473 | } |
| 1474 | } |
| 1475 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1476 | // ---------------------------------------------------------------------------- |
| 1477 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1478 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, |
| 1479 | AudioStreamOut* output, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1480 | audio_io_handle_t id, |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 1481 | uint32_t device, |
| 1482 | type_t type) |
| 1483 | : ThreadBase(audioFlinger, id, device, type), |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 1484 | mMixBuffer(NULL), mSuspended(0), mBytesWritten(0), |
| 1485 | // Assumes constructor is called by AudioFlinger with it's mLock held, |
| 1486 | // but it would be safer to explicitly pass initial masterMute as parameter |
| 1487 | mMasterMute(audioFlinger->masterMute_l()), |
| 1488 | // mStreamTypes[] initialized in constructor body |
| 1489 | mOutput(output), |
| 1490 | // Assumes constructor is called by AudioFlinger with it's mLock held, |
| 1491 | // but it would be safer to explicitly pass initial masterVolume as parameter |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1492 | mMasterVolume(audioFlinger->masterVolumeSW_l()), |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 1493 | mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), |
Glenn Kasten | aa4397f | 2012-03-12 18:13:59 -0700 | [diff] [blame] | 1494 | mMixerStatus(MIXER_IDLE), |
Glenn Kasten | 8102804 | 2012-04-30 18:15:12 -0700 | [diff] [blame] | 1495 | mMixerStatusIgnoringFastTracks(MIXER_IDLE), |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1496 | standbyDelay(AudioFlinger::mStandbyTimeInNsecs), |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 1497 | // index 0 is reserved for normal mixer's submix |
| 1498 | mFastTrackAvailMask(((1 << FastMixerState::kMaxFastTracks) - 1) & ~1) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1499 | { |
Glenn Kasten | 480b468 | 2012-02-28 12:30:08 -0800 | [diff] [blame] | 1500 | snprintf(mName, kNameLength, "AudioOut_%X", id); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1501 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1502 | readOutputParameters(); |
| 1503 | |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 1504 | // mStreamTypes[AUDIO_STREAM_CNT] is initialized by stream_type_t default constructor |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1505 | // There is no AUDIO_STREAM_MIN, and ++ operator does not compile |
| 1506 | for (audio_stream_type_t stream = (audio_stream_type_t) 0; stream < AUDIO_STREAM_CNT; |
| 1507 | stream = (audio_stream_type_t) (stream + 1)) { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1508 | mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream); |
| 1509 | mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1510 | } |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1511 | // mStreamTypes[AUDIO_STREAM_CNT] exists but isn't explicitly initialized here, |
| 1512 | // because mAudioFlinger doesn't have one to copy from |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | AudioFlinger::PlaybackThread::~PlaybackThread() |
| 1516 | { |
| 1517 | delete [] mMixBuffer; |
| 1518 | } |
| 1519 | |
| 1520 | status_t AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) |
| 1521 | { |
| 1522 | dumpInternals(fd, args); |
| 1523 | dumpTracks(fd, args); |
| 1524 | dumpEffectChains(fd, args); |
| 1525 | return NO_ERROR; |
| 1526 | } |
| 1527 | |
| 1528 | status_t AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args) |
| 1529 | { |
| 1530 | const size_t SIZE = 256; |
| 1531 | char buffer[SIZE]; |
| 1532 | String8 result; |
| 1533 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1534 | result.appendFormat("Output thread %p stream volumes in dB:\n ", this); |
| 1535 | for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { |
| 1536 | const stream_type_t *st = &mStreamTypes[i]; |
| 1537 | if (i > 0) { |
| 1538 | result.appendFormat(", "); |
| 1539 | } |
| 1540 | result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); |
| 1541 | if (st->mute) { |
| 1542 | result.append("M"); |
| 1543 | } |
| 1544 | } |
| 1545 | result.append("\n"); |
| 1546 | write(fd, result.string(), result.length()); |
| 1547 | result.clear(); |
| 1548 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1549 | snprintf(buffer, SIZE, "Output thread %p tracks\n", this); |
| 1550 | result.append(buffer); |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 1551 | Track::appendDumpHeader(result); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1552 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1553 | sp<Track> track = mTracks[i]; |
| 1554 | if (track != 0) { |
| 1555 | track->dump(buffer, SIZE); |
| 1556 | result.append(buffer); |
| 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | snprintf(buffer, SIZE, "Output thread %p active tracks\n", this); |
| 1561 | result.append(buffer); |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 1562 | Track::appendDumpHeader(result); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1563 | for (size_t i = 0; i < mActiveTracks.size(); ++i) { |
Glenn Kasten | 77c1119 | 2012-01-25 14:27:41 -0800 | [diff] [blame] | 1564 | sp<Track> track = mActiveTracks[i].promote(); |
| 1565 | if (track != 0) { |
| 1566 | track->dump(buffer, SIZE); |
| 1567 | result.append(buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1568 | } |
| 1569 | } |
| 1570 | write(fd, result.string(), result.size()); |
| 1571 | return NO_ERROR; |
| 1572 | } |
| 1573 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1574 | status_t AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) |
| 1575 | { |
| 1576 | const size_t SIZE = 256; |
| 1577 | char buffer[SIZE]; |
| 1578 | String8 result; |
| 1579 | |
| 1580 | snprintf(buffer, SIZE, "\nOutput thread %p internals\n", this); |
| 1581 | result.append(buffer); |
| 1582 | snprintf(buffer, SIZE, "last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime)); |
| 1583 | result.append(buffer); |
| 1584 | snprintf(buffer, SIZE, "total writes: %d\n", mNumWrites); |
| 1585 | result.append(buffer); |
| 1586 | snprintf(buffer, SIZE, "delayed writes: %d\n", mNumDelayedWrites); |
| 1587 | result.append(buffer); |
| 1588 | snprintf(buffer, SIZE, "blocked in write: %d\n", mInWrite); |
| 1589 | result.append(buffer); |
| 1590 | snprintf(buffer, SIZE, "suspend count: %d\n", mSuspended); |
| 1591 | result.append(buffer); |
| 1592 | snprintf(buffer, SIZE, "mix buffer : %p\n", mMixBuffer); |
| 1593 | result.append(buffer); |
| 1594 | write(fd, result.string(), result.size()); |
| 1595 | |
| 1596 | dumpBase(fd, args); |
| 1597 | |
| 1598 | return NO_ERROR; |
| 1599 | } |
| 1600 | |
| 1601 | // Thread virtuals |
| 1602 | status_t AudioFlinger::PlaybackThread::readyToRun() |
| 1603 | { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1604 | status_t status = initCheck(); |
| 1605 | if (status == NO_ERROR) { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1606 | ALOGI("AudioFlinger's thread %p ready to run", this); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1607 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1608 | ALOGE("No working audio driver found."); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1609 | } |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1610 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | void AudioFlinger::PlaybackThread::onFirstRef() |
| 1614 | { |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1615 | run(mName, ANDROID_PRIORITY_URGENT_AUDIO); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 1619 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1620 | const sp<AudioFlinger::Client>& client, |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1621 | audio_stream_type_t streamType, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1622 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 1623 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1624 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1625 | int frameCount, |
| 1626 | const sp<IMemory>& sharedBuffer, |
| 1627 | int sessionId, |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1628 | IAudioFlinger::track_flags_t flags, |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1629 | pid_t tid, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1630 | status_t *status) |
| 1631 | { |
| 1632 | sp<Track> track; |
| 1633 | status_t lStatus; |
| 1634 | |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1635 | bool isTimed = (flags & IAudioFlinger::TRACK_TIMED) != 0; |
| 1636 | |
| 1637 | // client expresses a preference for FAST, but we get the final say |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1638 | if (flags & IAudioFlinger::TRACK_FAST) { |
| 1639 | if ( |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1640 | // not timed |
| 1641 | (!isTimed) && |
| 1642 | // either of these use cases: |
| 1643 | ( |
| 1644 | // use case 1: shared buffer with any frame count |
| 1645 | ( |
| 1646 | (sharedBuffer != 0) |
| 1647 | ) || |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1648 | // 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] | 1649 | ( |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1650 | (tid != -1) && |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1651 | ((frameCount == 0) || |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 1652 | (frameCount >= (int) (mFrameCount * 2))) // * 2 is due to SRC jitter, see below |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1653 | ) |
| 1654 | ) && |
| 1655 | // PCM data |
| 1656 | audio_is_linear_pcm(format) && |
| 1657 | // mono or stereo |
| 1658 | ( (channelMask == AUDIO_CHANNEL_OUT_MONO) || |
| 1659 | (channelMask == AUDIO_CHANNEL_OUT_STEREO) ) && |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1660 | #ifndef FAST_TRACKS_AT_NON_NATIVE_SAMPLE_RATE |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1661 | // hardware sample rate |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1662 | (sampleRate == mSampleRate) && |
| 1663 | #endif |
| 1664 | // normal mixer has an associated fast mixer |
| 1665 | hasFastMixer() && |
| 1666 | // there are sufficient fast track slots available |
| 1667 | (mFastTrackAvailMask != 0) |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1668 | // FIXME test that MixerThread for this fast track has a capable output HAL |
| 1669 | // FIXME add a permission test also? |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1670 | ) { |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1671 | // if frameCount not specified, then it defaults to fast mixer (HAL) frame count |
| 1672 | if (frameCount == 0) { |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 1673 | 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] | 1674 | } |
Glenn Kasten | 31dfd1d | 2012-05-01 11:07:08 -0700 | [diff] [blame^] | 1675 | ALOGV("AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%d mFrameCount=%d", |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 1676 | frameCount, mFrameCount); |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1677 | } else { |
| 1678 | ALOGW("AUDIO_OUTPUT_FLAG_FAST denied: isTimed=%d sharedBuffer=%p frameCount=%d " |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1679 | "mFrameCount=%d format=%d isLinear=%d channelMask=%d sampleRate=%d mSampleRate=%d " |
| 1680 | "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", |
| 1681 | isTimed, sharedBuffer.get(), frameCount, mFrameCount, format, |
| 1682 | audio_is_linear_pcm(format), |
| 1683 | channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1684 | flags &= ~IAudioFlinger::TRACK_FAST; |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1685 | // For compatibility with AudioTrack calculation, buffer depth is forced |
| 1686 | // to be at least 2 x the normal mixer frame count and cover audio hardware latency. |
| 1687 | // This is probably too conservative, but legacy application code may depend on it. |
| 1688 | // If you change this calculation, also review the start threshold which is related. |
| 1689 | uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream); |
| 1690 | uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate); |
| 1691 | if (minBufCount < 2) { |
| 1692 | minBufCount = 2; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1693 | } |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1694 | int minFrameCount = mNormalFrameCount * minBufCount; |
| 1695 | if (frameCount < minFrameCount) { |
| 1696 | frameCount = minFrameCount; |
| 1697 | } |
| 1698 | } |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1699 | } |
| 1700 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1701 | if (mType == DIRECT) { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1702 | if ((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM) { |
| 1703 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1704 | 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] | 1705 | "for output %p with format %d", |
| 1706 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1707 | lStatus = BAD_VALUE; |
| 1708 | goto Exit; |
| 1709 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1710 | } |
| 1711 | } else { |
| 1712 | // Resampler implementation limits input sampling rate to 2 x output sampling rate. |
| 1713 | if (sampleRate > mSampleRate*2) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1714 | ALOGE("Sample rate out of range: %d mSampleRate %d", sampleRate, mSampleRate); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1715 | lStatus = BAD_VALUE; |
| 1716 | goto Exit; |
| 1717 | } |
| 1718 | } |
| 1719 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1720 | lStatus = initCheck(); |
| 1721 | if (lStatus != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1722 | ALOGE("Audio driver not initialized."); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1723 | goto Exit; |
| 1724 | } |
| 1725 | |
| 1726 | { // scope for mLock |
| 1727 | Mutex::Autolock _l(mLock); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1728 | |
| 1729 | // all tracks in same audio session must share the same routing strategy otherwise |
| 1730 | // conflicts will happen when tracks are moved from one output to another by audio policy |
| 1731 | // manager |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 1732 | uint32_t strategy = AudioSystem::getStrategyForStream(streamType); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1733 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1734 | sp<Track> t = mTracks[i]; |
Glenn Kasten | 639dbee | 2012-03-07 12:26:34 -0800 | [diff] [blame] | 1735 | if (t != 0 && !t->isOutputTrack()) { |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 1736 | uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); |
Glenn Kasten | d879601 | 2011-10-28 10:31:42 -0700 | [diff] [blame] | 1737 | if (sessionId == t->sessionId() && strategy != actual) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1738 | ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", |
Glenn Kasten | d879601 | 2011-10-28 10:31:42 -0700 | [diff] [blame] | 1739 | strategy, actual); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1740 | lStatus = BAD_VALUE; |
| 1741 | goto Exit; |
| 1742 | } |
| 1743 | } |
| 1744 | } |
| 1745 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1746 | if (!isTimed) { |
| 1747 | track = new Track(this, client, streamType, sampleRate, format, |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1748 | channelMask, frameCount, sharedBuffer, sessionId, flags); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1749 | } else { |
| 1750 | track = TimedTrack::create(this, client, streamType, sampleRate, format, |
| 1751 | channelMask, frameCount, sharedBuffer, sessionId); |
| 1752 | } |
| 1753 | if (track == NULL || track->getCblk() == NULL || track->name() < 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1754 | lStatus = NO_MEMORY; |
| 1755 | goto Exit; |
| 1756 | } |
| 1757 | mTracks.add(track); |
| 1758 | |
| 1759 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1760 | if (chain != 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1761 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1762 | track->setMainBuffer(chain->inBuffer()); |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 1763 | chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1764 | chain->incTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1765 | } |
| 1766 | } |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1767 | |
| 1768 | #ifdef HAVE_REQUEST_PRIORITY |
| 1769 | if ((flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { |
| 1770 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 1771 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 1772 | // so ask activity manager to do this on our behalf |
| 1773 | int err = requestPriority(callingPid, tid, 1); |
| 1774 | if (err != 0) { |
| 1775 | ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", |
| 1776 | 1, callingPid, tid, err); |
| 1777 | } |
| 1778 | } |
| 1779 | #endif |
| 1780 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1781 | lStatus = NO_ERROR; |
| 1782 | |
| 1783 | Exit: |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1784 | if (status) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1785 | *status = lStatus; |
| 1786 | } |
| 1787 | return track; |
| 1788 | } |
| 1789 | |
| 1790 | uint32_t AudioFlinger::PlaybackThread::latency() const |
| 1791 | { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1792 | Mutex::Autolock _l(mLock); |
| 1793 | if (initCheck() == NO_ERROR) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1794 | return mOutput->stream->get_latency(mOutput->stream); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1795 | } else { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1796 | return 0; |
| 1797 | } |
| 1798 | } |
| 1799 | |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1800 | void AudioFlinger::PlaybackThread::setMasterVolume(float value) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1801 | { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1802 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1803 | mMasterVolume = value; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1804 | } |
| 1805 | |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1806 | void AudioFlinger::PlaybackThread::setMasterMute(bool muted) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1807 | { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1808 | Mutex::Autolock _l(mLock); |
| 1809 | setMasterMute_l(muted); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1810 | } |
| 1811 | |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1812 | void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1813 | { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1814 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1815 | mStreamTypes[stream].volume = value; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1816 | } |
| 1817 | |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1818 | void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1819 | { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1820 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1821 | mStreamTypes[stream].mute = muted; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1822 | } |
| 1823 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1824 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1825 | { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1826 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1827 | return mStreamTypes[stream].volume; |
| 1828 | } |
| 1829 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1830 | // addTrack_l() must be called with ThreadBase::mLock held |
| 1831 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) |
| 1832 | { |
| 1833 | status_t status = ALREADY_EXISTS; |
| 1834 | |
| 1835 | // set retry count for buffer fill |
| 1836 | track->mRetryCount = kMaxTrackStartupRetries; |
| 1837 | if (mActiveTracks.indexOf(track) < 0) { |
| 1838 | // the track is newly added, make sure it fills up all its |
| 1839 | // buffers before playing. This is to ensure the client will |
| 1840 | // effectively get the latency it requested. |
| 1841 | track->mFillingUpStatus = Track::FS_FILLING; |
| 1842 | track->mResetDone = false; |
| 1843 | mActiveTracks.add(track); |
| 1844 | if (track->mainBuffer() != mMixBuffer) { |
| 1845 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 1846 | if (chain != 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1847 | 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] | 1848 | chain->incActiveTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1849 | } |
| 1850 | } |
| 1851 | |
| 1852 | status = NO_ERROR; |
| 1853 | } |
| 1854 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1855 | ALOGV("mWaitWorkCV.broadcast"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1856 | mWaitWorkCV.broadcast(); |
| 1857 | |
| 1858 | return status; |
| 1859 | } |
| 1860 | |
| 1861 | // destroyTrack_l() must be called with ThreadBase::mLock held |
| 1862 | void AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) |
| 1863 | { |
| 1864 | track->mState = TrackBase::TERMINATED; |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 1865 | // active tracks are removed by threadLoop() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1866 | if (mActiveTracks.indexOf(track) < 0) { |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1867 | removeTrack_l(track); |
| 1868 | } |
| 1869 | } |
| 1870 | |
| 1871 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) |
| 1872 | { |
| 1873 | mTracks.remove(track); |
| 1874 | deleteTrackName_l(track->name()); |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 1875 | // redundant as track is about to be destroyed, for dumpsys only |
| 1876 | track->mName = -1; |
| 1877 | if (track->isFastTrack()) { |
| 1878 | int index = track->mFastIndex; |
| 1879 | ALOG_ASSERT(0 < index && index < FastMixerState::kMaxFastTracks); |
| 1880 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); |
| 1881 | mFastTrackAvailMask |= 1 << index; |
| 1882 | // redundant as track is about to be destroyed, for dumpsys only |
| 1883 | track->mFastIndex = -1; |
| 1884 | } |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1885 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 1886 | if (chain != 0) { |
| 1887 | chain->decTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) |
| 1892 | { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1893 | String8 out_s8 = String8(""); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1894 | char *s; |
| 1895 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1896 | Mutex::Autolock _l(mLock); |
| 1897 | if (initCheck() != NO_ERROR) { |
| 1898 | return out_s8; |
| 1899 | } |
| 1900 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1901 | s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1902 | out_s8 = String8(s); |
| 1903 | free(s); |
| 1904 | return out_s8; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1905 | } |
| 1906 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1907 | // audioConfigChanged_l() must be called with AudioFlinger::mLock held |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1908 | void AudioFlinger::PlaybackThread::audioConfigChanged_l(int event, int param) { |
| 1909 | AudioSystem::OutputDescriptor desc; |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 1910 | void *param2 = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1911 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1912 | 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] | 1913 | |
| 1914 | switch (event) { |
| 1915 | case AudioSystem::OUTPUT_OPENED: |
| 1916 | case AudioSystem::OUTPUT_CONFIG_CHANGED: |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1917 | desc.channels = mChannelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1918 | desc.samplingRate = mSampleRate; |
| 1919 | desc.format = mFormat; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1920 | desc.frameCount = mNormalFrameCount; // FIXME see AudioFlinger::frameCount(audio_io_handle_t) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1921 | desc.latency = latency(); |
| 1922 | param2 = &desc; |
| 1923 | break; |
| 1924 | |
| 1925 | case AudioSystem::STREAM_CONFIG_CHANGED: |
| 1926 | param2 = ¶m; |
| 1927 | case AudioSystem::OUTPUT_CLOSED: |
| 1928 | default: |
| 1929 | break; |
| 1930 | } |
| 1931 | mAudioFlinger->audioConfigChanged_l(event, mId, param2); |
| 1932 | } |
| 1933 | |
| 1934 | void AudioFlinger::PlaybackThread::readOutputParameters() |
| 1935 | { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1936 | mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1937 | mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common); |
| 1938 | mChannelCount = (uint16_t)popcount(mChannelMask); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1939 | mFormat = mOutput->stream->common.get_format(&mOutput->stream->common); |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 1940 | mFrameSize = audio_stream_frame_size(&mOutput->stream->common); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1941 | mFrameCount = mOutput->stream->common.get_buffer_size(&mOutput->stream->common) / mFrameSize; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1942 | if (mFrameCount & 15) { |
| 1943 | ALOGW("HAL output buffer size is %u frames but AudioMixer requires multiples of 16 frames", |
| 1944 | mFrameCount); |
| 1945 | } |
| 1946 | |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 1947 | // Calculate size of normal mix buffer relative to the HAL output buffer size |
| 1948 | uint32_t multiple = 1; |
| 1949 | if (mType == MIXER && (kUseFastMixer == FastMixer_Static || kUseFastMixer == FastMixer_Dynamic)) { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1950 | size_t minNormalFrameCount = (kMinNormalMixBufferSizeMs * mSampleRate) / 1000; |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 1951 | multiple = (minNormalFrameCount + mFrameCount - 1) / mFrameCount; |
| 1952 | // force multiple to be even, for compatibility with doubling of fast tracks due to HAL SRC |
| 1953 | // (it would be unusual for the normal mix buffer size to not be a multiple of fast track) |
| 1954 | // FIXME this rounding up should not be done if no HAL SRC |
| 1955 | if ((multiple > 2) && (multiple & 1)) { |
| 1956 | ++multiple; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1957 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1958 | } |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 1959 | mNormalFrameCount = multiple * mFrameCount; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1960 | 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] | 1961 | |
| 1962 | // FIXME - Current mixer implementation only supports stereo output: Always |
| 1963 | // Allocate a stereo buffer even if HW output is mono. |
Glenn Kasten | e9dd017 | 2012-01-27 18:08:45 -0800 | [diff] [blame] | 1964 | delete[] mMixBuffer; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1965 | mMixBuffer = new int16_t[mNormalFrameCount * 2]; |
| 1966 | memset(mMixBuffer, 0, mNormalFrameCount * 2 * sizeof(int16_t)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1967 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1968 | // force reconfiguration of effect chains and engines to take new buffer size and audio |
| 1969 | // parameters into account |
| 1970 | // Note that mLock is not held when readOutputParameters() is called from the constructor |
| 1971 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't |
| 1972 | // matter. |
| 1973 | // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains |
| 1974 | Vector< sp<EffectChain> > effectChains = mEffectChains; |
| 1975 | for (size_t i = 0; i < effectChains.size(); i ++) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1976 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1977 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) |
| 1981 | { |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 1982 | if (halFrames == NULL || dspFrames == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1983 | return BAD_VALUE; |
| 1984 | } |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1985 | Mutex::Autolock _l(mLock); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1986 | if (initCheck() != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1987 | return INVALID_OPERATION; |
| 1988 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1989 | *halFrames = mBytesWritten / audio_stream_frame_size(&mOutput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1990 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1991 | return mOutput->stream->get_render_position(mOutput->stream, dspFrames); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1992 | } |
| 1993 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1994 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1995 | { |
| 1996 | Mutex::Autolock _l(mLock); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1997 | uint32_t result = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1998 | if (getEffectChain_l(sessionId) != 0) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1999 | result = EFFECT_SESSION; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2003 | sp<Track> track = mTracks[i]; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2004 | if (sessionId == track->sessionId() && |
| 2005 | !(track->mCblk->flags & CBLK_INVALID_MSK)) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2006 | result |= TRACK_SESSION; |
| 2007 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2008 | } |
| 2009 | } |
| 2010 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2011 | return result; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2012 | } |
| 2013 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2014 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId) |
| 2015 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2016 | // 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] | 2017 | // 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] | 2018 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 2019 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2020 | } |
| 2021 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 2022 | sp<Track> track = mTracks[i]; |
| 2023 | if (sessionId == track->sessionId() && |
| 2024 | !(track->mCblk->flags & CBLK_INVALID_MSK)) { |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 2025 | return AudioSystem::getStrategyForStream(track->streamType()); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2026 | } |
| 2027 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2028 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2029 | } |
| 2030 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2031 | |
Glenn Kasten | aed850d | 2012-01-26 09:46:34 -0800 | [diff] [blame] | 2032 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2033 | { |
| 2034 | Mutex::Autolock _l(mLock); |
| 2035 | return mOutput; |
| 2036 | } |
| 2037 | |
| 2038 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() |
| 2039 | { |
| 2040 | Mutex::Autolock _l(mLock); |
| 2041 | AudioStreamOut *output = mOutput; |
| 2042 | mOutput = NULL; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2043 | // FIXME FastMixer might also have a raw ptr to mOutputSink; |
| 2044 | // must push a NULL and wait for ack |
| 2045 | mOutputSink.clear(); |
| 2046 | mPipeSink.clear(); |
| 2047 | mNormalSink.clear(); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2048 | return output; |
| 2049 | } |
| 2050 | |
| 2051 | // 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] | 2052 | audio_stream_t* AudioFlinger::PlaybackThread::stream() const |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2053 | { |
| 2054 | if (mOutput == NULL) { |
| 2055 | return NULL; |
| 2056 | } |
| 2057 | return &mOutput->stream->common; |
| 2058 | } |
| 2059 | |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 2060 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const |
Eric Laurent | 162b40b | 2011-12-05 09:47:19 -0800 | [diff] [blame] | 2061 | { |
| 2062 | // A2DP output latency is not due only to buffering capacity. It also reflects encoding, |
| 2063 | // decoding and transfer time. So sleeping for half of the latency would likely cause |
| 2064 | // underruns |
| 2065 | if (audio_is_a2dp_device((audio_devices_t)mDevice)) { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2066 | return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
Eric Laurent | 162b40b | 2011-12-05 09:47:19 -0800 | [diff] [blame] | 2067 | } else { |
| 2068 | return (uint32_t)(mOutput->stream->get_latency(mOutput->stream) * 1000) / 2; |
| 2069 | } |
| 2070 | } |
| 2071 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 2072 | status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) |
| 2073 | { |
| 2074 | if (!isValidSyncEvent(event)) { |
| 2075 | return BAD_VALUE; |
| 2076 | } |
| 2077 | |
| 2078 | Mutex::Autolock _l(mLock); |
| 2079 | |
| 2080 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2081 | sp<Track> track = mTracks[i]; |
| 2082 | if (event->triggerSession() == track->sessionId()) { |
| 2083 | track->setSyncEvent(event); |
| 2084 | return NO_ERROR; |
| 2085 | } |
| 2086 | } |
| 2087 | |
| 2088 | return NAME_NOT_FOUND; |
| 2089 | } |
| 2090 | |
| 2091 | bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) |
| 2092 | { |
| 2093 | switch (event->type()) { |
| 2094 | case AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE: |
| 2095 | return true; |
| 2096 | default: |
| 2097 | break; |
| 2098 | } |
| 2099 | return false; |
| 2100 | } |
| 2101 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2102 | // ---------------------------------------------------------------------------- |
| 2103 | |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 2104 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2105 | audio_io_handle_t id, uint32_t device, type_t type) |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2106 | : PlaybackThread(audioFlinger, output, id, device, type), |
| 2107 | // mAudioMixer below |
| 2108 | #ifdef SOAKER |
| 2109 | mSoaker(NULL), |
| 2110 | #endif |
| 2111 | // mFastMixer below |
| 2112 | mFastMixerFutex(0) |
| 2113 | // mOutputSink below |
| 2114 | // mPipeSink below |
| 2115 | // mNormalSink below |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2116 | { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2117 | ALOGV("MixerThread() id=%d device=%d type=%d", id, device, type); |
| 2118 | ALOGV("mSampleRate=%d, mChannelMask=%d, mChannelCount=%d, mFormat=%d, mFrameSize=%d, " |
| 2119 | "mFrameCount=%d, mNormalFrameCount=%d", |
| 2120 | mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, |
| 2121 | mNormalFrameCount); |
| 2122 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 2123 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2124 | // FIXME - Current mixer implementation only supports stereo output |
| 2125 | if (mChannelCount == 1) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 2126 | ALOGE("Invalid audio hardware channel count"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2127 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2128 | |
| 2129 | // create an NBAIO sink for the HAL output stream, and negotiate |
| 2130 | mOutputSink = new AudioStreamOutSink(output->stream); |
| 2131 | size_t numCounterOffers = 0; |
| 2132 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount)}; |
| 2133 | ssize_t index = mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); |
| 2134 | ALOG_ASSERT(index == 0); |
| 2135 | |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 2136 | // initialize fast mixer depending on configuration |
| 2137 | bool initFastMixer; |
| 2138 | switch (kUseFastMixer) { |
| 2139 | case FastMixer_Never: |
| 2140 | initFastMixer = false; |
| 2141 | break; |
| 2142 | case FastMixer_Always: |
| 2143 | initFastMixer = true; |
| 2144 | break; |
| 2145 | case FastMixer_Static: |
| 2146 | case FastMixer_Dynamic: |
| 2147 | initFastMixer = mFrameCount < mNormalFrameCount; |
| 2148 | break; |
| 2149 | } |
| 2150 | if (initFastMixer) { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2151 | |
| 2152 | // create a MonoPipe to connect our submix to FastMixer |
| 2153 | NBAIO_Format format = mOutputSink->format(); |
| 2154 | // frame count will be rounded up to a power of 2, so this formula should work well |
| 2155 | MonoPipe *monoPipe = new MonoPipe((mNormalFrameCount * 3) / 2, format, |
| 2156 | true /*writeCanBlock*/); |
| 2157 | const NBAIO_Format offers[1] = {format}; |
| 2158 | size_t numCounterOffers = 0; |
| 2159 | ssize_t index = monoPipe->negotiate(offers, 1, NULL, numCounterOffers); |
| 2160 | ALOG_ASSERT(index == 0); |
| 2161 | mPipeSink = monoPipe; |
| 2162 | |
| 2163 | #ifdef SOAKER |
| 2164 | // create a soaker as workaround for governor issues |
| 2165 | mSoaker = new Soaker(); |
| 2166 | // FIXME Soaker should only run when needed, i.e. when FastMixer is not in COLD_IDLE |
| 2167 | mSoaker->run("Soaker", PRIORITY_LOWEST); |
| 2168 | #endif |
| 2169 | |
| 2170 | // create fast mixer and configure it initially with just one fast track for our submix |
| 2171 | mFastMixer = new FastMixer(); |
| 2172 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 2173 | FastMixerState *state = sq->begin(); |
| 2174 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 2175 | // wrap the source side of the MonoPipe to make it an AudioBufferProvider |
| 2176 | fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); |
| 2177 | fastTrack->mVolumeProvider = NULL; |
| 2178 | fastTrack->mGeneration++; |
| 2179 | state->mFastTracksGen++; |
| 2180 | state->mTrackMask = 1; |
| 2181 | // fast mixer will use the HAL output sink |
| 2182 | state->mOutputSink = mOutputSink.get(); |
| 2183 | state->mOutputSinkGen++; |
| 2184 | state->mFrameCount = mFrameCount; |
| 2185 | state->mCommand = FastMixerState::COLD_IDLE; |
| 2186 | // already done in constructor initialization list |
| 2187 | //mFastMixerFutex = 0; |
| 2188 | state->mColdFutexAddr = &mFastMixerFutex; |
| 2189 | state->mColdGen++; |
| 2190 | state->mDumpState = &mFastMixerDumpState; |
| 2191 | sq->end(); |
| 2192 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 2193 | |
| 2194 | // start the fast mixer |
| 2195 | mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); |
| 2196 | #ifdef HAVE_REQUEST_PRIORITY |
| 2197 | pid_t tid = mFastMixer->getTid(); |
| 2198 | int err = requestPriority(getpid_cached, tid, 2); |
| 2199 | if (err != 0) { |
| 2200 | ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", |
| 2201 | 2, getpid_cached, tid, err); |
| 2202 | } |
| 2203 | #endif |
| 2204 | |
| 2205 | } else { |
| 2206 | mFastMixer = NULL; |
| 2207 | } |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 2208 | |
| 2209 | switch (kUseFastMixer) { |
| 2210 | case FastMixer_Never: |
| 2211 | case FastMixer_Dynamic: |
| 2212 | mNormalSink = mOutputSink; |
| 2213 | break; |
| 2214 | case FastMixer_Always: |
| 2215 | mNormalSink = mPipeSink; |
| 2216 | break; |
| 2217 | case FastMixer_Static: |
| 2218 | mNormalSink = initFastMixer ? mPipeSink : mOutputSink; |
| 2219 | break; |
| 2220 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | AudioFlinger::MixerThread::~MixerThread() |
| 2224 | { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2225 | if (mFastMixer != NULL) { |
| 2226 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 2227 | FastMixerState *state = sq->begin(); |
| 2228 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
| 2229 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 2230 | if (old == -1) { |
| 2231 | __futex_syscall3(&mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
| 2232 | } |
| 2233 | } |
| 2234 | state->mCommand = FastMixerState::EXIT; |
| 2235 | sq->end(); |
| 2236 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 2237 | mFastMixer->join(); |
| 2238 | // Though the fast mixer thread has exited, it's state queue is still valid. |
| 2239 | // We'll use that extract the final state which contains one remaining fast track |
| 2240 | // corresponding to our sub-mix. |
| 2241 | state = sq->begin(); |
| 2242 | ALOG_ASSERT(state->mTrackMask == 1); |
| 2243 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 2244 | ALOG_ASSERT(fastTrack->mBufferProvider != NULL); |
| 2245 | delete fastTrack->mBufferProvider; |
| 2246 | sq->end(false /*didModify*/); |
| 2247 | delete mFastMixer; |
| 2248 | #ifdef SOAKER |
| 2249 | if (mSoaker != NULL) { |
| 2250 | mSoaker->requestExitAndWait(); |
| 2251 | } |
| 2252 | delete mSoaker; |
| 2253 | #endif |
| 2254 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2255 | delete mAudioMixer; |
| 2256 | } |
| 2257 | |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2258 | class CpuStats { |
| 2259 | public: |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2260 | CpuStats(); |
| 2261 | void sample(const String8 &title); |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2262 | #ifdef DEBUG_CPU_USAGE |
| 2263 | private: |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2264 | ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns |
| 2265 | CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns |
| 2266 | |
| 2267 | CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles |
| 2268 | |
| 2269 | int mCpuNum; // thread's current CPU number |
| 2270 | int mCpukHz; // frequency of thread's current CPU in kHz |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2271 | #endif |
| 2272 | }; |
| 2273 | |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2274 | CpuStats::CpuStats() |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2275 | #ifdef DEBUG_CPU_USAGE |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2276 | : mCpuNum(-1), mCpukHz(-1) |
| 2277 | #endif |
| 2278 | { |
| 2279 | } |
| 2280 | |
| 2281 | void CpuStats::sample(const String8 &title) { |
| 2282 | #ifdef DEBUG_CPU_USAGE |
| 2283 | // get current thread's delta CPU time in wall clock ns |
| 2284 | double wcNs; |
| 2285 | bool valid = mCpuUsage.sampleAndEnable(wcNs); |
| 2286 | |
| 2287 | // record sample for wall clock statistics |
| 2288 | if (valid) { |
| 2289 | mWcStats.sample(wcNs); |
| 2290 | } |
| 2291 | |
| 2292 | // get the current CPU number |
| 2293 | int cpuNum = sched_getcpu(); |
| 2294 | |
| 2295 | // get the current CPU frequency in kHz |
| 2296 | int cpukHz = mCpuUsage.getCpukHz(cpuNum); |
| 2297 | |
| 2298 | // check if either CPU number or frequency changed |
| 2299 | if (cpuNum != mCpuNum || cpukHz != mCpukHz) { |
| 2300 | mCpuNum = cpuNum; |
| 2301 | mCpukHz = cpukHz; |
| 2302 | // ignore sample for purposes of cycles |
| 2303 | valid = false; |
| 2304 | } |
| 2305 | |
| 2306 | // if no change in CPU number or frequency, then record sample for cycle statistics |
| 2307 | if (valid && mCpukHz > 0) { |
| 2308 | double cycles = wcNs * cpukHz * 0.000001; |
| 2309 | mHzStats.sample(cycles); |
| 2310 | } |
| 2311 | |
| 2312 | unsigned n = mWcStats.n(); |
| 2313 | // mCpuUsage.elapsed() is expensive, so don't call it every loop |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2314 | if ((n & 127) == 1) { |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2315 | long long elapsed = mCpuUsage.elapsed(); |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2316 | if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { |
| 2317 | double perLoop = elapsed / (double) n; |
| 2318 | double perLoop100 = perLoop * 0.01; |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2319 | double perLoop1k = perLoop * 0.001; |
| 2320 | double mean = mWcStats.mean(); |
| 2321 | double stddev = mWcStats.stddev(); |
| 2322 | double minimum = mWcStats.minimum(); |
| 2323 | double maximum = mWcStats.maximum(); |
| 2324 | double meanCycles = mHzStats.mean(); |
| 2325 | double stddevCycles = mHzStats.stddev(); |
| 2326 | double minCycles = mHzStats.minimum(); |
| 2327 | double maxCycles = mHzStats.maximum(); |
| 2328 | mCpuUsage.resetElapsed(); |
| 2329 | mWcStats.reset(); |
| 2330 | mHzStats.reset(); |
| 2331 | ALOGD("CPU usage for %s over past %.1f secs\n" |
| 2332 | " (%u mixer loops at %.1f mean ms per loop):\n" |
| 2333 | " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" |
| 2334 | " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" |
| 2335 | " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", |
| 2336 | title.string(), |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2337 | elapsed * .000000001, n, perLoop * .000001, |
| 2338 | mean * .001, |
| 2339 | stddev * .001, |
| 2340 | minimum * .001, |
| 2341 | maximum * .001, |
| 2342 | mean / perLoop100, |
| 2343 | stddev / perLoop100, |
| 2344 | minimum / perLoop100, |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2345 | maximum / perLoop100, |
| 2346 | meanCycles / perLoop1k, |
| 2347 | stddevCycles / perLoop1k, |
| 2348 | minCycles / perLoop1k, |
| 2349 | maxCycles / perLoop1k); |
| 2350 | |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2351 | } |
| 2352 | } |
| 2353 | #endif |
| 2354 | }; |
| 2355 | |
Glenn Kasten | 37d825e | 2012-02-24 07:21:48 -0800 | [diff] [blame] | 2356 | void AudioFlinger::PlaybackThread::checkSilentMode_l() |
| 2357 | { |
| 2358 | if (!mMasterMute) { |
| 2359 | char value[PROPERTY_VALUE_MAX]; |
| 2360 | if (property_get("ro.audio.silent", value, "0") > 0) { |
| 2361 | char *endptr; |
| 2362 | unsigned long ul = strtoul(value, &endptr, 0); |
| 2363 | if (*endptr == '\0' && ul != 0) { |
| 2364 | ALOGD("Silence is golden"); |
| 2365 | // The setprop command will not allow a property to be changed after |
| 2366 | // the first time it is set, so we don't have to worry about un-muting. |
| 2367 | setMasterMute_l(true); |
| 2368 | } |
| 2369 | } |
| 2370 | } |
| 2371 | } |
| 2372 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2373 | bool AudioFlinger::PlaybackThread::threadLoop() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2374 | { |
| 2375 | Vector< sp<Track> > tracksToRemove; |
Glenn Kasten | 688a640 | 2012-02-29 07:57:06 -0800 | [diff] [blame] | 2376 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2377 | standbyTime = systemTime(); |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2378 | |
| 2379 | // MIXER |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2380 | nsecs_t lastWarning = 0; |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2381 | if (mType == MIXER) { |
| 2382 | longStandbyExit = false; |
| 2383 | } |
Glenn Kasten | 688a640 | 2012-02-29 07:57:06 -0800 | [diff] [blame] | 2384 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2385 | // DUPLICATING |
| 2386 | // FIXME could this be made local to while loop? |
| 2387 | writeFrames = 0; |
Glenn Kasten | 688a640 | 2012-02-29 07:57:06 -0800 | [diff] [blame] | 2388 | |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 2389 | cacheParameters_l(); |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2390 | sleepTime = idleSleepTime; |
| 2391 | |
| 2392 | if (mType == MIXER) { |
| 2393 | sleepTimeShift = 0; |
| 2394 | } |
| 2395 | |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2396 | CpuStats cpuStats; |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2397 | 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] | 2398 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2399 | acquireWakeLock(); |
| 2400 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2401 | while (!exitPending()) |
| 2402 | { |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2403 | cpuStats.sample(myName); |
Glenn Kasten | 688a640 | 2012-02-29 07:57:06 -0800 | [diff] [blame] | 2404 | |
Glenn Kasten | 73ca0f5 | 2012-02-29 07:56:15 -0800 | [diff] [blame] | 2405 | Vector< sp<EffectChain> > effectChains; |
| 2406 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2407 | processConfigEvents(); |
| 2408 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2409 | { // scope for mLock |
| 2410 | |
| 2411 | Mutex::Autolock _l(mLock); |
| 2412 | |
| 2413 | if (checkForNewParameters_l()) { |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 2414 | cacheParameters_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2415 | } |
| 2416 | |
Glenn Kasten | fa26a85 | 2012-03-06 11:28:04 -0800 | [diff] [blame] | 2417 | saveOutputTracks(); |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2418 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2419 | // put audio hardware into standby after short delay |
Glenn Kasten | 3e07470 | 2012-02-28 18:40:35 -0800 | [diff] [blame] | 2420 | if (CC_UNLIKELY((!mActiveTracks.size() && systemTime() > standbyTime) || |
Glenn Kasten | c455fe9 | 2012-02-29 07:07:30 -0800 | [diff] [blame] | 2421 | mSuspended > 0)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2422 | if (!mStandby) { |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2423 | |
| 2424 | threadLoop_standby(); |
| 2425 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2426 | mStandby = true; |
| 2427 | mBytesWritten = 0; |
| 2428 | } |
| 2429 | |
Glenn Kasten | 3e07470 | 2012-02-28 18:40:35 -0800 | [diff] [blame] | 2430 | if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2431 | // we're about to wait, flush the binder command buffer |
| 2432 | IPCThreadState::self()->flushCommands(); |
| 2433 | |
Glenn Kasten | fa26a85 | 2012-03-06 11:28:04 -0800 | [diff] [blame] | 2434 | clearOutputTracks(); |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2435 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2436 | if (exitPending()) break; |
| 2437 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2438 | releaseWakeLock_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2439 | // wait until we have something to do... |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2440 | ALOGV("%s going to sleep", myName.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2441 | mWaitWorkCV.wait(mLock); |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2442 | ALOGV("%s waking up", myName.string()); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2443 | acquireWakeLock_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2444 | |
Eric Laurent | da74744 | 2012-04-25 18:53:13 -0700 | [diff] [blame] | 2445 | mMixerStatus = MIXER_IDLE; |
Glenn Kasten | 8102804 | 2012-04-30 18:15:12 -0700 | [diff] [blame] | 2446 | mMixerStatusIgnoringFastTracks = MIXER_IDLE; |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2447 | |
Glenn Kasten | 37d825e | 2012-02-24 07:21:48 -0800 | [diff] [blame] | 2448 | checkSilentMode_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2449 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2450 | standbyTime = systemTime() + standbyDelay; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2451 | sleepTime = idleSleepTime; |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 2452 | if (mType == MIXER) { |
| 2453 | sleepTimeShift = 0; |
| 2454 | } |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2455 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2456 | continue; |
| 2457 | } |
| 2458 | } |
| 2459 | |
Glenn Kasten | 8102804 | 2012-04-30 18:15:12 -0700 | [diff] [blame] | 2460 | // mMixerStatusIgnoringFastTracks is also updated internally |
Eric Laurent | da74744 | 2012-04-25 18:53:13 -0700 | [diff] [blame] | 2461 | mMixerStatus = prepareTracks_l(&tracksToRemove); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2462 | |
| 2463 | // prevent any changes in effect chain list and in each effect chain |
| 2464 | // during mixing and effect process as the audio buffers could be deleted |
| 2465 | // or modified if an effect is created or deleted |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2466 | lockEffectChains_l(effectChains); |
Glenn Kasten | c5ac4cb | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 2467 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2468 | |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 2469 | if (CC_LIKELY(mMixerStatus == MIXER_TRACKS_READY)) { |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2470 | threadLoop_mix(); |
| 2471 | } else { |
| 2472 | threadLoop_sleepTime(); |
| 2473 | } |
| 2474 | |
| 2475 | if (mSuspended > 0) { |
| 2476 | sleepTime = suspendSleepTimeUs(); |
| 2477 | } |
| 2478 | |
| 2479 | // only process effects if we're going to write |
| 2480 | if (sleepTime == 0) { |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2481 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2482 | effectChains[i]->process_l(); |
| 2483 | } |
| 2484 | } |
| 2485 | |
| 2486 | // enable changes in effect chain |
| 2487 | unlockEffectChains(effectChains); |
| 2488 | |
| 2489 | // sleepTime == 0 means we must write to audio hardware |
| 2490 | if (sleepTime == 0) { |
| 2491 | |
| 2492 | threadLoop_write(); |
| 2493 | |
| 2494 | if (mType == MIXER) { |
| 2495 | // write blocked detection |
| 2496 | nsecs_t now = systemTime(); |
| 2497 | nsecs_t delta = now - mLastWriteTime; |
| 2498 | if (!mStandby && delta > maxPeriod) { |
| 2499 | mNumDelayedWrites++; |
| 2500 | if ((now - lastWarning) > kWarningThrottleNs) { |
| 2501 | ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", |
| 2502 | ns2ms(delta), mNumDelayedWrites, this); |
| 2503 | lastWarning = now; |
| 2504 | } |
| 2505 | // FIXME this is broken: longStandbyExit should be handled out of the if() and with |
| 2506 | // a different threshold. Or completely removed for what it is worth anyway... |
| 2507 | if (mStandby) { |
| 2508 | longStandbyExit = true; |
| 2509 | } |
| 2510 | } |
| 2511 | } |
| 2512 | |
| 2513 | mStandby = false; |
| 2514 | } else { |
| 2515 | usleep(sleepTime); |
| 2516 | } |
| 2517 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2518 | // Finally let go of removed track(s), without the lock held |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2519 | // since we can't guarantee the destructors won't acquire that |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2520 | // same lock. This will also mutate and push a new fast mixer state. |
| 2521 | threadLoop_removeTracks(tracksToRemove); |
Glenn Kasten | 1465f0c | 2012-03-06 11:23:32 -0800 | [diff] [blame] | 2522 | tracksToRemove.clear(); |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2523 | |
Glenn Kasten | fa26a85 | 2012-03-06 11:28:04 -0800 | [diff] [blame] | 2524 | // FIXME I don't understand the need for this here; |
| 2525 | // it was in the original code but maybe the |
| 2526 | // assignment in saveOutputTracks() makes this unnecessary? |
| 2527 | clearOutputTracks(); |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2528 | |
| 2529 | // Effect chains will be actually deleted here if they were removed from |
| 2530 | // mEffectChains list during mixing or effects processing |
| 2531 | effectChains.clear(); |
| 2532 | |
| 2533 | // FIXME Note that the above .clear() is no longer necessary since effectChains |
| 2534 | // is now local to this block, but will keep it for now (at least until merge done). |
| 2535 | } |
| 2536 | |
| 2537 | if (mType == MIXER || mType == DIRECT) { |
| 2538 | // put output stream into standby mode |
| 2539 | if (!mStandby) { |
| 2540 | mOutput->stream->common.standby(&mOutput->stream->common); |
| 2541 | } |
| 2542 | } |
| 2543 | if (mType == DUPLICATING) { |
| 2544 | // for DuplicatingThread, standby mode is handled by the outputTracks |
| 2545 | } |
| 2546 | |
| 2547 | releaseWakeLock(); |
| 2548 | |
| 2549 | ALOGV("Thread %p type %d exiting", this, mType); |
| 2550 | return false; |
| 2551 | } |
| 2552 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 2553 | // returns (via tracksToRemove) a set of tracks to remove. |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2554 | void AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) |
| 2555 | { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2556 | PlaybackThread::threadLoop_removeTracks(tracksToRemove); |
| 2557 | } |
| 2558 | |
| 2559 | void AudioFlinger::MixerThread::threadLoop_write() |
| 2560 | { |
| 2561 | // FIXME we should only do one push per cycle; confirm this is true |
| 2562 | // Start the fast mixer if it's not already running |
| 2563 | if (mFastMixer != NULL) { |
| 2564 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 2565 | FastMixerState *state = sq->begin(); |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 2566 | if (state->mCommand != FastMixerState::MIX_WRITE && |
| 2567 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2568 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
| 2569 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 2570 | if (old == -1) { |
| 2571 | __futex_syscall3(&mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
| 2572 | } |
| 2573 | } |
| 2574 | state->mCommand = FastMixerState::MIX_WRITE; |
| 2575 | sq->end(); |
| 2576 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 2577 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 2578 | mNormalSink = mPipeSink; |
| 2579 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2580 | } else { |
| 2581 | sq->end(false /*didModify*/); |
| 2582 | } |
| 2583 | } |
| 2584 | PlaybackThread::threadLoop_write(); |
| 2585 | } |
| 2586 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2587 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
| 2588 | void AudioFlinger::PlaybackThread::threadLoop_write() |
| 2589 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2590 | // FIXME rewrite to reduce number of system calls |
| 2591 | mLastWriteTime = systemTime(); |
| 2592 | mInWrite = true; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2593 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2594 | #define mBitShift 2 // FIXME |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 2595 | size_t count = mixBufferSize >> mBitShift; |
| 2596 | ssize_t framesWritten = mNormalSink->write(mMixBuffer, count); |
| 2597 | if (framesWritten > 0) { |
| 2598 | size_t bytesWritten = framesWritten << mBitShift; |
| 2599 | mBytesWritten += bytesWritten; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2600 | } |
| 2601 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2602 | mNumWrites++; |
| 2603 | mInWrite = false; |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2604 | } |
| 2605 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2606 | void AudioFlinger::MixerThread::threadLoop_standby() |
| 2607 | { |
| 2608 | // Idle the fast mixer if it's currently running |
| 2609 | if (mFastMixer != NULL) { |
| 2610 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 2611 | FastMixerState *state = sq->begin(); |
| 2612 | if (!(state->mCommand & FastMixerState::IDLE)) { |
| 2613 | state->mCommand = FastMixerState::COLD_IDLE; |
| 2614 | state->mColdFutexAddr = &mFastMixerFutex; |
| 2615 | state->mColdGen++; |
| 2616 | mFastMixerFutex = 0; |
| 2617 | sq->end(); |
| 2618 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 2619 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 2620 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 2621 | mNormalSink = mOutputSink; |
| 2622 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2623 | } else { |
| 2624 | sq->end(false /*didModify*/); |
| 2625 | } |
| 2626 | } |
| 2627 | PlaybackThread::threadLoop_standby(); |
| 2628 | } |
| 2629 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2630 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
| 2631 | void AudioFlinger::PlaybackThread::threadLoop_standby() |
| 2632 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2633 | ALOGV("Audio hardware entering standby, mixer %p, suspend count %u", this, mSuspended); |
| 2634 | mOutput->stream->common.standby(&mOutput->stream->common); |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2635 | } |
| 2636 | |
| 2637 | void AudioFlinger::MixerThread::threadLoop_mix() |
| 2638 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2639 | // obtain the presentation timestamp of the next output buffer |
| 2640 | int64_t pts; |
| 2641 | status_t status = INVALID_OPERATION; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 2642 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2643 | if (NULL != mOutput->stream->get_next_write_timestamp) { |
| 2644 | status = mOutput->stream->get_next_write_timestamp( |
| 2645 | mOutput->stream, &pts); |
| 2646 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 2647 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2648 | if (status != NO_ERROR) { |
| 2649 | pts = AudioBufferProvider::kInvalidPTS; |
| 2650 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 2651 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2652 | // mix buffers... |
| 2653 | mAudioMixer->process(pts); |
| 2654 | // increase sleep time progressively when application underrun condition clears. |
| 2655 | // Only increase sleep time if the mixer is ready for two consecutive times to avoid |
| 2656 | // that a steady state of alternating ready/not ready conditions keeps the sleep time |
| 2657 | // such that we would underrun the audio HAL. |
| 2658 | if ((sleepTime == 0) && (sleepTimeShift > 0)) { |
| 2659 | sleepTimeShift--; |
| 2660 | } |
| 2661 | sleepTime = 0; |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 2662 | standbyTime = systemTime() + standbyDelay; |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2663 | //TODO: delay standby when effects have a tail |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2664 | } |
| 2665 | |
| 2666 | void AudioFlinger::MixerThread::threadLoop_sleepTime() |
| 2667 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2668 | // If no tracks are ready, sleep once for the duration of an output |
| 2669 | // buffer size, then write 0s to the output |
| 2670 | if (sleepTime == 0) { |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 2671 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2672 | sleepTime = activeSleepTime >> sleepTimeShift; |
| 2673 | if (sleepTime < kMinThreadSleepTimeUs) { |
| 2674 | sleepTime = kMinThreadSleepTimeUs; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2675 | } |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2676 | // reduce sleep time in case of consecutive application underruns to avoid |
| 2677 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer |
| 2678 | // duration we would end up writing less data than needed by the audio HAL if |
| 2679 | // the condition persists. |
| 2680 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { |
| 2681 | sleepTimeShift++; |
| 2682 | } |
| 2683 | } else { |
| 2684 | sleepTime = idleSleepTime; |
| 2685 | } |
| 2686 | } else if (mBytesWritten != 0 || |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 2687 | (mMixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)) { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2688 | memset (mMixBuffer, 0, mixBufferSize); |
| 2689 | sleepTime = 0; |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 2690 | ALOGV_IF((mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)), "anticipated start"); |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2691 | } |
| 2692 | // TODO add standby time extension fct of effect tail |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | // prepareTracks_l() must be called with ThreadBase::mLock held |
Glenn Kasten | 29c23c3 | 2012-01-26 13:37:52 -0800 | [diff] [blame] | 2696 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( |
Glenn Kasten | 3e07470 | 2012-02-28 18:40:35 -0800 | [diff] [blame] | 2697 | Vector< sp<Track> > *tracksToRemove) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2698 | { |
| 2699 | |
Glenn Kasten | 29c23c3 | 2012-01-26 13:37:52 -0800 | [diff] [blame] | 2700 | mixer_state mixerStatus = MIXER_IDLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2701 | // find out which tracks need to be processed |
Glenn Kasten | 3e07470 | 2012-02-28 18:40:35 -0800 | [diff] [blame] | 2702 | size_t count = mActiveTracks.size(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2703 | size_t mixedTracks = 0; |
| 2704 | size_t tracksWithEffect = 0; |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 2705 | // counts only _active_ fast tracks |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2706 | size_t fastTracks = 0; |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 2707 | 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] | 2708 | |
| 2709 | float masterVolume = mMasterVolume; |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 2710 | bool masterMute = mMasterMute; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2711 | |
Eric Laurent | 571d49c | 2010-08-11 05:20:11 -0700 | [diff] [blame] | 2712 | if (masterMute) { |
| 2713 | masterVolume = 0; |
| 2714 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2715 | // 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] | 2716 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2717 | if (chain != 0) { |
Eric Laurent | 571d49c | 2010-08-11 05:20:11 -0700 | [diff] [blame] | 2718 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2719 | chain->setVolume_l(&v, &v); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2720 | masterVolume = (float)((v + (1 << 23)) >> 24); |
| 2721 | chain.clear(); |
| 2722 | } |
| 2723 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 2724 | // prepare a new state to push |
| 2725 | FastMixerStateQueue *sq = NULL; |
| 2726 | FastMixerState *state = NULL; |
| 2727 | bool didModify = false; |
| 2728 | FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; |
| 2729 | if (mFastMixer != NULL) { |
| 2730 | sq = mFastMixer->sq(); |
| 2731 | state = sq->begin(); |
| 2732 | } |
| 2733 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2734 | for (size_t i=0 ; i<count ; i++) { |
Glenn Kasten | 3e07470 | 2012-02-28 18:40:35 -0800 | [diff] [blame] | 2735 | sp<Track> t = mActiveTracks[i].promote(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2736 | if (t == 0) continue; |
| 2737 | |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2738 | // this const just means the local variable doesn't change |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2739 | Track* const track = t.get(); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2740 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 2741 | // process fast tracks |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2742 | if (track->isFastTrack()) { |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 2743 | |
| 2744 | // It's theoretically possible (though unlikely) for a fast track to be created |
| 2745 | // and then removed within the same normal mix cycle. This is not a problem, as |
| 2746 | // the track never becomes active so it's fast mixer slot is never touched. |
| 2747 | // The converse, of removing an (active) track and then creating a new track |
| 2748 | // at the identical fast mixer slot within the same normal mix cycle, |
| 2749 | // is impossible because the slot isn't marked available until the end of each cycle. |
| 2750 | int j = track->mFastIndex; |
| 2751 | FastTrack *fastTrack = &state->mFastTracks[j]; |
| 2752 | |
| 2753 | // Determine whether the track is currently in underrun condition, |
| 2754 | // and whether it had a recent underrun. |
| 2755 | uint32_t underruns = mFastMixerDumpState.mTracks[j].mUnderruns; |
| 2756 | uint32_t recentUnderruns = (underruns - (track->mObservedUnderruns & ~1)) >> 1; |
| 2757 | // don't count underruns that occur while stopping or pausing |
| 2758 | if (!(track->isStopped() || track->isPausing())) { |
| 2759 | track->mUnderrunCount += recentUnderruns; |
| 2760 | } |
| 2761 | track->mObservedUnderruns = underruns; |
| 2762 | |
| 2763 | // This is similar to the formula for normal tracks, |
| 2764 | // with a few modifications for fast tracks. |
| 2765 | bool isActive; |
| 2766 | if (track->isStopped()) { |
| 2767 | // track stays active after stop() until first underrun |
| 2768 | isActive = recentUnderruns == 0; |
| 2769 | } else if (track->isPaused() || track->isTerminated()) { |
| 2770 | isActive = false; |
| 2771 | } else if (track->isPausing()) { |
| 2772 | // ramp down is not yet implemented |
| 2773 | isActive = true; |
| 2774 | track->setPaused(); |
| 2775 | } else if (track->isResuming()) { |
| 2776 | // ramp up is not yet implemented |
| 2777 | isActive = true; |
| 2778 | track->mState = TrackBase::ACTIVE; |
| 2779 | } else { |
| 2780 | // no minimum frame count for fast tracks; continual underrun is allowed, |
| 2781 | // but later could implement automatic pause after several consecutive underruns, |
| 2782 | // or auto-mute yet still consider the track active and continue to service it |
| 2783 | isActive = true; |
| 2784 | } |
| 2785 | |
| 2786 | if (isActive) { |
| 2787 | // was it previously inactive? |
| 2788 | if (!(state->mTrackMask & (1 << j))) { |
| 2789 | ExtendedAudioBufferProvider *eabp = track; |
| 2790 | VolumeProvider *vp = track; |
| 2791 | fastTrack->mBufferProvider = eabp; |
| 2792 | fastTrack->mVolumeProvider = vp; |
| 2793 | fastTrack->mSampleRate = track->mSampleRate; |
| 2794 | fastTrack->mChannelMask = track->mChannelMask; |
| 2795 | fastTrack->mGeneration++; |
| 2796 | state->mTrackMask |= 1 << j; |
| 2797 | didModify = true; |
| 2798 | // no acknowledgement required for newly active tracks |
| 2799 | } |
| 2800 | // cache the combined master volume and stream type volume for fast mixer; this |
| 2801 | // lacks any synchronization or barrier so VolumeProvider may read a stale value |
| 2802 | track->mCachedVolume = track->isMuted() ? |
| 2803 | 0 : masterVolume * mStreamTypes[track->streamType()].volume; |
| 2804 | ++fastTracks; |
| 2805 | } else { |
| 2806 | // was it previously active? |
| 2807 | if (state->mTrackMask & (1 << j)) { |
| 2808 | fastTrack->mBufferProvider = NULL; |
| 2809 | fastTrack->mGeneration++; |
| 2810 | state->mTrackMask &= ~(1 << j); |
| 2811 | didModify = true; |
| 2812 | // If any fast tracks were removed, we must wait for acknowledgement |
| 2813 | // because we're about to decrement the last sp<> on those tracks. |
| 2814 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 2815 | } |
| 2816 | // Remainder of this block is copied from similar code for normal tracks |
| 2817 | if (track->isStopped()) { |
| 2818 | // Can't reset directly, as fast mixer is still polling this track |
| 2819 | // track->reset(); |
| 2820 | // So instead mark this track as needing to be reset after push with ack |
| 2821 | resetMask |= 1 << i; |
| 2822 | } |
| 2823 | // This would be incomplete if we auto-paused on underrun |
| 2824 | size_t audioHALFrames = |
| 2825 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
| 2826 | size_t framesWritten = |
| 2827 | mBytesWritten / audio_stream_frame_size(&mOutput->stream->common); |
| 2828 | if (track->presentationComplete(framesWritten, audioHALFrames)) { |
| 2829 | tracksToRemove->add(track); |
| 2830 | } |
| 2831 | // Avoids a misleading display in dumpsys |
| 2832 | track->mObservedUnderruns &= ~1; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2833 | } |
| 2834 | continue; |
| 2835 | } |
| 2836 | |
| 2837 | { // local variable scope to avoid goto warning |
| 2838 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2839 | audio_track_cblk_t* cblk = track->cblk(); |
| 2840 | |
| 2841 | // The first time a track is added we wait |
| 2842 | // for all its buffers to be filled before processing it |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2843 | int name = track->name(); |
Eric Laurent | a47b69c | 2011-11-08 18:10:16 -0800 | [diff] [blame] | 2844 | // make sure that we have enough frames to mix one full buffer. |
| 2845 | // enforce this condition only once to enable draining the buffer in case the client |
| 2846 | // app does not call stop() and relies on underrun to stop: |
Eric Laurent | da74744 | 2012-04-25 18:53:13 -0700 | [diff] [blame] | 2847 | // 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] | 2848 | // during last round |
Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2849 | uint32_t minFrames = 1; |
Eric Laurent | 83faee0 | 2012-04-27 18:24:29 -0700 | [diff] [blame] | 2850 | if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && |
Glenn Kasten | 8102804 | 2012-04-30 18:15:12 -0700 | [diff] [blame] | 2851 | (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { |
Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2852 | if (t->sampleRate() == (int)mSampleRate) { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2853 | minFrames = mNormalFrameCount; |
Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2854 | } else { |
Eric Laurent | 071ccd5 | 2011-12-22 16:08:41 -0800 | [diff] [blame] | 2855 | // +1 for rounding and +1 for additional sample needed for interpolation |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2856 | minFrames = (mNormalFrameCount * t->sampleRate()) / mSampleRate + 1 + 1; |
Eric Laurent | 071ccd5 | 2011-12-22 16:08:41 -0800 | [diff] [blame] | 2857 | // add frames already consumed but not yet released by the resampler |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 2858 | // because cblk->framesReady() will include these frames |
Eric Laurent | 071ccd5 | 2011-12-22 16:08:41 -0800 | [diff] [blame] | 2859 | minFrames += mAudioMixer->getUnreleasedFrames(track->name()); |
| 2860 | // the minimum track buffer size is normally twice the number of frames necessary |
| 2861 | // to fill one buffer and the resampler should not leave more than one buffer worth |
| 2862 | // of unreleased frames after each pass, but just in case... |
Steve Block | c1dc1cb | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 2863 | ALOG_ASSERT(minFrames <= cblk->frameCount); |
Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2864 | } |
| 2865 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 2866 | if ((track->framesReady() >= minFrames) && track->isReady() && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2867 | !track->isPaused() && !track->isTerminated()) |
| 2868 | { |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2869 | //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] | 2870 | |
| 2871 | mixedTracks++; |
| 2872 | |
| 2873 | // track->mainBuffer() != mMixBuffer means there is an effect chain |
| 2874 | // connected to the track |
| 2875 | chain.clear(); |
| 2876 | if (track->mainBuffer() != mMixBuffer) { |
| 2877 | chain = getEffectChain_l(track->sessionId()); |
| 2878 | // Delegate volume control to effect in track effect chain if needed |
| 2879 | if (chain != 0) { |
| 2880 | tracksWithEffect++; |
| 2881 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2882 | 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] | 2883 | name, track->sessionId()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2884 | } |
| 2885 | } |
| 2886 | |
| 2887 | |
| 2888 | int param = AudioMixer::VOLUME; |
| 2889 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 2890 | // no ramp for the first volume setting |
| 2891 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 2892 | if (track->mState == TrackBase::RESUMING) { |
| 2893 | track->mState = TrackBase::ACTIVE; |
| 2894 | param = AudioMixer::RAMP_VOLUME; |
| 2895 | } |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2896 | mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2897 | } else if (cblk->server != 0) { |
| 2898 | // If the track is stopped before the first frame was mixed, |
| 2899 | // do not apply ramp |
| 2900 | param = AudioMixer::RAMP_VOLUME; |
| 2901 | } |
| 2902 | |
| 2903 | // compute volume for this track |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2904 | uint32_t vl, vr, va; |
Eric Laurent | 8569f0d | 2010-07-29 23:43:43 -0700 | [diff] [blame] | 2905 | if (track->isMuted() || track->isPausing() || |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 2906 | mStreamTypes[track->streamType()].mute) { |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2907 | vl = vr = va = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2908 | if (track->isPausing()) { |
| 2909 | track->setPaused(); |
| 2910 | } |
| 2911 | } else { |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2912 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2913 | // read original volumes with volume control |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 2914 | float typeVolume = mStreamTypes[track->streamType()].volume; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2915 | float v = masterVolume * typeVolume; |
Glenn Kasten | 83d8653 | 2012-01-17 14:39:34 -0800 | [diff] [blame] | 2916 | uint32_t vlr = cblk->getVolumeLR(); |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2917 | vl = vlr & 0xFFFF; |
| 2918 | vr = vlr >> 16; |
| 2919 | // track volumes come from shared memory, so can't be trusted and must be clamped |
| 2920 | if (vl > MAX_GAIN_INT) { |
| 2921 | ALOGV("Track left volume out of range: %04X", vl); |
| 2922 | vl = MAX_GAIN_INT; |
| 2923 | } |
| 2924 | if (vr > MAX_GAIN_INT) { |
| 2925 | ALOGV("Track right volume out of range: %04X", vr); |
| 2926 | vr = MAX_GAIN_INT; |
| 2927 | } |
| 2928 | // now apply the master volume and stream type volume |
| 2929 | vl = (uint32_t)(v * vl) << 12; |
| 2930 | vr = (uint32_t)(v * vr) << 12; |
| 2931 | // assuming master volume and stream type volume each go up to 1.0, |
| 2932 | // vl and vr are now in 8.24 format |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2933 | |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 2934 | uint16_t sendLevel = cblk->getSendLevel_U4_12(); |
| 2935 | // send level comes from shared memory and so may be corrupt |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 2936 | if (sendLevel > MAX_GAIN_INT) { |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 2937 | ALOGV("Track send level out of range: %04X", sendLevel); |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2938 | sendLevel = MAX_GAIN_INT; |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 2939 | } |
| 2940 | va = (uint32_t)(v * sendLevel); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2941 | } |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2942 | // Delegate volume control to effect in track effect chain if needed |
| 2943 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { |
| 2944 | // Do not ramp volume if volume is controlled by effect |
| 2945 | param = AudioMixer::VOLUME; |
| 2946 | track->mHasVolumeController = true; |
| 2947 | } else { |
| 2948 | // force no volume ramp when volume controller was just disabled or removed |
| 2949 | // from effect chain to avoid volume spike |
| 2950 | if (track->mHasVolumeController) { |
| 2951 | param = AudioMixer::VOLUME; |
| 2952 | } |
| 2953 | track->mHasVolumeController = false; |
| 2954 | } |
| 2955 | |
| 2956 | // Convert volumes from 8.24 to 4.12 format |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2957 | // This additional clamping is needed in case chain->setVolume_l() overshot |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 2958 | vl = (vl + (1 << 11)) >> 12; |
| 2959 | if (vl > MAX_GAIN_INT) vl = MAX_GAIN_INT; |
| 2960 | vr = (vr + (1 << 11)) >> 12; |
| 2961 | if (vr > MAX_GAIN_INT) vr = MAX_GAIN_INT; |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2962 | |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 2963 | 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] | 2964 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2965 | // XXX: these things DON'T need to be done each time |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2966 | mAudioMixer->setBufferProvider(name, track); |
| 2967 | mAudioMixer->enable(name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2968 | |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 2969 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, (void *)vl); |
| 2970 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, (void *)vr); |
| 2971 | mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, (void *)va); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2972 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2973 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2974 | AudioMixer::TRACK, |
| 2975 | AudioMixer::FORMAT, (void *)track->format()); |
| 2976 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2977 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2978 | AudioMixer::TRACK, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 2979 | AudioMixer::CHANNEL_MASK, (void *)track->channelMask()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2980 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2981 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2982 | AudioMixer::RESAMPLE, |
| 2983 | AudioMixer::SAMPLE_RATE, |
| 2984 | (void *)(cblk->sampleRate)); |
| 2985 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2986 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2987 | AudioMixer::TRACK, |
| 2988 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); |
| 2989 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2990 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2991 | AudioMixer::TRACK, |
| 2992 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); |
| 2993 | |
| 2994 | // reset retry count |
| 2995 | track->mRetryCount = kMaxTrackRetries; |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 2996 | |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 2997 | // If one track is ready, set the mixer ready if: |
| 2998 | // - the mixer was not ready during previous round OR |
| 2999 | // - no other track is not ready |
Glenn Kasten | 8102804 | 2012-04-30 18:15:12 -0700 | [diff] [blame] | 3000 | if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 3001 | mixerStatus != MIXER_TRACKS_ENABLED) { |
| 3002 | mixerStatus = MIXER_TRACKS_READY; |
| 3003 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3004 | } else { |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 3005 | //ALOGV("track %d u=%08x, s=%08x [NOT READY] on thread %p", name, cblk->user, cblk->server, this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3006 | if (track->isStopped()) { |
| 3007 | track->reset(); |
| 3008 | } |
Eric Laurent | 83faee0 | 2012-04-27 18:24:29 -0700 | [diff] [blame] | 3009 | if ((track->sharedBuffer() != 0) || track->isTerminated() || |
| 3010 | track->isStopped() || track->isPaused()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3011 | // We have consumed all the buffers of this track. |
| 3012 | // Remove it from the list of active tracks. |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 3013 | // TODO: use actual buffer filling status instead of latency when available from |
| 3014 | // audio HAL |
| 3015 | size_t audioHALFrames = |
| 3016 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
| 3017 | size_t framesWritten = |
| 3018 | mBytesWritten / audio_stream_frame_size(&mOutput->stream->common); |
| 3019 | if (track->presentationComplete(framesWritten, audioHALFrames)) { |
| 3020 | tracksToRemove->add(track); |
| 3021 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3022 | } else { |
| 3023 | // No buffers for this track. Give it a few chances to |
| 3024 | // fill a buffer, then remove it from active list. |
| 3025 | if (--(track->mRetryCount) <= 0) { |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 3026 | 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] | 3027 | tracksToRemove->add(track); |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 3028 | // indicate to client process that the track was disabled because of underrun; |
| 3029 | // it will then automatically call start() when data is available |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3030 | android_atomic_or(CBLK_DISABLED_ON, &cblk->flags); |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 3031 | // If one track is not ready, mark the mixer also not ready if: |
| 3032 | // - the mixer was ready during previous round OR |
| 3033 | // - no other track is ready |
Glenn Kasten | 8102804 | 2012-04-30 18:15:12 -0700 | [diff] [blame] | 3034 | } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 3035 | mixerStatus != MIXER_TRACKS_READY) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3036 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 3037 | } |
| 3038 | } |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 3039 | mAudioMixer->disable(name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3040 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3041 | |
| 3042 | } // local variable scope to avoid goto warning |
| 3043 | track_is_ready: ; |
| 3044 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3045 | } |
| 3046 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 3047 | // Push the new FastMixer state if necessary |
| 3048 | if (didModify) { |
| 3049 | state->mFastTracksGen++; |
| 3050 | // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle |
| 3051 | if (kUseFastMixer == FastMixer_Dynamic && |
| 3052 | state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { |
| 3053 | state->mCommand = FastMixerState::COLD_IDLE; |
| 3054 | state->mColdFutexAddr = &mFastMixerFutex; |
| 3055 | state->mColdGen++; |
| 3056 | mFastMixerFutex = 0; |
| 3057 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 3058 | mNormalSink = mOutputSink; |
| 3059 | } |
| 3060 | // If we go into cold idle, need to wait for acknowledgement |
| 3061 | // so that fast mixer stops doing I/O. |
| 3062 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 3063 | } |
| 3064 | sq->end(); |
| 3065 | } |
| 3066 | if (sq != NULL) { |
| 3067 | sq->end(didModify); |
| 3068 | sq->push(block); |
| 3069 | } |
| 3070 | |
| 3071 | // Now perform the deferred reset on fast tracks that have stopped |
| 3072 | while (resetMask != 0) { |
| 3073 | size_t i = __builtin_ctz(resetMask); |
| 3074 | ALOG_ASSERT(i < count); |
| 3075 | resetMask &= ~(1 << i); |
| 3076 | sp<Track> t = mActiveTracks[i].promote(); |
| 3077 | if (t == 0) continue; |
| 3078 | Track* track = t.get(); |
| 3079 | ALOG_ASSERT(track->isFastTrack() && track->isStopped()); |
| 3080 | track->reset(); |
| 3081 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3082 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3083 | // remove all the tracks that need to be... |
| 3084 | count = tracksToRemove->size(); |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 3085 | if (CC_UNLIKELY(count)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3086 | for (size_t i=0 ; i<count ; i++) { |
| 3087 | const sp<Track>& track = tracksToRemove->itemAt(i); |
| 3088 | mActiveTracks.remove(track); |
| 3089 | if (track->mainBuffer() != mMixBuffer) { |
| 3090 | chain = getEffectChain_l(track->sessionId()); |
| 3091 | if (chain != 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3092 | 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] | 3093 | chain->decActiveTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3094 | } |
| 3095 | } |
| 3096 | if (track->isTerminated()) { |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 3097 | removeTrack_l(track); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3098 | } |
| 3099 | } |
| 3100 | } |
| 3101 | |
| 3102 | // mix buffer must be cleared if all tracks are connected to an |
| 3103 | // effect chain as in this case the mixer will not write to |
| 3104 | // mix buffer and track effects will accumulate into it |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3105 | if ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || (mixedTracks == 0 && fastTracks > 0)) { |
| 3106 | // FIXME as a performance optimization, should remember previous zero status |
| 3107 | memset(mMixBuffer, 0, mNormalFrameCount * mChannelCount * sizeof(int16_t)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3108 | } |
| 3109 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3110 | // if any fast tracks, then status is ready |
Glenn Kasten | 8102804 | 2012-04-30 18:15:12 -0700 | [diff] [blame] | 3111 | mMixerStatusIgnoringFastTracks = mixerStatus; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3112 | if (fastTracks > 0) { |
| 3113 | mixerStatus = MIXER_TRACKS_READY; |
| 3114 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3115 | return mixerStatus; |
| 3116 | } |
| 3117 | |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 3118 | /* |
| 3119 | The derived values that are cached: |
| 3120 | - mixBufferSize from frame count * frame size |
| 3121 | - activeSleepTime from activeSleepTimeUs() |
| 3122 | - idleSleepTime from idleSleepTimeUs() |
| 3123 | - standbyDelay from mActiveSleepTimeUs (DIRECT only) |
| 3124 | - maxPeriod from frame count and sample rate (MIXER only) |
| 3125 | |
| 3126 | The parameters that affect these derived values are: |
| 3127 | - frame count |
| 3128 | - frame size |
| 3129 | - sample rate |
| 3130 | - device type: A2DP or not |
| 3131 | - device latency |
| 3132 | - format: PCM or not |
| 3133 | - active sleep time |
| 3134 | - idle sleep time |
| 3135 | */ |
| 3136 | |
| 3137 | void AudioFlinger::PlaybackThread::cacheParameters_l() |
| 3138 | { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3139 | mixBufferSize = mNormalFrameCount * mFrameSize; |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 3140 | activeSleepTime = activeSleepTimeUs(); |
| 3141 | idleSleepTime = idleSleepTimeUs(); |
| 3142 | } |
| 3143 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 3144 | void AudioFlinger::MixerThread::invalidateTracks(audio_stream_type_t streamType) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3145 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3146 | ALOGV ("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3147 | this, streamType, mTracks.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3148 | Mutex::Autolock _l(mLock); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3149 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3150 | size_t size = mTracks.size(); |
| 3151 | for (size_t i = 0; i < size; i++) { |
| 3152 | sp<Track> t = mTracks[i]; |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 3153 | if (t->streamType() == streamType) { |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3154 | android_atomic_or(CBLK_INVALID_ON, &t->mCblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3155 | t->mCblk->cv.signal(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3156 | } |
| 3157 | } |
| 3158 | } |
| 3159 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3160 | // getTrackName_l() must be called with ThreadBase::mLock held |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 3161 | int AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3162 | { |
Jean-Michel Trivi | 9bd2322 | 2012-04-16 13:43:48 -0700 | [diff] [blame] | 3163 | return mAudioMixer->getTrackName(channelMask); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3164 | } |
| 3165 | |
| 3166 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 3167 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) |
| 3168 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3169 | ALOGV("remove track (%d) and delete from mixer", name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3170 | mAudioMixer->deleteTrackName(name); |
| 3171 | } |
| 3172 | |
| 3173 | // checkForNewParameters_l() must be called with ThreadBase::mLock held |
| 3174 | bool AudioFlinger::MixerThread::checkForNewParameters_l() |
| 3175 | { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3176 | // if !&IDLE, holds the FastMixer state to restore after new parameters processed |
| 3177 | FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3178 | bool reconfig = false; |
| 3179 | |
| 3180 | while (!mNewParameters.isEmpty()) { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3181 | |
| 3182 | if (mFastMixer != NULL) { |
| 3183 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3184 | FastMixerState *state = sq->begin(); |
| 3185 | if (!(state->mCommand & FastMixerState::IDLE)) { |
| 3186 | previousCommand = state->mCommand; |
| 3187 | state->mCommand = FastMixerState::HOT_IDLE; |
| 3188 | sq->end(); |
| 3189 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 3190 | } else { |
| 3191 | sq->end(false /*didModify*/); |
| 3192 | } |
| 3193 | } |
| 3194 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3195 | status_t status = NO_ERROR; |
| 3196 | String8 keyValuePair = mNewParameters[0]; |
| 3197 | AudioParameter param = AudioParameter(keyValuePair); |
| 3198 | int value; |
| 3199 | |
| 3200 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 3201 | reconfig = true; |
| 3202 | } |
| 3203 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 3204 | if ((audio_format_t) value != AUDIO_FORMAT_PCM_16_BIT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3205 | status = BAD_VALUE; |
| 3206 | } else { |
| 3207 | reconfig = true; |
| 3208 | } |
| 3209 | } |
| 3210 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3211 | if (value != AUDIO_CHANNEL_OUT_STEREO) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3212 | status = BAD_VALUE; |
| 3213 | } else { |
| 3214 | reconfig = true; |
| 3215 | } |
| 3216 | } |
| 3217 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 3218 | // 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] | 3219 | // size depends on frame count and correct behavior would not be guaranteed |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3220 | // if frame count is changed after track creation |
| 3221 | if (!mTracks.isEmpty()) { |
| 3222 | status = INVALID_OPERATION; |
| 3223 | } else { |
| 3224 | reconfig = true; |
| 3225 | } |
| 3226 | } |
| 3227 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 3228 | #ifdef ADD_BATTERY_DATA |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3229 | // when changing the audio output device, call addBatteryData to notify |
| 3230 | // the change |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 3231 | if ((int)mDevice != value) { |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3232 | uint32_t params = 0; |
| 3233 | // check whether speaker is on |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3234 | if (value & AUDIO_DEVICE_OUT_SPEAKER) { |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3235 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
| 3236 | } |
| 3237 | |
| 3238 | int deviceWithoutSpeaker |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3239 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3240 | // check if any other device (except speaker) is on |
| 3241 | if (value & deviceWithoutSpeaker ) { |
| 3242 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 3243 | } |
| 3244 | |
| 3245 | if (params != 0) { |
| 3246 | addBatteryData(params); |
| 3247 | } |
| 3248 | } |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 3249 | #endif |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3250 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3251 | // forward device change to effects that have requested to be |
| 3252 | // aware of attached audio device. |
| 3253 | mDevice = (uint32_t)value; |
| 3254 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 3255 | mEffectChains[i]->setDevice_l(mDevice); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3256 | } |
| 3257 | } |
| 3258 | |
| 3259 | if (status == NO_ERROR) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 3260 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3261 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3262 | if (!mStandby && status == INVALID_OPERATION) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 3263 | mOutput->stream->common.standby(&mOutput->stream->common); |
| 3264 | mStandby = true; |
| 3265 | mBytesWritten = 0; |
| 3266 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3267 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3268 | } |
| 3269 | if (status == NO_ERROR && reconfig) { |
| 3270 | delete mAudioMixer; |
Glenn Kasten | e9dd017 | 2012-01-27 18:08:45 -0800 | [diff] [blame] | 3271 | // for safety in case readOutputParameters() accesses mAudioMixer (it doesn't) |
| 3272 | mAudioMixer = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3273 | readOutputParameters(); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3274 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3275 | for (size_t i = 0; i < mTracks.size() ; i++) { |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 3276 | int name = getTrackName_l((audio_channel_mask_t)mTracks[i]->mChannelMask); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3277 | if (name < 0) break; |
| 3278 | mTracks[i]->mName = name; |
| 3279 | // limit track sample rate to 2 x new output sample rate |
| 3280 | if (mTracks[i]->mCblk->sampleRate > 2 * sampleRate()) { |
| 3281 | mTracks[i]->mCblk->sampleRate = 2 * sampleRate(); |
| 3282 | } |
| 3283 | } |
| 3284 | sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); |
| 3285 | } |
| 3286 | } |
| 3287 | |
| 3288 | mNewParameters.removeAt(0); |
| 3289 | |
| 3290 | mParamStatus = status; |
| 3291 | mParamCond.signal(); |
Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 3292 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() |
| 3293 | // 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] | 3294 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3295 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3296 | |
| 3297 | if (!(previousCommand & FastMixerState::IDLE)) { |
| 3298 | ALOG_ASSERT(mFastMixer != NULL); |
| 3299 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3300 | FastMixerState *state = sq->begin(); |
| 3301 | ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); |
| 3302 | state->mCommand = previousCommand; |
| 3303 | sq->end(); |
| 3304 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3305 | } |
| 3306 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3307 | return reconfig; |
| 3308 | } |
| 3309 | |
| 3310 | status_t AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) |
| 3311 | { |
| 3312 | const size_t SIZE = 256; |
| 3313 | char buffer[SIZE]; |
| 3314 | String8 result; |
| 3315 | |
| 3316 | PlaybackThread::dumpInternals(fd, args); |
| 3317 | |
| 3318 | snprintf(buffer, SIZE, "AudioMixer tracks: %08x\n", mAudioMixer->trackNames()); |
| 3319 | result.append(buffer); |
| 3320 | write(fd, result.string(), result.size()); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3321 | |
| 3322 | // Make a non-atomic copy of fast mixer dump state so it won't change underneath us |
| 3323 | FastMixerDumpState copy = mFastMixerDumpState; |
| 3324 | copy.dump(fd); |
| 3325 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3326 | return NO_ERROR; |
| 3327 | } |
| 3328 | |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 3329 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3330 | { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3331 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3332 | } |
| 3333 | |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 3334 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 3335 | { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3336 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 3337 | } |
| 3338 | |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 3339 | void AudioFlinger::MixerThread::cacheParameters_l() |
| 3340 | { |
| 3341 | PlaybackThread::cacheParameters_l(); |
| 3342 | |
| 3343 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 3344 | // Should be reduced to 2x after the vendor fixes the driver issue |
| 3345 | // increase threshold again due to low power audio mode. The way this warning |
| 3346 | // threshold is calculated and its usefulness should be reconsidered anyway. |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3347 | maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 3348 | } |
| 3349 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3350 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 3351 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
| 3352 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device) |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 3353 | : PlaybackThread(audioFlinger, output, id, device, DIRECT) |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 3354 | // mLeftVolFloat, mRightVolFloat |
| 3355 | // mLeftVolShort, mRightVolShort |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3356 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3357 | } |
| 3358 | |
| 3359 | AudioFlinger::DirectOutputThread::~DirectOutputThread() |
| 3360 | { |
| 3361 | } |
| 3362 | |
Glenn Kasten | 1465f0c | 2012-03-06 11:23:32 -0800 | [diff] [blame] | 3363 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( |
| 3364 | Vector< sp<Track> > *tracksToRemove |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3365 | ) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3366 | { |
Glenn Kasten | 1465f0c | 2012-03-06 11:23:32 -0800 | [diff] [blame] | 3367 | sp<Track> trackToRemove; |
| 3368 | |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 3369 | mixer_state mixerStatus = MIXER_IDLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3370 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3371 | // find out which tracks need to be processed |
| 3372 | if (mActiveTracks.size() != 0) { |
| 3373 | sp<Track> t = mActiveTracks[0].promote(); |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 3374 | // The track died recently |
| 3375 | if (t == 0) return MIXER_IDLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3376 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3377 | Track* const track = t.get(); |
| 3378 | audio_track_cblk_t* cblk = track->cblk(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3379 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3380 | // The first time a track is added we wait |
| 3381 | // for all its buffers to be filled before processing it |
| 3382 | if (cblk->framesReady() && track->isReady() && |
| 3383 | !track->isPaused() && !track->isTerminated()) |
| 3384 | { |
| 3385 | //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] | 3386 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3387 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 3388 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 3389 | mLeftVolFloat = mRightVolFloat = 0; |
| 3390 | mLeftVolShort = mRightVolShort = 0; |
| 3391 | if (track->mState == TrackBase::RESUMING) { |
| 3392 | track->mState = TrackBase::ACTIVE; |
| 3393 | rampVolume = true; |
| 3394 | } |
| 3395 | } else if (cblk->server != 0) { |
| 3396 | // If the track is stopped before the first frame was mixed, |
| 3397 | // do not apply ramp |
| 3398 | rampVolume = true; |
| 3399 | } |
| 3400 | // compute volume for this track |
| 3401 | float left, right; |
| 3402 | if (track->isMuted() || mMasterMute || track->isPausing() || |
| 3403 | mStreamTypes[track->streamType()].mute) { |
| 3404 | left = right = 0; |
| 3405 | if (track->isPausing()) { |
| 3406 | track->setPaused(); |
| 3407 | } |
| 3408 | } else { |
| 3409 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 3410 | float v = mMasterVolume * typeVolume; |
| 3411 | uint32_t vlr = cblk->getVolumeLR(); |
| 3412 | float v_clamped = v * (vlr & 0xFFFF); |
| 3413 | if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN; |
| 3414 | left = v_clamped/MAX_GAIN; |
| 3415 | v_clamped = v * (vlr >> 16); |
| 3416 | if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN; |
| 3417 | right = v_clamped/MAX_GAIN; |
| 3418 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3419 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3420 | if (left != mLeftVolFloat || right != mRightVolFloat) { |
| 3421 | mLeftVolFloat = left; |
| 3422 | mRightVolFloat = right; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3423 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3424 | // If audio HAL implements volume control, |
| 3425 | // force software volume to nominal value |
| 3426 | if (mOutput->stream->set_volume(mOutput->stream, left, right) == NO_ERROR) { |
| 3427 | left = 1.0f; |
| 3428 | right = 1.0f; |
| 3429 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3430 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3431 | // Convert volumes from float to 8.24 |
| 3432 | uint32_t vl = (uint32_t)(left * (1 << 24)); |
| 3433 | uint32_t vr = (uint32_t)(right * (1 << 24)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3434 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3435 | // Delegate volume control to effect in track effect chain if needed |
| 3436 | // only one effect chain can be present on DirectOutputThread, so if |
| 3437 | // there is one, the track is connected to it |
| 3438 | if (!mEffectChains.isEmpty()) { |
| 3439 | // Do not ramp volume if volume is controlled by effect |
| 3440 | if (mEffectChains[0]->setVolume_l(&vl, &vr)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3441 | rampVolume = false; |
| 3442 | } |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3443 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3444 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3445 | // Convert volumes from 8.24 to 4.12 format |
| 3446 | uint32_t v_clamped = (vl + (1 << 11)) >> 12; |
| 3447 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 3448 | leftVol = (uint16_t)v_clamped; |
| 3449 | v_clamped = (vr + (1 << 11)) >> 12; |
| 3450 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 3451 | rightVol = (uint16_t)v_clamped; |
| 3452 | } else { |
| 3453 | leftVol = mLeftVolShort; |
| 3454 | rightVol = mRightVolShort; |
| 3455 | rampVolume = false; |
| 3456 | } |
| 3457 | |
| 3458 | // reset retry count |
| 3459 | track->mRetryCount = kMaxTrackRetriesDirect; |
Glenn Kasten | b071e9b | 2012-03-07 17:05:59 -0800 | [diff] [blame] | 3460 | mActiveTrack = t; |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 3461 | mixerStatus = MIXER_TRACKS_READY; |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3462 | } else { |
| 3463 | //ALOGV("track %d u=%08x, s=%08x [NOT READY]", track->name(), cblk->user, cblk->server); |
| 3464 | if (track->isStopped()) { |
| 3465 | track->reset(); |
| 3466 | } |
| 3467 | if (track->isTerminated() || track->isStopped() || track->isPaused()) { |
| 3468 | // We have consumed all the buffers of this track. |
| 3469 | // Remove it from the list of active tracks. |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 3470 | // TODO: implement behavior for compressed audio |
| 3471 | size_t audioHALFrames = |
| 3472 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
| 3473 | size_t framesWritten = |
| 3474 | mBytesWritten / audio_stream_frame_size(&mOutput->stream->common); |
| 3475 | if (track->presentationComplete(framesWritten, audioHALFrames)) { |
| 3476 | trackToRemove = track; |
| 3477 | } |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3478 | } else { |
| 3479 | // No buffers for this track. Give it a few chances to |
| 3480 | // fill a buffer, then remove it from active list. |
| 3481 | if (--(track->mRetryCount) <= 0) { |
| 3482 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); |
| 3483 | trackToRemove = track; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3484 | } else { |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 3485 | mixerStatus = MIXER_TRACKS_ENABLED; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3486 | } |
| 3487 | } |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3488 | } |
| 3489 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3490 | |
Glenn Kasten | 1465f0c | 2012-03-06 11:23:32 -0800 | [diff] [blame] | 3491 | // FIXME merge this with similar code for removing multiple tracks |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3492 | // remove all the tracks that need to be... |
| 3493 | if (CC_UNLIKELY(trackToRemove != 0)) { |
Glenn Kasten | 1465f0c | 2012-03-06 11:23:32 -0800 | [diff] [blame] | 3494 | tracksToRemove->add(trackToRemove); |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3495 | mActiveTracks.remove(trackToRemove); |
| 3496 | if (!mEffectChains.isEmpty()) { |
Glenn Kasten | 5798d4e | 2012-03-08 12:18:35 -0800 | [diff] [blame] | 3497 | 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] | 3498 | trackToRemove->sessionId()); |
| 3499 | mEffectChains[0]->decActiveTrackCnt(); |
| 3500 | } |
| 3501 | if (trackToRemove->isTerminated()) { |
| 3502 | removeTrack_l(trackToRemove); |
| 3503 | } |
| 3504 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3505 | |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 3506 | return mixerStatus; |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3507 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3508 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3509 | void AudioFlinger::DirectOutputThread::threadLoop_mix() |
| 3510 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3511 | AudioBufferProvider::Buffer buffer; |
| 3512 | size_t frameCount = mFrameCount; |
| 3513 | int8_t *curBuf = (int8_t *)mMixBuffer; |
| 3514 | // output audio to hardware |
| 3515 | while (frameCount) { |
| 3516 | buffer.frameCount = frameCount; |
Glenn Kasten | b071e9b | 2012-03-07 17:05:59 -0800 | [diff] [blame] | 3517 | mActiveTrack->getNextBuffer(&buffer); |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3518 | if (CC_UNLIKELY(buffer.raw == NULL)) { |
| 3519 | memset(curBuf, 0, frameCount * mFrameSize); |
| 3520 | break; |
| 3521 | } |
| 3522 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); |
| 3523 | frameCount -= buffer.frameCount; |
| 3524 | curBuf += buffer.frameCount * mFrameSize; |
Glenn Kasten | b071e9b | 2012-03-07 17:05:59 -0800 | [diff] [blame] | 3525 | mActiveTrack->releaseBuffer(&buffer); |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3526 | } |
| 3527 | sleepTime = 0; |
| 3528 | standbyTime = systemTime() + standbyDelay; |
Glenn Kasten | b071e9b | 2012-03-07 17:05:59 -0800 | [diff] [blame] | 3529 | mActiveTrack.clear(); |
Glenn Kasten | 73f4bc3 | 2012-03-09 12:08:48 -0800 | [diff] [blame] | 3530 | |
| 3531 | // apply volume |
| 3532 | |
| 3533 | // Do not apply volume on compressed audio |
| 3534 | if (!audio_is_linear_pcm(mFormat)) { |
| 3535 | return; |
| 3536 | } |
| 3537 | |
| 3538 | // convert to signed 16 bit before volume calculation |
| 3539 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT) { |
| 3540 | size_t count = mFrameCount * mChannelCount; |
| 3541 | uint8_t *src = (uint8_t *)mMixBuffer + count-1; |
| 3542 | int16_t *dst = mMixBuffer + count-1; |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 3543 | while (count--) { |
Glenn Kasten | 73f4bc3 | 2012-03-09 12:08:48 -0800 | [diff] [blame] | 3544 | *dst-- = (int16_t)(*src--^0x80) << 8; |
| 3545 | } |
| 3546 | } |
| 3547 | |
| 3548 | frameCount = mFrameCount; |
| 3549 | int16_t *out = mMixBuffer; |
| 3550 | if (rampVolume) { |
| 3551 | if (mChannelCount == 1) { |
| 3552 | int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16; |
| 3553 | int32_t vlInc = d / (int32_t)frameCount; |
| 3554 | int32_t vl = ((int32_t)mLeftVolShort << 16); |
| 3555 | do { |
| 3556 | out[0] = clamp16(mul(out[0], vl >> 16) >> 12); |
| 3557 | out++; |
| 3558 | vl += vlInc; |
| 3559 | } while (--frameCount); |
| 3560 | |
| 3561 | } else { |
| 3562 | int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16; |
| 3563 | int32_t vlInc = d / (int32_t)frameCount; |
| 3564 | d = ((int32_t)rightVol - (int32_t)mRightVolShort) << 16; |
| 3565 | int32_t vrInc = d / (int32_t)frameCount; |
| 3566 | int32_t vl = ((int32_t)mLeftVolShort << 16); |
| 3567 | int32_t vr = ((int32_t)mRightVolShort << 16); |
| 3568 | do { |
| 3569 | out[0] = clamp16(mul(out[0], vl >> 16) >> 12); |
| 3570 | out[1] = clamp16(mul(out[1], vr >> 16) >> 12); |
| 3571 | out += 2; |
| 3572 | vl += vlInc; |
| 3573 | vr += vrInc; |
| 3574 | } while (--frameCount); |
| 3575 | } |
| 3576 | } else { |
| 3577 | if (mChannelCount == 1) { |
| 3578 | do { |
| 3579 | out[0] = clamp16(mul(out[0], leftVol) >> 12); |
| 3580 | out++; |
| 3581 | } while (--frameCount); |
| 3582 | } else { |
| 3583 | do { |
| 3584 | out[0] = clamp16(mul(out[0], leftVol) >> 12); |
| 3585 | out[1] = clamp16(mul(out[1], rightVol) >> 12); |
| 3586 | out += 2; |
| 3587 | } while (--frameCount); |
| 3588 | } |
| 3589 | } |
| 3590 | |
| 3591 | // convert back to unsigned 8 bit after volume calculation |
| 3592 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT) { |
| 3593 | size_t count = mFrameCount * mChannelCount; |
| 3594 | int16_t *src = mMixBuffer; |
| 3595 | uint8_t *dst = (uint8_t *)mMixBuffer; |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 3596 | while (count--) { |
Glenn Kasten | 73f4bc3 | 2012-03-09 12:08:48 -0800 | [diff] [blame] | 3597 | *dst++ = (uint8_t)(((int32_t)*src++ + (1<<7)) >> 8)^0x80; |
| 3598 | } |
| 3599 | } |
| 3600 | |
| 3601 | mLeftVolShort = leftVol; |
| 3602 | mRightVolShort = rightVol; |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3603 | } |
| 3604 | |
| 3605 | void AudioFlinger::DirectOutputThread::threadLoop_sleepTime() |
| 3606 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3607 | if (sleepTime == 0) { |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 3608 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3609 | sleepTime = activeSleepTime; |
| 3610 | } else { |
| 3611 | sleepTime = idleSleepTime; |
| 3612 | } |
| 3613 | } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3614 | memset(mMixBuffer, 0, mFrameCount * mFrameSize); |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3615 | sleepTime = 0; |
| 3616 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3617 | } |
| 3618 | |
| 3619 | // getTrackName_l() must be called with ThreadBase::mLock held |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 3620 | int AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3621 | { |
| 3622 | return 0; |
| 3623 | } |
| 3624 | |
| 3625 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 3626 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name) |
| 3627 | { |
| 3628 | } |
| 3629 | |
| 3630 | // checkForNewParameters_l() must be called with ThreadBase::mLock held |
| 3631 | bool AudioFlinger::DirectOutputThread::checkForNewParameters_l() |
| 3632 | { |
| 3633 | bool reconfig = false; |
| 3634 | |
| 3635 | while (!mNewParameters.isEmpty()) { |
| 3636 | status_t status = NO_ERROR; |
| 3637 | String8 keyValuePair = mNewParameters[0]; |
| 3638 | AudioParameter param = AudioParameter(keyValuePair); |
| 3639 | int value; |
| 3640 | |
| 3641 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 3642 | // do not accept frame count changes if tracks are open as the track buffer |
| 3643 | // size depends on frame count and correct behavior would not be garantied |
| 3644 | // if frame count is changed after track creation |
| 3645 | if (!mTracks.isEmpty()) { |
| 3646 | status = INVALID_OPERATION; |
| 3647 | } else { |
| 3648 | reconfig = true; |
| 3649 | } |
| 3650 | } |
| 3651 | if (status == NO_ERROR) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 3652 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3653 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3654 | if (!mStandby && status == INVALID_OPERATION) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 3655 | mOutput->stream->common.standby(&mOutput->stream->common); |
| 3656 | mStandby = true; |
| 3657 | mBytesWritten = 0; |
| 3658 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3659 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3660 | } |
| 3661 | if (status == NO_ERROR && reconfig) { |
| 3662 | readOutputParameters(); |
| 3663 | sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); |
| 3664 | } |
| 3665 | } |
| 3666 | |
| 3667 | mNewParameters.removeAt(0); |
| 3668 | |
| 3669 | mParamStatus = status; |
| 3670 | mParamCond.signal(); |
Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 3671 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() |
| 3672 | // 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] | 3673 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3674 | } |
| 3675 | return reconfig; |
| 3676 | } |
| 3677 | |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 3678 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3679 | { |
| 3680 | uint32_t time; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3681 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 162b40b | 2011-12-05 09:47:19 -0800 | [diff] [blame] | 3682 | time = PlaybackThread::activeSleepTimeUs(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3683 | } else { |
| 3684 | time = 10000; |
| 3685 | } |
| 3686 | return time; |
| 3687 | } |
| 3688 | |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 3689 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3690 | { |
| 3691 | uint32_t time; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3692 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 60e1824 | 2010-07-29 06:50:24 -0700 | [diff] [blame] | 3693 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3694 | } else { |
| 3695 | time = 10000; |
| 3696 | } |
| 3697 | return time; |
| 3698 | } |
| 3699 | |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 3700 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 3701 | { |
| 3702 | uint32_t time; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3703 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 3704 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 3705 | } else { |
| 3706 | time = 10000; |
| 3707 | } |
| 3708 | return time; |
| 3709 | } |
| 3710 | |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 3711 | void AudioFlinger::DirectOutputThread::cacheParameters_l() |
| 3712 | { |
| 3713 | PlaybackThread::cacheParameters_l(); |
| 3714 | |
| 3715 | // use shorter standby delay as on normal output to release |
| 3716 | // hardware resources as soon as possible |
| 3717 | standbyDelay = microseconds(activeSleepTime*2); |
| 3718 | } |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 3719 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3720 | // ---------------------------------------------------------------------------- |
| 3721 | |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 3722 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 3723 | AudioFlinger::MixerThread* mainThread, audio_io_handle_t id) |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 3724 | : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->device(), DUPLICATING), |
| 3725 | mWaitTimeMs(UINT_MAX) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3726 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3727 | addOutputTrack(mainThread); |
| 3728 | } |
| 3729 | |
| 3730 | AudioFlinger::DuplicatingThread::~DuplicatingThread() |
| 3731 | { |
| 3732 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 3733 | mOutputTracks[i]->destroy(); |
| 3734 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3735 | } |
| 3736 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3737 | void AudioFlinger::DuplicatingThread::threadLoop_mix() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3738 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3739 | // mix buffers... |
| 3740 | if (outputsReady(outputTracks)) { |
| 3741 | mAudioMixer->process(AudioBufferProvider::kInvalidPTS); |
| 3742 | } else { |
| 3743 | memset(mMixBuffer, 0, mixBufferSize); |
| 3744 | } |
| 3745 | sleepTime = 0; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3746 | writeFrames = mNormalFrameCount; |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3747 | } |
| 3748 | |
| 3749 | void AudioFlinger::DuplicatingThread::threadLoop_sleepTime() |
| 3750 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3751 | if (sleepTime == 0) { |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 3752 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3753 | sleepTime = activeSleepTime; |
| 3754 | } else { |
| 3755 | sleepTime = idleSleepTime; |
| 3756 | } |
| 3757 | } else if (mBytesWritten != 0) { |
| 3758 | // flush remaining overflow buffers in output tracks |
| 3759 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 3760 | if (outputTracks[i]->isActive()) { |
| 3761 | sleepTime = 0; |
| 3762 | writeFrames = 0; |
| 3763 | memset(mMixBuffer, 0, mixBufferSize); |
| 3764 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3765 | } |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3766 | } |
| 3767 | } |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3768 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3769 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3770 | void AudioFlinger::DuplicatingThread::threadLoop_write() |
| 3771 | { |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 3772 | standbyTime = systemTime() + standbyDelay; |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3773 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 3774 | outputTracks[i]->write(mMixBuffer, writeFrames); |
| 3775 | } |
| 3776 | mBytesWritten += mixBufferSize; |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3777 | } |
Glenn Kasten | 688a640 | 2012-02-29 07:57:06 -0800 | [diff] [blame] | 3778 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3779 | void AudioFlinger::DuplicatingThread::threadLoop_standby() |
| 3780 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3781 | // DuplicatingThread implements standby by stopping all tracks |
| 3782 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 3783 | outputTracks[i]->stop(); |
| 3784 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3785 | } |
| 3786 | |
Glenn Kasten | fa26a85 | 2012-03-06 11:28:04 -0800 | [diff] [blame] | 3787 | void AudioFlinger::DuplicatingThread::saveOutputTracks() |
| 3788 | { |
| 3789 | outputTracks = mOutputTracks; |
| 3790 | } |
| 3791 | |
| 3792 | void AudioFlinger::DuplicatingThread::clearOutputTracks() |
| 3793 | { |
| 3794 | outputTracks.clear(); |
| 3795 | } |
| 3796 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3797 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) |
| 3798 | { |
Glenn Kasten | b6b7406 | 2012-02-24 14:12:20 -0800 | [diff] [blame] | 3799 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 3800 | // FIXME explain this formula |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3801 | int frameCount = (3 * mNormalFrameCount * mSampleRate) / thread->sampleRate(); |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 3802 | OutputTrack *outputTrack = new OutputTrack(thread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3803 | this, |
| 3804 | mSampleRate, |
| 3805 | mFormat, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3806 | mChannelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3807 | frameCount); |
| 3808 | if (outputTrack->cblk() != NULL) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3809 | thread->setStreamVolume(AUDIO_STREAM_CNT, 1.0f); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3810 | mOutputTracks.add(outputTrack); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3811 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread); |
Glenn Kasten | 438b036 | 2012-03-06 11:24:48 -0800 | [diff] [blame] | 3812 | updateWaitTime_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3813 | } |
| 3814 | } |
| 3815 | |
| 3816 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) |
| 3817 | { |
| 3818 | Mutex::Autolock _l(mLock); |
| 3819 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 3820 | if (mOutputTracks[i]->thread() == thread) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3821 | mOutputTracks[i]->destroy(); |
| 3822 | mOutputTracks.removeAt(i); |
Glenn Kasten | 438b036 | 2012-03-06 11:24:48 -0800 | [diff] [blame] | 3823 | updateWaitTime_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3824 | return; |
| 3825 | } |
| 3826 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3827 | ALOGV("removeOutputTrack(): unkonwn thread: %p", thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3828 | } |
| 3829 | |
Glenn Kasten | 438b036 | 2012-03-06 11:24:48 -0800 | [diff] [blame] | 3830 | // caller must hold mLock |
| 3831 | void AudioFlinger::DuplicatingThread::updateWaitTime_l() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3832 | { |
| 3833 | mWaitTimeMs = UINT_MAX; |
| 3834 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 3835 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 3836 | if (strong != 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3837 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); |
| 3838 | if (waitTimeMs < mWaitTimeMs) { |
| 3839 | mWaitTimeMs = waitTimeMs; |
| 3840 | } |
| 3841 | } |
| 3842 | } |
| 3843 | } |
| 3844 | |
| 3845 | |
Glenn Kasten | 02fe1bf | 2012-02-24 15:42:17 -0800 | [diff] [blame] | 3846 | bool AudioFlinger::DuplicatingThread::outputsReady(const SortedVector< sp<OutputTrack> > &outputTracks) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3847 | { |
| 3848 | for (size_t i = 0; i < outputTracks.size(); i++) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 3849 | sp<ThreadBase> thread = outputTracks[i]->thread().promote(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3850 | if (thread == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3851 | 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] | 3852 | return false; |
| 3853 | } |
| 3854 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3855 | if (playbackThread->standby() && !playbackThread->isSuspended()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3856 | 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] | 3857 | return false; |
| 3858 | } |
| 3859 | } |
| 3860 | return true; |
| 3861 | } |
| 3862 | |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 3863 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3864 | { |
| 3865 | return (mWaitTimeMs * 1000) / 2; |
| 3866 | } |
| 3867 | |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 3868 | void AudioFlinger::DuplicatingThread::cacheParameters_l() |
| 3869 | { |
| 3870 | // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first |
| 3871 | updateWaitTime_l(); |
| 3872 | |
| 3873 | MixerThread::cacheParameters_l(); |
| 3874 | } |
| 3875 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3876 | // ---------------------------------------------------------------------------- |
| 3877 | |
| 3878 | // TrackBase constructor must be called with AudioFlinger::mLock held |
| 3879 | AudioFlinger::ThreadBase::TrackBase::TrackBase( |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 3880 | ThreadBase *thread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3881 | const sp<Client>& client, |
| 3882 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 3883 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3884 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3885 | int frameCount, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3886 | const sp<IMemory>& sharedBuffer, |
| 3887 | int sessionId) |
| 3888 | : RefBase(), |
| 3889 | mThread(thread), |
| 3890 | mClient(client), |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 3891 | mCblk(NULL), |
| 3892 | // mBuffer |
| 3893 | // mBufferEnd |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3894 | mFrameCount(0), |
| 3895 | mState(IDLE), |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3896 | mSampleRate(sampleRate), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3897 | mFormat(format), |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame] | 3898 | mStepServerFailed(false), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3899 | mSessionId(sessionId) |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 3900 | // mChannelCount |
| 3901 | // mChannelMask |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3902 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3903 | ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3904 | |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3905 | // ALOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 3906 | size_t size = sizeof(audio_track_cblk_t); |
| 3907 | uint8_t channelCount = popcount(channelMask); |
| 3908 | size_t bufferSize = frameCount*channelCount*sizeof(int16_t); |
| 3909 | if (sharedBuffer == 0) { |
| 3910 | size += bufferSize; |
| 3911 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3912 | |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 3913 | if (client != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3914 | mCblkMemory = client->heap()->allocate(size); |
| 3915 | if (mCblkMemory != 0) { |
| 3916 | mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer()); |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 3917 | if (mCblk != NULL) { // construct the shared structure in-place. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3918 | new(mCblk) audio_track_cblk_t(); |
| 3919 | // clear all buffers |
| 3920 | mCblk->frameCount = frameCount; |
| 3921 | mCblk->sampleRate = sampleRate; |
Marco Nelissen | a1472d9 | 2012-03-30 14:36:54 -0700 | [diff] [blame] | 3922 | // uncomment the following lines to quickly test 32-bit wraparound |
| 3923 | // mCblk->user = 0xffff0000; |
| 3924 | // mCblk->server = 0xffff0000; |
| 3925 | // mCblk->userBase = 0xffff0000; |
| 3926 | // mCblk->serverBase = 0xffff0000; |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3927 | mChannelCount = channelCount; |
| 3928 | mChannelMask = channelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3929 | if (sharedBuffer == 0) { |
| 3930 | mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 3931 | memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t)); |
| 3932 | // Force underrun condition to avoid false underrun callback until first data is |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 3933 | // written to buffer (other flags are cleared) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3934 | mCblk->flags = CBLK_UNDERRUN_ON; |
| 3935 | } else { |
| 3936 | mBuffer = sharedBuffer->pointer(); |
| 3937 | } |
| 3938 | mBufferEnd = (uint8_t *)mBuffer + bufferSize; |
| 3939 | } |
| 3940 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3941 | ALOGE("not enough memory for AudioTrack size=%u", size); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3942 | client->heap()->dump("AudioTrack"); |
| 3943 | return; |
| 3944 | } |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 3945 | } else { |
| 3946 | mCblk = (audio_track_cblk_t *)(new uint8_t[size]); |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 3947 | // construct the shared structure in-place. |
| 3948 | new(mCblk) audio_track_cblk_t(); |
| 3949 | // clear all buffers |
| 3950 | mCblk->frameCount = frameCount; |
| 3951 | mCblk->sampleRate = sampleRate; |
Marco Nelissen | a1472d9 | 2012-03-30 14:36:54 -0700 | [diff] [blame] | 3952 | // uncomment the following lines to quickly test 32-bit wraparound |
| 3953 | // mCblk->user = 0xffff0000; |
| 3954 | // mCblk->server = 0xffff0000; |
| 3955 | // mCblk->userBase = 0xffff0000; |
| 3956 | // mCblk->serverBase = 0xffff0000; |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 3957 | mChannelCount = channelCount; |
| 3958 | mChannelMask = channelMask; |
| 3959 | mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 3960 | memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t)); |
| 3961 | // Force underrun condition to avoid false underrun callback until first data is |
| 3962 | // written to buffer (other flags are cleared) |
| 3963 | mCblk->flags = CBLK_UNDERRUN_ON; |
| 3964 | mBufferEnd = (uint8_t *)mBuffer + bufferSize; |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 3965 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3966 | } |
| 3967 | |
| 3968 | AudioFlinger::ThreadBase::TrackBase::~TrackBase() |
| 3969 | { |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 3970 | if (mCblk != NULL) { |
Glenn Kasten | 1a0ae5b | 2012-02-03 10:24:48 -0800 | [diff] [blame] | 3971 | if (mClient == 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3972 | delete mCblk; |
Glenn Kasten | 1a0ae5b | 2012-02-03 10:24:48 -0800 | [diff] [blame] | 3973 | } else { |
| 3974 | mCblk->~audio_track_cblk_t(); // destroy our shared-structure. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3975 | } |
| 3976 | } |
Glenn Kasten | dbfafaf | 2012-01-25 15:27:15 -0800 | [diff] [blame] | 3977 | 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] | 3978 | if (mClient != 0) { |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 3979 | // Client destructor must run with AudioFlinger mutex locked |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3980 | Mutex::Autolock _l(mClient->audioFlinger()->mLock); |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 3981 | // If the client's reference count drops to zero, the associated destructor |
| 3982 | // must run with AudioFlinger lock held. Thus the explicit clear() rather than |
| 3983 | // relying on the automatic clear() at end of scope. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3984 | mClient.clear(); |
| 3985 | } |
| 3986 | } |
| 3987 | |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 3988 | // AudioBufferProvider interface |
| 3989 | // getNextBuffer() = 0; |
| 3990 | // 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] | 3991 | void AudioFlinger::ThreadBase::TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
| 3992 | { |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3993 | buffer->raw = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3994 | mFrameCount = buffer->frameCount; |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 3995 | // FIXME See note at getNextBuffer() |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 3996 | (void) step(); // ignore return value of step() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3997 | buffer->frameCount = 0; |
| 3998 | } |
| 3999 | |
| 4000 | bool AudioFlinger::ThreadBase::TrackBase::step() { |
| 4001 | bool result; |
| 4002 | audio_track_cblk_t* cblk = this->cblk(); |
| 4003 | |
| 4004 | result = cblk->stepServer(mFrameCount); |
| 4005 | if (!result) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4006 | ALOGV("stepServer failed acquiring cblk mutex"); |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame] | 4007 | mStepServerFailed = true; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4008 | } |
| 4009 | return result; |
| 4010 | } |
| 4011 | |
| 4012 | void AudioFlinger::ThreadBase::TrackBase::reset() { |
| 4013 | audio_track_cblk_t* cblk = this->cblk(); |
| 4014 | |
| 4015 | cblk->user = 0; |
| 4016 | cblk->server = 0; |
| 4017 | cblk->userBase = 0; |
| 4018 | cblk->serverBase = 0; |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame] | 4019 | mStepServerFailed = false; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4020 | ALOGV("TrackBase::reset"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4021 | } |
| 4022 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4023 | int AudioFlinger::ThreadBase::TrackBase::sampleRate() const { |
| 4024 | return (int)mCblk->sampleRate; |
| 4025 | } |
| 4026 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4027 | void* AudioFlinger::ThreadBase::TrackBase::getBuffer(uint32_t offset, uint32_t frames) const { |
| 4028 | audio_track_cblk_t* cblk = this->cblk(); |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 4029 | size_t frameSize = cblk->frameSize; |
| 4030 | int8_t *bufferStart = (int8_t *)mBuffer + (offset-cblk->serverBase)*frameSize; |
| 4031 | int8_t *bufferEnd = bufferStart + frames * frameSize; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4032 | |
| 4033 | // 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] | 4034 | ALOG_ASSERT(!(bufferStart < mBuffer || bufferStart > bufferEnd || bufferEnd > mBufferEnd), |
| 4035 | "TrackBase::getBuffer buffer out of range:\n" |
| 4036 | " start: %p, end %p , mBuffer %p mBufferEnd %p\n" |
| 4037 | " server %u, serverBase %u, user %u, userBase %u, frameSize %d", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4038 | bufferStart, bufferEnd, mBuffer, mBufferEnd, |
Jean-Michel Trivi | acb86cc | 2012-04-16 12:43:57 -0700 | [diff] [blame] | 4039 | cblk->server, cblk->serverBase, cblk->user, cblk->userBase, frameSize); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4040 | |
| 4041 | return bufferStart; |
| 4042 | } |
| 4043 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 4044 | status_t AudioFlinger::ThreadBase::TrackBase::setSyncEvent(const sp<SyncEvent>& event) |
| 4045 | { |
| 4046 | mSyncEvents.add(event); |
| 4047 | return NO_ERROR; |
| 4048 | } |
| 4049 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4050 | // ---------------------------------------------------------------------------- |
| 4051 | |
| 4052 | // Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held |
| 4053 | AudioFlinger::PlaybackThread::Track::Track( |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 4054 | PlaybackThread *thread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4055 | const sp<Client>& client, |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 4056 | audio_stream_type_t streamType, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4057 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 4058 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4059 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4060 | int frameCount, |
| 4061 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 4062 | int sessionId, |
| 4063 | IAudioFlinger::track_flags_t flags) |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame] | 4064 | : TrackBase(thread, client, sampleRate, format, channelMask, frameCount, sharedBuffer, sessionId), |
Glenn Kasten | f995901 | 2012-03-19 11:14:37 -0700 | [diff] [blame] | 4065 | mMute(false), |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4066 | mFillingUpStatus(FS_INVALID), |
Glenn Kasten | f995901 | 2012-03-19 11:14:37 -0700 | [diff] [blame] | 4067 | // mRetryCount initialized later when needed |
| 4068 | mSharedBuffer(sharedBuffer), |
| 4069 | mStreamType(streamType), |
| 4070 | mName(-1), // see note below |
| 4071 | mMainBuffer(thread->mixBuffer()), |
| 4072 | mAuxBuffer(NULL), |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 4073 | mAuxEffectId(0), mHasVolumeController(false), |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 4074 | mPresentationCompleteFrames(0), |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4075 | mFlags(flags), |
| 4076 | mFastIndex(-1), |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4077 | mObservedUnderruns(0), |
| 4078 | mUnderrunCount(0), |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4079 | mCachedVolume(1.0) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4080 | { |
| 4081 | if (mCblk != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4082 | // NOTE: audio_track_cblk_t::frameSize for 8 bit PCM data is based on a sample size of |
| 4083 | // 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] | 4084 | 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] | 4085 | if (flags & IAudioFlinger::TRACK_FAST) { |
| 4086 | mCblk->flags |= CBLK_FAST; // atomic op not needed yet |
| 4087 | ALOG_ASSERT(thread->mFastTrackAvailMask != 0); |
| 4088 | int i = __builtin_ctz(thread->mFastTrackAvailMask); |
| 4089 | ALOG_ASSERT(0 < i && i < FastMixerState::kMaxFastTracks); |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4090 | // FIXME This is too eager. We allocate a fast track index before the |
| 4091 | // fast track becomes active. Since fast tracks are a scarce resource, |
| 4092 | // this means we are potentially denying other more important fast tracks from |
| 4093 | // being created. It would be better to allocate the index dynamically. |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4094 | mFastIndex = i; |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4095 | // Read the initial underruns because this field is never cleared by the fast mixer |
| 4096 | mObservedUnderruns = thread->getFastTrackUnderruns(i) & ~1; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4097 | thread->mFastTrackAvailMask &= ~(1 << i); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4098 | } |
Glenn Kasten | f995901 | 2012-03-19 11:14:37 -0700 | [diff] [blame] | 4099 | // 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] | 4100 | mName = thread->getTrackName_l((audio_channel_mask_t)channelMask); |
Glenn Kasten | f995901 | 2012-03-19 11:14:37 -0700 | [diff] [blame] | 4101 | if (mName < 0) { |
| 4102 | ALOGE("no more track names available"); |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4103 | // FIXME bug - if sufficient fast track indices, but insufficient normal mixer names, |
| 4104 | // then we leak a fast track index. Should swap these two sections, or better yet |
| 4105 | // only allocate a normal mixer name for normal tracks. |
Glenn Kasten | f995901 | 2012-03-19 11:14:37 -0700 | [diff] [blame] | 4106 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4107 | } |
Glenn Kasten | f995901 | 2012-03-19 11:14:37 -0700 | [diff] [blame] | 4108 | ALOGV("Track constructor name %d, calling pid %d", mName, IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4109 | } |
| 4110 | |
| 4111 | AudioFlinger::PlaybackThread::Track::~Track() |
| 4112 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4113 | ALOGV("PlaybackThread::Track destructor"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4114 | sp<ThreadBase> thread = mThread.promote(); |
| 4115 | if (thread != 0) { |
| 4116 | Mutex::Autolock _l(thread->mLock); |
| 4117 | mState = TERMINATED; |
| 4118 | } |
| 4119 | } |
| 4120 | |
| 4121 | void AudioFlinger::PlaybackThread::Track::destroy() |
| 4122 | { |
| 4123 | // NOTE: destroyTrack_l() can remove a strong reference to this Track |
| 4124 | // 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] | 4125 | // destructor is called. As the destructor needs to lock mLock, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4126 | // we must acquire a strong reference on this Track before locking mLock |
| 4127 | // here so that the destructor is called only when exiting this function. |
| 4128 | // On the other hand, as long as Track::destroy() is only called by |
| 4129 | // TrackHandle destructor, the TrackHandle still holds a strong ref on |
| 4130 | // this Track with its member mTrack. |
| 4131 | sp<Track> keep(this); |
| 4132 | { // scope for mLock |
| 4133 | sp<ThreadBase> thread = mThread.promote(); |
| 4134 | if (thread != 0) { |
| 4135 | if (!isOutputTrack()) { |
| 4136 | if (mState == ACTIVE || mState == RESUMING) { |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 4137 | AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 4138 | |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 4139 | #ifdef ADD_BATTERY_DATA |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 4140 | // to track the speaker usage |
| 4141 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 4142 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4143 | } |
| 4144 | AudioSystem::releaseOutput(thread->id()); |
| 4145 | } |
| 4146 | Mutex::Autolock _l(thread->mLock); |
| 4147 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 4148 | playbackThread->destroyTrack_l(this); |
| 4149 | } |
| 4150 | } |
| 4151 | } |
| 4152 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4153 | /*static*/ void AudioFlinger::PlaybackThread::Track::appendDumpHeader(String8& result) |
| 4154 | { |
| 4155 | result.append(" Name Client Type Fmt Chn mask Session Frames S M F SRate L dB R dB " |
| 4156 | " Server User Main buf Aux Buf FastUnder\n"); |
| 4157 | |
| 4158 | } |
| 4159 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4160 | void AudioFlinger::PlaybackThread::Track::dump(char* buffer, size_t size) |
| 4161 | { |
Glenn Kasten | 83d8653 | 2012-01-17 14:39:34 -0800 | [diff] [blame] | 4162 | uint32_t vlr = mCblk->getVolumeLR(); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4163 | if (isFastTrack()) { |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4164 | sprintf(buffer, " F %2d", mFastIndex); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4165 | } else { |
| 4166 | sprintf(buffer, " %4d", mName - AudioMixer::TRACK0); |
| 4167 | } |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4168 | track_state state = mState; |
| 4169 | char stateChar; |
| 4170 | switch (state) { |
| 4171 | case IDLE: |
| 4172 | stateChar = 'I'; |
| 4173 | break; |
| 4174 | case TERMINATED: |
| 4175 | stateChar = 'T'; |
| 4176 | break; |
| 4177 | case STOPPED: |
| 4178 | stateChar = 'S'; |
| 4179 | break; |
| 4180 | case RESUMING: |
| 4181 | stateChar = 'R'; |
| 4182 | break; |
| 4183 | case ACTIVE: |
| 4184 | stateChar = 'A'; |
| 4185 | break; |
| 4186 | case PAUSING: |
| 4187 | stateChar = 'p'; |
| 4188 | break; |
| 4189 | case PAUSED: |
| 4190 | stateChar = 'P'; |
| 4191 | break; |
| 4192 | default: |
| 4193 | stateChar = '?'; |
| 4194 | break; |
| 4195 | } |
| 4196 | bool nowInUnderrun = mObservedUnderruns & 1; |
| 4197 | snprintf(&buffer[7], size-7, " %6d %4u %3u 0x%08x %7u %6u %1c %1d %1d %5u %5.2g %5.2g " |
| 4198 | "0x%08x 0x%08x 0x%08x 0x%08x %9u%c\n", |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 4199 | (mClient == 0) ? getpid_cached : mClient->pid(), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4200 | mStreamType, |
| 4201 | mFormat, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4202 | mChannelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4203 | mSessionId, |
| 4204 | mFrameCount, |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4205 | stateChar, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4206 | mMute, |
| 4207 | mFillingUpStatus, |
| 4208 | mCblk->sampleRate, |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4209 | 20.0 * log10((vlr & 0xFFFF) / 4096.0), |
| 4210 | 20.0 * log10((vlr >> 16) / 4096.0), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4211 | mCblk->server, |
| 4212 | mCblk->user, |
| 4213 | (int)mMainBuffer, |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4214 | (int)mAuxBuffer, |
| 4215 | mUnderrunCount, |
| 4216 | nowInUnderrun ? '*' : ' '); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4217 | } |
| 4218 | |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 4219 | // AudioBufferProvider interface |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4220 | status_t AudioFlinger::PlaybackThread::Track::getNextBuffer( |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4221 | AudioBufferProvider::Buffer* buffer, int64_t pts) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4222 | { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4223 | audio_track_cblk_t* cblk = this->cblk(); |
| 4224 | uint32_t framesReady; |
| 4225 | uint32_t framesReq = buffer->frameCount; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4226 | |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4227 | // Check if last stepServer failed, try to step now |
| 4228 | if (mStepServerFailed) { |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4229 | // FIXME When called by fast mixer, this takes a mutex with tryLock(). |
| 4230 | // Since the fast mixer is higher priority than client callback thread, |
| 4231 | // it does not result in priority inversion for client. |
| 4232 | // But a non-blocking solution would be preferable to avoid |
| 4233 | // fast mixer being unable to tryLock(), and |
| 4234 | // to avoid the extra context switches if the client wakes up, |
| 4235 | // 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] | 4236 | if (!step()) goto getNextBuffer_exit; |
| 4237 | ALOGV("stepServer recovered"); |
| 4238 | mStepServerFailed = false; |
| 4239 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4240 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4241 | // FIXME Same as above |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4242 | framesReady = cblk->framesReady(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4243 | |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4244 | if (CC_LIKELY(framesReady)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4245 | uint32_t s = cblk->server; |
| 4246 | uint32_t bufferEnd = cblk->serverBase + cblk->frameCount; |
| 4247 | |
| 4248 | bufferEnd = (cblk->loopEnd < bufferEnd) ? cblk->loopEnd : bufferEnd; |
| 4249 | if (framesReq > framesReady) { |
| 4250 | framesReq = framesReady; |
| 4251 | } |
Marco Nelissen | a1472d9 | 2012-03-30 14:36:54 -0700 | [diff] [blame] | 4252 | if (framesReq > bufferEnd - s) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4253 | framesReq = bufferEnd - s; |
| 4254 | } |
| 4255 | |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4256 | buffer->raw = getBuffer(s, framesReq); |
| 4257 | if (buffer->raw == NULL) goto getNextBuffer_exit; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4258 | |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4259 | buffer->frameCount = framesReq; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4260 | return NO_ERROR; |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4261 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4262 | |
| 4263 | getNextBuffer_exit: |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4264 | buffer->raw = NULL; |
| 4265 | buffer->frameCount = 0; |
| 4266 | ALOGV("getNextBuffer() no more data for track %d on thread %p", mName, mThread.unsafe_get()); |
| 4267 | return NOT_ENOUGH_DATA; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4268 | } |
| 4269 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4270 | // Note that framesReady() takes a mutex on the control block using tryLock(). |
| 4271 | // This could result in priority inversion if framesReady() is called by the normal mixer, |
| 4272 | // as the normal mixer thread runs at lower |
| 4273 | // priority than the client's callback thread: there is a short window within framesReady() |
| 4274 | // during which the normal mixer could be preempted, and the client callback would block. |
| 4275 | // Another problem can occur if framesReady() is called by the fast mixer: |
| 4276 | // the tryLock() could block for up to 1 ms, and a sequence of these could delay fast mixer. |
| 4277 | // FIXME Replace AudioTrackShared control block implementation by a non-blocking FIFO queue. |
| 4278 | size_t AudioFlinger::PlaybackThread::Track::framesReady() const { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4279 | return mCblk->framesReady(); |
| 4280 | } |
| 4281 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 4282 | // 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] | 4283 | bool AudioFlinger::PlaybackThread::Track::isReady() const { |
Eric Laurent | af59ce2 | 2010-10-05 14:41:42 -0700 | [diff] [blame] | 4284 | if (mFillingUpStatus != FS_FILLING || isStopped() || isPausing()) return true; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4285 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4286 | if (framesReady() >= mCblk->frameCount || |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4287 | (mCblk->flags & CBLK_FORCEREADY_MSK)) { |
| 4288 | mFillingUpStatus = FS_FILLED; |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 4289 | android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4290 | return true; |
| 4291 | } |
| 4292 | return false; |
| 4293 | } |
| 4294 | |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 4295 | status_t AudioFlinger::PlaybackThread::Track::start(AudioSystem::sync_event_t event, |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 4296 | int triggerSession) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4297 | { |
| 4298 | status_t status = NO_ERROR; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4299 | ALOGV("start(%d), calling pid %d session %d", |
| 4300 | mName, IPCThreadState::self()->getCallingPid(), mSessionId); |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 4301 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4302 | sp<ThreadBase> thread = mThread.promote(); |
| 4303 | if (thread != 0) { |
| 4304 | Mutex::Autolock _l(thread->mLock); |
Glenn Kasten | b853e98 | 2012-01-26 13:39:18 -0800 | [diff] [blame] | 4305 | track_state state = mState; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4306 | // here the track could be either new, or restarted |
| 4307 | // in both cases "unstop" the track |
| 4308 | if (mState == PAUSED) { |
| 4309 | mState = TrackBase::RESUMING; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4310 | ALOGV("PAUSED => RESUMING (%d) on thread %p", mName, this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4311 | } else { |
| 4312 | mState = TrackBase::ACTIVE; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4313 | ALOGV("? => ACTIVE (%d) on thread %p", mName, this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4314 | } |
| 4315 | |
| 4316 | if (!isOutputTrack() && state != ACTIVE && state != RESUMING) { |
| 4317 | thread->mLock.unlock(); |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 4318 | status = AudioSystem::startOutput(thread->id(), mStreamType, mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4319 | thread->mLock.lock(); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 4320 | |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 4321 | #ifdef ADD_BATTERY_DATA |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 4322 | // to track the speaker usage |
| 4323 | if (status == NO_ERROR) { |
| 4324 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); |
| 4325 | } |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 4326 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4327 | } |
| 4328 | if (status == NO_ERROR) { |
| 4329 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 4330 | playbackThread->addTrack_l(this); |
| 4331 | } else { |
| 4332 | mState = state; |
| 4333 | } |
| 4334 | } else { |
| 4335 | status = BAD_VALUE; |
| 4336 | } |
| 4337 | return status; |
| 4338 | } |
| 4339 | |
| 4340 | void AudioFlinger::PlaybackThread::Track::stop() |
| 4341 | { |
Glenn Kasten | 23d82a9 | 2012-02-03 11:10:00 -0800 | [diff] [blame] | 4342 | ALOGV("stop(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4343 | sp<ThreadBase> thread = mThread.promote(); |
| 4344 | if (thread != 0) { |
| 4345 | Mutex::Autolock _l(thread->mLock); |
Glenn Kasten | b853e98 | 2012-01-26 13:39:18 -0800 | [diff] [blame] | 4346 | track_state state = mState; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4347 | if (mState > STOPPED) { |
| 4348 | mState = STOPPED; |
| 4349 | // If the track is not active (PAUSED and buffers full), flush buffers |
| 4350 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 4351 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { |
| 4352 | reset(); |
| 4353 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4354 | ALOGV("(> STOPPED) => STOPPED (%d) on thread %p", mName, playbackThread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4355 | } |
| 4356 | if (!isOutputTrack() && (state == ACTIVE || state == RESUMING)) { |
| 4357 | thread->mLock.unlock(); |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 4358 | AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4359 | thread->mLock.lock(); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 4360 | |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 4361 | #ifdef ADD_BATTERY_DATA |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 4362 | // to track the speaker usage |
| 4363 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 4364 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4365 | } |
| 4366 | } |
| 4367 | } |
| 4368 | |
| 4369 | void AudioFlinger::PlaybackThread::Track::pause() |
| 4370 | { |
Glenn Kasten | 23d82a9 | 2012-02-03 11:10:00 -0800 | [diff] [blame] | 4371 | ALOGV("pause(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4372 | sp<ThreadBase> thread = mThread.promote(); |
| 4373 | if (thread != 0) { |
| 4374 | Mutex::Autolock _l(thread->mLock); |
| 4375 | if (mState == ACTIVE || mState == RESUMING) { |
| 4376 | mState = PAUSING; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4377 | ALOGV("ACTIVE/RESUMING => PAUSING (%d) on thread %p", mName, thread.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4378 | if (!isOutputTrack()) { |
| 4379 | thread->mLock.unlock(); |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 4380 | AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4381 | thread->mLock.lock(); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 4382 | |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 4383 | #ifdef ADD_BATTERY_DATA |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 4384 | // to track the speaker usage |
| 4385 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 4386 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4387 | } |
| 4388 | } |
| 4389 | } |
| 4390 | } |
| 4391 | |
| 4392 | void AudioFlinger::PlaybackThread::Track::flush() |
| 4393 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4394 | ALOGV("flush(%d)", mName); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4395 | sp<ThreadBase> thread = mThread.promote(); |
| 4396 | if (thread != 0) { |
| 4397 | Mutex::Autolock _l(thread->mLock); |
| 4398 | if (mState != STOPPED && mState != PAUSED && mState != PAUSING) { |
| 4399 | return; |
| 4400 | } |
| 4401 | // No point remaining in PAUSED state after a flush => go to |
| 4402 | // STOPPED state |
| 4403 | mState = STOPPED; |
| 4404 | |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 4405 | // do not reset the track if it is still in the process of being stopped or paused. |
| 4406 | // this will be done by prepareTracks_l() when the track is stopped. |
| 4407 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 4408 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { |
| 4409 | reset(); |
| 4410 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4411 | } |
| 4412 | } |
| 4413 | |
| 4414 | void AudioFlinger::PlaybackThread::Track::reset() |
| 4415 | { |
| 4416 | // Do not reset twice to avoid discarding data written just after a flush and before |
| 4417 | // the audioflinger thread detects the track is stopped. |
| 4418 | if (!mResetDone) { |
| 4419 | TrackBase::reset(); |
| 4420 | // Force underrun condition to avoid false underrun callback until first data is |
| 4421 | // written to buffer |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 4422 | android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags); |
| 4423 | android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4424 | mFillingUpStatus = FS_FILLING; |
| 4425 | mResetDone = true; |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 4426 | mPresentationCompleteFrames = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4427 | } |
| 4428 | } |
| 4429 | |
| 4430 | void AudioFlinger::PlaybackThread::Track::mute(bool muted) |
| 4431 | { |
| 4432 | mMute = muted; |
| 4433 | } |
| 4434 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4435 | status_t AudioFlinger::PlaybackThread::Track::attachAuxEffect(int EffectId) |
| 4436 | { |
| 4437 | status_t status = DEAD_OBJECT; |
| 4438 | sp<ThreadBase> thread = mThread.promote(); |
| 4439 | if (thread != 0) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4440 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 4441 | status = playbackThread->attachAuxEffect(this, EffectId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4442 | } |
| 4443 | return status; |
| 4444 | } |
| 4445 | |
| 4446 | void AudioFlinger::PlaybackThread::Track::setAuxBuffer(int EffectId, int32_t *buffer) |
| 4447 | { |
| 4448 | mAuxEffectId = EffectId; |
| 4449 | mAuxBuffer = buffer; |
| 4450 | } |
| 4451 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 4452 | bool AudioFlinger::PlaybackThread::Track::presentationComplete(size_t framesWritten, |
| 4453 | size_t audioHalFrames) |
| 4454 | { |
| 4455 | // a track is considered presented when the total number of frames written to audio HAL |
| 4456 | // corresponds to the number of frames written when presentationComplete() is called for the |
| 4457 | // first time (mPresentationCompleteFrames == 0) plus the buffer filling status at that time. |
| 4458 | if (mPresentationCompleteFrames == 0) { |
| 4459 | mPresentationCompleteFrames = framesWritten + audioHalFrames; |
| 4460 | ALOGV("presentationComplete() reset: mPresentationCompleteFrames %d audioHalFrames %d", |
| 4461 | mPresentationCompleteFrames, audioHalFrames); |
| 4462 | } |
| 4463 | if (framesWritten >= mPresentationCompleteFrames) { |
| 4464 | ALOGV("presentationComplete() session %d complete: framesWritten %d", |
| 4465 | mSessionId, framesWritten); |
| 4466 | triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
| 4467 | mPresentationCompleteFrames = 0; |
| 4468 | return true; |
| 4469 | } |
| 4470 | return false; |
| 4471 | } |
| 4472 | |
| 4473 | void AudioFlinger::PlaybackThread::Track::triggerEvents(AudioSystem::sync_event_t type) |
| 4474 | { |
| 4475 | for (int i = 0; i < (int)mSyncEvents.size(); i++) { |
| 4476 | if (mSyncEvents[i]->type() == type) { |
| 4477 | mSyncEvents[i]->trigger(); |
| 4478 | mSyncEvents.removeAt(i); |
| 4479 | i--; |
| 4480 | } |
| 4481 | } |
| 4482 | } |
| 4483 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4484 | // implement VolumeBufferProvider interface |
| 4485 | |
| 4486 | uint32_t AudioFlinger::PlaybackThread::Track::getVolumeLR() |
| 4487 | { |
| 4488 | // called by FastMixer, so not allowed to take any locks, block, or do I/O including logs |
| 4489 | ALOG_ASSERT(isFastTrack() && (mCblk != NULL)); |
| 4490 | uint32_t vlr = mCblk->getVolumeLR(); |
| 4491 | uint32_t vl = vlr & 0xFFFF; |
| 4492 | uint32_t vr = vlr >> 16; |
| 4493 | // track volumes come from shared memory, so can't be trusted and must be clamped |
| 4494 | if (vl > MAX_GAIN_INT) { |
| 4495 | vl = MAX_GAIN_INT; |
| 4496 | } |
| 4497 | if (vr > MAX_GAIN_INT) { |
| 4498 | vr = MAX_GAIN_INT; |
| 4499 | } |
| 4500 | // now apply the cached master volume and stream type volume; |
| 4501 | // this is trusted but lacks any synchronization or barrier so may be stale |
| 4502 | float v = mCachedVolume; |
| 4503 | vl *= v; |
| 4504 | vr *= v; |
| 4505 | // re-combine into U4.16 |
| 4506 | vlr = (vr << 16) | (vl & 0xFFFF); |
| 4507 | // FIXME look at mute, pause, and stop flags |
| 4508 | return vlr; |
| 4509 | } |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 4510 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4511 | // timed audio tracks |
| 4512 | |
| 4513 | sp<AudioFlinger::PlaybackThread::TimedTrack> |
| 4514 | AudioFlinger::PlaybackThread::TimedTrack::create( |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 4515 | PlaybackThread *thread, |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4516 | const sp<Client>& client, |
| 4517 | audio_stream_type_t streamType, |
| 4518 | uint32_t sampleRate, |
| 4519 | audio_format_t format, |
| 4520 | uint32_t channelMask, |
| 4521 | int frameCount, |
| 4522 | const sp<IMemory>& sharedBuffer, |
| 4523 | int sessionId) { |
| 4524 | if (!client->reserveTimedTrack()) |
| 4525 | return NULL; |
| 4526 | |
Glenn Kasten | a035676 | 2012-03-19 10:38:51 -0700 | [diff] [blame] | 4527 | return new TimedTrack( |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4528 | thread, client, streamType, sampleRate, format, channelMask, frameCount, |
| 4529 | sharedBuffer, sessionId); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4530 | } |
| 4531 | |
| 4532 | AudioFlinger::PlaybackThread::TimedTrack::TimedTrack( |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 4533 | PlaybackThread *thread, |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4534 | const sp<Client>& client, |
| 4535 | audio_stream_type_t streamType, |
| 4536 | uint32_t sampleRate, |
| 4537 | audio_format_t format, |
| 4538 | uint32_t channelMask, |
| 4539 | int frameCount, |
| 4540 | const sp<IMemory>& sharedBuffer, |
| 4541 | int sessionId) |
| 4542 | : Track(thread, client, streamType, sampleRate, format, channelMask, |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 4543 | frameCount, sharedBuffer, sessionId, IAudioFlinger::TRACK_TIMED), |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4544 | mQueueHeadInFlight(false), |
| 4545 | mTrimQueueHeadOnRelease(false), |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4546 | mFramesPendingInQueue(0), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4547 | mTimedSilenceBuffer(NULL), |
| 4548 | mTimedSilenceBufferSize(0), |
| 4549 | mTimedAudioOutputOnTime(false), |
| 4550 | mMediaTimeTransformValid(false) |
| 4551 | { |
| 4552 | LocalClock lc; |
| 4553 | mLocalTimeFreq = lc.getLocalFreq(); |
| 4554 | |
| 4555 | mLocalTimeToSampleTransform.a_zero = 0; |
| 4556 | mLocalTimeToSampleTransform.b_zero = 0; |
| 4557 | mLocalTimeToSampleTransform.a_to_b_numer = sampleRate; |
| 4558 | mLocalTimeToSampleTransform.a_to_b_denom = mLocalTimeFreq; |
| 4559 | LinearTransform::reduce(&mLocalTimeToSampleTransform.a_to_b_numer, |
| 4560 | &mLocalTimeToSampleTransform.a_to_b_denom); |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4561 | |
| 4562 | mMediaTimeToSampleTransform.a_zero = 0; |
| 4563 | mMediaTimeToSampleTransform.b_zero = 0; |
| 4564 | mMediaTimeToSampleTransform.a_to_b_numer = sampleRate; |
| 4565 | mMediaTimeToSampleTransform.a_to_b_denom = 1000000; |
| 4566 | LinearTransform::reduce(&mMediaTimeToSampleTransform.a_to_b_numer, |
| 4567 | &mMediaTimeToSampleTransform.a_to_b_denom); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4568 | } |
| 4569 | |
| 4570 | AudioFlinger::PlaybackThread::TimedTrack::~TimedTrack() { |
| 4571 | mClient->releaseTimedTrack(); |
| 4572 | delete [] mTimedSilenceBuffer; |
| 4573 | } |
| 4574 | |
| 4575 | status_t AudioFlinger::PlaybackThread::TimedTrack::allocateTimedBuffer( |
| 4576 | size_t size, sp<IMemory>* buffer) { |
| 4577 | |
| 4578 | Mutex::Autolock _l(mTimedBufferQueueLock); |
| 4579 | |
| 4580 | trimTimedBufferQueue_l(); |
| 4581 | |
| 4582 | // lazily initialize the shared memory heap for timed buffers |
| 4583 | if (mTimedMemoryDealer == NULL) { |
| 4584 | const int kTimedBufferHeapSize = 512 << 10; |
| 4585 | |
| 4586 | mTimedMemoryDealer = new MemoryDealer(kTimedBufferHeapSize, |
| 4587 | "AudioFlingerTimed"); |
| 4588 | if (mTimedMemoryDealer == NULL) |
| 4589 | return NO_MEMORY; |
| 4590 | } |
| 4591 | |
| 4592 | sp<IMemory> newBuffer = mTimedMemoryDealer->allocate(size); |
| 4593 | if (newBuffer == NULL) { |
| 4594 | newBuffer = mTimedMemoryDealer->allocate(size); |
| 4595 | if (newBuffer == NULL) |
| 4596 | return NO_MEMORY; |
| 4597 | } |
| 4598 | |
| 4599 | *buffer = newBuffer; |
| 4600 | return NO_ERROR; |
| 4601 | } |
| 4602 | |
| 4603 | // caller must hold mTimedBufferQueueLock |
| 4604 | void AudioFlinger::PlaybackThread::TimedTrack::trimTimedBufferQueue_l() { |
| 4605 | int64_t mediaTimeNow; |
| 4606 | { |
| 4607 | Mutex::Autolock mttLock(mMediaTimeTransformLock); |
| 4608 | if (!mMediaTimeTransformValid) |
| 4609 | return; |
| 4610 | |
| 4611 | int64_t targetTimeNow; |
| 4612 | status_t res = (mMediaTimeTransformTarget == TimedAudioTrack::COMMON_TIME) |
| 4613 | ? mCCHelper.getCommonTime(&targetTimeNow) |
| 4614 | : mCCHelper.getLocalTime(&targetTimeNow); |
| 4615 | |
| 4616 | if (OK != res) |
| 4617 | return; |
| 4618 | |
| 4619 | if (!mMediaTimeTransform.doReverseTransform(targetTimeNow, |
| 4620 | &mediaTimeNow)) { |
| 4621 | return; |
| 4622 | } |
| 4623 | } |
| 4624 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4625 | size_t trimEnd; |
| 4626 | for (trimEnd = 0; trimEnd < mTimedBufferQueue.size(); trimEnd++) { |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4627 | int64_t bufEnd; |
| 4628 | |
John Grossman | c95cfbb | 2012-04-12 11:53:11 -0700 | [diff] [blame] | 4629 | if ((trimEnd + 1) < mTimedBufferQueue.size()) { |
| 4630 | // We have a next buffer. Just use its PTS as the PTS of the frame |
| 4631 | // following the last frame in this buffer. If the stream is sparse |
| 4632 | // (ie, there are deliberate gaps left in the stream which should be |
| 4633 | // filled with silence by the TimedAudioTrack), then this can result |
| 4634 | // in one extra buffer being left un-trimmed when it could have |
| 4635 | // been. In general, this is not typical, and we would rather |
| 4636 | // optimized away the TS calculation below for the more common case |
| 4637 | // where PTSes are contiguous. |
| 4638 | bufEnd = mTimedBufferQueue[trimEnd + 1].pts(); |
| 4639 | } else { |
| 4640 | // We have no next buffer. Compute the PTS of the frame following |
| 4641 | // the last frame in this buffer by computing the duration of of |
| 4642 | // this frame in media time units and adding it to the PTS of the |
| 4643 | // buffer. |
| 4644 | int64_t frameCount = mTimedBufferQueue[trimEnd].buffer()->size() |
| 4645 | / mCblk->frameSize; |
| 4646 | |
| 4647 | if (!mMediaTimeToSampleTransform.doReverseTransform(frameCount, |
| 4648 | &bufEnd)) { |
| 4649 | ALOGE("Failed to convert frame count of %lld to media time" |
| 4650 | " duration" " (scale factor %d/%u) in %s", |
| 4651 | frameCount, |
| 4652 | mMediaTimeToSampleTransform.a_to_b_numer, |
| 4653 | mMediaTimeToSampleTransform.a_to_b_denom, |
| 4654 | __PRETTY_FUNCTION__); |
| 4655 | break; |
| 4656 | } |
| 4657 | bufEnd += mTimedBufferQueue[trimEnd].pts(); |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4658 | } |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4659 | |
| 4660 | if (bufEnd > mediaTimeNow) |
| 4661 | break; |
| 4662 | |
| 4663 | // Is the buffer we want to use in the middle of a mix operation right |
| 4664 | // now? If so, don't actually trim it. Just wait for the releaseBuffer |
| 4665 | // from the mixer which should be coming back shortly. |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4666 | if (!trimEnd && mQueueHeadInFlight) { |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4667 | mTrimQueueHeadOnRelease = true; |
| 4668 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4669 | } |
| 4670 | |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4671 | size_t trimStart = mTrimQueueHeadOnRelease ? 1 : 0; |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4672 | if (trimStart < trimEnd) { |
| 4673 | // Update the bookkeeping for framesReady() |
| 4674 | for (size_t i = trimStart; i < trimEnd; ++i) { |
| 4675 | updateFramesPendingAfterTrim_l(mTimedBufferQueue[i], "trim"); |
| 4676 | } |
| 4677 | |
| 4678 | // Now actually remove the buffers from the queue. |
| 4679 | mTimedBufferQueue.removeItemsAt(trimStart, trimEnd); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4680 | } |
| 4681 | } |
| 4682 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4683 | void AudioFlinger::PlaybackThread::TimedTrack::trimTimedBufferQueueHead_l( |
| 4684 | const char* logTag) { |
John Grossman | d3030da | 2012-04-12 11:56:36 -0700 | [diff] [blame] | 4685 | ALOG_ASSERT(mTimedBufferQueue.size() > 0, |
| 4686 | "%s called (reason \"%s\"), but timed buffer queue has no" |
| 4687 | " elements to trim.", __FUNCTION__, logTag); |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4688 | |
| 4689 | updateFramesPendingAfterTrim_l(mTimedBufferQueue[0], logTag); |
| 4690 | mTimedBufferQueue.removeAt(0); |
| 4691 | } |
| 4692 | |
| 4693 | void AudioFlinger::PlaybackThread::TimedTrack::updateFramesPendingAfterTrim_l( |
| 4694 | const TimedBuffer& buf, |
| 4695 | const char* logTag) { |
| 4696 | uint32_t bufBytes = buf.buffer()->size(); |
| 4697 | uint32_t consumedAlready = buf.position(); |
| 4698 | |
Eric Laurent | b388e53 | 2012-04-14 13:32:48 -0700 | [diff] [blame] | 4699 | ALOG_ASSERT(consumedAlready <= bufBytes, |
John Grossman | d3030da | 2012-04-12 11:56:36 -0700 | [diff] [blame] | 4700 | "Bad bookkeeping while updating frames pending. Timed buffer is" |
| 4701 | " only %u bytes long, but claims to have consumed %u" |
| 4702 | " bytes. (update reason: \"%s\")", |
Eric Laurent | b388e53 | 2012-04-14 13:32:48 -0700 | [diff] [blame] | 4703 | bufBytes, consumedAlready, logTag); |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4704 | |
| 4705 | uint32_t bufFrames = (bufBytes - consumedAlready) / mCblk->frameSize; |
John Grossman | d3030da | 2012-04-12 11:56:36 -0700 | [diff] [blame] | 4706 | ALOG_ASSERT(mFramesPendingInQueue >= bufFrames, |
| 4707 | "Bad bookkeeping while updating frames pending. Should have at" |
| 4708 | " least %u queued frames, but we think we have only %u. (update" |
| 4709 | " reason: \"%s\")", |
| 4710 | bufFrames, mFramesPendingInQueue, logTag); |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4711 | |
| 4712 | mFramesPendingInQueue -= bufFrames; |
| 4713 | } |
| 4714 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4715 | status_t AudioFlinger::PlaybackThread::TimedTrack::queueTimedBuffer( |
| 4716 | const sp<IMemory>& buffer, int64_t pts) { |
| 4717 | |
| 4718 | { |
| 4719 | Mutex::Autolock mttLock(mMediaTimeTransformLock); |
| 4720 | if (!mMediaTimeTransformValid) |
| 4721 | return INVALID_OPERATION; |
| 4722 | } |
| 4723 | |
| 4724 | Mutex::Autolock _l(mTimedBufferQueueLock); |
| 4725 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4726 | uint32_t bufFrames = buffer->size() / mCblk->frameSize; |
| 4727 | mFramesPendingInQueue += bufFrames; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4728 | mTimedBufferQueue.add(TimedBuffer(buffer, pts)); |
| 4729 | |
| 4730 | return NO_ERROR; |
| 4731 | } |
| 4732 | |
| 4733 | status_t AudioFlinger::PlaybackThread::TimedTrack::setMediaTimeTransform( |
| 4734 | const LinearTransform& xform, TimedAudioTrack::TargetTimeline target) { |
| 4735 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4736 | ALOGVV("setMediaTimeTransform az=%lld bz=%lld n=%d d=%u tgt=%d", |
| 4737 | xform.a_zero, xform.b_zero, xform.a_to_b_numer, xform.a_to_b_denom, |
| 4738 | target); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4739 | |
| 4740 | if (!(target == TimedAudioTrack::LOCAL_TIME || |
| 4741 | target == TimedAudioTrack::COMMON_TIME)) { |
| 4742 | return BAD_VALUE; |
| 4743 | } |
| 4744 | |
| 4745 | Mutex::Autolock lock(mMediaTimeTransformLock); |
| 4746 | mMediaTimeTransform = xform; |
| 4747 | mMediaTimeTransformTarget = target; |
| 4748 | mMediaTimeTransformValid = true; |
| 4749 | |
| 4750 | return NO_ERROR; |
| 4751 | } |
| 4752 | |
| 4753 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
| 4754 | |
| 4755 | // implementation of getNextBuffer for tracks whose buffers have timestamps |
| 4756 | status_t AudioFlinger::PlaybackThread::TimedTrack::getNextBuffer( |
| 4757 | AudioBufferProvider::Buffer* buffer, int64_t pts) |
| 4758 | { |
| 4759 | if (pts == AudioBufferProvider::kInvalidPTS) { |
| 4760 | buffer->raw = 0; |
| 4761 | buffer->frameCount = 0; |
John Grossman | 8d314b7 | 2012-04-19 12:08:17 -0700 | [diff] [blame] | 4762 | mTimedAudioOutputOnTime = false; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4763 | return INVALID_OPERATION; |
| 4764 | } |
| 4765 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4766 | Mutex::Autolock _l(mTimedBufferQueueLock); |
| 4767 | |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4768 | ALOG_ASSERT(!mQueueHeadInFlight, |
| 4769 | "getNextBuffer called without releaseBuffer!"); |
| 4770 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4771 | while (true) { |
| 4772 | |
| 4773 | // if we have no timed buffers, then fail |
| 4774 | if (mTimedBufferQueue.isEmpty()) { |
| 4775 | buffer->raw = 0; |
| 4776 | buffer->frameCount = 0; |
| 4777 | return NOT_ENOUGH_DATA; |
| 4778 | } |
| 4779 | |
| 4780 | TimedBuffer& head = mTimedBufferQueue.editItemAt(0); |
| 4781 | |
| 4782 | // calculate the PTS of the head of the timed buffer queue expressed in |
| 4783 | // local time |
| 4784 | int64_t headLocalPTS; |
| 4785 | { |
| 4786 | Mutex::Autolock mttLock(mMediaTimeTransformLock); |
| 4787 | |
Glenn Kasten | 5798d4e | 2012-03-08 12:18:35 -0800 | [diff] [blame] | 4788 | ALOG_ASSERT(mMediaTimeTransformValid, "media time transform invalid"); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4789 | |
| 4790 | if (mMediaTimeTransform.a_to_b_denom == 0) { |
| 4791 | // the transform represents a pause, so yield silence |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4792 | timedYieldSilence_l(buffer->frameCount, buffer); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4793 | return NO_ERROR; |
| 4794 | } |
| 4795 | |
| 4796 | int64_t transformedPTS; |
| 4797 | if (!mMediaTimeTransform.doForwardTransform(head.pts(), |
| 4798 | &transformedPTS)) { |
| 4799 | // the transform failed. this shouldn't happen, but if it does |
| 4800 | // then just drop this buffer |
| 4801 | ALOGW("timedGetNextBuffer transform failed"); |
| 4802 | buffer->raw = 0; |
| 4803 | buffer->frameCount = 0; |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4804 | trimTimedBufferQueueHead_l("getNextBuffer; no transform"); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4805 | return NO_ERROR; |
| 4806 | } |
| 4807 | |
| 4808 | if (mMediaTimeTransformTarget == TimedAudioTrack::COMMON_TIME) { |
| 4809 | if (OK != mCCHelper.commonTimeToLocalTime(transformedPTS, |
| 4810 | &headLocalPTS)) { |
| 4811 | buffer->raw = 0; |
| 4812 | buffer->frameCount = 0; |
| 4813 | return INVALID_OPERATION; |
| 4814 | } |
| 4815 | } else { |
| 4816 | headLocalPTS = transformedPTS; |
| 4817 | } |
| 4818 | } |
| 4819 | |
| 4820 | // adjust the head buffer's PTS to reflect the portion of the head buffer |
| 4821 | // that has already been consumed |
| 4822 | int64_t effectivePTS = headLocalPTS + |
| 4823 | ((head.position() / mCblk->frameSize) * mLocalTimeFreq / sampleRate()); |
| 4824 | |
| 4825 | // Calculate the delta in samples between the head of the input buffer |
| 4826 | // queue and the start of the next output buffer that will be written. |
| 4827 | // If the transformation fails because of over or underflow, it means |
| 4828 | // that the sample's position in the output stream is so far out of |
| 4829 | // whack that it should just be dropped. |
| 4830 | int64_t sampleDelta; |
| 4831 | if (llabs(effectivePTS - pts) >= (static_cast<int64_t>(1) << 31)) { |
| 4832 | ALOGV("*** head buffer is too far from PTS: dropped buffer"); |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4833 | trimTimedBufferQueueHead_l("getNextBuffer, buf pts too far from" |
| 4834 | " mix"); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4835 | continue; |
| 4836 | } |
| 4837 | if (!mLocalTimeToSampleTransform.doForwardTransform( |
| 4838 | (effectivePTS - pts) << 32, &sampleDelta)) { |
John Grossman | d3030da | 2012-04-12 11:56:36 -0700 | [diff] [blame] | 4839 | ALOGV("*** too late during sample rate transform: dropped buffer"); |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4840 | trimTimedBufferQueueHead_l("getNextBuffer, bad local to sample"); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4841 | continue; |
| 4842 | } |
| 4843 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4844 | ALOGVV("*** getNextBuffer head.pts=%lld head.pos=%d pts=%lld" |
| 4845 | " sampleDelta=[%d.%08x]", |
| 4846 | head.pts(), head.position(), pts, |
| 4847 | static_cast<int32_t>((sampleDelta >= 0 ? 0 : 1) |
| 4848 | + (sampleDelta >> 32)), |
| 4849 | static_cast<uint32_t>(sampleDelta & 0xFFFFFFFF)); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4850 | |
| 4851 | // if the delta between the ideal placement for the next input sample and |
| 4852 | // the current output position is within this threshold, then we will |
| 4853 | // concatenate the next input samples to the previous output |
| 4854 | const int64_t kSampleContinuityThreshold = |
John Grossman | 8d314b7 | 2012-04-19 12:08:17 -0700 | [diff] [blame] | 4855 | (static_cast<int64_t>(sampleRate()) << 32) / 250; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4856 | |
| 4857 | // if this is the first buffer of audio that we're emitting from this track |
| 4858 | // then it should be almost exactly on time. |
| 4859 | const int64_t kSampleStartupThreshold = 1LL << 32; |
| 4860 | |
| 4861 | if ((mTimedAudioOutputOnTime && llabs(sampleDelta) <= kSampleContinuityThreshold) || |
John Grossman | 8d314b7 | 2012-04-19 12:08:17 -0700 | [diff] [blame] | 4862 | (!mTimedAudioOutputOnTime && llabs(sampleDelta) <= kSampleStartupThreshold)) { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4863 | // the next input is close enough to being on time, so concatenate it |
| 4864 | // with the last output |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4865 | timedYieldSamples_l(buffer); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4866 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4867 | ALOGVV("*** on time: head.pos=%d frameCount=%u", |
| 4868 | head.position(), buffer->frameCount); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4869 | return NO_ERROR; |
John Grossman | 8d314b7 | 2012-04-19 12:08:17 -0700 | [diff] [blame] | 4870 | } |
| 4871 | |
| 4872 | // Looks like our output is not on time. Reset our on timed status. |
| 4873 | // Next time we mix samples from our input queue, then should be within |
| 4874 | // the StartupThreshold. |
| 4875 | mTimedAudioOutputOnTime = false; |
| 4876 | if (sampleDelta > 0) { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4877 | // the gap between the current output position and the proper start of |
| 4878 | // the next input sample is too big, so fill it with silence |
| 4879 | uint32_t framesUntilNextInput = (sampleDelta + 0x80000000) >> 32; |
| 4880 | |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4881 | timedYieldSilence_l(framesUntilNextInput, buffer); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4882 | ALOGV("*** silence: frameCount=%u", buffer->frameCount); |
| 4883 | return NO_ERROR; |
| 4884 | } else { |
| 4885 | // the next input sample is late |
| 4886 | uint32_t lateFrames = static_cast<uint32_t>(-((sampleDelta + 0x80000000) >> 32)); |
| 4887 | size_t onTimeSamplePosition = |
| 4888 | head.position() + lateFrames * mCblk->frameSize; |
| 4889 | |
| 4890 | if (onTimeSamplePosition > head.buffer()->size()) { |
| 4891 | // all the remaining samples in the head are too late, so |
| 4892 | // drop it and move on |
| 4893 | ALOGV("*** too late: dropped buffer"); |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4894 | trimTimedBufferQueueHead_l("getNextBuffer, dropped late buffer"); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4895 | continue; |
| 4896 | } else { |
| 4897 | // skip over the late samples |
| 4898 | head.setPosition(onTimeSamplePosition); |
| 4899 | |
| 4900 | // yield the available samples |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4901 | timedYieldSamples_l(buffer); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4902 | |
| 4903 | ALOGV("*** late: head.pos=%d frameCount=%u", head.position(), buffer->frameCount); |
| 4904 | return NO_ERROR; |
| 4905 | } |
| 4906 | } |
| 4907 | } |
| 4908 | } |
| 4909 | |
| 4910 | // Yield samples from the timed buffer queue head up to the given output |
| 4911 | // buffer's capacity. |
| 4912 | // |
| 4913 | // Caller must hold mTimedBufferQueueLock |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4914 | void AudioFlinger::PlaybackThread::TimedTrack::timedYieldSamples_l( |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4915 | AudioBufferProvider::Buffer* buffer) { |
| 4916 | |
| 4917 | const TimedBuffer& head = mTimedBufferQueue[0]; |
| 4918 | |
| 4919 | buffer->raw = (static_cast<uint8_t*>(head.buffer()->pointer()) + |
| 4920 | head.position()); |
| 4921 | |
| 4922 | uint32_t framesLeftInHead = ((head.buffer()->size() - head.position()) / |
| 4923 | mCblk->frameSize); |
| 4924 | size_t framesRequested = buffer->frameCount; |
| 4925 | buffer->frameCount = min(framesLeftInHead, framesRequested); |
| 4926 | |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4927 | mQueueHeadInFlight = true; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4928 | mTimedAudioOutputOnTime = true; |
| 4929 | } |
| 4930 | |
| 4931 | // Yield samples of silence up to the given output buffer's capacity |
| 4932 | // |
| 4933 | // Caller must hold mTimedBufferQueueLock |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4934 | void AudioFlinger::PlaybackThread::TimedTrack::timedYieldSilence_l( |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4935 | uint32_t numFrames, AudioBufferProvider::Buffer* buffer) { |
| 4936 | |
| 4937 | // lazily allocate a buffer filled with silence |
| 4938 | if (mTimedSilenceBufferSize < numFrames * mCblk->frameSize) { |
| 4939 | delete [] mTimedSilenceBuffer; |
| 4940 | mTimedSilenceBufferSize = numFrames * mCblk->frameSize; |
| 4941 | mTimedSilenceBuffer = new uint8_t[mTimedSilenceBufferSize]; |
| 4942 | memset(mTimedSilenceBuffer, 0, mTimedSilenceBufferSize); |
| 4943 | } |
| 4944 | |
| 4945 | buffer->raw = mTimedSilenceBuffer; |
| 4946 | size_t framesRequested = buffer->frameCount; |
| 4947 | buffer->frameCount = min(numFrames, framesRequested); |
| 4948 | |
| 4949 | mTimedAudioOutputOnTime = false; |
| 4950 | } |
| 4951 | |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 4952 | // AudioBufferProvider interface |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4953 | void AudioFlinger::PlaybackThread::TimedTrack::releaseBuffer( |
| 4954 | AudioBufferProvider::Buffer* buffer) { |
| 4955 | |
| 4956 | Mutex::Autolock _l(mTimedBufferQueueLock); |
| 4957 | |
John Grossman | fe5b3ba | 2012-02-12 17:51:21 -0800 | [diff] [blame] | 4958 | // If the buffer which was just released is part of the buffer at the head |
| 4959 | // of the queue, be sure to update the amt of the buffer which has been |
| 4960 | // consumed. If the buffer being returned is not part of the head of the |
| 4961 | // queue, its either because the buffer is part of the silence buffer, or |
| 4962 | // because the head of the timed queue was trimmed after the mixer called |
| 4963 | // getNextBuffer but before the mixer called releaseBuffer. |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4964 | if (buffer->raw == mTimedSilenceBuffer) { |
| 4965 | ALOG_ASSERT(!mQueueHeadInFlight, |
| 4966 | "Queue head in flight during release of silence buffer!"); |
| 4967 | goto done; |
| 4968 | } |
| 4969 | |
| 4970 | ALOG_ASSERT(mQueueHeadInFlight, |
| 4971 | "TimedTrack::releaseBuffer of non-silence buffer, but no queue" |
| 4972 | " head in flight."); |
| 4973 | |
| 4974 | if (mTimedBufferQueue.size()) { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4975 | TimedBuffer& head = mTimedBufferQueue.editItemAt(0); |
John Grossman | fe5b3ba | 2012-02-12 17:51:21 -0800 | [diff] [blame] | 4976 | |
| 4977 | void* start = head.buffer()->pointer(); |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4978 | void* end = reinterpret_cast<void*>( |
| 4979 | reinterpret_cast<uint8_t*>(head.buffer()->pointer()) |
| 4980 | + head.buffer()->size()); |
John Grossman | fe5b3ba | 2012-02-12 17:51:21 -0800 | [diff] [blame] | 4981 | |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4982 | ALOG_ASSERT((buffer->raw >= start) && (buffer->raw < end), |
| 4983 | "released buffer not within the head of the timed buffer" |
| 4984 | " queue; qHead = [%p, %p], released buffer = %p", |
| 4985 | start, end, buffer->raw); |
| 4986 | |
| 4987 | head.setPosition(head.position() + |
| 4988 | (buffer->frameCount * mCblk->frameSize)); |
| 4989 | mQueueHeadInFlight = false; |
| 4990 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4991 | ALOG_ASSERT(mFramesPendingInQueue >= buffer->frameCount, |
| 4992 | "Bad bookkeeping during releaseBuffer! Should have at" |
| 4993 | " least %u queued frames, but we think we have only %u", |
| 4994 | buffer->frameCount, mFramesPendingInQueue); |
| 4995 | |
| 4996 | mFramesPendingInQueue -= buffer->frameCount; |
| 4997 | |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4998 | if ((static_cast<size_t>(head.position()) >= head.buffer()->size()) |
| 4999 | || mTrimQueueHeadOnRelease) { |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 5000 | trimTimedBufferQueueHead_l("releaseBuffer"); |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 5001 | mTrimQueueHeadOnRelease = false; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5002 | } |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 5003 | } else { |
| 5004 | LOG_FATAL("TimedTrack::releaseBuffer of non-silence buffer with no" |
| 5005 | " buffers in the timed buffer queue"); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5006 | } |
| 5007 | |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 5008 | done: |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5009 | buffer->raw = 0; |
| 5010 | buffer->frameCount = 0; |
| 5011 | } |
| 5012 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 5013 | size_t AudioFlinger::PlaybackThread::TimedTrack::framesReady() const { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5014 | Mutex::Autolock _l(mTimedBufferQueueLock); |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 5015 | return mFramesPendingInQueue; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5016 | } |
| 5017 | |
| 5018 | AudioFlinger::PlaybackThread::TimedTrack::TimedBuffer::TimedBuffer() |
| 5019 | : mPTS(0), mPosition(0) {} |
| 5020 | |
| 5021 | AudioFlinger::PlaybackThread::TimedTrack::TimedBuffer::TimedBuffer( |
| 5022 | const sp<IMemory>& buffer, int64_t pts) |
| 5023 | : mBuffer(buffer), mPTS(pts), mPosition(0) {} |
| 5024 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5025 | // ---------------------------------------------------------------------------- |
| 5026 | |
| 5027 | // RecordTrack constructor must be called with AudioFlinger::mLock held |
| 5028 | AudioFlinger::RecordThread::RecordTrack::RecordTrack( |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 5029 | RecordThread *thread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5030 | const sp<Client>& client, |
| 5031 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5032 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 5033 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5034 | int frameCount, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5035 | int sessionId) |
| 5036 | : TrackBase(thread, client, sampleRate, format, |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame] | 5037 | channelMask, frameCount, 0 /*sharedBuffer*/, sessionId), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5038 | mOverflow(false) |
| 5039 | { |
| 5040 | if (mCblk != NULL) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 5041 | ALOGV("RecordTrack constructor, size %d", (int)mBufferEnd - (int)mBuffer); |
| 5042 | if (format == AUDIO_FORMAT_PCM_16_BIT) { |
| 5043 | mCblk->frameSize = mChannelCount * sizeof(int16_t); |
| 5044 | } else if (format == AUDIO_FORMAT_PCM_8_BIT) { |
| 5045 | mCblk->frameSize = mChannelCount * sizeof(int8_t); |
| 5046 | } else { |
| 5047 | mCblk->frameSize = sizeof(int8_t); |
| 5048 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5049 | } |
| 5050 | } |
| 5051 | |
| 5052 | AudioFlinger::RecordThread::RecordTrack::~RecordTrack() |
| 5053 | { |
| 5054 | sp<ThreadBase> thread = mThread.promote(); |
| 5055 | if (thread != 0) { |
| 5056 | AudioSystem::releaseInput(thread->id()); |
| 5057 | } |
| 5058 | } |
| 5059 | |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 5060 | // AudioBufferProvider interface |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5061 | status_t AudioFlinger::RecordThread::RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5062 | { |
| 5063 | audio_track_cblk_t* cblk = this->cblk(); |
| 5064 | uint32_t framesAvail; |
| 5065 | uint32_t framesReq = buffer->frameCount; |
| 5066 | |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 5067 | // Check if last stepServer failed, try to step now |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame] | 5068 | if (mStepServerFailed) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5069 | if (!step()) goto getNextBuffer_exit; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5070 | ALOGV("stepServer recovered"); |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame] | 5071 | mStepServerFailed = false; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5072 | } |
| 5073 | |
| 5074 | framesAvail = cblk->framesAvailable_l(); |
| 5075 | |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 5076 | if (CC_LIKELY(framesAvail)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5077 | uint32_t s = cblk->server; |
| 5078 | uint32_t bufferEnd = cblk->serverBase + cblk->frameCount; |
| 5079 | |
| 5080 | if (framesReq > framesAvail) { |
| 5081 | framesReq = framesAvail; |
| 5082 | } |
Marco Nelissen | a1472d9 | 2012-03-30 14:36:54 -0700 | [diff] [blame] | 5083 | if (framesReq > bufferEnd - s) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5084 | framesReq = bufferEnd - s; |
| 5085 | } |
| 5086 | |
| 5087 | buffer->raw = getBuffer(s, framesReq); |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 5088 | if (buffer->raw == NULL) goto getNextBuffer_exit; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5089 | |
| 5090 | buffer->frameCount = framesReq; |
| 5091 | return NO_ERROR; |
| 5092 | } |
| 5093 | |
| 5094 | getNextBuffer_exit: |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 5095 | buffer->raw = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5096 | buffer->frameCount = 0; |
| 5097 | return NOT_ENOUGH_DATA; |
| 5098 | } |
| 5099 | |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5100 | status_t AudioFlinger::RecordThread::RecordTrack::start(AudioSystem::sync_event_t event, |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 5101 | int triggerSession) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5102 | { |
| 5103 | sp<ThreadBase> thread = mThread.promote(); |
| 5104 | if (thread != 0) { |
| 5105 | RecordThread *recordThread = (RecordThread *)thread.get(); |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5106 | return recordThread->start(this, event, triggerSession); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5107 | } else { |
| 5108 | return BAD_VALUE; |
| 5109 | } |
| 5110 | } |
| 5111 | |
| 5112 | void AudioFlinger::RecordThread::RecordTrack::stop() |
| 5113 | { |
| 5114 | sp<ThreadBase> thread = mThread.promote(); |
| 5115 | if (thread != 0) { |
| 5116 | RecordThread *recordThread = (RecordThread *)thread.get(); |
| 5117 | recordThread->stop(this); |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 5118 | TrackBase::reset(); |
Glenn Kasten | 17a736c | 2012-02-14 08:52:15 -0800 | [diff] [blame] | 5119 | // Force overrun condition to avoid false overrun callback until first data is |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 5120 | // read from buffer |
| 5121 | android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5122 | } |
| 5123 | } |
| 5124 | |
| 5125 | void AudioFlinger::RecordThread::RecordTrack::dump(char* buffer, size_t size) |
| 5126 | { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 5127 | 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] | 5128 | (mClient == 0) ? getpid_cached : mClient->pid(), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5129 | mFormat, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 5130 | mChannelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5131 | mSessionId, |
| 5132 | mFrameCount, |
| 5133 | mState, |
| 5134 | mCblk->sampleRate, |
| 5135 | mCblk->server, |
| 5136 | mCblk->user); |
| 5137 | } |
| 5138 | |
| 5139 | |
| 5140 | // ---------------------------------------------------------------------------- |
| 5141 | |
| 5142 | AudioFlinger::PlaybackThread::OutputTrack::OutputTrack( |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 5143 | PlaybackThread *playbackThread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5144 | DuplicatingThread *sourceThread, |
| 5145 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5146 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 5147 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5148 | int frameCount) |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 5149 | : Track(playbackThread, NULL, AUDIO_STREAM_CNT, sampleRate, format, channelMask, frameCount, |
| 5150 | NULL, 0, IAudioFlinger::TRACK_DEFAULT), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5151 | mActive(false), mSourceThread(sourceThread) |
| 5152 | { |
| 5153 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5154 | if (mCblk != NULL) { |
| 5155 | mCblk->flags |= CBLK_DIRECTION_OUT; |
| 5156 | mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5157 | mOutBuffer.frameCount = 0; |
| 5158 | playbackThread->mTracks.add(this); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5159 | ALOGV("OutputTrack constructor mCblk %p, mBuffer %p, mCblk->buffers %p, " \ |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 5160 | "mCblk->frameCount %d, mCblk->sampleRate %d, mChannelMask 0x%08x mBufferEnd %p", |
| 5161 | mCblk, mBuffer, mCblk->buffers, |
| 5162 | mCblk->frameCount, mCblk->sampleRate, mChannelMask, mBufferEnd); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5163 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5164 | ALOGW("Error creating output track on thread %p", playbackThread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5165 | } |
| 5166 | } |
| 5167 | |
| 5168 | AudioFlinger::PlaybackThread::OutputTrack::~OutputTrack() |
| 5169 | { |
| 5170 | clearBufferQueue(); |
| 5171 | } |
| 5172 | |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5173 | status_t AudioFlinger::PlaybackThread::OutputTrack::start(AudioSystem::sync_event_t event, |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 5174 | int triggerSession) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5175 | { |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5176 | status_t status = Track::start(event, triggerSession); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5177 | if (status != NO_ERROR) { |
| 5178 | return status; |
| 5179 | } |
| 5180 | |
| 5181 | mActive = true; |
| 5182 | mRetryCount = 127; |
| 5183 | return status; |
| 5184 | } |
| 5185 | |
| 5186 | void AudioFlinger::PlaybackThread::OutputTrack::stop() |
| 5187 | { |
| 5188 | Track::stop(); |
| 5189 | clearBufferQueue(); |
| 5190 | mOutBuffer.frameCount = 0; |
| 5191 | mActive = false; |
| 5192 | } |
| 5193 | |
| 5194 | bool AudioFlinger::PlaybackThread::OutputTrack::write(int16_t* data, uint32_t frames) |
| 5195 | { |
| 5196 | Buffer *pInBuffer; |
| 5197 | Buffer inBuffer; |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 5198 | uint32_t channelCount = mChannelCount; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5199 | bool outputBufferFull = false; |
| 5200 | inBuffer.frameCount = frames; |
| 5201 | inBuffer.i16 = data; |
| 5202 | |
| 5203 | uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs(); |
| 5204 | |
| 5205 | if (!mActive && frames != 0) { |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5206 | start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5207 | sp<ThreadBase> thread = mThread.promote(); |
| 5208 | if (thread != 0) { |
| 5209 | MixerThread *mixerThread = (MixerThread *)thread.get(); |
| 5210 | if (mCblk->frameCount > frames){ |
| 5211 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { |
| 5212 | uint32_t startFrames = (mCblk->frameCount - frames); |
| 5213 | pInBuffer = new Buffer; |
| 5214 | pInBuffer->mBuffer = new int16_t[startFrames * channelCount]; |
| 5215 | pInBuffer->frameCount = startFrames; |
| 5216 | pInBuffer->i16 = pInBuffer->mBuffer; |
| 5217 | memset(pInBuffer->raw, 0, startFrames * channelCount * sizeof(int16_t)); |
| 5218 | mBufferQueue.add(pInBuffer); |
| 5219 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5220 | ALOGW ("OutputTrack::write() %p no more buffers in queue", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5221 | } |
| 5222 | } |
| 5223 | } |
| 5224 | } |
| 5225 | |
| 5226 | while (waitTimeLeftMs) { |
| 5227 | // First write pending buffers, then new data |
| 5228 | if (mBufferQueue.size()) { |
| 5229 | pInBuffer = mBufferQueue.itemAt(0); |
| 5230 | } else { |
| 5231 | pInBuffer = &inBuffer; |
| 5232 | } |
| 5233 | |
| 5234 | if (pInBuffer->frameCount == 0) { |
| 5235 | break; |
| 5236 | } |
| 5237 | |
| 5238 | if (mOutBuffer.frameCount == 0) { |
| 5239 | mOutBuffer.frameCount = pInBuffer->frameCount; |
| 5240 | nsecs_t startTime = systemTime(); |
Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 5241 | if (obtainBuffer(&mOutBuffer, waitTimeLeftMs) == (status_t)NO_MORE_BUFFERS) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5242 | 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] | 5243 | outputBufferFull = true; |
| 5244 | break; |
| 5245 | } |
| 5246 | uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime); |
| 5247 | if (waitTimeLeftMs >= waitTimeMs) { |
| 5248 | waitTimeLeftMs -= waitTimeMs; |
| 5249 | } else { |
| 5250 | waitTimeLeftMs = 0; |
| 5251 | } |
| 5252 | } |
| 5253 | |
| 5254 | uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount : pInBuffer->frameCount; |
| 5255 | memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * channelCount * sizeof(int16_t)); |
| 5256 | mCblk->stepUser(outFrames); |
| 5257 | pInBuffer->frameCount -= outFrames; |
| 5258 | pInBuffer->i16 += outFrames * channelCount; |
| 5259 | mOutBuffer.frameCount -= outFrames; |
| 5260 | mOutBuffer.i16 += outFrames * channelCount; |
| 5261 | |
| 5262 | if (pInBuffer->frameCount == 0) { |
| 5263 | if (mBufferQueue.size()) { |
| 5264 | mBufferQueue.removeAt(0); |
| 5265 | delete [] pInBuffer->mBuffer; |
| 5266 | delete pInBuffer; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5267 | 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] | 5268 | } else { |
| 5269 | break; |
| 5270 | } |
| 5271 | } |
| 5272 | } |
| 5273 | |
| 5274 | // If we could not write all frames, allocate a buffer and queue it for next time. |
| 5275 | if (inBuffer.frameCount) { |
| 5276 | sp<ThreadBase> thread = mThread.promote(); |
| 5277 | if (thread != 0 && !thread->standby()) { |
| 5278 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { |
| 5279 | pInBuffer = new Buffer; |
| 5280 | pInBuffer->mBuffer = new int16_t[inBuffer.frameCount * channelCount]; |
| 5281 | pInBuffer->frameCount = inBuffer.frameCount; |
| 5282 | pInBuffer->i16 = pInBuffer->mBuffer; |
| 5283 | memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * channelCount * sizeof(int16_t)); |
| 5284 | mBufferQueue.add(pInBuffer); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5285 | 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] | 5286 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5287 | 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] | 5288 | } |
| 5289 | } |
| 5290 | } |
| 5291 | |
| 5292 | // Calling write() with a 0 length buffer, means that no more data will be written: |
| 5293 | // If no more buffers are pending, fill output track buffer to make sure it is started |
| 5294 | // by output mixer. |
| 5295 | if (frames == 0 && mBufferQueue.size() == 0) { |
| 5296 | if (mCblk->user < mCblk->frameCount) { |
| 5297 | frames = mCblk->frameCount - mCblk->user; |
| 5298 | pInBuffer = new Buffer; |
| 5299 | pInBuffer->mBuffer = new int16_t[frames * channelCount]; |
| 5300 | pInBuffer->frameCount = frames; |
| 5301 | pInBuffer->i16 = pInBuffer->mBuffer; |
| 5302 | memset(pInBuffer->raw, 0, frames * channelCount * sizeof(int16_t)); |
| 5303 | mBufferQueue.add(pInBuffer); |
| 5304 | } else if (mActive) { |
| 5305 | stop(); |
| 5306 | } |
| 5307 | } |
| 5308 | |
| 5309 | return outputBufferFull; |
| 5310 | } |
| 5311 | |
| 5312 | status_t AudioFlinger::PlaybackThread::OutputTrack::obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs) |
| 5313 | { |
| 5314 | int active; |
| 5315 | status_t result; |
| 5316 | audio_track_cblk_t* cblk = mCblk; |
| 5317 | uint32_t framesReq = buffer->frameCount; |
| 5318 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5319 | // ALOGV("OutputTrack::obtainBuffer user %d, server %d", cblk->user, cblk->server); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5320 | buffer->frameCount = 0; |
| 5321 | |
| 5322 | uint32_t framesAvail = cblk->framesAvailable(); |
| 5323 | |
| 5324 | |
| 5325 | if (framesAvail == 0) { |
| 5326 | Mutex::Autolock _l(cblk->lock); |
| 5327 | goto start_loop_here; |
| 5328 | while (framesAvail == 0) { |
| 5329 | active = mActive; |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 5330 | if (CC_UNLIKELY(!active)) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5331 | ALOGV("Not active and NO_MORE_BUFFERS"); |
Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 5332 | return NO_MORE_BUFFERS; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5333 | } |
| 5334 | result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs)); |
| 5335 | if (result != NO_ERROR) { |
Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 5336 | return NO_MORE_BUFFERS; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5337 | } |
| 5338 | // read the server count again |
| 5339 | start_loop_here: |
| 5340 | framesAvail = cblk->framesAvailable_l(); |
| 5341 | } |
| 5342 | } |
| 5343 | |
| 5344 | // if (framesAvail < framesReq) { |
Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 5345 | // return NO_MORE_BUFFERS; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5346 | // } |
| 5347 | |
| 5348 | if (framesReq > framesAvail) { |
| 5349 | framesReq = framesAvail; |
| 5350 | } |
| 5351 | |
| 5352 | uint32_t u = cblk->user; |
| 5353 | uint32_t bufferEnd = cblk->userBase + cblk->frameCount; |
| 5354 | |
Marco Nelissen | a1472d9 | 2012-03-30 14:36:54 -0700 | [diff] [blame] | 5355 | if (framesReq > bufferEnd - u) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5356 | framesReq = bufferEnd - u; |
| 5357 | } |
| 5358 | |
| 5359 | buffer->frameCount = framesReq; |
| 5360 | buffer->raw = (void *)cblk->buffer(u); |
| 5361 | return NO_ERROR; |
| 5362 | } |
| 5363 | |
| 5364 | |
| 5365 | void AudioFlinger::PlaybackThread::OutputTrack::clearBufferQueue() |
| 5366 | { |
| 5367 | size_t size = mBufferQueue.size(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5368 | |
| 5369 | for (size_t i = 0; i < size; i++) { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 5370 | Buffer *pBuffer = mBufferQueue.itemAt(i); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5371 | delete [] pBuffer->mBuffer; |
| 5372 | delete pBuffer; |
| 5373 | } |
| 5374 | mBufferQueue.clear(); |
| 5375 | } |
| 5376 | |
| 5377 | // ---------------------------------------------------------------------------- |
| 5378 | |
| 5379 | AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid) |
| 5380 | : RefBase(), |
| 5381 | mAudioFlinger(audioFlinger), |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 5382 | // 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] | 5383 | mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5384 | mPid(pid), |
| 5385 | mTimedTrackCount(0) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5386 | { |
| 5387 | // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer |
| 5388 | } |
| 5389 | |
| 5390 | // Client destructor must be called with AudioFlinger::mLock held |
| 5391 | AudioFlinger::Client::~Client() |
| 5392 | { |
| 5393 | mAudioFlinger->removeClient_l(mPid); |
| 5394 | } |
| 5395 | |
Glenn Kasten | 435dbe6 | 2012-01-30 10:15:48 -0800 | [diff] [blame] | 5396 | sp<MemoryDealer> AudioFlinger::Client::heap() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5397 | { |
| 5398 | return mMemoryDealer; |
| 5399 | } |
| 5400 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5401 | // Reserve one of the limited slots for a timed audio track associated |
| 5402 | // with this client |
| 5403 | bool AudioFlinger::Client::reserveTimedTrack() |
| 5404 | { |
| 5405 | const int kMaxTimedTracksPerClient = 4; |
| 5406 | |
| 5407 | Mutex::Autolock _l(mTimedTrackLock); |
| 5408 | |
| 5409 | if (mTimedTrackCount >= kMaxTimedTracksPerClient) { |
| 5410 | ALOGW("can not create timed track - pid %d has exceeded the limit", |
| 5411 | mPid); |
| 5412 | return false; |
| 5413 | } |
| 5414 | |
| 5415 | mTimedTrackCount++; |
| 5416 | return true; |
| 5417 | } |
| 5418 | |
| 5419 | // Release a slot for a timed audio track |
| 5420 | void AudioFlinger::Client::releaseTimedTrack() |
| 5421 | { |
| 5422 | Mutex::Autolock _l(mTimedTrackLock); |
| 5423 | mTimedTrackCount--; |
| 5424 | } |
| 5425 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5426 | // ---------------------------------------------------------------------------- |
| 5427 | |
| 5428 | AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger, |
| 5429 | const sp<IAudioFlingerClient>& client, |
| 5430 | pid_t pid) |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 5431 | : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5432 | { |
| 5433 | } |
| 5434 | |
| 5435 | AudioFlinger::NotificationClient::~NotificationClient() |
| 5436 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5437 | } |
| 5438 | |
| 5439 | void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who) |
| 5440 | { |
| 5441 | sp<NotificationClient> keep(this); |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 5442 | mAudioFlinger->removeNotificationClient(mPid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5443 | } |
| 5444 | |
| 5445 | // ---------------------------------------------------------------------------- |
| 5446 | |
| 5447 | AudioFlinger::TrackHandle::TrackHandle(const sp<AudioFlinger::PlaybackThread::Track>& track) |
| 5448 | : BnAudioTrack(), |
| 5449 | mTrack(track) |
| 5450 | { |
| 5451 | } |
| 5452 | |
| 5453 | AudioFlinger::TrackHandle::~TrackHandle() { |
| 5454 | // just stop the track on deletion, associated resources |
| 5455 | // will be freed from the main thread once all pending buffers have |
| 5456 | // been played. Unless it's not in the active track list, in which |
| 5457 | // case we free everything now... |
| 5458 | mTrack->destroy(); |
| 5459 | } |
| 5460 | |
Glenn Kasten | 90716c5 | 2012-01-26 13:40:12 -0800 | [diff] [blame] | 5461 | sp<IMemory> AudioFlinger::TrackHandle::getCblk() const { |
| 5462 | return mTrack->getCblk(); |
| 5463 | } |
| 5464 | |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5465 | status_t AudioFlinger::TrackHandle::start() { |
| 5466 | return mTrack->start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5467 | } |
| 5468 | |
| 5469 | void AudioFlinger::TrackHandle::stop() { |
| 5470 | mTrack->stop(); |
| 5471 | } |
| 5472 | |
| 5473 | void AudioFlinger::TrackHandle::flush() { |
| 5474 | mTrack->flush(); |
| 5475 | } |
| 5476 | |
| 5477 | void AudioFlinger::TrackHandle::mute(bool e) { |
| 5478 | mTrack->mute(e); |
| 5479 | } |
| 5480 | |
| 5481 | void AudioFlinger::TrackHandle::pause() { |
| 5482 | mTrack->pause(); |
| 5483 | } |
| 5484 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5485 | status_t AudioFlinger::TrackHandle::attachAuxEffect(int EffectId) |
| 5486 | { |
| 5487 | return mTrack->attachAuxEffect(EffectId); |
| 5488 | } |
| 5489 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5490 | status_t AudioFlinger::TrackHandle::allocateTimedBuffer(size_t size, |
| 5491 | sp<IMemory>* buffer) { |
| 5492 | if (!mTrack->isTimedTrack()) |
| 5493 | return INVALID_OPERATION; |
| 5494 | |
| 5495 | PlaybackThread::TimedTrack* tt = |
| 5496 | reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get()); |
| 5497 | return tt->allocateTimedBuffer(size, buffer); |
| 5498 | } |
| 5499 | |
| 5500 | status_t AudioFlinger::TrackHandle::queueTimedBuffer(const sp<IMemory>& buffer, |
| 5501 | int64_t pts) { |
| 5502 | if (!mTrack->isTimedTrack()) |
| 5503 | return INVALID_OPERATION; |
| 5504 | |
| 5505 | PlaybackThread::TimedTrack* tt = |
| 5506 | reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get()); |
| 5507 | return tt->queueTimedBuffer(buffer, pts); |
| 5508 | } |
| 5509 | |
| 5510 | status_t AudioFlinger::TrackHandle::setMediaTimeTransform( |
| 5511 | const LinearTransform& xform, int target) { |
| 5512 | |
| 5513 | if (!mTrack->isTimedTrack()) |
| 5514 | return INVALID_OPERATION; |
| 5515 | |
| 5516 | PlaybackThread::TimedTrack* tt = |
| 5517 | reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get()); |
| 5518 | return tt->setMediaTimeTransform( |
| 5519 | xform, static_cast<TimedAudioTrack::TargetTimeline>(target)); |
| 5520 | } |
| 5521 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5522 | status_t AudioFlinger::TrackHandle::onTransact( |
| 5523 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 5524 | { |
| 5525 | return BnAudioTrack::onTransact(code, data, reply, flags); |
| 5526 | } |
| 5527 | |
| 5528 | // ---------------------------------------------------------------------------- |
| 5529 | |
| 5530 | sp<IAudioRecord> AudioFlinger::openRecord( |
| 5531 | pid_t pid, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 5532 | audio_io_handle_t input, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5533 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5534 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 5535 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5536 | int frameCount, |
Glenn Kasten | a075db4 | 2012-03-06 11:22:44 -0800 | [diff] [blame] | 5537 | IAudioFlinger::track_flags_t flags, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5538 | int *sessionId, |
| 5539 | status_t *status) |
| 5540 | { |
| 5541 | sp<RecordThread::RecordTrack> recordTrack; |
| 5542 | sp<RecordHandle> recordHandle; |
| 5543 | sp<Client> client; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5544 | status_t lStatus; |
| 5545 | RecordThread *thread; |
| 5546 | size_t inFrameCount; |
| 5547 | int lSessionId; |
| 5548 | |
| 5549 | // check calling permissions |
| 5550 | if (!recordingAllowed()) { |
| 5551 | lStatus = PERMISSION_DENIED; |
| 5552 | goto Exit; |
| 5553 | } |
| 5554 | |
| 5555 | // add client to list |
| 5556 | { // scope for mLock |
| 5557 | Mutex::Autolock _l(mLock); |
| 5558 | thread = checkRecordThread_l(input); |
| 5559 | if (thread == NULL) { |
| 5560 | lStatus = BAD_VALUE; |
| 5561 | goto Exit; |
| 5562 | } |
| 5563 | |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 5564 | client = registerPid_l(pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5565 | |
| 5566 | // If no audio session id is provided, create one here |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5567 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5568 | lSessionId = *sessionId; |
| 5569 | } else { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5570 | lSessionId = nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5571 | if (sessionId != NULL) { |
| 5572 | *sessionId = lSessionId; |
| 5573 | } |
| 5574 | } |
| 5575 | // 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] | 5576 | recordTrack = thread->createRecordTrack_l(client, |
| 5577 | sampleRate, |
| 5578 | format, |
| 5579 | channelMask, |
| 5580 | frameCount, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5581 | lSessionId, |
| 5582 | &lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5583 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5584 | if (lStatus != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5585 | // remove local strong reference to Client before deleting the RecordTrack so that the Client |
| 5586 | // destructor is called by the TrackBase destructor with mLock held |
| 5587 | client.clear(); |
| 5588 | recordTrack.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5589 | goto Exit; |
| 5590 | } |
| 5591 | |
| 5592 | // return to handle to client |
| 5593 | recordHandle = new RecordHandle(recordTrack); |
| 5594 | lStatus = NO_ERROR; |
| 5595 | |
| 5596 | Exit: |
| 5597 | if (status) { |
| 5598 | *status = lStatus; |
| 5599 | } |
| 5600 | return recordHandle; |
| 5601 | } |
| 5602 | |
| 5603 | // ---------------------------------------------------------------------------- |
| 5604 | |
| 5605 | AudioFlinger::RecordHandle::RecordHandle(const sp<AudioFlinger::RecordThread::RecordTrack>& recordTrack) |
| 5606 | : BnAudioRecord(), |
| 5607 | mRecordTrack(recordTrack) |
| 5608 | { |
| 5609 | } |
| 5610 | |
| 5611 | AudioFlinger::RecordHandle::~RecordHandle() { |
| 5612 | stop(); |
| 5613 | } |
| 5614 | |
Glenn Kasten | 90716c5 | 2012-01-26 13:40:12 -0800 | [diff] [blame] | 5615 | sp<IMemory> AudioFlinger::RecordHandle::getCblk() const { |
| 5616 | return mRecordTrack->getCblk(); |
| 5617 | } |
| 5618 | |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5619 | status_t AudioFlinger::RecordHandle::start(int event, int triggerSession) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5620 | ALOGV("RecordHandle::start()"); |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5621 | return mRecordTrack->start((AudioSystem::sync_event_t)event, triggerSession); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5622 | } |
| 5623 | |
| 5624 | void AudioFlinger::RecordHandle::stop() { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5625 | ALOGV("RecordHandle::stop()"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5626 | mRecordTrack->stop(); |
| 5627 | } |
| 5628 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5629 | status_t AudioFlinger::RecordHandle::onTransact( |
| 5630 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 5631 | { |
| 5632 | return BnAudioRecord::onTransact(code, data, reply, flags); |
| 5633 | } |
| 5634 | |
| 5635 | // ---------------------------------------------------------------------------- |
| 5636 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5637 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, |
| 5638 | AudioStreamIn *input, |
| 5639 | uint32_t sampleRate, |
| 5640 | uint32_t channels, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 5641 | audio_io_handle_t id, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5642 | uint32_t device) : |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 5643 | ThreadBase(audioFlinger, id, device, RECORD), |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 5644 | mInput(input), mTrack(NULL), mResampler(NULL), mRsmpOutBuffer(NULL), mRsmpInBuffer(NULL), |
| 5645 | // mRsmpInIndex and mInputBytes set by readInputParameters() |
| 5646 | mReqChannelCount(popcount(channels)), |
| 5647 | mReqSampleRate(sampleRate) |
| 5648 | // mBytesRead is only meaningful while active, and so is cleared in start() |
| 5649 | // (but might be better to also clear here for dump?) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5650 | { |
Glenn Kasten | 480b468 | 2012-02-28 12:30:08 -0800 | [diff] [blame] | 5651 | snprintf(mName, kNameLength, "AudioIn_%X", id); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 5652 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5653 | readInputParameters(); |
| 5654 | } |
| 5655 | |
| 5656 | |
| 5657 | AudioFlinger::RecordThread::~RecordThread() |
| 5658 | { |
| 5659 | delete[] mRsmpInBuffer; |
Glenn Kasten | e9dd017 | 2012-01-27 18:08:45 -0800 | [diff] [blame] | 5660 | delete mResampler; |
| 5661 | delete[] mRsmpOutBuffer; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5662 | } |
| 5663 | |
| 5664 | void AudioFlinger::RecordThread::onFirstRef() |
| 5665 | { |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 5666 | run(mName, PRIORITY_URGENT_AUDIO); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5667 | } |
| 5668 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5669 | status_t AudioFlinger::RecordThread::readyToRun() |
| 5670 | { |
| 5671 | status_t status = initCheck(); |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5672 | ALOGW_IF(status != NO_ERROR,"RecordThread %p could not initialize", this); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5673 | return status; |
| 5674 | } |
| 5675 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5676 | bool AudioFlinger::RecordThread::threadLoop() |
| 5677 | { |
| 5678 | AudioBufferProvider::Buffer buffer; |
| 5679 | sp<RecordTrack> activeTrack; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5680 | Vector< sp<EffectChain> > effectChains; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5681 | |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 5682 | nsecs_t lastWarning = 0; |
| 5683 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 5684 | acquireWakeLock(); |
| 5685 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5686 | // start recording |
| 5687 | while (!exitPending()) { |
| 5688 | |
| 5689 | processConfigEvents(); |
| 5690 | |
| 5691 | { // scope for mLock |
| 5692 | Mutex::Autolock _l(mLock); |
| 5693 | checkForNewParameters_l(); |
| 5694 | if (mActiveTrack == 0 && mConfigEvents.isEmpty()) { |
| 5695 | if (!mStandby) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5696 | mInput->stream->common.standby(&mInput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5697 | mStandby = true; |
| 5698 | } |
| 5699 | |
| 5700 | if (exitPending()) break; |
| 5701 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 5702 | releaseWakeLock_l(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5703 | ALOGV("RecordThread: loop stopping"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5704 | // go to sleep |
| 5705 | mWaitWorkCV.wait(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5706 | ALOGV("RecordThread: loop starting"); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 5707 | acquireWakeLock_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5708 | continue; |
| 5709 | } |
| 5710 | if (mActiveTrack != 0) { |
| 5711 | if (mActiveTrack->mState == TrackBase::PAUSING) { |
| 5712 | if (!mStandby) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5713 | mInput->stream->common.standby(&mInput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5714 | mStandby = true; |
| 5715 | } |
| 5716 | mActiveTrack.clear(); |
| 5717 | mStartStopCond.broadcast(); |
| 5718 | } else if (mActiveTrack->mState == TrackBase::RESUMING) { |
| 5719 | if (mReqChannelCount != mActiveTrack->channelCount()) { |
| 5720 | mActiveTrack.clear(); |
| 5721 | mStartStopCond.broadcast(); |
| 5722 | } else if (mBytesRead != 0) { |
| 5723 | // record start succeeds only if first read from audio input |
| 5724 | // succeeds |
| 5725 | if (mBytesRead > 0) { |
| 5726 | mActiveTrack->mState = TrackBase::ACTIVE; |
| 5727 | } else { |
| 5728 | mActiveTrack.clear(); |
| 5729 | } |
| 5730 | mStartStopCond.broadcast(); |
| 5731 | } |
| 5732 | mStandby = false; |
| 5733 | } |
| 5734 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5735 | lockEffectChains_l(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5736 | } |
| 5737 | |
| 5738 | if (mActiveTrack != 0) { |
| 5739 | if (mActiveTrack->mState != TrackBase::ACTIVE && |
| 5740 | mActiveTrack->mState != TrackBase::RESUMING) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5741 | unlockEffectChains(effectChains); |
| 5742 | usleep(kRecordThreadSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5743 | continue; |
| 5744 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5745 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 5746 | effectChains[i]->process_l(); |
| 5747 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5748 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5749 | buffer.frameCount = mFrameCount; |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 5750 | if (CC_LIKELY(mActiveTrack->getNextBuffer(&buffer) == NO_ERROR)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5751 | size_t framesOut = buffer.frameCount; |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 5752 | if (mResampler == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5753 | // no resampling |
| 5754 | while (framesOut) { |
| 5755 | size_t framesIn = mFrameCount - mRsmpInIndex; |
| 5756 | if (framesIn) { |
| 5757 | int8_t *src = (int8_t *)mRsmpInBuffer + mRsmpInIndex * mFrameSize; |
| 5758 | int8_t *dst = buffer.i8 + (buffer.frameCount - framesOut) * mActiveTrack->mCblk->frameSize; |
| 5759 | if (framesIn > framesOut) |
| 5760 | framesIn = framesOut; |
| 5761 | mRsmpInIndex += framesIn; |
| 5762 | framesOut -= framesIn; |
| 5763 | if ((int)mChannelCount == mReqChannelCount || |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5764 | mFormat != AUDIO_FORMAT_PCM_16_BIT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5765 | memcpy(dst, src, framesIn * mFrameSize); |
| 5766 | } else { |
| 5767 | int16_t *src16 = (int16_t *)src; |
| 5768 | int16_t *dst16 = (int16_t *)dst; |
| 5769 | if (mChannelCount == 1) { |
| 5770 | while (framesIn--) { |
| 5771 | *dst16++ = *src16; |
| 5772 | *dst16++ = *src16++; |
| 5773 | } |
| 5774 | } else { |
| 5775 | while (framesIn--) { |
| 5776 | *dst16++ = (int16_t)(((int32_t)*src16 + (int32_t)*(src16 + 1)) >> 1); |
| 5777 | src16 += 2; |
| 5778 | } |
| 5779 | } |
| 5780 | } |
| 5781 | } |
| 5782 | if (framesOut && mFrameCount == mRsmpInIndex) { |
| 5783 | if (framesOut == mFrameCount && |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5784 | ((int)mChannelCount == mReqChannelCount || mFormat != AUDIO_FORMAT_PCM_16_BIT)) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5785 | mBytesRead = mInput->stream->read(mInput->stream, buffer.raw, mInputBytes); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5786 | framesOut = 0; |
| 5787 | } else { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5788 | mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5789 | mRsmpInIndex = 0; |
| 5790 | } |
| 5791 | if (mBytesRead < 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 5792 | ALOGE("Error reading audio input"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5793 | if (mActiveTrack->mState == TrackBase::ACTIVE) { |
| 5794 | // Force input into standby so that it tries to |
| 5795 | // recover at next read attempt |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5796 | mInput->stream->common.standby(&mInput->stream->common); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5797 | usleep(kRecordThreadSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5798 | } |
| 5799 | mRsmpInIndex = mFrameCount; |
| 5800 | framesOut = 0; |
| 5801 | buffer.frameCount = 0; |
| 5802 | } |
| 5803 | } |
| 5804 | } |
| 5805 | } else { |
| 5806 | // resampling |
| 5807 | |
| 5808 | memset(mRsmpOutBuffer, 0, framesOut * 2 * sizeof(int32_t)); |
| 5809 | // alter output frame count as if we were expecting stereo samples |
| 5810 | if (mChannelCount == 1 && mReqChannelCount == 1) { |
| 5811 | framesOut >>= 1; |
| 5812 | } |
| 5813 | mResampler->resample(mRsmpOutBuffer, framesOut, this); |
| 5814 | // ditherAndClamp() works as long as all buffers returned by mActiveTrack->getNextBuffer() |
| 5815 | // are 32 bit aligned which should be always true. |
| 5816 | if (mChannelCount == 2 && mReqChannelCount == 1) { |
Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 5817 | ditherAndClamp(mRsmpOutBuffer, mRsmpOutBuffer, framesOut); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5818 | // the resampler always outputs stereo samples: do post stereo to mono conversion |
| 5819 | int16_t *src = (int16_t *)mRsmpOutBuffer; |
| 5820 | int16_t *dst = buffer.i16; |
| 5821 | while (framesOut--) { |
| 5822 | *dst++ = (int16_t)(((int32_t)*src + (int32_t)*(src + 1)) >> 1); |
| 5823 | src += 2; |
| 5824 | } |
| 5825 | } else { |
Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 5826 | ditherAndClamp((int32_t *)buffer.raw, mRsmpOutBuffer, framesOut); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5827 | } |
| 5828 | |
| 5829 | } |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 5830 | if (mFramestoDrop == 0) { |
| 5831 | mActiveTrack->releaseBuffer(&buffer); |
| 5832 | } else { |
| 5833 | if (mFramestoDrop > 0) { |
| 5834 | mFramestoDrop -= buffer.frameCount; |
| 5835 | if (mFramestoDrop < 0) { |
| 5836 | mFramestoDrop = 0; |
| 5837 | } |
| 5838 | } |
| 5839 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5840 | mActiveTrack->overflow(); |
| 5841 | } |
| 5842 | // client isn't retrieving buffers fast enough |
| 5843 | else { |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 5844 | if (!mActiveTrack->setOverflow()) { |
| 5845 | nsecs_t now = systemTime(); |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 5846 | if ((now - lastWarning) > kWarningThrottleNs) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5847 | ALOGW("RecordThread: buffer overflow"); |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 5848 | lastWarning = now; |
| 5849 | } |
| 5850 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5851 | // Release the processor for a while before asking for a new buffer. |
| 5852 | // This will give the application more chance to read from the buffer and |
| 5853 | // clear the overflow. |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5854 | usleep(kRecordThreadSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5855 | } |
| 5856 | } |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 5857 | // enable changes in effect chain |
| 5858 | unlockEffectChains(effectChains); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5859 | effectChains.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5860 | } |
| 5861 | |
| 5862 | if (!mStandby) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5863 | mInput->stream->common.standby(&mInput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5864 | } |
| 5865 | mActiveTrack.clear(); |
| 5866 | |
| 5867 | mStartStopCond.broadcast(); |
| 5868 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 5869 | releaseWakeLock(); |
| 5870 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5871 | ALOGV("RecordThread %p exiting", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5872 | return false; |
| 5873 | } |
| 5874 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5875 | |
| 5876 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( |
| 5877 | const sp<AudioFlinger::Client>& client, |
| 5878 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5879 | audio_format_t format, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5880 | int channelMask, |
| 5881 | int frameCount, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5882 | int sessionId, |
| 5883 | status_t *status) |
| 5884 | { |
| 5885 | sp<RecordTrack> track; |
| 5886 | status_t lStatus; |
| 5887 | |
| 5888 | lStatus = initCheck(); |
| 5889 | if (lStatus != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 5890 | ALOGE("Audio driver not initialized."); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5891 | goto Exit; |
| 5892 | } |
| 5893 | |
| 5894 | { // scope for mLock |
| 5895 | Mutex::Autolock _l(mLock); |
| 5896 | |
| 5897 | track = new RecordTrack(this, client, sampleRate, |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame] | 5898 | format, channelMask, frameCount, sessionId); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5899 | |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 5900 | if (track->getCblk() == 0) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5901 | lStatus = NO_MEMORY; |
| 5902 | goto Exit; |
| 5903 | } |
| 5904 | |
| 5905 | mTrack = track.get(); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 5906 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 5907 | bool suspend = audio_is_bluetooth_sco_device( |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 5908 | (audio_devices_t)(mDevice & AUDIO_DEVICE_IN_ALL)) && mAudioFlinger->btNrecIsOff(); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 5909 | setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); |
| 5910 | setEffectSuspended_l(FX_IID_NS, suspend, sessionId); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5911 | } |
| 5912 | lStatus = NO_ERROR; |
| 5913 | |
| 5914 | Exit: |
| 5915 | if (status) { |
| 5916 | *status = lStatus; |
| 5917 | } |
| 5918 | return track; |
| 5919 | } |
| 5920 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 5921 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5922 | AudioSystem::sync_event_t event, |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 5923 | int triggerSession) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5924 | { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 5925 | ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 5926 | sp<ThreadBase> strongMe = this; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5927 | status_t status = NO_ERROR; |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 5928 | |
| 5929 | if (event == AudioSystem::SYNC_EVENT_NONE) { |
| 5930 | mSyncStartEvent.clear(); |
| 5931 | mFramestoDrop = 0; |
| 5932 | } else if (event != AudioSystem::SYNC_EVENT_SAME) { |
| 5933 | mSyncStartEvent = mAudioFlinger->createSyncEvent(event, |
| 5934 | triggerSession, |
| 5935 | recordTrack->sessionId(), |
| 5936 | syncStartEventCallback, |
| 5937 | this); |
| 5938 | mFramestoDrop = -1; |
| 5939 | } |
| 5940 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5941 | { |
Glenn Kasten | a7d8d6f | 2012-01-05 15:41:56 -0800 | [diff] [blame] | 5942 | AutoMutex lock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5943 | if (mActiveTrack != 0) { |
| 5944 | if (recordTrack != mActiveTrack.get()) { |
| 5945 | status = -EBUSY; |
| 5946 | } else if (mActiveTrack->mState == TrackBase::PAUSING) { |
| 5947 | mActiveTrack->mState = TrackBase::ACTIVE; |
| 5948 | } |
| 5949 | return status; |
| 5950 | } |
| 5951 | |
| 5952 | recordTrack->mState = TrackBase::IDLE; |
| 5953 | mActiveTrack = recordTrack; |
| 5954 | mLock.unlock(); |
| 5955 | status_t status = AudioSystem::startInput(mId); |
| 5956 | mLock.lock(); |
| 5957 | if (status != NO_ERROR) { |
| 5958 | mActiveTrack.clear(); |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 5959 | clearSyncStartEvent(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5960 | return status; |
| 5961 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5962 | mRsmpInIndex = mFrameCount; |
| 5963 | mBytesRead = 0; |
Eric Laurent | 243f5f9 | 2011-02-28 16:52:51 -0800 | [diff] [blame] | 5964 | if (mResampler != NULL) { |
| 5965 | mResampler->reset(); |
| 5966 | } |
| 5967 | mActiveTrack->mState = TrackBase::RESUMING; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5968 | // signal thread to start |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5969 | ALOGV("Signal record thread"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5970 | mWaitWorkCV.signal(); |
| 5971 | // do not wait for mStartStopCond if exiting |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 5972 | if (exitPending()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5973 | mActiveTrack.clear(); |
| 5974 | status = INVALID_OPERATION; |
| 5975 | goto startError; |
| 5976 | } |
| 5977 | mStartStopCond.wait(mLock); |
| 5978 | if (mActiveTrack == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5979 | ALOGV("Record failed to start"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5980 | status = BAD_VALUE; |
| 5981 | goto startError; |
| 5982 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5983 | ALOGV("Record started OK"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5984 | return status; |
| 5985 | } |
| 5986 | startError: |
| 5987 | AudioSystem::stopInput(mId); |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 5988 | clearSyncStartEvent(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5989 | return status; |
| 5990 | } |
| 5991 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 5992 | void AudioFlinger::RecordThread::clearSyncStartEvent() |
| 5993 | { |
| 5994 | if (mSyncStartEvent != 0) { |
| 5995 | mSyncStartEvent->cancel(); |
| 5996 | } |
| 5997 | mSyncStartEvent.clear(); |
| 5998 | } |
| 5999 | |
| 6000 | void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) |
| 6001 | { |
| 6002 | sp<SyncEvent> strongEvent = event.promote(); |
| 6003 | |
| 6004 | if (strongEvent != 0) { |
| 6005 | RecordThread *me = (RecordThread *)strongEvent->cookie(); |
| 6006 | me->handleSyncStartEvent(strongEvent); |
| 6007 | } |
| 6008 | } |
| 6009 | |
| 6010 | void AudioFlinger::RecordThread::handleSyncStartEvent(const sp<SyncEvent>& event) |
| 6011 | { |
| 6012 | ALOGV("handleSyncStartEvent() mActiveTrack %p session %d event->listenerSession() %d", |
| 6013 | mActiveTrack.get(), |
| 6014 | mActiveTrack.get() ? mActiveTrack->sessionId() : 0, |
| 6015 | event->listenerSession()); |
| 6016 | |
| 6017 | if (mActiveTrack != 0 && |
| 6018 | event == mSyncStartEvent) { |
| 6019 | // TODO: use actual buffer filling status instead of 2 buffers when info is available |
| 6020 | // from audio HAL |
| 6021 | mFramestoDrop = mFrameCount * 2; |
| 6022 | mSyncStartEvent.clear(); |
| 6023 | } |
| 6024 | } |
| 6025 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6026 | void AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6027 | ALOGV("RecordThread::stop"); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 6028 | sp<ThreadBase> strongMe = this; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6029 | { |
Glenn Kasten | a7d8d6f | 2012-01-05 15:41:56 -0800 | [diff] [blame] | 6030 | AutoMutex lock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6031 | if (mActiveTrack != 0 && recordTrack == mActiveTrack.get()) { |
| 6032 | mActiveTrack->mState = TrackBase::PAUSING; |
| 6033 | // do not wait for mStartStopCond if exiting |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 6034 | if (exitPending()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6035 | return; |
| 6036 | } |
| 6037 | mStartStopCond.wait(mLock); |
| 6038 | // if we have been restarted, recordTrack == mActiveTrack.get() here |
| 6039 | if (mActiveTrack == 0 || recordTrack != mActiveTrack.get()) { |
| 6040 | mLock.unlock(); |
| 6041 | AudioSystem::stopInput(mId); |
| 6042 | mLock.lock(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6043 | ALOGV("Record stopped OK"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6044 | } |
| 6045 | } |
| 6046 | } |
| 6047 | } |
| 6048 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 6049 | bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event) |
| 6050 | { |
| 6051 | return false; |
| 6052 | } |
| 6053 | |
| 6054 | status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event) |
| 6055 | { |
| 6056 | if (!isValidSyncEvent(event)) { |
| 6057 | return BAD_VALUE; |
| 6058 | } |
| 6059 | |
| 6060 | Mutex::Autolock _l(mLock); |
| 6061 | |
| 6062 | if (mTrack != NULL && event->triggerSession() == mTrack->sessionId()) { |
| 6063 | mTrack->setSyncEvent(event); |
| 6064 | return NO_ERROR; |
| 6065 | } |
| 6066 | return NAME_NOT_FOUND; |
| 6067 | } |
| 6068 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6069 | status_t AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) |
| 6070 | { |
| 6071 | const size_t SIZE = 256; |
| 6072 | char buffer[SIZE]; |
| 6073 | String8 result; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6074 | |
| 6075 | snprintf(buffer, SIZE, "\nInput thread %p internals\n", this); |
| 6076 | result.append(buffer); |
| 6077 | |
| 6078 | if (mActiveTrack != 0) { |
| 6079 | result.append("Active Track:\n"); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 6080 | 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] | 6081 | mActiveTrack->dump(buffer, SIZE); |
| 6082 | result.append(buffer); |
| 6083 | |
| 6084 | snprintf(buffer, SIZE, "In index: %d\n", mRsmpInIndex); |
| 6085 | result.append(buffer); |
| 6086 | snprintf(buffer, SIZE, "In size: %d\n", mInputBytes); |
| 6087 | result.append(buffer); |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 6088 | snprintf(buffer, SIZE, "Resampling: %d\n", (mResampler != NULL)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6089 | result.append(buffer); |
| 6090 | snprintf(buffer, SIZE, "Out channel count: %d\n", mReqChannelCount); |
| 6091 | result.append(buffer); |
| 6092 | snprintf(buffer, SIZE, "Out sample rate: %d\n", mReqSampleRate); |
| 6093 | result.append(buffer); |
| 6094 | |
| 6095 | |
| 6096 | } else { |
| 6097 | result.append("No record client\n"); |
| 6098 | } |
| 6099 | write(fd, result.string(), result.size()); |
| 6100 | |
| 6101 | dumpBase(fd, args); |
Eric Laurent | 1d2bff0 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 6102 | dumpEffectChains(fd, args); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6103 | |
| 6104 | return NO_ERROR; |
| 6105 | } |
| 6106 | |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 6107 | // AudioBufferProvider interface |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 6108 | status_t AudioFlinger::RecordThread::getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6109 | { |
| 6110 | size_t framesReq = buffer->frameCount; |
| 6111 | size_t framesReady = mFrameCount - mRsmpInIndex; |
| 6112 | int channelCount; |
| 6113 | |
| 6114 | if (framesReady == 0) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6115 | mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6116 | if (mBytesRead < 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 6117 | ALOGE("RecordThread::getNextBuffer() Error reading audio input"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6118 | if (mActiveTrack->mState == TrackBase::ACTIVE) { |
| 6119 | // Force input into standby so that it tries to |
| 6120 | // recover at next read attempt |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6121 | mInput->stream->common.standby(&mInput->stream->common); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6122 | usleep(kRecordThreadSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6123 | } |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 6124 | buffer->raw = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6125 | buffer->frameCount = 0; |
| 6126 | return NOT_ENOUGH_DATA; |
| 6127 | } |
| 6128 | mRsmpInIndex = 0; |
| 6129 | framesReady = mFrameCount; |
| 6130 | } |
| 6131 | |
| 6132 | if (framesReq > framesReady) { |
| 6133 | framesReq = framesReady; |
| 6134 | } |
| 6135 | |
| 6136 | if (mChannelCount == 1 && mReqChannelCount == 2) { |
| 6137 | channelCount = 1; |
| 6138 | } else { |
| 6139 | channelCount = 2; |
| 6140 | } |
| 6141 | buffer->raw = mRsmpInBuffer + mRsmpInIndex * channelCount; |
| 6142 | buffer->frameCount = framesReq; |
| 6143 | return NO_ERROR; |
| 6144 | } |
| 6145 | |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 6146 | // AudioBufferProvider interface |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6147 | void AudioFlinger::RecordThread::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
| 6148 | { |
| 6149 | mRsmpInIndex += buffer->frameCount; |
| 6150 | buffer->frameCount = 0; |
| 6151 | } |
| 6152 | |
| 6153 | bool AudioFlinger::RecordThread::checkForNewParameters_l() |
| 6154 | { |
| 6155 | bool reconfig = false; |
| 6156 | |
| 6157 | while (!mNewParameters.isEmpty()) { |
| 6158 | status_t status = NO_ERROR; |
| 6159 | String8 keyValuePair = mNewParameters[0]; |
| 6160 | AudioParameter param = AudioParameter(keyValuePair); |
| 6161 | int value; |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 6162 | audio_format_t reqFormat = mFormat; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6163 | int reqSamplingRate = mReqSampleRate; |
| 6164 | int reqChannelCount = mReqChannelCount; |
| 6165 | |
| 6166 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 6167 | reqSamplingRate = value; |
| 6168 | reconfig = true; |
| 6169 | } |
| 6170 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 6171 | reqFormat = (audio_format_t) value; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6172 | reconfig = true; |
| 6173 | } |
| 6174 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6175 | reqChannelCount = popcount(value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6176 | reconfig = true; |
| 6177 | } |
| 6178 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 6179 | // 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] | 6180 | // size depends on frame count and correct behavior would not be guaranteed |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6181 | // if frame count is changed after track creation |
| 6182 | if (mActiveTrack != 0) { |
| 6183 | status = INVALID_OPERATION; |
| 6184 | } else { |
| 6185 | reconfig = true; |
| 6186 | } |
| 6187 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6188 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 6189 | // forward device change to effects that have requested to be |
| 6190 | // aware of attached audio device. |
| 6191 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 6192 | mEffectChains[i]->setDevice_l(value); |
| 6193 | } |
| 6194 | // store input device and output device but do not forward output device to audio HAL. |
| 6195 | // Note that status is ignored by the caller for output device |
| 6196 | // (see AudioFlinger::setParameters() |
| 6197 | if (value & AUDIO_DEVICE_OUT_ALL) { |
| 6198 | mDevice &= (uint32_t)~(value & AUDIO_DEVICE_OUT_ALL); |
| 6199 | status = BAD_VALUE; |
| 6200 | } else { |
| 6201 | mDevice &= (uint32_t)~(value & AUDIO_DEVICE_IN_ALL); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 6202 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 6203 | if (mTrack != NULL) { |
| 6204 | bool suspend = audio_is_bluetooth_sco_device( |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 6205 | (audio_devices_t)value) && mAudioFlinger->btNrecIsOff(); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 6206 | setEffectSuspended_l(FX_IID_AEC, suspend, mTrack->sessionId()); |
| 6207 | setEffectSuspended_l(FX_IID_NS, suspend, mTrack->sessionId()); |
| 6208 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6209 | } |
| 6210 | mDevice |= (uint32_t)value; |
| 6211 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6212 | if (status == NO_ERROR) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6213 | status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6214 | if (status == INVALID_OPERATION) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 6215 | mInput->stream->common.standby(&mInput->stream->common); |
| 6216 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 6217 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6218 | } |
| 6219 | if (reconfig) { |
| 6220 | if (status == BAD_VALUE && |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6221 | reqFormat == mInput->stream->common.get_format(&mInput->stream->common) && |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6222 | reqFormat == AUDIO_FORMAT_PCM_16_BIT && |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6223 | ((int)mInput->stream->common.get_sample_rate(&mInput->stream->common) <= (2 * reqSamplingRate)) && |
Glenn Kasten | 53d76db | 2012-03-08 12:32:47 -0800 | [diff] [blame] | 6224 | popcount(mInput->stream->common.get_channels(&mInput->stream->common)) <= FCC_2 && |
| 6225 | (reqChannelCount <= FCC_2)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6226 | status = NO_ERROR; |
| 6227 | } |
| 6228 | if (status == NO_ERROR) { |
| 6229 | readInputParameters(); |
| 6230 | sendConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED); |
| 6231 | } |
| 6232 | } |
| 6233 | } |
| 6234 | |
| 6235 | mNewParameters.removeAt(0); |
| 6236 | |
| 6237 | mParamStatus = status; |
| 6238 | mParamCond.signal(); |
Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 6239 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() |
| 6240 | // 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] | 6241 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6242 | } |
| 6243 | return reconfig; |
| 6244 | } |
| 6245 | |
| 6246 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) |
| 6247 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6248 | char *s; |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6249 | String8 out_s8 = String8(); |
| 6250 | |
| 6251 | Mutex::Autolock _l(mLock); |
| 6252 | if (initCheck() != NO_ERROR) { |
| 6253 | return out_s8; |
| 6254 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6255 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6256 | s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6257 | out_s8 = String8(s); |
| 6258 | free(s); |
| 6259 | return out_s8; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6260 | } |
| 6261 | |
| 6262 | void AudioFlinger::RecordThread::audioConfigChanged_l(int event, int param) { |
| 6263 | AudioSystem::OutputDescriptor desc; |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 6264 | void *param2 = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6265 | |
| 6266 | switch (event) { |
| 6267 | case AudioSystem::INPUT_OPENED: |
| 6268 | case AudioSystem::INPUT_CONFIG_CHANGED: |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 6269 | desc.channels = mChannelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6270 | desc.samplingRate = mSampleRate; |
| 6271 | desc.format = mFormat; |
| 6272 | desc.frameCount = mFrameCount; |
| 6273 | desc.latency = 0; |
| 6274 | param2 = &desc; |
| 6275 | break; |
| 6276 | |
| 6277 | case AudioSystem::INPUT_CLOSED: |
| 6278 | default: |
| 6279 | break; |
| 6280 | } |
| 6281 | mAudioFlinger->audioConfigChanged_l(event, mId, param2); |
| 6282 | } |
| 6283 | |
| 6284 | void AudioFlinger::RecordThread::readInputParameters() |
| 6285 | { |
Glenn Kasten | e9dd017 | 2012-01-27 18:08:45 -0800 | [diff] [blame] | 6286 | delete mRsmpInBuffer; |
| 6287 | // mRsmpInBuffer is always assigned a new[] below |
| 6288 | delete mRsmpOutBuffer; |
| 6289 | mRsmpOutBuffer = NULL; |
| 6290 | delete mResampler; |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 6291 | mResampler = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6292 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6293 | mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 6294 | mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); |
| 6295 | mChannelCount = (uint16_t)popcount(mChannelMask); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6296 | mFormat = mInput->stream->common.get_format(&mInput->stream->common); |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 6297 | mFrameSize = audio_stream_frame_size(&mInput->stream->common); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6298 | mInputBytes = mInput->stream->common.get_buffer_size(&mInput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6299 | mFrameCount = mInputBytes / mFrameSize; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 6300 | mNormalFrameCount = mFrameCount; // not used by record, but used by input effects |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6301 | mRsmpInBuffer = new int16_t[mFrameCount * mChannelCount]; |
| 6302 | |
Glenn Kasten | 53d76db | 2012-03-08 12:32:47 -0800 | [diff] [blame] | 6303 | if (mSampleRate != mReqSampleRate && mChannelCount <= FCC_2 && mReqChannelCount <= FCC_2) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6304 | { |
| 6305 | int channelCount; |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 6306 | // optimization: if mono to mono, use the resampler in stereo to stereo mode to avoid |
| 6307 | // stereo to mono post process as the resampler always outputs stereo. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6308 | if (mChannelCount == 1 && mReqChannelCount == 2) { |
| 6309 | channelCount = 1; |
| 6310 | } else { |
| 6311 | channelCount = 2; |
| 6312 | } |
| 6313 | mResampler = AudioResampler::create(16, channelCount, mReqSampleRate); |
| 6314 | mResampler->setSampleRate(mSampleRate); |
| 6315 | mResampler->setVolume(AudioMixer::UNITY_GAIN, AudioMixer::UNITY_GAIN); |
| 6316 | mRsmpOutBuffer = new int32_t[mFrameCount * 2]; |
| 6317 | |
| 6318 | // optmization: if mono to mono, alter input frame count as if we were inputing stereo samples |
| 6319 | if (mChannelCount == 1 && mReqChannelCount == 1) { |
| 6320 | mFrameCount >>= 1; |
| 6321 | } |
| 6322 | |
| 6323 | } |
| 6324 | mRsmpInIndex = mFrameCount; |
| 6325 | } |
| 6326 | |
| 6327 | unsigned int AudioFlinger::RecordThread::getInputFramesLost() |
| 6328 | { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6329 | Mutex::Autolock _l(mLock); |
| 6330 | if (initCheck() != NO_ERROR) { |
| 6331 | return 0; |
| 6332 | } |
| 6333 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6334 | return mInput->stream->get_input_frames_lost(mInput->stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6335 | } |
| 6336 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6337 | uint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) |
| 6338 | { |
| 6339 | Mutex::Autolock _l(mLock); |
| 6340 | uint32_t result = 0; |
| 6341 | if (getEffectChain_l(sessionId) != 0) { |
| 6342 | result = EFFECT_SESSION; |
| 6343 | } |
| 6344 | |
| 6345 | if (mTrack != NULL && sessionId == mTrack->sessionId()) { |
| 6346 | result |= TRACK_SESSION; |
| 6347 | } |
| 6348 | |
| 6349 | return result; |
| 6350 | } |
| 6351 | |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 6352 | AudioFlinger::RecordThread::RecordTrack* AudioFlinger::RecordThread::track() |
| 6353 | { |
| 6354 | Mutex::Autolock _l(mLock); |
| 6355 | return mTrack; |
| 6356 | } |
| 6357 | |
Glenn Kasten | aed850d | 2012-01-26 09:46:34 -0800 | [diff] [blame] | 6358 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::getInput() const |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6359 | { |
| 6360 | Mutex::Autolock _l(mLock); |
| 6361 | return mInput; |
| 6362 | } |
| 6363 | |
| 6364 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() |
| 6365 | { |
| 6366 | Mutex::Autolock _l(mLock); |
| 6367 | AudioStreamIn *input = mInput; |
| 6368 | mInput = NULL; |
| 6369 | return input; |
| 6370 | } |
| 6371 | |
| 6372 | // 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] | 6373 | audio_stream_t* AudioFlinger::RecordThread::stream() const |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6374 | { |
| 6375 | if (mInput == NULL) { |
| 6376 | return NULL; |
| 6377 | } |
| 6378 | return &mInput->stream->common; |
| 6379 | } |
| 6380 | |
| 6381 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6382 | // ---------------------------------------------------------------------------- |
| 6383 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6384 | audio_module_handle_t AudioFlinger::loadHwModule(const char *name) |
| 6385 | { |
| 6386 | if (!settingsAllowed()) { |
| 6387 | return 0; |
| 6388 | } |
| 6389 | Mutex::Autolock _l(mLock); |
| 6390 | return loadHwModule_l(name); |
| 6391 | } |
| 6392 | |
| 6393 | // loadHwModule_l() must be called with AudioFlinger::mLock held |
| 6394 | audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name) |
| 6395 | { |
| 6396 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 6397 | if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) { |
| 6398 | ALOGW("loadHwModule() module %s already loaded", name); |
| 6399 | return mAudioHwDevs.keyAt(i); |
| 6400 | } |
| 6401 | } |
| 6402 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6403 | audio_hw_device_t *dev; |
| 6404 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6405 | int rc = load_audio_interface(name, &dev); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6406 | if (rc) { |
| 6407 | ALOGI("loadHwModule() error %d loading module %s ", rc, name); |
| 6408 | return 0; |
| 6409 | } |
| 6410 | |
| 6411 | mHardwareStatus = AUDIO_HW_INIT; |
| 6412 | rc = dev->init_check(dev); |
| 6413 | mHardwareStatus = AUDIO_HW_IDLE; |
| 6414 | if (rc) { |
| 6415 | ALOGI("loadHwModule() init check error %d for module %s ", rc, name); |
| 6416 | return 0; |
| 6417 | } |
| 6418 | |
| 6419 | if ((mMasterVolumeSupportLvl != MVS_NONE) && |
| 6420 | (NULL != dev->set_master_volume)) { |
| 6421 | AutoMutex lock(mHardwareLock); |
| 6422 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
| 6423 | dev->set_master_volume(dev, mMasterVolume); |
| 6424 | mHardwareStatus = AUDIO_HW_IDLE; |
| 6425 | } |
| 6426 | |
| 6427 | audio_module_handle_t handle = nextUniqueId(); |
| 6428 | mAudioHwDevs.add(handle, new AudioHwDevice(name, dev)); |
| 6429 | |
| 6430 | ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d", |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6431 | name, dev->common.module->name, dev->common.module->id, handle); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6432 | |
| 6433 | return handle; |
| 6434 | |
| 6435 | } |
| 6436 | |
| 6437 | audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module, |
| 6438 | audio_devices_t *pDevices, |
| 6439 | uint32_t *pSamplingRate, |
| 6440 | audio_format_t *pFormat, |
| 6441 | audio_channel_mask_t *pChannelMask, |
| 6442 | uint32_t *pLatencyMs, |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 6443 | audio_output_flags_t flags) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6444 | { |
| 6445 | status_t status; |
| 6446 | PlaybackThread *thread = NULL; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6447 | struct audio_config config = { |
| 6448 | sample_rate: pSamplingRate ? *pSamplingRate : 0, |
| 6449 | channel_mask: pChannelMask ? *pChannelMask : 0, |
| 6450 | format: pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT, |
| 6451 | }; |
| 6452 | audio_stream_out_t *outStream = NULL; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6453 | audio_hw_device_t *outHwDev; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6454 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6455 | ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %d, Channels %x, flags %x", |
| 6456 | module, |
Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 6457 | (pDevices != NULL) ? (int)*pDevices : 0, |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6458 | config.sample_rate, |
| 6459 | config.format, |
| 6460 | config.channel_mask, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6461 | flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6462 | |
| 6463 | if (pDevices == NULL || *pDevices == 0) { |
| 6464 | return 0; |
| 6465 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6466 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6467 | Mutex::Autolock _l(mLock); |
| 6468 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6469 | outHwDev = findSuitableHwDev_l(module, *pDevices); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6470 | if (outHwDev == NULL) |
| 6471 | return 0; |
| 6472 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6473 | audio_io_handle_t id = nextUniqueId(); |
| 6474 | |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 6475 | mHardwareStatus = AUDIO_HW_OUTPUT_OPEN; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6476 | |
| 6477 | status = outHwDev->open_output_stream(outHwDev, |
| 6478 | id, |
| 6479 | *pDevices, |
| 6480 | (audio_output_flags_t)flags, |
| 6481 | &config, |
| 6482 | &outStream); |
| 6483 | |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 6484 | mHardwareStatus = AUDIO_HW_IDLE; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6485 | 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] | 6486 | outStream, |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6487 | config.sample_rate, |
| 6488 | config.format, |
| 6489 | config.channel_mask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6490 | status); |
| 6491 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6492 | if (status == NO_ERROR && outStream != NULL) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6493 | AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6494 | |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 6495 | if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) || |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6496 | (config.format != AUDIO_FORMAT_PCM_16_BIT) || |
| 6497 | (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6498 | thread = new DirectOutputThread(this, output, id, *pDevices); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6499 | ALOGV("openOutput() created direct output: ID %d thread %p", id, thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6500 | } else { |
| 6501 | thread = new MixerThread(this, output, id, *pDevices); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6502 | ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6503 | } |
| 6504 | mPlaybackThreads.add(id, thread); |
| 6505 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6506 | if (pSamplingRate != NULL) *pSamplingRate = config.sample_rate; |
| 6507 | if (pFormat != NULL) *pFormat = config.format; |
| 6508 | if (pChannelMask != NULL) *pChannelMask = config.channel_mask; |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 6509 | if (pLatencyMs != NULL) *pLatencyMs = thread->latency(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6510 | |
| 6511 | // notify client processes of the new output creation |
| 6512 | thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6513 | |
| 6514 | // the first primary output opened designates the primary hw device |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 6515 | if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6516 | ALOGI("Using module %d has the primary audio interface", module); |
| 6517 | mPrimaryHardwareDev = outHwDev; |
| 6518 | |
| 6519 | AutoMutex lock(mHardwareLock); |
| 6520 | mHardwareStatus = AUDIO_HW_SET_MODE; |
| 6521 | outHwDev->set_mode(outHwDev, mMode); |
| 6522 | |
| 6523 | // Determine the level of master volume support the primary audio HAL has, |
| 6524 | // and set the initial master volume at the same time. |
| 6525 | float initialVolume = 1.0; |
| 6526 | mMasterVolumeSupportLvl = MVS_NONE; |
| 6527 | |
| 6528 | mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME; |
| 6529 | if ((NULL != outHwDev->get_master_volume) && |
| 6530 | (NO_ERROR == outHwDev->get_master_volume(outHwDev, &initialVolume))) { |
| 6531 | mMasterVolumeSupportLvl = MVS_FULL; |
| 6532 | } else { |
| 6533 | mMasterVolumeSupportLvl = MVS_SETONLY; |
| 6534 | initialVolume = 1.0; |
| 6535 | } |
| 6536 | |
| 6537 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
| 6538 | if ((NULL == outHwDev->set_master_volume) || |
| 6539 | (NO_ERROR != outHwDev->set_master_volume(outHwDev, initialVolume))) { |
| 6540 | mMasterVolumeSupportLvl = MVS_NONE; |
| 6541 | } |
| 6542 | // now that we have a primary device, initialize master volume on other devices |
| 6543 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 6544 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
| 6545 | |
| 6546 | if ((dev != mPrimaryHardwareDev) && |
| 6547 | (NULL != dev->set_master_volume)) { |
| 6548 | dev->set_master_volume(dev, initialVolume); |
| 6549 | } |
| 6550 | } |
| 6551 | mHardwareStatus = AUDIO_HW_IDLE; |
| 6552 | mMasterVolumeSW = (MVS_NONE == mMasterVolumeSupportLvl) |
| 6553 | ? initialVolume |
| 6554 | : 1.0; |
| 6555 | mMasterVolume = initialVolume; |
| 6556 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6557 | return id; |
| 6558 | } |
| 6559 | |
| 6560 | return 0; |
| 6561 | } |
| 6562 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6563 | audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1, |
| 6564 | audio_io_handle_t output2) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6565 | { |
| 6566 | Mutex::Autolock _l(mLock); |
| 6567 | MixerThread *thread1 = checkMixerThread_l(output1); |
| 6568 | MixerThread *thread2 = checkMixerThread_l(output2); |
| 6569 | |
| 6570 | if (thread1 == NULL || thread2 == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6571 | 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] | 6572 | return 0; |
| 6573 | } |
| 6574 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6575 | audio_io_handle_t id = nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6576 | DuplicatingThread *thread = new DuplicatingThread(this, thread1, id); |
| 6577 | thread->addOutputTrack(thread2); |
| 6578 | mPlaybackThreads.add(id, thread); |
| 6579 | // notify client processes of the new output creation |
| 6580 | thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); |
| 6581 | return id; |
| 6582 | } |
| 6583 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6584 | status_t AudioFlinger::closeOutput(audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6585 | { |
| 6586 | // keep strong reference on the playback thread so that |
| 6587 | // it is not destroyed while exit() is executed |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 6588 | sp<PlaybackThread> thread; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6589 | { |
| 6590 | Mutex::Autolock _l(mLock); |
| 6591 | thread = checkPlaybackThread_l(output); |
| 6592 | if (thread == NULL) { |
| 6593 | return BAD_VALUE; |
| 6594 | } |
| 6595 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6596 | ALOGV("closeOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6597 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6598 | if (thread->type() == ThreadBase::MIXER) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6599 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6600 | if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6601 | DuplicatingThread *dupThread = (DuplicatingThread *)mPlaybackThreads.valueAt(i).get(); |
| 6602 | dupThread->removeOutputTrack((MixerThread *)thread.get()); |
| 6603 | } |
| 6604 | } |
| 6605 | } |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 6606 | audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6607 | mPlaybackThreads.removeItem(output); |
| 6608 | } |
| 6609 | thread->exit(); |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 6610 | // The thread entity (active unit of execution) is no longer running here, |
| 6611 | // but the ThreadBase container still exists. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6612 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6613 | if (thread->type() != ThreadBase::DUPLICATING) { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6614 | AudioStreamOut *out = thread->clearOutput(); |
Glenn Kasten | 5798d4e | 2012-03-08 12:18:35 -0800 | [diff] [blame] | 6615 | ALOG_ASSERT(out != NULL, "out shouldn't be NULL"); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6616 | // from now on thread->mOutput is NULL |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6617 | out->hwDev->close_output_stream(out->hwDev, out->stream); |
| 6618 | delete out; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6619 | } |
| 6620 | return NO_ERROR; |
| 6621 | } |
| 6622 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6623 | status_t AudioFlinger::suspendOutput(audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6624 | { |
| 6625 | Mutex::Autolock _l(mLock); |
| 6626 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 6627 | |
| 6628 | if (thread == NULL) { |
| 6629 | return BAD_VALUE; |
| 6630 | } |
| 6631 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6632 | ALOGV("suspendOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6633 | thread->suspend(); |
| 6634 | |
| 6635 | return NO_ERROR; |
| 6636 | } |
| 6637 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6638 | status_t AudioFlinger::restoreOutput(audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6639 | { |
| 6640 | Mutex::Autolock _l(mLock); |
| 6641 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 6642 | |
| 6643 | if (thread == NULL) { |
| 6644 | return BAD_VALUE; |
| 6645 | } |
| 6646 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6647 | ALOGV("restoreOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6648 | |
| 6649 | thread->restore(); |
| 6650 | |
| 6651 | return NO_ERROR; |
| 6652 | } |
| 6653 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6654 | audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module, |
| 6655 | audio_devices_t *pDevices, |
| 6656 | uint32_t *pSamplingRate, |
| 6657 | audio_format_t *pFormat, |
| 6658 | uint32_t *pChannelMask) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6659 | { |
| 6660 | status_t status; |
| 6661 | RecordThread *thread = NULL; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6662 | struct audio_config config = { |
| 6663 | sample_rate: pSamplingRate ? *pSamplingRate : 0, |
| 6664 | channel_mask: pChannelMask ? *pChannelMask : 0, |
| 6665 | format: pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT, |
| 6666 | }; |
| 6667 | uint32_t reqSamplingRate = config.sample_rate; |
| 6668 | audio_format_t reqFormat = config.format; |
| 6669 | audio_channel_mask_t reqChannels = config.channel_mask; |
| 6670 | audio_stream_in_t *inStream = NULL; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6671 | audio_hw_device_t *inHwDev; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6672 | |
| 6673 | if (pDevices == NULL || *pDevices == 0) { |
| 6674 | return 0; |
| 6675 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6676 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6677 | Mutex::Autolock _l(mLock); |
| 6678 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6679 | inHwDev = findSuitableHwDev_l(module, *pDevices); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6680 | if (inHwDev == NULL) |
| 6681 | return 0; |
| 6682 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6683 | audio_io_handle_t id = nextUniqueId(); |
| 6684 | |
| 6685 | status = inHwDev->open_input_stream(inHwDev, id, *pDevices, &config, |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6686 | &inStream); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6687 | 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] | 6688 | inStream, |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6689 | config.sample_rate, |
| 6690 | config.format, |
| 6691 | config.channel_mask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6692 | status); |
| 6693 | |
| 6694 | // If the input could not be opened with the requested parameters and we can handle the conversion internally, |
| 6695 | // try to open again with the proposed parameters. The AudioFlinger can resample the input and do mono to stereo |
| 6696 | // or stereo to mono conversions on 16 bit PCM inputs. |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6697 | if (status == BAD_VALUE && |
| 6698 | reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT && |
| 6699 | (config.sample_rate <= 2 * reqSamplingRate) && |
| 6700 | (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannels) <= FCC_2)) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6701 | ALOGV("openInput() reopening with proposed sampling rate and channels"); |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6702 | inStream = NULL; |
| 6703 | status = inHwDev->open_input_stream(inHwDev, id, *pDevices, &config, &inStream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6704 | } |
| 6705 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6706 | if (status == NO_ERROR && inStream != NULL) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6707 | AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream); |
| 6708 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6709 | // Start record thread |
| 6710 | // RecorThread require both input and output device indication to forward to audio |
| 6711 | // pre processing modules |
| 6712 | uint32_t device = (*pDevices) | primaryOutputDevice_l(); |
| 6713 | thread = new RecordThread(this, |
| 6714 | input, |
| 6715 | reqSamplingRate, |
| 6716 | reqChannels, |
| 6717 | id, |
| 6718 | device); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6719 | mRecordThreads.add(id, thread); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6720 | ALOGV("openInput() created record thread: ID %d thread %p", id, thread); |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 6721 | if (pSamplingRate != NULL) *pSamplingRate = reqSamplingRate; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6722 | if (pFormat != NULL) *pFormat = config.format; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6723 | if (pChannelMask != NULL) *pChannelMask = reqChannels; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6724 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6725 | input->stream->common.standby(&input->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6726 | |
| 6727 | // notify client processes of the new input creation |
| 6728 | thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED); |
| 6729 | return id; |
| 6730 | } |
| 6731 | |
| 6732 | return 0; |
| 6733 | } |
| 6734 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6735 | status_t AudioFlinger::closeInput(audio_io_handle_t input) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6736 | { |
| 6737 | // keep strong reference on the record thread so that |
| 6738 | // it is not destroyed while exit() is executed |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 6739 | sp<RecordThread> thread; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6740 | { |
| 6741 | Mutex::Autolock _l(mLock); |
| 6742 | thread = checkRecordThread_l(input); |
| 6743 | if (thread == NULL) { |
| 6744 | return BAD_VALUE; |
| 6745 | } |
| 6746 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6747 | ALOGV("closeInput() %d", input); |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 6748 | audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6749 | mRecordThreads.removeItem(input); |
| 6750 | } |
| 6751 | thread->exit(); |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 6752 | // The thread entity (active unit of execution) is no longer running here, |
| 6753 | // but the ThreadBase container still exists. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6754 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6755 | AudioStreamIn *in = thread->clearInput(); |
Glenn Kasten | 5798d4e | 2012-03-08 12:18:35 -0800 | [diff] [blame] | 6756 | ALOG_ASSERT(in != NULL, "in shouldn't be NULL"); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6757 | // from now on thread->mInput is NULL |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6758 | in->hwDev->close_input_stream(in->hwDev, in->stream); |
| 6759 | delete in; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6760 | |
| 6761 | return NO_ERROR; |
| 6762 | } |
| 6763 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6764 | 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] | 6765 | { |
| 6766 | Mutex::Autolock _l(mLock); |
| 6767 | MixerThread *dstThread = checkMixerThread_l(output); |
| 6768 | if (dstThread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6769 | ALOGW("setStreamOutput() bad output id %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6770 | return BAD_VALUE; |
| 6771 | } |
| 6772 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6773 | ALOGV("setStreamOutput() stream %d to output %d", stream, output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6774 | audioConfigChanged_l(AudioSystem::STREAM_CONFIG_CHANGED, output, &stream); |
| 6775 | |
| 6776 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 6777 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 6778 | if (thread != dstThread && thread->type() != ThreadBase::DIRECT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6779 | MixerThread *srcThread = (MixerThread *)thread; |
| 6780 | srcThread->invalidateTracks(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6781 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6782 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6783 | |
| 6784 | return NO_ERROR; |
| 6785 | } |
| 6786 | |
| 6787 | |
| 6788 | int AudioFlinger::newAudioSessionId() |
| 6789 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6790 | return nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6791 | } |
| 6792 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6793 | void AudioFlinger::acquireAudioSessionId(int audioSession) |
| 6794 | { |
| 6795 | Mutex::Autolock _l(mLock); |
Glenn Kasten | bb00192 | 2012-02-03 11:10:26 -0800 | [diff] [blame] | 6796 | pid_t caller = IPCThreadState::self()->getCallingPid(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6797 | ALOGV("acquiring %d from %d", audioSession, caller); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 6798 | size_t num = mAudioSessionRefs.size(); |
| 6799 | for (size_t i = 0; i< num; i++) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6800 | AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 6801 | if (ref->mSessionid == audioSession && ref->mPid == caller) { |
| 6802 | ref->mCnt++; |
| 6803 | ALOGV(" incremented refcount to %d", ref->mCnt); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6804 | return; |
| 6805 | } |
| 6806 | } |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 6807 | mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller)); |
| 6808 | ALOGV(" added new entry for %d", audioSession); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6809 | } |
| 6810 | |
| 6811 | void AudioFlinger::releaseAudioSessionId(int audioSession) |
| 6812 | { |
| 6813 | Mutex::Autolock _l(mLock); |
Glenn Kasten | bb00192 | 2012-02-03 11:10:26 -0800 | [diff] [blame] | 6814 | pid_t caller = IPCThreadState::self()->getCallingPid(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6815 | ALOGV("releasing %d from %d", audioSession, caller); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 6816 | size_t num = mAudioSessionRefs.size(); |
| 6817 | for (size_t i = 0; i< num; i++) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6818 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 6819 | if (ref->mSessionid == audioSession && ref->mPid == caller) { |
| 6820 | ref->mCnt--; |
| 6821 | ALOGV(" decremented refcount to %d", ref->mCnt); |
| 6822 | if (ref->mCnt == 0) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6823 | mAudioSessionRefs.removeAt(i); |
| 6824 | delete ref; |
| 6825 | purgeStaleEffects_l(); |
| 6826 | } |
| 6827 | return; |
| 6828 | } |
| 6829 | } |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6830 | ALOGW("session id %d not found for pid %d", audioSession, caller); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6831 | } |
| 6832 | |
| 6833 | void AudioFlinger::purgeStaleEffects_l() { |
| 6834 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6835 | ALOGV("purging stale effects"); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6836 | |
| 6837 | Vector< sp<EffectChain> > chains; |
| 6838 | |
| 6839 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 6840 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 6841 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 6842 | sp<EffectChain> ec = t->mEffectChains[j]; |
Marco Nelissen | 0270b18 | 2011-08-12 14:14:39 -0700 | [diff] [blame] | 6843 | if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) { |
| 6844 | chains.push(ec); |
| 6845 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6846 | } |
| 6847 | } |
| 6848 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 6849 | sp<RecordThread> t = mRecordThreads.valueAt(i); |
| 6850 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 6851 | sp<EffectChain> ec = t->mEffectChains[j]; |
| 6852 | chains.push(ec); |
| 6853 | } |
| 6854 | } |
| 6855 | |
| 6856 | for (size_t i = 0; i < chains.size(); i++) { |
| 6857 | sp<EffectChain> ec = chains[i]; |
| 6858 | int sessionid = ec->sessionId(); |
| 6859 | sp<ThreadBase> t = ec->mThread.promote(); |
| 6860 | if (t == 0) { |
| 6861 | continue; |
| 6862 | } |
| 6863 | size_t numsessionrefs = mAudioSessionRefs.size(); |
| 6864 | bool found = false; |
| 6865 | for (size_t k = 0; k < numsessionrefs; k++) { |
| 6866 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(k); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 6867 | if (ref->mSessionid == sessionid) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6868 | ALOGV(" session %d still exists for %d with %d refs", |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 6869 | sessionid, ref->mPid, ref->mCnt); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6870 | found = true; |
| 6871 | break; |
| 6872 | } |
| 6873 | } |
| 6874 | if (!found) { |
| 6875 | // remove all effects from the chain |
| 6876 | while (ec->mEffects.size()) { |
| 6877 | sp<EffectModule> effect = ec->mEffects[0]; |
| 6878 | effect->unPin(); |
| 6879 | Mutex::Autolock _l (t->mLock); |
| 6880 | t->removeEffect_l(effect); |
| 6881 | for (size_t j = 0; j < effect->mHandles.size(); j++) { |
| 6882 | sp<EffectHandle> handle = effect->mHandles[j].promote(); |
| 6883 | if (handle != 0) { |
| 6884 | handle->mEffect.clear(); |
Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 6885 | if (handle->mHasControl && handle->mEnabled) { |
| 6886 | t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId()); |
| 6887 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6888 | } |
| 6889 | } |
| 6890 | AudioSystem::unregisterEffect(effect->id()); |
| 6891 | } |
| 6892 | } |
| 6893 | } |
| 6894 | return; |
| 6895 | } |
| 6896 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6897 | // checkPlaybackThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6898 | AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6899 | { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 6900 | return mPlaybackThreads.valueFor(output).get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6901 | } |
| 6902 | |
| 6903 | // checkMixerThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6904 | AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6905 | { |
| 6906 | PlaybackThread *thread = checkPlaybackThread_l(output); |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 6907 | return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6908 | } |
| 6909 | |
| 6910 | // checkRecordThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6911 | AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6912 | { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 6913 | return mRecordThreads.valueFor(input).get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6914 | } |
| 6915 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6916 | uint32_t AudioFlinger::nextUniqueId() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6917 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6918 | return android_atomic_inc(&mNextUniqueId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6919 | } |
| 6920 | |
Glenn Kasten | 02fe1bf | 2012-02-24 15:42:17 -0800 | [diff] [blame] | 6921 | AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6922 | { |
| 6923 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 6924 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6925 | AudioStreamOut *output = thread->getOutput(); |
| 6926 | if (output != NULL && output->hwDev == mPrimaryHardwareDev) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6927 | return thread; |
| 6928 | } |
| 6929 | } |
| 6930 | return NULL; |
| 6931 | } |
| 6932 | |
Glenn Kasten | 02fe1bf | 2012-02-24 15:42:17 -0800 | [diff] [blame] | 6933 | uint32_t AudioFlinger::primaryOutputDevice_l() const |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6934 | { |
| 6935 | PlaybackThread *thread = primaryPlaybackThread_l(); |
| 6936 | |
| 6937 | if (thread == NULL) { |
| 6938 | return 0; |
| 6939 | } |
| 6940 | |
| 6941 | return thread->device(); |
| 6942 | } |
| 6943 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 6944 | sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type, |
| 6945 | int triggerSession, |
| 6946 | int listenerSession, |
| 6947 | sync_event_callback_t callBack, |
| 6948 | void *cookie) |
| 6949 | { |
| 6950 | Mutex::Autolock _l(mLock); |
| 6951 | |
| 6952 | sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie); |
| 6953 | status_t playStatus = NAME_NOT_FOUND; |
| 6954 | status_t recStatus = NAME_NOT_FOUND; |
| 6955 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 6956 | playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event); |
| 6957 | if (playStatus == NO_ERROR) { |
| 6958 | return event; |
| 6959 | } |
| 6960 | } |
| 6961 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 6962 | recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event); |
| 6963 | if (recStatus == NO_ERROR) { |
| 6964 | return event; |
| 6965 | } |
| 6966 | } |
| 6967 | if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) { |
| 6968 | mPendingSyncEvents.add(event); |
| 6969 | } else { |
| 6970 | ALOGV("createSyncEvent() invalid event %d", event->type()); |
| 6971 | event.clear(); |
| 6972 | } |
| 6973 | return event; |
| 6974 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6975 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6976 | // ---------------------------------------------------------------------------- |
| 6977 | // Effect management |
| 6978 | // ---------------------------------------------------------------------------- |
| 6979 | |
| 6980 | |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 6981 | status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6982 | { |
| 6983 | Mutex::Autolock _l(mLock); |
| 6984 | return EffectQueryNumberEffects(numEffects); |
| 6985 | } |
| 6986 | |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 6987 | status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6988 | { |
| 6989 | Mutex::Autolock _l(mLock); |
| 6990 | return EffectQueryEffect(index, descriptor); |
| 6991 | } |
| 6992 | |
Glenn Kasten | 5e92a78 | 2012-01-30 07:40:52 -0800 | [diff] [blame] | 6993 | status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid, |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 6994 | effect_descriptor_t *descriptor) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6995 | { |
| 6996 | Mutex::Autolock _l(mLock); |
| 6997 | return EffectGetDescriptor(pUuid, descriptor); |
| 6998 | } |
| 6999 | |
| 7000 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7001 | sp<IEffect> AudioFlinger::createEffect(pid_t pid, |
| 7002 | effect_descriptor_t *pDesc, |
| 7003 | const sp<IEffectClient>& effectClient, |
| 7004 | int32_t priority, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 7005 | audio_io_handle_t io, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7006 | int sessionId, |
| 7007 | status_t *status, |
| 7008 | int *id, |
| 7009 | int *enabled) |
| 7010 | { |
| 7011 | status_t lStatus = NO_ERROR; |
| 7012 | sp<EffectHandle> handle; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7013 | effect_descriptor_t desc; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7014 | |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 7015 | ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d", |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7016 | pid, effectClient.get(), priority, sessionId, io); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7017 | |
| 7018 | if (pDesc == NULL) { |
| 7019 | lStatus = BAD_VALUE; |
| 7020 | goto Exit; |
| 7021 | } |
| 7022 | |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7023 | // check audio settings permission for global effects |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7024 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7025 | lStatus = PERMISSION_DENIED; |
| 7026 | goto Exit; |
| 7027 | } |
| 7028 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7029 | // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7030 | // 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] | 7031 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7032 | lStatus = PERMISSION_DENIED; |
| 7033 | goto Exit; |
| 7034 | } |
| 7035 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7036 | if (io == 0) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7037 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7038 | // output must be specified by AudioPolicyManager when using session |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7039 | // AUDIO_SESSION_OUTPUT_STAGE |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7040 | lStatus = BAD_VALUE; |
| 7041 | goto Exit; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7042 | } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7043 | // if the output returned by getOutputForEffect() is removed before we lock the |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7044 | // mutex below, the call to checkPlaybackThread_l(io) below will detect it |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7045 | // and we will exit safely |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7046 | io = AudioSystem::getOutputForEffect(&desc); |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7047 | } |
| 7048 | } |
| 7049 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7050 | { |
| 7051 | Mutex::Autolock _l(mLock); |
| 7052 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7053 | |
| 7054 | if (!EffectIsNullUuid(&pDesc->uuid)) { |
| 7055 | // if uuid is specified, request effect descriptor |
| 7056 | lStatus = EffectGetDescriptor(&pDesc->uuid, &desc); |
| 7057 | if (lStatus < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7058 | ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7059 | goto Exit; |
| 7060 | } |
| 7061 | } else { |
| 7062 | // if uuid is not specified, look for an available implementation |
| 7063 | // of the required type in effect factory |
| 7064 | if (EffectIsNullUuid(&pDesc->type)) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7065 | ALOGW("createEffect() no effect type"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7066 | lStatus = BAD_VALUE; |
| 7067 | goto Exit; |
| 7068 | } |
| 7069 | uint32_t numEffects = 0; |
| 7070 | effect_descriptor_t d; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7071 | d.flags = 0; // prevent compiler warning |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7072 | bool found = false; |
| 7073 | |
| 7074 | lStatus = EffectQueryNumberEffects(&numEffects); |
| 7075 | if (lStatus < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7076 | ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7077 | goto Exit; |
| 7078 | } |
| 7079 | for (uint32_t i = 0; i < numEffects; i++) { |
| 7080 | lStatus = EffectQueryEffect(i, &desc); |
| 7081 | if (lStatus < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7082 | ALOGW("createEffect() error %d from EffectQueryEffect", lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7083 | continue; |
| 7084 | } |
| 7085 | if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) { |
| 7086 | // If matching type found save effect descriptor. If the session is |
| 7087 | // 0 and the effect is not auxiliary, continue enumeration in case |
| 7088 | // an auxiliary version of this effect type is available |
| 7089 | found = true; |
| 7090 | memcpy(&d, &desc, sizeof(effect_descriptor_t)); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7091 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX || |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7092 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7093 | break; |
| 7094 | } |
| 7095 | } |
| 7096 | } |
| 7097 | if (!found) { |
| 7098 | lStatus = BAD_VALUE; |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7099 | ALOGW("createEffect() effect not found"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7100 | goto Exit; |
| 7101 | } |
| 7102 | // For same effect type, chose auxiliary version over insert version if |
| 7103 | // connect to output mix (Compliance to OpenSL ES) |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7104 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7105 | (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7106 | memcpy(&desc, &d, sizeof(effect_descriptor_t)); |
| 7107 | } |
| 7108 | } |
| 7109 | |
| 7110 | // 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] | 7111 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7112 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7113 | lStatus = INVALID_OPERATION; |
| 7114 | goto Exit; |
| 7115 | } |
| 7116 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7117 | // check recording permission for visualizer |
| 7118 | if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) && |
| 7119 | !recordingAllowed()) { |
| 7120 | lStatus = PERMISSION_DENIED; |
| 7121 | goto Exit; |
| 7122 | } |
| 7123 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7124 | // return effect descriptor |
| 7125 | memcpy(pDesc, &desc, sizeof(effect_descriptor_t)); |
| 7126 | |
| 7127 | // If output is not specified try to find a matching audio session ID in one of the |
| 7128 | // output threads. |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7129 | // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX |
| 7130 | // because of code checking output when entering the function. |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7131 | // Note: io is never 0 when creating an effect on an input |
| 7132 | if (io == 0) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 7133 | // look for the thread where the specified audio session is present |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7134 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 7135 | if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7136 | io = mPlaybackThreads.keyAt(i); |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7137 | break; |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 7138 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7139 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7140 | if (io == 0) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 7141 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 7142 | if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { |
| 7143 | io = mRecordThreads.keyAt(i); |
| 7144 | break; |
| 7145 | } |
| 7146 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7147 | } |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7148 | // If no output thread contains the requested session ID, default to |
| 7149 | // first output. The effect chain will be moved to the correct output |
| 7150 | // thread when a track with the same session ID is created |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7151 | if (io == 0 && mPlaybackThreads.size()) { |
| 7152 | io = mPlaybackThreads.keyAt(0); |
| 7153 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7154 | ALOGV("createEffect() got io %d for effect %s", io, desc.name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7155 | } |
| 7156 | ThreadBase *thread = checkRecordThread_l(io); |
| 7157 | if (thread == NULL) { |
| 7158 | thread = checkPlaybackThread_l(io); |
| 7159 | if (thread == NULL) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 7160 | ALOGE("createEffect() unknown output thread"); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7161 | lStatus = BAD_VALUE; |
| 7162 | goto Exit; |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7163 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7164 | } |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7165 | |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 7166 | sp<Client> client = registerPid_l(pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7167 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7168 | // create effect on selected output thread |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7169 | handle = thread->createEffect_l(client, effectClient, priority, sessionId, |
| 7170 | &desc, enabled, &lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7171 | if (handle != 0 && id != NULL) { |
| 7172 | *id = handle->id(); |
| 7173 | } |
| 7174 | } |
| 7175 | |
| 7176 | Exit: |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 7177 | if (status != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7178 | *status = lStatus; |
| 7179 | } |
| 7180 | return handle; |
| 7181 | } |
| 7182 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 7183 | status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput, |
| 7184 | audio_io_handle_t dstOutput) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7185 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7186 | ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7187 | sessionId, srcOutput, dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7188 | Mutex::Autolock _l(mLock); |
| 7189 | if (srcOutput == dstOutput) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7190 | ALOGW("moveEffects() same dst and src outputs %d", dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7191 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7192 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7193 | PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput); |
| 7194 | if (srcThread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7195 | ALOGW("moveEffects() bad srcOutput %d", srcOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7196 | return BAD_VALUE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7197 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7198 | PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput); |
| 7199 | if (dstThread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7200 | ALOGW("moveEffects() bad dstOutput %d", dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7201 | return BAD_VALUE; |
| 7202 | } |
| 7203 | |
| 7204 | Mutex::Autolock _dl(dstThread->mLock); |
| 7205 | Mutex::Autolock _sl(srcThread->mLock); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7206 | moveEffectChain_l(sessionId, srcThread, dstThread, false); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7207 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7208 | return NO_ERROR; |
| 7209 | } |
| 7210 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7211 | // moveEffectChain_l must be called with both srcThread and dstThread mLocks held |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7212 | status_t AudioFlinger::moveEffectChain_l(int sessionId, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7213 | AudioFlinger::PlaybackThread *srcThread, |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 7214 | AudioFlinger::PlaybackThread *dstThread, |
| 7215 | bool reRegister) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7216 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7217 | ALOGV("moveEffectChain_l() session %d from thread %p to thread %p", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7218 | sessionId, srcThread, dstThread); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7219 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7220 | sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7221 | if (chain == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7222 | 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] | 7223 | sessionId, srcThread); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7224 | return INVALID_OPERATION; |
| 7225 | } |
| 7226 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 7227 | // 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] | 7228 | // 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] | 7229 | // 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] | 7230 | // removed. |
| 7231 | srcThread->removeEffectChain_l(chain); |
| 7232 | |
| 7233 | // transfer all effects one by one so that new effect chain is created on new thread with |
| 7234 | // correct buffer sizes and audio parameters and effect engines reconfigured accordingly |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 7235 | audio_io_handle_t dstOutput = dstThread->id(); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 7236 | sp<EffectChain> dstChain; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7237 | uint32_t strategy = 0; // prevent compiler warning |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7238 | sp<EffectModule> effect = chain->getEffectFromId_l(0); |
| 7239 | while (effect != 0) { |
| 7240 | srcThread->removeEffect_l(effect); |
| 7241 | dstThread->addEffect_l(effect); |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 7242 | // removeEffect_l() has stopped the effect if it was active so it must be restarted |
| 7243 | if (effect->state() == EffectModule::ACTIVE || |
| 7244 | effect->state() == EffectModule::STOPPING) { |
| 7245 | effect->start(); |
| 7246 | } |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 7247 | // if the move request is not received from audio policy manager, the effect must be |
| 7248 | // re-registered with the new strategy and output |
| 7249 | if (dstChain == 0) { |
| 7250 | dstChain = effect->chain().promote(); |
| 7251 | if (dstChain == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7252 | ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get()); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 7253 | srcThread->addEffect_l(effect); |
| 7254 | return NO_INIT; |
| 7255 | } |
| 7256 | strategy = dstChain->strategy(); |
| 7257 | } |
| 7258 | if (reRegister) { |
| 7259 | AudioSystem::unregisterEffect(effect->id()); |
| 7260 | AudioSystem::registerEffect(&effect->desc(), |
| 7261 | dstOutput, |
| 7262 | strategy, |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7263 | sessionId, |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 7264 | effect->id()); |
| 7265 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7266 | effect = chain->getEffectFromId_l(0); |
| 7267 | } |
| 7268 | |
| 7269 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7270 | } |
| 7271 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7272 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7273 | // PlaybackThread::createEffect_l() must be called with AudioFlinger::mLock held |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7274 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7275 | const sp<AudioFlinger::Client>& client, |
| 7276 | const sp<IEffectClient>& effectClient, |
| 7277 | int32_t priority, |
| 7278 | int sessionId, |
| 7279 | effect_descriptor_t *desc, |
| 7280 | int *enabled, |
| 7281 | status_t *status |
| 7282 | ) |
| 7283 | { |
| 7284 | sp<EffectModule> effect; |
| 7285 | sp<EffectHandle> handle; |
| 7286 | status_t lStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7287 | sp<EffectChain> chain; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7288 | bool chainCreated = false; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7289 | bool effectCreated = false; |
| 7290 | bool effectRegistered = false; |
| 7291 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7292 | lStatus = initCheck(); |
| 7293 | if (lStatus != NO_ERROR) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7294 | ALOGW("createEffect_l() Audio driver not initialized."); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7295 | goto Exit; |
| 7296 | } |
| 7297 | |
| 7298 | // Do not allow effects with session ID 0 on direct output or duplicating threads |
| 7299 | // 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] | 7300 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && mType != MIXER) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7301 | ALOGW("createEffect_l() Cannot add auxiliary effect %s to session %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7302 | desc->name, sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7303 | lStatus = BAD_VALUE; |
| 7304 | goto Exit; |
| 7305 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7306 | // 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] | 7307 | 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] | 7308 | 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] | 7309 | desc->name, desc->flags, mType); |
| 7310 | lStatus = BAD_VALUE; |
| 7311 | goto Exit; |
| 7312 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7313 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7314 | 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] | 7315 | |
| 7316 | { // scope for mLock |
| 7317 | Mutex::Autolock _l(mLock); |
| 7318 | |
| 7319 | // check for existing effect chain with the requested audio session |
| 7320 | chain = getEffectChain_l(sessionId); |
| 7321 | if (chain == 0) { |
| 7322 | // create a new chain for this session |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7323 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7324 | chain = new EffectChain(this, sessionId); |
| 7325 | addEffectChain_l(chain); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7326 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 7327 | chainCreated = true; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7328 | } else { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7329 | effect = chain->getEffectFromDesc_l(desc); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7330 | } |
| 7331 | |
Glenn Kasten | 7fc9a6f | 2012-01-10 10:46:34 -0800 | [diff] [blame] | 7332 | 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] | 7333 | |
| 7334 | if (effect == 0) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7335 | int id = mAudioFlinger->nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7336 | // Check CPU and memory usage |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7337 | lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7338 | if (lStatus != NO_ERROR) { |
| 7339 | goto Exit; |
| 7340 | } |
| 7341 | effectRegistered = true; |
| 7342 | // create a new effect module if none present in the chain |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7343 | effect = new EffectModule(this, chain, desc, id, sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7344 | lStatus = effect->status(); |
| 7345 | if (lStatus != NO_ERROR) { |
| 7346 | goto Exit; |
| 7347 | } |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7348 | lStatus = chain->addEffect_l(effect); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7349 | if (lStatus != NO_ERROR) { |
| 7350 | goto Exit; |
| 7351 | } |
| 7352 | effectCreated = true; |
| 7353 | |
| 7354 | effect->setDevice(mDevice); |
| 7355 | effect->setMode(mAudioFlinger->getMode()); |
| 7356 | } |
| 7357 | // create effect handle and connect it to effect module |
| 7358 | handle = new EffectHandle(effect, client, effectClient, priority); |
| 7359 | lStatus = effect->addHandle(handle); |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 7360 | if (enabled != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7361 | *enabled = (int)effect->isEnabled(); |
| 7362 | } |
| 7363 | } |
| 7364 | |
| 7365 | Exit: |
| 7366 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7367 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7368 | if (effectCreated) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7369 | chain->removeEffect_l(effect); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7370 | } |
| 7371 | if (effectRegistered) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7372 | AudioSystem::unregisterEffect(effect->id()); |
| 7373 | } |
| 7374 | if (chainCreated) { |
| 7375 | removeEffectChain_l(chain); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7376 | } |
| 7377 | handle.clear(); |
| 7378 | } |
| 7379 | |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 7380 | if (status != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7381 | *status = lStatus; |
| 7382 | } |
| 7383 | return handle; |
| 7384 | } |
| 7385 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7386 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId) |
| 7387 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7388 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 7389 | return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7390 | } |
| 7391 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7392 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and |
| 7393 | // PlaybackThread::mLock held |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7394 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7395 | { |
| 7396 | // check for existing effect chain with the requested audio session |
| 7397 | int sessionId = effect->sessionId(); |
| 7398 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 7399 | bool chainCreated = false; |
| 7400 | |
| 7401 | if (chain == 0) { |
| 7402 | // create a new chain for this session |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7403 | ALOGV("addEffect_l() new effect chain for session %d", sessionId); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7404 | chain = new EffectChain(this, sessionId); |
| 7405 | addEffectChain_l(chain); |
| 7406 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 7407 | chainCreated = true; |
| 7408 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7409 | 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] | 7410 | |
| 7411 | if (chain->getEffectFromId_l(effect->id()) != 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7412 | ALOGW("addEffect_l() %p effect %s already present in chain %p", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7413 | this, effect->desc().name, chain.get()); |
| 7414 | return BAD_VALUE; |
| 7415 | } |
| 7416 | |
| 7417 | status_t status = chain->addEffect_l(effect); |
| 7418 | if (status != NO_ERROR) { |
| 7419 | if (chainCreated) { |
| 7420 | removeEffectChain_l(chain); |
| 7421 | } |
| 7422 | return status; |
| 7423 | } |
| 7424 | |
| 7425 | effect->setDevice(mDevice); |
| 7426 | effect->setMode(mAudioFlinger->getMode()); |
| 7427 | return NO_ERROR; |
| 7428 | } |
| 7429 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7430 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7431 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7432 | ALOGV("removeEffect_l() %p effect %p", this, effect.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7433 | effect_descriptor_t desc = effect->desc(); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7434 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7435 | detachAuxEffect_l(effect->id()); |
| 7436 | } |
| 7437 | |
| 7438 | sp<EffectChain> chain = effect->chain().promote(); |
| 7439 | if (chain != 0) { |
| 7440 | // remove effect chain if removing last effect |
| 7441 | if (chain->removeEffect_l(effect) == 0) { |
| 7442 | removeEffectChain_l(chain); |
| 7443 | } |
| 7444 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7445 | 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] | 7446 | } |
| 7447 | } |
| 7448 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7449 | void AudioFlinger::ThreadBase::lockEffectChains_l( |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 7450 | Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7451 | { |
| 7452 | effectChains = mEffectChains; |
| 7453 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7454 | mEffectChains[i]->lock(); |
| 7455 | } |
| 7456 | } |
| 7457 | |
| 7458 | void AudioFlinger::ThreadBase::unlockEffectChains( |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 7459 | const Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7460 | { |
| 7461 | for (size_t i = 0; i < effectChains.size(); i++) { |
| 7462 | effectChains[i]->unlock(); |
| 7463 | } |
| 7464 | } |
| 7465 | |
| 7466 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId) |
| 7467 | { |
| 7468 | Mutex::Autolock _l(mLock); |
| 7469 | return getEffectChain_l(sessionId); |
| 7470 | } |
| 7471 | |
| 7472 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) |
| 7473 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7474 | size_t size = mEffectChains.size(); |
| 7475 | for (size_t i = 0; i < size; i++) { |
| 7476 | if (mEffectChains[i]->sessionId() == sessionId) { |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 7477 | return mEffectChains[i]; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7478 | } |
| 7479 | } |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 7480 | return 0; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7481 | } |
| 7482 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 7483 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7484 | { |
| 7485 | Mutex::Autolock _l(mLock); |
| 7486 | size_t size = mEffectChains.size(); |
| 7487 | for (size_t i = 0; i < size; i++) { |
| 7488 | mEffectChains[i]->setMode_l(mode); |
| 7489 | } |
| 7490 | } |
| 7491 | |
| 7492 | void AudioFlinger::ThreadBase::disconnectEffect(const sp<EffectModule>& effect, |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7493 | const wp<EffectHandle>& handle, |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 7494 | bool unpinIfLast) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7495 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7496 | Mutex::Autolock _l(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7497 | ALOGV("disconnectEffect() %p effect %p", this, effect.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7498 | // delete the effect module if removing last handle on it |
| 7499 | if (effect->removeHandle(handle) == 0) { |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 7500 | if (!effect->isPinned() || unpinIfLast) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7501 | removeEffect_l(effect); |
| 7502 | AudioSystem::unregisterEffect(effect->id()); |
| 7503 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7504 | } |
| 7505 | } |
| 7506 | |
| 7507 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 7508 | { |
| 7509 | int session = chain->sessionId(); |
| 7510 | int16_t *buffer = mMixBuffer; |
| 7511 | bool ownsBuffer = false; |
| 7512 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7513 | 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] | 7514 | if (session > 0) { |
| 7515 | // Only one effect chain can be present in direct output thread and it uses |
| 7516 | // the mix buffer as input |
| 7517 | if (mType != DIRECT) { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 7518 | size_t numSamples = mNormalFrameCount * mChannelCount; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7519 | buffer = new int16_t[numSamples]; |
| 7520 | memset(buffer, 0, numSamples * sizeof(int16_t)); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7521 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7522 | ownsBuffer = true; |
| 7523 | } |
| 7524 | |
| 7525 | // Attach all tracks with same session ID to this chain. |
| 7526 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 7527 | sp<Track> track = mTracks[i]; |
| 7528 | if (session == track->sessionId()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7529 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7530 | track->setMainBuffer(buffer); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7531 | chain->incTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7532 | } |
| 7533 | } |
| 7534 | |
| 7535 | // indicate all active tracks in the chain |
| 7536 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 7537 | sp<Track> track = mActiveTracks[i].promote(); |
| 7538 | if (track == 0) continue; |
| 7539 | if (session == track->sessionId()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7540 | ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7541 | chain->incActiveTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7542 | } |
| 7543 | } |
| 7544 | } |
| 7545 | |
| 7546 | chain->setInBuffer(buffer, ownsBuffer); |
| 7547 | chain->setOutBuffer(mMixBuffer); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7548 | // 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] | 7549 | // 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] | 7550 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before |
| 7551 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7552 | // after track specific effects and before output stage |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7553 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and |
| 7554 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7555 | // Effect chain for other sessions are inserted at beginning of effect |
| 7556 | // chains list to be processed before output mix effects. Relative order between other |
| 7557 | // sessions is not important |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7558 | size_t size = mEffectChains.size(); |
| 7559 | size_t i = 0; |
| 7560 | for (i = 0; i < size; i++) { |
| 7561 | if (mEffectChains[i]->sessionId() < session) break; |
| 7562 | } |
| 7563 | mEffectChains.insertAt(chain, i); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7564 | checkSuspendOnAddEffectChain_l(chain); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7565 | |
| 7566 | return NO_ERROR; |
| 7567 | } |
| 7568 | |
| 7569 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 7570 | { |
| 7571 | int session = chain->sessionId(); |
| 7572 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7573 | 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] | 7574 | |
| 7575 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7576 | if (chain == mEffectChains[i]) { |
| 7577 | mEffectChains.removeAt(i); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7578 | // detach all active tracks from the chain |
| 7579 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 7580 | sp<Track> track = mActiveTracks[i].promote(); |
| 7581 | if (track == 0) continue; |
| 7582 | if (session == track->sessionId()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7583 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7584 | chain.get(), session); |
| 7585 | chain->decActiveTrackCnt(); |
| 7586 | } |
| 7587 | } |
| 7588 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7589 | // detach all tracks with same session ID from this chain |
| 7590 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 7591 | sp<Track> track = mTracks[i]; |
| 7592 | if (session == track->sessionId()) { |
| 7593 | track->setMainBuffer(mMixBuffer); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7594 | chain->decTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7595 | } |
| 7596 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7597 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7598 | } |
| 7599 | } |
| 7600 | return mEffectChains.size(); |
| 7601 | } |
| 7602 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7603 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( |
| 7604 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7605 | { |
| 7606 | Mutex::Autolock _l(mLock); |
| 7607 | return attachAuxEffect_l(track, EffectId); |
| 7608 | } |
| 7609 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7610 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( |
| 7611 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7612 | { |
| 7613 | status_t status = NO_ERROR; |
| 7614 | |
| 7615 | if (EffectId == 0) { |
| 7616 | track->setAuxBuffer(0, NULL); |
| 7617 | } else { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7618 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX |
| 7619 | sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7620 | if (effect != 0) { |
| 7621 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7622 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); |
| 7623 | } else { |
| 7624 | status = INVALID_OPERATION; |
| 7625 | } |
| 7626 | } else { |
| 7627 | status = BAD_VALUE; |
| 7628 | } |
| 7629 | } |
| 7630 | return status; |
| 7631 | } |
| 7632 | |
| 7633 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) |
| 7634 | { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 7635 | for (size_t i = 0; i < mTracks.size(); ++i) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7636 | sp<Track> track = mTracks[i]; |
| 7637 | if (track->auxEffectId() == effectId) { |
| 7638 | attachAuxEffect_l(track, 0); |
| 7639 | } |
| 7640 | } |
| 7641 | } |
| 7642 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7643 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 7644 | { |
| 7645 | // only one chain per input thread |
| 7646 | if (mEffectChains.size() != 0) { |
| 7647 | return INVALID_OPERATION; |
| 7648 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7649 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7650 | |
| 7651 | chain->setInBuffer(NULL); |
| 7652 | chain->setOutBuffer(NULL); |
| 7653 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7654 | checkSuspendOnAddEffectChain_l(chain); |
| 7655 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7656 | mEffectChains.add(chain); |
| 7657 | |
| 7658 | return NO_ERROR; |
| 7659 | } |
| 7660 | |
| 7661 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 7662 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7663 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7664 | ALOGW_IF(mEffectChains.size() != 1, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7665 | "removeEffectChain_l() %p invalid chain size %d on thread %p", |
| 7666 | chain.get(), mEffectChains.size(), this); |
| 7667 | if (mEffectChains.size() == 1) { |
| 7668 | mEffectChains.removeAt(0); |
| 7669 | } |
| 7670 | return 0; |
| 7671 | } |
| 7672 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7673 | // ---------------------------------------------------------------------------- |
| 7674 | // EffectModule implementation |
| 7675 | // ---------------------------------------------------------------------------- |
| 7676 | |
| 7677 | #undef LOG_TAG |
| 7678 | #define LOG_TAG "AudioFlinger::EffectModule" |
| 7679 | |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 7680 | AudioFlinger::EffectModule::EffectModule(ThreadBase *thread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7681 | const wp<AudioFlinger::EffectChain>& chain, |
| 7682 | effect_descriptor_t *desc, |
| 7683 | int id, |
| 7684 | int sessionId) |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 7685 | : mThread(thread), mChain(chain), mId(id), mSessionId(sessionId), mEffectInterface(NULL), |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7686 | mStatus(NO_INIT), mState(IDLE), mSuspended(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7687 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7688 | ALOGV("Constructor %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7689 | int lStatus; |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 7690 | if (thread == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7691 | return; |
| 7692 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7693 | |
| 7694 | memcpy(&mDescriptor, desc, sizeof(effect_descriptor_t)); |
| 7695 | |
| 7696 | // create effect engine from effect factory |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7697 | mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7698 | |
| 7699 | if (mStatus != NO_ERROR) { |
| 7700 | return; |
| 7701 | } |
| 7702 | lStatus = init(); |
| 7703 | if (lStatus < 0) { |
| 7704 | mStatus = lStatus; |
| 7705 | goto Error; |
| 7706 | } |
| 7707 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7708 | if (mSessionId > AUDIO_SESSION_OUTPUT_MIX) { |
| 7709 | mPinned = true; |
| 7710 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7711 | ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7712 | return; |
| 7713 | Error: |
| 7714 | EffectRelease(mEffectInterface); |
| 7715 | mEffectInterface = NULL; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7716 | ALOGV("Constructor Error %d", mStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7717 | } |
| 7718 | |
| 7719 | AudioFlinger::EffectModule::~EffectModule() |
| 7720 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7721 | ALOGV("Destructor %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7722 | if (mEffectInterface != NULL) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7723 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 7724 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { |
| 7725 | sp<ThreadBase> thread = mThread.promote(); |
| 7726 | if (thread != 0) { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 7727 | audio_stream_t *stream = thread->stream(); |
| 7728 | if (stream != NULL) { |
| 7729 | stream->remove_audio_effect(stream, mEffectInterface); |
| 7730 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7731 | } |
| 7732 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7733 | // release effect engine |
| 7734 | EffectRelease(mEffectInterface); |
| 7735 | } |
| 7736 | } |
| 7737 | |
Glenn Kasten | 435dbe6 | 2012-01-30 10:15:48 -0800 | [diff] [blame] | 7738 | status_t AudioFlinger::EffectModule::addHandle(const sp<EffectHandle>& handle) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7739 | { |
| 7740 | status_t status; |
| 7741 | |
| 7742 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7743 | int priority = handle->priority(); |
| 7744 | size_t size = mHandles.size(); |
| 7745 | sp<EffectHandle> h; |
| 7746 | size_t i; |
| 7747 | for (i = 0; i < size; i++) { |
| 7748 | h = mHandles[i].promote(); |
| 7749 | if (h == 0) continue; |
| 7750 | if (h->priority() <= priority) break; |
| 7751 | } |
| 7752 | // if inserted in first place, move effect control from previous owner to this handle |
| 7753 | if (i == 0) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7754 | bool enabled = false; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7755 | if (h != 0) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7756 | enabled = h->enabled(); |
| 7757 | h->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7758 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7759 | handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7760 | status = NO_ERROR; |
| 7761 | } else { |
| 7762 | status = ALREADY_EXISTS; |
| 7763 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7764 | 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] | 7765 | mHandles.insertAt(handle, i); |
| 7766 | return status; |
| 7767 | } |
| 7768 | |
| 7769 | size_t AudioFlinger::EffectModule::removeHandle(const wp<EffectHandle>& handle) |
| 7770 | { |
| 7771 | Mutex::Autolock _l(mLock); |
| 7772 | size_t size = mHandles.size(); |
| 7773 | size_t i; |
| 7774 | for (i = 0; i < size; i++) { |
| 7775 | if (mHandles[i] == handle) break; |
| 7776 | } |
| 7777 | if (i == size) { |
| 7778 | return size; |
| 7779 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7780 | 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] | 7781 | |
| 7782 | bool enabled = false; |
| 7783 | EffectHandle *hdl = handle.unsafe_get(); |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 7784 | if (hdl != NULL) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7785 | ALOGV("removeHandle() unsafe_get OK"); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7786 | enabled = hdl->enabled(); |
| 7787 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7788 | mHandles.removeAt(i); |
| 7789 | size = mHandles.size(); |
| 7790 | // if removed from first place, move effect control from this handle to next in line |
| 7791 | if (i == 0 && size != 0) { |
| 7792 | sp<EffectHandle> h = mHandles[0].promote(); |
| 7793 | if (h != 0) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7794 | h->setControl(true /*hasControl*/, true /*signal*/ , enabled /*enabled*/); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7795 | } |
| 7796 | } |
| 7797 | |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 7798 | // Prevent calls to process() and other functions on effect interface from now on. |
| 7799 | // The effect engine will be released by the destructor when the last strong reference on |
| 7800 | // this object is released which can happen after next process is called. |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7801 | if (size == 0 && !mPinned) { |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 7802 | mState = DESTROYED; |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7803 | } |
| 7804 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7805 | return size; |
| 7806 | } |
| 7807 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7808 | sp<AudioFlinger::EffectHandle> AudioFlinger::EffectModule::controlHandle() |
| 7809 | { |
| 7810 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 7811 | return mHandles.size() != 0 ? mHandles[0].promote() : 0; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7812 | } |
| 7813 | |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 7814 | void AudioFlinger::EffectModule::disconnect(const wp<EffectHandle>& handle, bool unpinIfLast) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7815 | { |
Glenn Kasten | 90bebef | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 7816 | ALOGV("disconnect() %p handle %p", this, handle.unsafe_get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7817 | // keep a strong reference on this EffectModule to avoid calling the |
| 7818 | // destructor before we exit |
| 7819 | sp<EffectModule> keep(this); |
| 7820 | { |
| 7821 | sp<ThreadBase> thread = mThread.promote(); |
| 7822 | if (thread != 0) { |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 7823 | thread->disconnectEffect(keep, handle, unpinIfLast); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7824 | } |
| 7825 | } |
| 7826 | } |
| 7827 | |
| 7828 | void AudioFlinger::EffectModule::updateState() { |
| 7829 | Mutex::Autolock _l(mLock); |
| 7830 | |
| 7831 | switch (mState) { |
| 7832 | case RESTART: |
| 7833 | reset_l(); |
| 7834 | // FALL THROUGH |
| 7835 | |
| 7836 | case STARTING: |
| 7837 | // clear auxiliary effect input buffer for next accumulation |
| 7838 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7839 | memset(mConfig.inputCfg.buffer.raw, |
| 7840 | 0, |
| 7841 | mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); |
| 7842 | } |
| 7843 | start_l(); |
| 7844 | mState = ACTIVE; |
| 7845 | break; |
| 7846 | case STOPPING: |
| 7847 | stop_l(); |
| 7848 | mDisableWaitCnt = mMaxDisableWaitCnt; |
| 7849 | mState = STOPPED; |
| 7850 | break; |
| 7851 | case STOPPED: |
| 7852 | // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the |
| 7853 | // turn off sequence. |
| 7854 | if (--mDisableWaitCnt == 0) { |
| 7855 | reset_l(); |
| 7856 | mState = IDLE; |
| 7857 | } |
| 7858 | break; |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 7859 | default: //IDLE , ACTIVE, DESTROYED |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7860 | break; |
| 7861 | } |
| 7862 | } |
| 7863 | |
| 7864 | void AudioFlinger::EffectModule::process() |
| 7865 | { |
| 7866 | Mutex::Autolock _l(mLock); |
| 7867 | |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 7868 | if (mState == DESTROYED || mEffectInterface == NULL || |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7869 | mConfig.inputCfg.buffer.raw == NULL || |
| 7870 | mConfig.outputCfg.buffer.raw == NULL) { |
| 7871 | return; |
| 7872 | } |
| 7873 | |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 7874 | if (isProcessEnabled()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7875 | // do 32 bit to 16 bit conversion for auxiliary effect input buffer |
| 7876 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 7877 | ditherAndClamp(mConfig.inputCfg.buffer.s32, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7878 | mConfig.inputCfg.buffer.s32, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7879 | mConfig.inputCfg.buffer.frameCount/2); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7880 | } |
| 7881 | |
| 7882 | // do the actual processing in the effect engine |
| 7883 | int ret = (*mEffectInterface)->process(mEffectInterface, |
| 7884 | &mConfig.inputCfg.buffer, |
| 7885 | &mConfig.outputCfg.buffer); |
| 7886 | |
| 7887 | // force transition to IDLE state when engine is ready |
| 7888 | if (mState == STOPPED && ret == -ENODATA) { |
| 7889 | mDisableWaitCnt = 1; |
| 7890 | } |
| 7891 | |
| 7892 | // clear auxiliary effect input buffer for next accumulation |
| 7893 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
Eric Laurent | 7333748 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 7894 | memset(mConfig.inputCfg.buffer.raw, 0, |
| 7895 | mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7896 | } |
| 7897 | } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT && |
Eric Laurent | 7333748 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 7898 | mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { |
| 7899 | // If an insert effect is idle and input buffer is different from output buffer, |
| 7900 | // accumulate input onto output |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7901 | sp<EffectChain> chain = mChain.promote(); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7902 | if (chain != 0 && chain->activeTrackCnt() != 0) { |
Eric Laurent | 7333748 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 7903 | size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2; //always stereo here |
| 7904 | int16_t *in = mConfig.inputCfg.buffer.s16; |
| 7905 | int16_t *out = mConfig.outputCfg.buffer.s16; |
| 7906 | for (size_t i = 0; i < frameCnt; i++) { |
| 7907 | out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7908 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7909 | } |
| 7910 | } |
| 7911 | } |
| 7912 | |
| 7913 | void AudioFlinger::EffectModule::reset_l() |
| 7914 | { |
| 7915 | if (mEffectInterface == NULL) { |
| 7916 | return; |
| 7917 | } |
| 7918 | (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL); |
| 7919 | } |
| 7920 | |
| 7921 | status_t AudioFlinger::EffectModule::configure() |
| 7922 | { |
| 7923 | uint32_t channels; |
| 7924 | if (mEffectInterface == NULL) { |
| 7925 | return NO_INIT; |
| 7926 | } |
| 7927 | |
| 7928 | sp<ThreadBase> thread = mThread.promote(); |
| 7929 | if (thread == 0) { |
| 7930 | return DEAD_OBJECT; |
| 7931 | } |
| 7932 | |
| 7933 | // TODO: handle configuration of effects replacing track process |
| 7934 | if (thread->channelCount() == 1) { |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 7935 | channels = AUDIO_CHANNEL_OUT_MONO; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7936 | } else { |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 7937 | channels = AUDIO_CHANNEL_OUT_STEREO; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7938 | } |
| 7939 | |
| 7940 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 7941 | mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7942 | } else { |
| 7943 | mConfig.inputCfg.channels = channels; |
| 7944 | } |
| 7945 | mConfig.outputCfg.channels = channels; |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 7946 | mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
| 7947 | mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7948 | mConfig.inputCfg.samplingRate = thread->sampleRate(); |
| 7949 | mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate; |
| 7950 | mConfig.inputCfg.bufferProvider.cookie = NULL; |
| 7951 | mConfig.inputCfg.bufferProvider.getBuffer = NULL; |
| 7952 | mConfig.inputCfg.bufferProvider.releaseBuffer = NULL; |
| 7953 | mConfig.outputCfg.bufferProvider.cookie = NULL; |
| 7954 | mConfig.outputCfg.bufferProvider.getBuffer = NULL; |
| 7955 | mConfig.outputCfg.bufferProvider.releaseBuffer = NULL; |
| 7956 | mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; |
| 7957 | // Insert effect: |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7958 | // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7959 | // always overwrites output buffer: input buffer == output buffer |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7960 | // - in other sessions: |
| 7961 | // last effect in the chain accumulates in output buffer: input buffer != output buffer |
| 7962 | // other effect: overwrites output buffer: input buffer == output buffer |
| 7963 | // Auxiliary effect: |
| 7964 | // accumulates in output buffer: input buffer != output buffer |
| 7965 | // Therefore: accumulate <=> input buffer != output buffer |
| 7966 | if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { |
| 7967 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE; |
| 7968 | } else { |
| 7969 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE; |
| 7970 | } |
| 7971 | mConfig.inputCfg.mask = EFFECT_CONFIG_ALL; |
| 7972 | mConfig.outputCfg.mask = EFFECT_CONFIG_ALL; |
| 7973 | mConfig.inputCfg.buffer.frameCount = thread->frameCount(); |
| 7974 | mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount; |
| 7975 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7976 | ALOGV("configure() %p thread %p buffer %p framecount %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7977 | this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount); |
| 7978 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7979 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 7980 | uint32_t size = sizeof(int); |
| 7981 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 7982 | EFFECT_CMD_SET_CONFIG, |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 7983 | sizeof(effect_config_t), |
| 7984 | &mConfig, |
| 7985 | &size, |
| 7986 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7987 | if (status == 0) { |
| 7988 | status = cmdStatus; |
| 7989 | } |
| 7990 | |
| 7991 | mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) / |
| 7992 | (1000 * mConfig.outputCfg.buffer.frameCount); |
| 7993 | |
| 7994 | return status; |
| 7995 | } |
| 7996 | |
| 7997 | status_t AudioFlinger::EffectModule::init() |
| 7998 | { |
| 7999 | Mutex::Autolock _l(mLock); |
| 8000 | if (mEffectInterface == NULL) { |
| 8001 | return NO_INIT; |
| 8002 | } |
| 8003 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8004 | uint32_t size = sizeof(status_t); |
| 8005 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 8006 | EFFECT_CMD_INIT, |
| 8007 | 0, |
| 8008 | NULL, |
| 8009 | &size, |
| 8010 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8011 | if (status == 0) { |
| 8012 | status = cmdStatus; |
| 8013 | } |
| 8014 | return status; |
| 8015 | } |
| 8016 | |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 8017 | status_t AudioFlinger::EffectModule::start() |
| 8018 | { |
| 8019 | Mutex::Autolock _l(mLock); |
| 8020 | return start_l(); |
| 8021 | } |
| 8022 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8023 | status_t AudioFlinger::EffectModule::start_l() |
| 8024 | { |
| 8025 | if (mEffectInterface == NULL) { |
| 8026 | return NO_INIT; |
| 8027 | } |
| 8028 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8029 | uint32_t size = sizeof(status_t); |
| 8030 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 8031 | EFFECT_CMD_ENABLE, |
| 8032 | 0, |
| 8033 | NULL, |
| 8034 | &size, |
| 8035 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8036 | if (status == 0) { |
| 8037 | status = cmdStatus; |
| 8038 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 8039 | if (status == 0 && |
| 8040 | ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 8041 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) { |
| 8042 | sp<ThreadBase> thread = mThread.promote(); |
| 8043 | if (thread != 0) { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 8044 | audio_stream_t *stream = thread->stream(); |
| 8045 | if (stream != NULL) { |
| 8046 | stream->add_audio_effect(stream, mEffectInterface); |
| 8047 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 8048 | } |
| 8049 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8050 | return status; |
| 8051 | } |
| 8052 | |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 8053 | status_t AudioFlinger::EffectModule::stop() |
| 8054 | { |
| 8055 | Mutex::Autolock _l(mLock); |
| 8056 | return stop_l(); |
| 8057 | } |
| 8058 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8059 | status_t AudioFlinger::EffectModule::stop_l() |
| 8060 | { |
| 8061 | if (mEffectInterface == NULL) { |
| 8062 | return NO_INIT; |
| 8063 | } |
| 8064 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8065 | uint32_t size = sizeof(status_t); |
| 8066 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 8067 | EFFECT_CMD_DISABLE, |
| 8068 | 0, |
| 8069 | NULL, |
| 8070 | &size, |
| 8071 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8072 | if (status == 0) { |
| 8073 | status = cmdStatus; |
| 8074 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 8075 | if (status == 0 && |
| 8076 | ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 8077 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) { |
| 8078 | sp<ThreadBase> thread = mThread.promote(); |
| 8079 | if (thread != 0) { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 8080 | audio_stream_t *stream = thread->stream(); |
| 8081 | if (stream != NULL) { |
| 8082 | stream->remove_audio_effect(stream, mEffectInterface); |
| 8083 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 8084 | } |
| 8085 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8086 | return status; |
| 8087 | } |
| 8088 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8089 | status_t AudioFlinger::EffectModule::command(uint32_t cmdCode, |
| 8090 | uint32_t cmdSize, |
| 8091 | void *pCmdData, |
| 8092 | uint32_t *replySize, |
| 8093 | void *pReplyData) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8094 | { |
| 8095 | Mutex::Autolock _l(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8096 | // ALOGV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8097 | |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 8098 | if (mState == DESTROYED || mEffectInterface == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8099 | return NO_INIT; |
| 8100 | } |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8101 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 8102 | cmdCode, |
| 8103 | cmdSize, |
| 8104 | pCmdData, |
| 8105 | replySize, |
| 8106 | pReplyData); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8107 | if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) { |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8108 | uint32_t size = (replySize == NULL) ? 0 : *replySize; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8109 | for (size_t i = 1; i < mHandles.size(); i++) { |
| 8110 | sp<EffectHandle> h = mHandles[i].promote(); |
| 8111 | if (h != 0) { |
| 8112 | h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData); |
| 8113 | } |
| 8114 | } |
| 8115 | } |
| 8116 | return status; |
| 8117 | } |
| 8118 | |
| 8119 | status_t AudioFlinger::EffectModule::setEnabled(bool enabled) |
| 8120 | { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 8121 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8122 | Mutex::Autolock _l(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8123 | ALOGV("setEnabled %p enabled %d", this, enabled); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8124 | |
| 8125 | if (enabled != isEnabled()) { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 8126 | status_t status = AudioSystem::setEffectEnabled(mId, enabled); |
| 8127 | if (enabled && status != NO_ERROR) { |
| 8128 | return status; |
| 8129 | } |
| 8130 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8131 | switch (mState) { |
| 8132 | // going from disabled to enabled |
| 8133 | case IDLE: |
| 8134 | mState = STARTING; |
| 8135 | break; |
| 8136 | case STOPPED: |
| 8137 | mState = RESTART; |
| 8138 | break; |
| 8139 | case STOPPING: |
| 8140 | mState = ACTIVE; |
| 8141 | break; |
| 8142 | |
| 8143 | // going from enabled to disabled |
| 8144 | case RESTART: |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 8145 | mState = STOPPED; |
| 8146 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8147 | case STARTING: |
| 8148 | mState = IDLE; |
| 8149 | break; |
| 8150 | case ACTIVE: |
| 8151 | mState = STOPPING; |
| 8152 | break; |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 8153 | case DESTROYED: |
| 8154 | return NO_ERROR; // simply ignore as we are being destroyed |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8155 | } |
| 8156 | for (size_t i = 1; i < mHandles.size(); i++) { |
| 8157 | sp<EffectHandle> h = mHandles[i].promote(); |
| 8158 | if (h != 0) { |
| 8159 | h->setEnabled(enabled); |
| 8160 | } |
| 8161 | } |
| 8162 | } |
| 8163 | return NO_ERROR; |
| 8164 | } |
| 8165 | |
Glenn Kasten | c59c004 | 2012-02-02 14:06:11 -0800 | [diff] [blame] | 8166 | bool AudioFlinger::EffectModule::isEnabled() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8167 | { |
| 8168 | switch (mState) { |
| 8169 | case RESTART: |
| 8170 | case STARTING: |
| 8171 | case ACTIVE: |
| 8172 | return true; |
| 8173 | case IDLE: |
| 8174 | case STOPPING: |
| 8175 | case STOPPED: |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 8176 | case DESTROYED: |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8177 | default: |
| 8178 | return false; |
| 8179 | } |
| 8180 | } |
| 8181 | |
Glenn Kasten | c59c004 | 2012-02-02 14:06:11 -0800 | [diff] [blame] | 8182 | bool AudioFlinger::EffectModule::isProcessEnabled() const |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 8183 | { |
| 8184 | switch (mState) { |
| 8185 | case RESTART: |
| 8186 | case ACTIVE: |
| 8187 | case STOPPING: |
| 8188 | case STOPPED: |
| 8189 | return true; |
| 8190 | case IDLE: |
| 8191 | case STARTING: |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 8192 | case DESTROYED: |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 8193 | default: |
| 8194 | return false; |
| 8195 | } |
| 8196 | } |
| 8197 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8198 | status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller) |
| 8199 | { |
| 8200 | Mutex::Autolock _l(mLock); |
| 8201 | status_t status = NO_ERROR; |
| 8202 | |
| 8203 | // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume |
| 8204 | // 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] | 8205 | if (isProcessEnabled() && |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 8206 | ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL || |
| 8207 | (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8208 | status_t cmdStatus; |
| 8209 | uint32_t volume[2]; |
| 8210 | uint32_t *pVolume = NULL; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8211 | uint32_t size = sizeof(volume); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8212 | volume[0] = *left; |
| 8213 | volume[1] = *right; |
| 8214 | if (controller) { |
| 8215 | pVolume = volume; |
| 8216 | } |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8217 | status = (*mEffectInterface)->command(mEffectInterface, |
| 8218 | EFFECT_CMD_SET_VOLUME, |
| 8219 | size, |
| 8220 | volume, |
| 8221 | &size, |
| 8222 | pVolume); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8223 | if (controller && status == NO_ERROR && size == sizeof(volume)) { |
| 8224 | *left = volume[0]; |
| 8225 | *right = volume[1]; |
| 8226 | } |
| 8227 | } |
| 8228 | return status; |
| 8229 | } |
| 8230 | |
| 8231 | status_t AudioFlinger::EffectModule::setDevice(uint32_t device) |
| 8232 | { |
| 8233 | Mutex::Autolock _l(mLock); |
| 8234 | status_t status = NO_ERROR; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 8235 | if (device && (mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) { |
| 8236 | // audio pre processing modules on RecordThread can receive both output and |
| 8237 | // input device indication in the same call |
| 8238 | uint32_t dev = device & AUDIO_DEVICE_OUT_ALL; |
| 8239 | if (dev) { |
| 8240 | status_t cmdStatus; |
| 8241 | uint32_t size = sizeof(status_t); |
| 8242 | |
| 8243 | status = (*mEffectInterface)->command(mEffectInterface, |
| 8244 | EFFECT_CMD_SET_DEVICE, |
| 8245 | sizeof(uint32_t), |
| 8246 | &dev, |
| 8247 | &size, |
| 8248 | &cmdStatus); |
| 8249 | if (status == NO_ERROR) { |
| 8250 | status = cmdStatus; |
| 8251 | } |
| 8252 | } |
| 8253 | dev = device & AUDIO_DEVICE_IN_ALL; |
| 8254 | if (dev) { |
| 8255 | status_t cmdStatus; |
| 8256 | uint32_t size = sizeof(status_t); |
| 8257 | |
| 8258 | status_t status2 = (*mEffectInterface)->command(mEffectInterface, |
| 8259 | EFFECT_CMD_SET_INPUT_DEVICE, |
| 8260 | sizeof(uint32_t), |
| 8261 | &dev, |
| 8262 | &size, |
| 8263 | &cmdStatus); |
| 8264 | if (status2 == NO_ERROR) { |
| 8265 | status2 = cmdStatus; |
| 8266 | } |
| 8267 | if (status == NO_ERROR) { |
| 8268 | status = status2; |
| 8269 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8270 | } |
| 8271 | } |
| 8272 | return status; |
| 8273 | } |
| 8274 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 8275 | status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8276 | { |
| 8277 | Mutex::Autolock _l(mLock); |
| 8278 | status_t status = NO_ERROR; |
| 8279 | 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] | 8280 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8281 | uint32_t size = sizeof(status_t); |
| 8282 | status = (*mEffectInterface)->command(mEffectInterface, |
| 8283 | EFFECT_CMD_SET_AUDIO_MODE, |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 8284 | sizeof(audio_mode_t), |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 8285 | &mode, |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8286 | &size, |
| 8287 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8288 | if (status == NO_ERROR) { |
| 8289 | status = cmdStatus; |
| 8290 | } |
| 8291 | } |
| 8292 | return status; |
| 8293 | } |
| 8294 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8295 | void AudioFlinger::EffectModule::setSuspended(bool suspended) |
| 8296 | { |
| 8297 | Mutex::Autolock _l(mLock); |
| 8298 | mSuspended = suspended; |
| 8299 | } |
Glenn Kasten | a3a8548 | 2012-01-04 11:01:11 -0800 | [diff] [blame] | 8300 | |
| 8301 | bool AudioFlinger::EffectModule::suspended() const |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8302 | { |
| 8303 | Mutex::Autolock _l(mLock); |
| 8304 | return mSuspended; |
| 8305 | } |
| 8306 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8307 | status_t AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args) |
| 8308 | { |
| 8309 | const size_t SIZE = 256; |
| 8310 | char buffer[SIZE]; |
| 8311 | String8 result; |
| 8312 | |
| 8313 | snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId); |
| 8314 | result.append(buffer); |
| 8315 | |
| 8316 | bool locked = tryLock(mLock); |
| 8317 | // failed to lock - AudioFlinger is probably deadlocked |
| 8318 | if (!locked) { |
| 8319 | result.append("\t\tCould not lock Fx mutex:\n"); |
| 8320 | } |
| 8321 | |
| 8322 | result.append("\t\tSession Status State Engine:\n"); |
| 8323 | snprintf(buffer, SIZE, "\t\t%05d %03d %03d 0x%08x\n", |
| 8324 | mSessionId, mStatus, mState, (uint32_t)mEffectInterface); |
| 8325 | result.append(buffer); |
| 8326 | |
| 8327 | result.append("\t\tDescriptor:\n"); |
| 8328 | snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", |
| 8329 | mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion, |
| 8330 | mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],mDescriptor.uuid.node[2], |
| 8331 | mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]); |
| 8332 | result.append(buffer); |
| 8333 | snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", |
| 8334 | mDescriptor.type.timeLow, mDescriptor.type.timeMid, mDescriptor.type.timeHiAndVersion, |
| 8335 | mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],mDescriptor.type.node[2], |
| 8336 | mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]); |
| 8337 | result.append(buffer); |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 8338 | 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] | 8339 | mDescriptor.apiVersion, |
| 8340 | mDescriptor.flags); |
| 8341 | result.append(buffer); |
| 8342 | snprintf(buffer, SIZE, "\t\t- name: %s\n", |
| 8343 | mDescriptor.name); |
| 8344 | result.append(buffer); |
| 8345 | snprintf(buffer, SIZE, "\t\t- implementor: %s\n", |
| 8346 | mDescriptor.implementor); |
| 8347 | result.append(buffer); |
| 8348 | |
| 8349 | result.append("\t\t- Input configuration:\n"); |
| 8350 | result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); |
| 8351 | snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n", |
| 8352 | (uint32_t)mConfig.inputCfg.buffer.raw, |
| 8353 | mConfig.inputCfg.buffer.frameCount, |
| 8354 | mConfig.inputCfg.samplingRate, |
| 8355 | mConfig.inputCfg.channels, |
| 8356 | mConfig.inputCfg.format); |
| 8357 | result.append(buffer); |
| 8358 | |
| 8359 | result.append("\t\t- Output configuration:\n"); |
| 8360 | result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); |
| 8361 | snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n", |
| 8362 | (uint32_t)mConfig.outputCfg.buffer.raw, |
| 8363 | mConfig.outputCfg.buffer.frameCount, |
| 8364 | mConfig.outputCfg.samplingRate, |
| 8365 | mConfig.outputCfg.channels, |
| 8366 | mConfig.outputCfg.format); |
| 8367 | result.append(buffer); |
| 8368 | |
| 8369 | snprintf(buffer, SIZE, "\t\t%d Clients:\n", mHandles.size()); |
| 8370 | result.append(buffer); |
| 8371 | result.append("\t\t\tPid Priority Ctrl Locked client server\n"); |
| 8372 | for (size_t i = 0; i < mHandles.size(); ++i) { |
| 8373 | sp<EffectHandle> handle = mHandles[i].promote(); |
| 8374 | if (handle != 0) { |
| 8375 | handle->dump(buffer, SIZE); |
| 8376 | result.append(buffer); |
| 8377 | } |
| 8378 | } |
| 8379 | |
| 8380 | result.append("\n"); |
| 8381 | |
| 8382 | write(fd, result.string(), result.length()); |
| 8383 | |
| 8384 | if (locked) { |
| 8385 | mLock.unlock(); |
| 8386 | } |
| 8387 | |
| 8388 | return NO_ERROR; |
| 8389 | } |
| 8390 | |
| 8391 | // ---------------------------------------------------------------------------- |
| 8392 | // EffectHandle implementation |
| 8393 | // ---------------------------------------------------------------------------- |
| 8394 | |
| 8395 | #undef LOG_TAG |
| 8396 | #define LOG_TAG "AudioFlinger::EffectHandle" |
| 8397 | |
| 8398 | AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect, |
| 8399 | const sp<AudioFlinger::Client>& client, |
| 8400 | const sp<IEffectClient>& effectClient, |
| 8401 | int32_t priority) |
| 8402 | : BnEffect(), |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8403 | mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL), |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8404 | mPriority(priority), mHasControl(false), mEnabled(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8405 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8406 | ALOGV("constructor %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8407 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8408 | if (client == 0) { |
| 8409 | return; |
| 8410 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8411 | int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int); |
| 8412 | mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset); |
| 8413 | if (mCblkMemory != 0) { |
| 8414 | mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer()); |
| 8415 | |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 8416 | if (mCblk != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8417 | new(mCblk) effect_param_cblk_t(); |
| 8418 | mBuffer = (uint8_t *)mCblk + bufOffset; |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 8419 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8420 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 8421 | 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] | 8422 | return; |
| 8423 | } |
| 8424 | } |
| 8425 | |
| 8426 | AudioFlinger::EffectHandle::~EffectHandle() |
| 8427 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8428 | ALOGV("Destructor %p", this); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8429 | disconnect(false); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8430 | ALOGV("Destructor DONE %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8431 | } |
| 8432 | |
| 8433 | status_t AudioFlinger::EffectHandle::enable() |
| 8434 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8435 | ALOGV("enable %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8436 | if (!mHasControl) return INVALID_OPERATION; |
| 8437 | if (mEffect == 0) return DEAD_OBJECT; |
| 8438 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 8439 | if (mEnabled) { |
| 8440 | return NO_ERROR; |
| 8441 | } |
| 8442 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8443 | mEnabled = true; |
| 8444 | |
| 8445 | sp<ThreadBase> thread = mEffect->thread().promote(); |
| 8446 | if (thread != 0) { |
| 8447 | thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId()); |
| 8448 | } |
| 8449 | |
| 8450 | // checkSuspendOnEffectEnabled() can suspend this same effect when enabled |
| 8451 | if (mEffect->suspended()) { |
| 8452 | return NO_ERROR; |
| 8453 | } |
| 8454 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 8455 | status_t status = mEffect->setEnabled(true); |
| 8456 | if (status != NO_ERROR) { |
| 8457 | if (thread != 0) { |
| 8458 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); |
| 8459 | } |
| 8460 | mEnabled = false; |
| 8461 | } |
| 8462 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8463 | } |
| 8464 | |
| 8465 | status_t AudioFlinger::EffectHandle::disable() |
| 8466 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8467 | ALOGV("disable %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8468 | if (!mHasControl) return INVALID_OPERATION; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8469 | if (mEffect == 0) return DEAD_OBJECT; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8470 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 8471 | if (!mEnabled) { |
| 8472 | return NO_ERROR; |
| 8473 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8474 | mEnabled = false; |
| 8475 | |
| 8476 | if (mEffect->suspended()) { |
| 8477 | return NO_ERROR; |
| 8478 | } |
| 8479 | |
| 8480 | status_t status = mEffect->setEnabled(false); |
| 8481 | |
| 8482 | sp<ThreadBase> thread = mEffect->thread().promote(); |
| 8483 | if (thread != 0) { |
| 8484 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); |
| 8485 | } |
| 8486 | |
| 8487 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8488 | } |
| 8489 | |
| 8490 | void AudioFlinger::EffectHandle::disconnect() |
| 8491 | { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8492 | disconnect(true); |
| 8493 | } |
| 8494 | |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 8495 | void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast) |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8496 | { |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 8497 | ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8498 | if (mEffect == 0) { |
| 8499 | return; |
| 8500 | } |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 8501 | mEffect->disconnect(this, unpinIfLast); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8502 | |
Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 8503 | if (mHasControl && mEnabled) { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 8504 | sp<ThreadBase> thread = mEffect->thread().promote(); |
| 8505 | if (thread != 0) { |
| 8506 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); |
| 8507 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8508 | } |
| 8509 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8510 | // release sp on module => module destructor can be called now |
| 8511 | mEffect.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8512 | if (mClient != 0) { |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 8513 | if (mCblk != NULL) { |
Glenn Kasten | 1a0ae5b | 2012-02-03 10:24:48 -0800 | [diff] [blame] | 8514 | // unlike ~TrackBase(), mCblk is never a local new, so don't delete |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8515 | mCblk->~effect_param_cblk_t(); // destroy our shared-structure. |
| 8516 | } |
Glenn Kasten | dbfafaf | 2012-01-25 15:27:15 -0800 | [diff] [blame] | 8517 | 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] | 8518 | // Client destructor must run with AudioFlinger mutex locked |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8519 | Mutex::Autolock _l(mClient->audioFlinger()->mLock); |
| 8520 | mClient.clear(); |
| 8521 | } |
| 8522 | } |
| 8523 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8524 | status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode, |
| 8525 | uint32_t cmdSize, |
| 8526 | void *pCmdData, |
| 8527 | uint32_t *replySize, |
| 8528 | void *pReplyData) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8529 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8530 | // ALOGV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p", |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8531 | // cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8532 | |
| 8533 | // only get parameter command is permitted for applications not controlling the effect |
| 8534 | if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) { |
| 8535 | return INVALID_OPERATION; |
| 8536 | } |
| 8537 | if (mEffect == 0) return DEAD_OBJECT; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8538 | if (mClient == 0) return INVALID_OPERATION; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8539 | |
| 8540 | // handle commands that are not forwarded transparently to effect engine |
| 8541 | if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) { |
| 8542 | // No need to trylock() here as this function is executed in the binder thread serving a particular client process: |
| 8543 | // no risk to block the whole media server process or mixer threads is we are stuck here |
| 8544 | Mutex::Autolock _l(mCblk->lock); |
| 8545 | if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE || |
| 8546 | mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) { |
| 8547 | mCblk->serverIndex = 0; |
| 8548 | mCblk->clientIndex = 0; |
| 8549 | return BAD_VALUE; |
| 8550 | } |
| 8551 | status_t status = NO_ERROR; |
| 8552 | while (mCblk->serverIndex < mCblk->clientIndex) { |
| 8553 | int reply; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8554 | uint32_t rsize = sizeof(int); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8555 | int *p = (int *)(mBuffer + mCblk->serverIndex); |
| 8556 | int size = *p++; |
| 8557 | if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 8558 | ALOGW("command(): invalid parameter block size"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8559 | break; |
| 8560 | } |
| 8561 | effect_param_t *param = (effect_param_t *)p; |
| 8562 | if (param->psize == 0 || param->vsize == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 8563 | ALOGW("command(): null parameter or value size"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8564 | mCblk->serverIndex += size; |
| 8565 | continue; |
| 8566 | } |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8567 | uint32_t psize = sizeof(effect_param_t) + |
| 8568 | ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + |
| 8569 | param->vsize; |
| 8570 | status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM, |
| 8571 | psize, |
| 8572 | p, |
| 8573 | &rsize, |
| 8574 | &reply); |
Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 8575 | // stop at first error encountered |
| 8576 | if (ret != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8577 | status = ret; |
Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 8578 | *(int *)pReplyData = reply; |
| 8579 | break; |
| 8580 | } else if (reply != NO_ERROR) { |
| 8581 | *(int *)pReplyData = reply; |
| 8582 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8583 | } |
| 8584 | mCblk->serverIndex += size; |
| 8585 | } |
| 8586 | mCblk->serverIndex = 0; |
| 8587 | mCblk->clientIndex = 0; |
| 8588 | return status; |
| 8589 | } else if (cmdCode == EFFECT_CMD_ENABLE) { |
Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 8590 | *(int *)pReplyData = NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8591 | return enable(); |
| 8592 | } else if (cmdCode == EFFECT_CMD_DISABLE) { |
Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 8593 | *(int *)pReplyData = NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8594 | return disable(); |
| 8595 | } |
| 8596 | |
| 8597 | return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 8598 | } |
| 8599 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8600 | void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8601 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8602 | ALOGV("setControl %p control %d", this, hasControl); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8603 | |
| 8604 | mHasControl = hasControl; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8605 | mEnabled = enabled; |
| 8606 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8607 | if (signal && mEffectClient != 0) { |
| 8608 | mEffectClient->controlStatusChanged(hasControl); |
| 8609 | } |
| 8610 | } |
| 8611 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8612 | void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode, |
| 8613 | uint32_t cmdSize, |
| 8614 | void *pCmdData, |
| 8615 | uint32_t replySize, |
| 8616 | void *pReplyData) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8617 | { |
| 8618 | if (mEffectClient != 0) { |
| 8619 | mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 8620 | } |
| 8621 | } |
| 8622 | |
| 8623 | |
| 8624 | |
| 8625 | void AudioFlinger::EffectHandle::setEnabled(bool enabled) |
| 8626 | { |
| 8627 | if (mEffectClient != 0) { |
| 8628 | mEffectClient->enableStatusChanged(enabled); |
| 8629 | } |
| 8630 | } |
| 8631 | |
| 8632 | status_t AudioFlinger::EffectHandle::onTransact( |
| 8633 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 8634 | { |
| 8635 | return BnEffect::onTransact(code, data, reply, flags); |
| 8636 | } |
| 8637 | |
| 8638 | |
| 8639 | void AudioFlinger::EffectHandle::dump(char* buffer, size_t size) |
| 8640 | { |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 8641 | bool locked = mCblk != NULL && tryLock(mCblk->lock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8642 | |
| 8643 | 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] | 8644 | (mClient == 0) ? getpid_cached : mClient->pid(), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8645 | mPriority, |
| 8646 | mHasControl, |
| 8647 | !locked, |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8648 | mCblk ? mCblk->clientIndex : 0, |
| 8649 | mCblk ? mCblk->serverIndex : 0 |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8650 | ); |
| 8651 | |
| 8652 | if (locked) { |
| 8653 | mCblk->lock.unlock(); |
| 8654 | } |
| 8655 | } |
| 8656 | |
| 8657 | #undef LOG_TAG |
| 8658 | #define LOG_TAG "AudioFlinger::EffectChain" |
| 8659 | |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 8660 | AudioFlinger::EffectChain::EffectChain(ThreadBase *thread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8661 | int sessionId) |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 8662 | : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0), |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 8663 | mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX), |
| 8664 | mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8665 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 8666 | mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 8667 | if (thread == NULL) { |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 8668 | return; |
| 8669 | } |
| 8670 | mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) / |
| 8671 | thread->frameCount(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8672 | } |
| 8673 | |
| 8674 | AudioFlinger::EffectChain::~EffectChain() |
| 8675 | { |
| 8676 | if (mOwnInBuffer) { |
| 8677 | delete mInBuffer; |
| 8678 | } |
| 8679 | |
| 8680 | } |
| 8681 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8682 | // getEffectFromDesc_l() must be called with ThreadBase::mLock held |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8683 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(effect_descriptor_t *descriptor) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8684 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8685 | size_t size = mEffects.size(); |
| 8686 | |
| 8687 | for (size_t i = 0; i < size; i++) { |
| 8688 | 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] | 8689 | return mEffects[i]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8690 | } |
| 8691 | } |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 8692 | return 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8693 | } |
| 8694 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8695 | // getEffectFromId_l() must be called with ThreadBase::mLock held |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8696 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8697 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8698 | size_t size = mEffects.size(); |
| 8699 | |
| 8700 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 8701 | // by convention, return first effect if id provided is 0 (0 is never a valid id) |
| 8702 | if (id == 0 || mEffects[i]->id() == id) { |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 8703 | return mEffects[i]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8704 | } |
| 8705 | } |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 8706 | return 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8707 | } |
| 8708 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8709 | // getEffectFromType_l() must be called with ThreadBase::mLock held |
| 8710 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l( |
| 8711 | const effect_uuid_t *type) |
| 8712 | { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8713 | size_t size = mEffects.size(); |
| 8714 | |
| 8715 | for (size_t i = 0; i < size; i++) { |
| 8716 | if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) { |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 8717 | return mEffects[i]; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8718 | } |
| 8719 | } |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 8720 | return 0; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8721 | } |
| 8722 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8723 | // Must be called with EffectChain::mLock locked |
| 8724 | void AudioFlinger::EffectChain::process_l() |
| 8725 | { |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 8726 | sp<ThreadBase> thread = mThread.promote(); |
| 8727 | if (thread == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 8728 | ALOGW("process_l(): cannot promote mixer thread"); |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 8729 | return; |
| 8730 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 8731 | bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) || |
| 8732 | (mSessionId == AUDIO_SESSION_OUTPUT_STAGE); |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 8733 | // always process effects unless no more tracks are on the session and the effect tail |
| 8734 | // has been rendered |
| 8735 | bool doProcess = true; |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 8736 | if (!isGlobalSession) { |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 8737 | bool tracksOnSession = (trackCnt() != 0); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 8738 | |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 8739 | if (!tracksOnSession && mTailBufferCount == 0) { |
| 8740 | doProcess = false; |
| 8741 | } |
| 8742 | |
| 8743 | if (activeTrackCnt() == 0) { |
| 8744 | // if no track is active and the effect tail has not been rendered, |
| 8745 | // the input buffer must be cleared here as the mixer process will not do it |
| 8746 | if (tracksOnSession || mTailBufferCount > 0) { |
| 8747 | size_t numSamples = thread->frameCount() * thread->channelCount(); |
| 8748 | memset(mInBuffer, 0, numSamples * sizeof(int16_t)); |
| 8749 | if (mTailBufferCount > 0) { |
| 8750 | mTailBufferCount--; |
| 8751 | } |
| 8752 | } |
| 8753 | } |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 8754 | } |
| 8755 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8756 | size_t size = mEffects.size(); |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 8757 | if (doProcess) { |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 8758 | for (size_t i = 0; i < size; i++) { |
| 8759 | mEffects[i]->process(); |
| 8760 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8761 | } |
| 8762 | for (size_t i = 0; i < size; i++) { |
| 8763 | mEffects[i]->updateState(); |
| 8764 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8765 | } |
| 8766 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8767 | // addEffect_l() must be called with PlaybackThread::mLock held |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 8768 | status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8769 | { |
| 8770 | effect_descriptor_t desc = effect->desc(); |
| 8771 | uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK; |
| 8772 | |
| 8773 | Mutex::Autolock _l(mLock); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 8774 | effect->setChain(this); |
| 8775 | sp<ThreadBase> thread = mThread.promote(); |
| 8776 | if (thread == 0) { |
| 8777 | return NO_INIT; |
| 8778 | } |
| 8779 | effect->setThread(thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8780 | |
| 8781 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 8782 | // Auxiliary effects are inserted at the beginning of mEffects vector as |
| 8783 | // they are processed first and accumulated in chain input buffer |
| 8784 | mEffects.insertAt(effect, 0); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 8785 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8786 | // the input buffer for auxiliary effect contains mono samples in |
| 8787 | // 32 bit format. This is to avoid saturation in AudoMixer |
| 8788 | // accumulation stage. Saturation is done in EffectModule::process() before |
| 8789 | // calling the process in effect engine |
| 8790 | size_t numSamples = thread->frameCount(); |
| 8791 | int32_t *buffer = new int32_t[numSamples]; |
| 8792 | memset(buffer, 0, numSamples * sizeof(int32_t)); |
| 8793 | effect->setInBuffer((int16_t *)buffer); |
| 8794 | // auxiliary effects output samples to chain input buffer for further processing |
| 8795 | // by insert effects |
| 8796 | effect->setOutBuffer(mInBuffer); |
| 8797 | } else { |
| 8798 | // Insert effects are inserted at the end of mEffects vector as they are processed |
| 8799 | // after track and auxiliary effects. |
| 8800 | // Insert effect order as a function of indicated preference: |
| 8801 | // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if |
| 8802 | // another effect is present |
| 8803 | // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the |
| 8804 | // last effect claiming first position |
| 8805 | // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the |
| 8806 | // first effect claiming last position |
| 8807 | // else if EFFECT_FLAG_INSERT_ANY insert after first or before last |
| 8808 | // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is |
| 8809 | // already present |
| 8810 | |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 8811 | size_t size = mEffects.size(); |
| 8812 | size_t idx_insert = size; |
| 8813 | ssize_t idx_insert_first = -1; |
| 8814 | ssize_t idx_insert_last = -1; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8815 | |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 8816 | for (size_t i = 0; i < size; i++) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8817 | effect_descriptor_t d = mEffects[i]->desc(); |
| 8818 | uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK; |
| 8819 | uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK; |
| 8820 | if (iMode == EFFECT_FLAG_TYPE_INSERT) { |
| 8821 | // check invalid effect chaining combinations |
| 8822 | if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE || |
| 8823 | iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 8824 | 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] | 8825 | return INVALID_OPERATION; |
| 8826 | } |
| 8827 | // remember position of first insert effect and by default |
| 8828 | // select this as insert position for new effect |
| 8829 | if (idx_insert == size) { |
| 8830 | idx_insert = i; |
| 8831 | } |
| 8832 | // remember position of last insert effect claiming |
| 8833 | // first position |
| 8834 | if (iPref == EFFECT_FLAG_INSERT_FIRST) { |
| 8835 | idx_insert_first = i; |
| 8836 | } |
| 8837 | // remember position of first insert effect claiming |
| 8838 | // last position |
| 8839 | if (iPref == EFFECT_FLAG_INSERT_LAST && |
| 8840 | idx_insert_last == -1) { |
| 8841 | idx_insert_last = i; |
| 8842 | } |
| 8843 | } |
| 8844 | } |
| 8845 | |
| 8846 | // modify idx_insert from first position if needed |
| 8847 | if (insertPref == EFFECT_FLAG_INSERT_LAST) { |
| 8848 | if (idx_insert_last != -1) { |
| 8849 | idx_insert = idx_insert_last; |
| 8850 | } else { |
| 8851 | idx_insert = size; |
| 8852 | } |
| 8853 | } else { |
| 8854 | if (idx_insert_first != -1) { |
| 8855 | idx_insert = idx_insert_first + 1; |
| 8856 | } |
| 8857 | } |
| 8858 | |
| 8859 | // always read samples from chain input buffer |
| 8860 | effect->setInBuffer(mInBuffer); |
| 8861 | |
| 8862 | // if last effect in the chain, output samples to chain |
| 8863 | // output buffer, otherwise to chain input buffer |
| 8864 | if (idx_insert == size) { |
| 8865 | if (idx_insert != 0) { |
| 8866 | mEffects[idx_insert-1]->setOutBuffer(mInBuffer); |
| 8867 | mEffects[idx_insert-1]->configure(); |
| 8868 | } |
| 8869 | effect->setOutBuffer(mOutBuffer); |
| 8870 | } else { |
| 8871 | effect->setOutBuffer(mInBuffer); |
| 8872 | } |
| 8873 | mEffects.insertAt(effect, idx_insert); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8874 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8875 | 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] | 8876 | } |
| 8877 | effect->configure(); |
| 8878 | return NO_ERROR; |
| 8879 | } |
| 8880 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8881 | // removeEffect_l() must be called with PlaybackThread::mLock held |
| 8882 | size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8883 | { |
| 8884 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 8885 | size_t size = mEffects.size(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8886 | uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK; |
| 8887 | |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 8888 | for (size_t i = 0; i < size; i++) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8889 | if (effect == mEffects[i]) { |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 8890 | // calling stop here will remove pre-processing effect from the audio HAL. |
| 8891 | // This is safe as we hold the EffectChain mutex which guarantees that we are not in |
| 8892 | // the middle of a read from audio HAL |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 8893 | if (mEffects[i]->state() == EffectModule::ACTIVE || |
| 8894 | mEffects[i]->state() == EffectModule::STOPPING) { |
| 8895 | mEffects[i]->stop(); |
| 8896 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8897 | if (type == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 8898 | delete[] effect->inBuffer(); |
| 8899 | } else { |
| 8900 | if (i == size - 1 && i != 0) { |
| 8901 | mEffects[i - 1]->setOutBuffer(mOutBuffer); |
| 8902 | mEffects[i - 1]->configure(); |
| 8903 | } |
| 8904 | } |
| 8905 | mEffects.removeAt(i); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8906 | 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] | 8907 | break; |
| 8908 | } |
| 8909 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8910 | |
| 8911 | return mEffects.size(); |
| 8912 | } |
| 8913 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8914 | // setDevice_l() must be called with PlaybackThread::mLock held |
| 8915 | void AudioFlinger::EffectChain::setDevice_l(uint32_t device) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8916 | { |
| 8917 | size_t size = mEffects.size(); |
| 8918 | for (size_t i = 0; i < size; i++) { |
| 8919 | mEffects[i]->setDevice(device); |
| 8920 | } |
| 8921 | } |
| 8922 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8923 | // setMode_l() must be called with PlaybackThread::mLock held |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 8924 | void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8925 | { |
| 8926 | size_t size = mEffects.size(); |
| 8927 | for (size_t i = 0; i < size; i++) { |
| 8928 | mEffects[i]->setMode(mode); |
| 8929 | } |
| 8930 | } |
| 8931 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8932 | // setVolume_l() must be called with PlaybackThread::mLock held |
| 8933 | bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8934 | { |
| 8935 | uint32_t newLeft = *left; |
| 8936 | uint32_t newRight = *right; |
| 8937 | bool hasControl = false; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8938 | int ctrlIdx = -1; |
| 8939 | size_t size = mEffects.size(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8940 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8941 | // first update volume controller |
| 8942 | for (size_t i = size; i > 0; i--) { |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 8943 | if (mEffects[i - 1]->isProcessEnabled() && |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8944 | (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) { |
| 8945 | ctrlIdx = i - 1; |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 8946 | hasControl = true; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8947 | break; |
| 8948 | } |
| 8949 | } |
| 8950 | |
| 8951 | if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) { |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 8952 | if (hasControl) { |
| 8953 | *left = mNewLeftVolume; |
| 8954 | *right = mNewRightVolume; |
| 8955 | } |
| 8956 | return hasControl; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8957 | } |
| 8958 | |
| 8959 | mVolumeCtrlIdx = ctrlIdx; |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 8960 | mLeftVolume = newLeft; |
| 8961 | mRightVolume = newRight; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8962 | |
| 8963 | // second get volume update from volume controller |
| 8964 | if (ctrlIdx >= 0) { |
| 8965 | mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true); |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 8966 | mNewLeftVolume = newLeft; |
| 8967 | mNewRightVolume = newRight; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8968 | } |
| 8969 | // then indicate volume to all other effects in chain. |
| 8970 | // Pass altered volume to effects before volume controller |
| 8971 | // and requested volume to effects after controller |
| 8972 | uint32_t lVol = newLeft; |
| 8973 | uint32_t rVol = newRight; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8974 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8975 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8976 | if ((int)i == ctrlIdx) continue; |
| 8977 | // this also works for ctrlIdx == -1 when there is no volume controller |
| 8978 | if ((int)i > ctrlIdx) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8979 | lVol = *left; |
| 8980 | rVol = *right; |
| 8981 | } |
| 8982 | mEffects[i]->setVolume(&lVol, &rVol, false); |
| 8983 | } |
| 8984 | *left = newLeft; |
| 8985 | *right = newRight; |
| 8986 | |
| 8987 | return hasControl; |
| 8988 | } |
| 8989 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8990 | status_t AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args) |
| 8991 | { |
| 8992 | const size_t SIZE = 256; |
| 8993 | char buffer[SIZE]; |
| 8994 | String8 result; |
| 8995 | |
| 8996 | snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId); |
| 8997 | result.append(buffer); |
| 8998 | |
| 8999 | bool locked = tryLock(mLock); |
| 9000 | // failed to lock - AudioFlinger is probably deadlocked |
| 9001 | if (!locked) { |
| 9002 | result.append("\tCould not lock mutex:\n"); |
| 9003 | } |
| 9004 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 9005 | result.append("\tNum fx In buffer Out buffer Active tracks:\n"); |
| 9006 | snprintf(buffer, SIZE, "\t%02d 0x%08x 0x%08x %d\n", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9007 | mEffects.size(), |
| 9008 | (uint32_t)mInBuffer, |
| 9009 | (uint32_t)mOutBuffer, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9010 | mActiveTrackCnt); |
| 9011 | result.append(buffer); |
| 9012 | write(fd, result.string(), result.size()); |
| 9013 | |
| 9014 | for (size_t i = 0; i < mEffects.size(); ++i) { |
| 9015 | sp<EffectModule> effect = mEffects[i]; |
| 9016 | if (effect != 0) { |
| 9017 | effect->dump(fd, args); |
| 9018 | } |
| 9019 | } |
| 9020 | |
| 9021 | if (locked) { |
| 9022 | mLock.unlock(); |
| 9023 | } |
| 9024 | |
| 9025 | return NO_ERROR; |
| 9026 | } |
| 9027 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9028 | // must be called with ThreadBase::mLock held |
| 9029 | void AudioFlinger::EffectChain::setEffectSuspended_l( |
| 9030 | const effect_uuid_t *type, bool suspend) |
| 9031 | { |
| 9032 | sp<SuspendedEffectDesc> desc; |
| 9033 | // use effect type UUID timelow as key as there is no real risk of identical |
| 9034 | // timeLow fields among effect type UUIDs. |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 9035 | ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9036 | if (suspend) { |
| 9037 | if (index >= 0) { |
| 9038 | desc = mSuspendedEffects.valueAt(index); |
| 9039 | } else { |
| 9040 | desc = new SuspendedEffectDesc(); |
| 9041 | memcpy(&desc->mType, type, sizeof(effect_uuid_t)); |
| 9042 | mSuspendedEffects.add(type->timeLow, desc); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 9043 | ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9044 | } |
| 9045 | if (desc->mRefCount++ == 0) { |
| 9046 | sp<EffectModule> effect = getEffectIfEnabled(type); |
| 9047 | if (effect != 0) { |
| 9048 | desc->mEffect = effect; |
| 9049 | effect->setSuspended(true); |
| 9050 | effect->setEnabled(false); |
| 9051 | } |
| 9052 | } |
| 9053 | } else { |
| 9054 | if (index < 0) { |
| 9055 | return; |
| 9056 | } |
| 9057 | desc = mSuspendedEffects.valueAt(index); |
| 9058 | if (desc->mRefCount <= 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 9059 | ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9060 | desc->mRefCount = 1; |
| 9061 | } |
| 9062 | if (--desc->mRefCount == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 9063 | ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9064 | if (desc->mEffect != 0) { |
| 9065 | sp<EffectModule> effect = desc->mEffect.promote(); |
| 9066 | if (effect != 0) { |
| 9067 | effect->setSuspended(false); |
| 9068 | sp<EffectHandle> handle = effect->controlHandle(); |
| 9069 | if (handle != 0) { |
| 9070 | effect->setEnabled(handle->enabled()); |
| 9071 | } |
| 9072 | } |
| 9073 | desc->mEffect.clear(); |
| 9074 | } |
| 9075 | mSuspendedEffects.removeItemsAt(index); |
| 9076 | } |
| 9077 | } |
| 9078 | } |
| 9079 | |
| 9080 | // must be called with ThreadBase::mLock held |
| 9081 | void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend) |
| 9082 | { |
| 9083 | sp<SuspendedEffectDesc> desc; |
| 9084 | |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 9085 | ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9086 | if (suspend) { |
| 9087 | if (index >= 0) { |
| 9088 | desc = mSuspendedEffects.valueAt(index); |
| 9089 | } else { |
| 9090 | desc = new SuspendedEffectDesc(); |
| 9091 | mSuspendedEffects.add((int)kKeyForSuspendAll, desc); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 9092 | ALOGV("setEffectSuspendedAll_l() add entry for 0"); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9093 | } |
| 9094 | if (desc->mRefCount++ == 0) { |
Glenn Kasten | d053971 | 2012-01-30 12:56:03 -0800 | [diff] [blame] | 9095 | Vector< sp<EffectModule> > effects; |
| 9096 | getSuspendEligibleEffects(effects); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9097 | for (size_t i = 0; i < effects.size(); i++) { |
| 9098 | setEffectSuspended_l(&effects[i]->desc().type, true); |
| 9099 | } |
| 9100 | } |
| 9101 | } else { |
| 9102 | if (index < 0) { |
| 9103 | return; |
| 9104 | } |
| 9105 | desc = mSuspendedEffects.valueAt(index); |
| 9106 | if (desc->mRefCount <= 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 9107 | ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9108 | desc->mRefCount = 1; |
| 9109 | } |
| 9110 | if (--desc->mRefCount == 0) { |
| 9111 | Vector<const effect_uuid_t *> types; |
| 9112 | for (size_t i = 0; i < mSuspendedEffects.size(); i++) { |
| 9113 | if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) { |
| 9114 | continue; |
| 9115 | } |
| 9116 | types.add(&mSuspendedEffects.valueAt(i)->mType); |
| 9117 | } |
| 9118 | for (size_t i = 0; i < types.size(); i++) { |
| 9119 | setEffectSuspended_l(types[i], false); |
| 9120 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 9121 | ALOGV("setEffectSuspendedAll_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9122 | mSuspendedEffects.removeItem((int)kKeyForSuspendAll); |
| 9123 | } |
| 9124 | } |
| 9125 | } |
| 9126 | |
Eric Laurent | 6bffdb8 | 2011-09-23 08:40:41 -0700 | [diff] [blame] | 9127 | |
| 9128 | // The volume effect is used for automated tests only |
| 9129 | #ifndef OPENSL_ES_H_ |
| 9130 | static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6, |
| 9131 | { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }; |
| 9132 | const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_; |
| 9133 | #endif //OPENSL_ES_H_ |
| 9134 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 9135 | bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc) |
| 9136 | { |
| 9137 | // auxiliary effects and visualizer are never suspended on output mix |
| 9138 | if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) && |
| 9139 | (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) || |
Eric Laurent | 6bffdb8 | 2011-09-23 08:40:41 -0700 | [diff] [blame] | 9140 | (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) || |
| 9141 | (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 9142 | return false; |
| 9143 | } |
| 9144 | return true; |
| 9145 | } |
| 9146 | |
Glenn Kasten | d053971 | 2012-01-30 12:56:03 -0800 | [diff] [blame] | 9147 | void AudioFlinger::EffectChain::getSuspendEligibleEffects(Vector< sp<AudioFlinger::EffectModule> > &effects) |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9148 | { |
Glenn Kasten | d053971 | 2012-01-30 12:56:03 -0800 | [diff] [blame] | 9149 | effects.clear(); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9150 | for (size_t i = 0; i < mEffects.size(); i++) { |
Glenn Kasten | d053971 | 2012-01-30 12:56:03 -0800 | [diff] [blame] | 9151 | if (isEffectEligibleForSuspend(mEffects[i]->desc())) { |
| 9152 | effects.add(mEffects[i]); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9153 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9154 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9155 | } |
| 9156 | |
| 9157 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled( |
| 9158 | const effect_uuid_t *type) |
| 9159 | { |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 9160 | sp<EffectModule> effect = getEffectFromType_l(type); |
| 9161 | return effect != 0 && effect->isEnabled() ? effect : 0; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9162 | } |
| 9163 | |
| 9164 | void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 9165 | bool enabled) |
| 9166 | { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 9167 | ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9168 | if (enabled) { |
| 9169 | if (index < 0) { |
| 9170 | // if the effect is not suspend check if all effects are suspended |
| 9171 | index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); |
| 9172 | if (index < 0) { |
| 9173 | return; |
| 9174 | } |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 9175 | if (!isEffectEligibleForSuspend(effect->desc())) { |
| 9176 | return; |
| 9177 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9178 | setEffectSuspended_l(&effect->desc().type, enabled); |
| 9179 | index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 9180 | if (index < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 9181 | ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!"); |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 9182 | return; |
| 9183 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9184 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 9185 | ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x", |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 9186 | effect->desc().type.timeLow); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9187 | sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); |
| 9188 | // if effect is requested to suspended but was not yet enabled, supend it now. |
| 9189 | if (desc->mEffect == 0) { |
| 9190 | desc->mEffect = effect; |
| 9191 | effect->setEnabled(false); |
| 9192 | effect->setSuspended(true); |
| 9193 | } |
| 9194 | } else { |
| 9195 | if (index < 0) { |
| 9196 | return; |
| 9197 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 9198 | ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x", |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 9199 | effect->desc().type.timeLow); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9200 | sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); |
| 9201 | desc->mEffect.clear(); |
| 9202 | effect->setSuspended(false); |
| 9203 | } |
| 9204 | } |
| 9205 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9206 | #undef LOG_TAG |
| 9207 | #define LOG_TAG "AudioFlinger" |
| 9208 | |
| 9209 | // ---------------------------------------------------------------------------- |
| 9210 | |
| 9211 | status_t AudioFlinger::onTransact( |
| 9212 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 9213 | { |
| 9214 | return BnAudioFlinger::onTransact(code, data, reply, flags); |
| 9215 | } |
| 9216 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9217 | }; // namespace android |