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 | |
Glenn Kasten | 153b9fe | 2013-07-15 11:23:36 -0700 | [diff] [blame] | 22 | #include "Configuration.h" |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 23 | #include <dirent.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 24 | #include <math.h> |
| 25 | #include <signal.h> |
| 26 | #include <sys/time.h> |
| 27 | #include <sys/resource.h> |
| 28 | |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 29 | #include <binder/IPCThreadState.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 30 | #include <binder/IServiceManager.h> |
| 31 | #include <utils/Log.h> |
Glenn Kasten | d8e6fd3 | 2012-05-07 11:07:57 -0700 | [diff] [blame] | 32 | #include <utils/Trace.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 33 | #include <binder/Parcel.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 34 | #include <utils/String16.h> |
| 35 | #include <utils/threads.h> |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 36 | #include <utils/Atomic.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 37 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 38 | #include <cutils/bitops.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 39 | #include <cutils/properties.h> |
| 40 | |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 41 | #include <system/audio.h> |
Dima Zavin | 7394a4f | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 42 | #include <hardware/audio.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 43 | |
| 44 | #include "AudioMixer.h" |
| 45 | #include "AudioFlinger.h" |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 46 | #include "ServiceUtilities.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 47 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 48 | #include <media/AudioResamplerPublic.h> |
| 49 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 50 | #include <media/EffectsFactoryApi.h> |
Eric Laurent | 6d8b694 | 2011-06-24 07:01:31 -0700 | [diff] [blame] | 51 | #include <audio_effects/effect_visualizer.h> |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 52 | #include <audio_effects/effect_ns.h> |
| 53 | #include <audio_effects/effect_aec.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 54 | |
Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 55 | #include <audio_utils/primitives.h> |
| 56 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 57 | #include <powermanager/PowerManager.h> |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 58 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 59 | #include <common_time/cc_helper.h> |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 60 | |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 61 | #include <media/IMediaLogService.h> |
| 62 | |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 63 | #include <media/nbaio/Pipe.h> |
| 64 | #include <media/nbaio/PipeReader.h> |
Glenn Kasten | 1ab85ec | 2013-05-31 09:18:43 -0700 | [diff] [blame] | 65 | #include <media/AudioParameter.h> |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 66 | #include <private/android_filesystem_config.h> |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 67 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 68 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 69 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 70 | // Note: the following macro is used for extremely verbose logging message. In |
| 71 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 72 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 73 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 74 | // turned on. Do not uncomment the #def below unless you really know what you |
| 75 | // are doing and want to see all of the extremely verbose messages. |
| 76 | //#define VERY_VERY_VERBOSE_LOGGING |
| 77 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 78 | #define ALOGVV ALOGV |
| 79 | #else |
| 80 | #define ALOGVV(a...) do { } while(0) |
| 81 | #endif |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 82 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 83 | namespace android { |
| 84 | |
Glenn Kasten | ec1d6b5 | 2011-12-12 09:04:45 -0800 | [diff] [blame] | 85 | static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n"; |
| 86 | static const char kHardwareLockedString[] = "Hardware lock is taken\n"; |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 87 | static const char kClientLockedString[] = "Client lock is taken\n"; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 88 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 89 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 90 | nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs; |
Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 91 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 92 | uint32_t AudioFlinger::mScreenState; |
Glenn Kasten | 3ed2920 | 2012-08-07 15:24:44 -0700 | [diff] [blame] | 93 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 94 | #ifdef TEE_SINK |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 95 | bool AudioFlinger::mTeeSinkInputEnabled = false; |
| 96 | bool AudioFlinger::mTeeSinkOutputEnabled = false; |
| 97 | bool AudioFlinger::mTeeSinkTrackEnabled = false; |
| 98 | |
| 99 | size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault; |
| 100 | size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault; |
| 101 | size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault; |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 102 | #endif |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 103 | |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 104 | // In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off |
| 105 | // we define a minimum time during which a global effect is considered enabled. |
| 106 | static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200); |
| 107 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 108 | // ---------------------------------------------------------------------------- |
| 109 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 110 | const char *formatToString(audio_format_t format) { |
aarti jadhav-gaikwad | 2829edc | 2014-06-18 15:25:26 +0530 | [diff] [blame] | 111 | switch (format & AUDIO_FORMAT_MAIN_MASK) { |
| 112 | case AUDIO_FORMAT_PCM: |
| 113 | switch (format) { |
| 114 | case AUDIO_FORMAT_PCM_16_BIT: return "pcm16"; |
| 115 | case AUDIO_FORMAT_PCM_8_BIT: return "pcm8"; |
| 116 | case AUDIO_FORMAT_PCM_32_BIT: return "pcm32"; |
| 117 | case AUDIO_FORMAT_PCM_8_24_BIT: return "pcm8.24"; |
| 118 | case AUDIO_FORMAT_PCM_FLOAT: return "pcmfloat"; |
| 119 | case AUDIO_FORMAT_PCM_24_BIT_PACKED: return "pcm24"; |
| 120 | default: |
| 121 | break; |
| 122 | } |
| 123 | break; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 124 | case AUDIO_FORMAT_MP3: return "mp3"; |
| 125 | case AUDIO_FORMAT_AMR_NB: return "amr-nb"; |
| 126 | case AUDIO_FORMAT_AMR_WB: return "amr-wb"; |
| 127 | case AUDIO_FORMAT_AAC: return "aac"; |
| 128 | case AUDIO_FORMAT_HE_AAC_V1: return "he-aac-v1"; |
| 129 | case AUDIO_FORMAT_HE_AAC_V2: return "he-aac-v2"; |
| 130 | case AUDIO_FORMAT_VORBIS: return "vorbis"; |
aarti jadhav-gaikwad | 2829edc | 2014-06-18 15:25:26 +0530 | [diff] [blame] | 131 | case AUDIO_FORMAT_OPUS: return "opus"; |
| 132 | case AUDIO_FORMAT_AC3: return "ac-3"; |
| 133 | case AUDIO_FORMAT_E_AC3: return "e-ac-3"; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 134 | default: |
| 135 | break; |
| 136 | } |
| 137 | return "unknown"; |
| 138 | } |
| 139 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 140 | 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] | 141 | { |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 142 | const hw_module_t *mod; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 143 | int rc; |
| 144 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 145 | rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod); |
| 146 | ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__, |
| 147 | AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); |
| 148 | if (rc) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 149 | goto out; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 150 | } |
| 151 | rc = audio_hw_device_open(mod, dev); |
| 152 | ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__, |
| 153 | AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); |
| 154 | if (rc) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 155 | goto out; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 156 | } |
Eric Laurent | 5806b35 | 2014-05-22 12:23:26 -0700 | [diff] [blame] | 157 | if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) { |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 158 | ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version); |
| 159 | rc = BAD_VALUE; |
| 160 | goto out; |
| 161 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 162 | return 0; |
| 163 | |
| 164 | out: |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 165 | *dev = NULL; |
| 166 | return rc; |
| 167 | } |
| 168 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 169 | // ---------------------------------------------------------------------------- |
| 170 | |
| 171 | AudioFlinger::AudioFlinger() |
| 172 | : BnAudioFlinger(), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 173 | mPrimaryHardwareDev(NULL), |
Glenn Kasten | 9ea65d0 | 2014-01-17 10:21:24 -0800 | [diff] [blame] | 174 | mAudioHwDevs(NULL), |
Glenn Kasten | 7d6c35b | 2012-07-02 12:45:10 -0700 | [diff] [blame] | 175 | mHardwareStatus(AUDIO_HW_IDLE), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 176 | mMasterVolume(1.0f), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 177 | mMasterMute(false), |
| 178 | mNextUniqueId(1), |
| 179 | mMode(AUDIO_MODE_INVALID), |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 180 | mBtNrecIsOff(false), |
| 181 | mIsLowRamDevice(true), |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 182 | mIsDeviceTypeKnown(false), |
Glenn Kasten | 4ea00a2 | 2014-06-02 08:29:22 -0700 | [diff] [blame] | 183 | mGlobalEffectEnableTime(0), |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 184 | mSystemReady(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 185 | { |
Glenn Kasten | 949a926 | 2013-04-16 12:35:20 -0700 | [diff] [blame] | 186 | getpid_cached = getpid(); |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 187 | char value[PROPERTY_VALUE_MAX]; |
| 188 | bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1); |
| 189 | if (doLog) { |
Glenn Kasten | b187de1 | 2014-12-30 08:18:15 -0800 | [diff] [blame] | 190 | mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters", |
| 191 | MemoryHeapBase::READ_ONLY); |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 192 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 193 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 194 | #ifdef TEE_SINK |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 195 | (void) property_get("ro.debuggable", value, "0"); |
| 196 | int debuggable = atoi(value); |
| 197 | int teeEnabled = 0; |
| 198 | if (debuggable) { |
| 199 | (void) property_get("af.tee", value, "0"); |
| 200 | teeEnabled = atoi(value); |
| 201 | } |
Glenn Kasten | f66b422 | 2014-02-20 10:23:28 -0800 | [diff] [blame] | 202 | // FIXME symbolic constants here |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 203 | if (teeEnabled & 1) { |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 204 | mTeeSinkInputEnabled = true; |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 205 | } |
| 206 | if (teeEnabled & 2) { |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 207 | mTeeSinkOutputEnabled = true; |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 208 | } |
| 209 | if (teeEnabled & 4) { |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 210 | mTeeSinkTrackEnabled = true; |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 211 | } |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 212 | #endif |
Dima Zavin | 5a61d2f | 2011-04-19 19:04:32 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void AudioFlinger::onFirstRef() |
| 216 | { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 217 | int rc = 0; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 218 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 219 | Mutex::Autolock _l(mLock); |
| 220 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 221 | /* TODO: move all this work into an Init() function */ |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 222 | char val_str[PROPERTY_VALUE_MAX] = { 0 }; |
| 223 | if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) { |
| 224 | uint32_t int_val; |
| 225 | if (1 == sscanf(val_str, "%u", &int_val)) { |
| 226 | mStandbyTimeInNsecs = milliseconds(int_val); |
| 227 | ALOGI("Using %u mSec as standby time.", int_val); |
| 228 | } else { |
| 229 | mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs; |
| 230 | ALOGI("Using default %u mSec as standby time.", |
| 231 | (uint32_t)(mStandbyTimeInNsecs / 1000000)); |
| 232 | } |
| 233 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 234 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 235 | mPatchPanel = new PatchPanel(this); |
| 236 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 237 | mMode = AUDIO_MODE_NORMAL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | AudioFlinger::~AudioFlinger() |
| 241 | { |
| 242 | while (!mRecordThreads.isEmpty()) { |
Glenn Kasten | c3ae93f | 2012-07-30 10:59:30 -0700 | [diff] [blame] | 243 | // closeInput_nonvirtual() will remove specified entry from mRecordThreads |
Glenn Kasten | d96c572 | 2012-04-25 13:44:49 -0700 | [diff] [blame] | 244 | closeInput_nonvirtual(mRecordThreads.keyAt(0)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 245 | } |
| 246 | while (!mPlaybackThreads.isEmpty()) { |
Glenn Kasten | c3ae93f | 2012-07-30 10:59:30 -0700 | [diff] [blame] | 247 | // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads |
Glenn Kasten | d96c572 | 2012-04-25 13:44:49 -0700 | [diff] [blame] | 248 | closeOutput_nonvirtual(mPlaybackThreads.keyAt(0)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 249 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 250 | |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 251 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 252 | // no mHardwareLock needed, as there are no other references to this |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 253 | audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice()); |
| 254 | delete mAudioHwDevs.valueAt(i); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 255 | } |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 256 | |
| 257 | // Tell media.log service about any old writers that still need to be unregistered |
| 258 | sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log")); |
| 259 | if (binder != 0) { |
| 260 | sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder)); |
| 261 | for (size_t count = mUnregisteredWriters.size(); count > 0; count--) { |
| 262 | sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory()); |
| 263 | mUnregisteredWriters.pop(); |
| 264 | mediaLogService->unregisterWriter(iMemory); |
| 265 | } |
| 266 | } |
| 267 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 270 | static const char * const audio_interfaces[] = { |
| 271 | AUDIO_HARDWARE_MODULE_ID_PRIMARY, |
| 272 | AUDIO_HARDWARE_MODULE_ID_A2DP, |
| 273 | AUDIO_HARDWARE_MODULE_ID_USB, |
| 274 | }; |
| 275 | #define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0]))) |
| 276 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 277 | AudioHwDevice* AudioFlinger::findSuitableHwDev_l( |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 278 | audio_module_handle_t module, |
| 279 | audio_devices_t devices) |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 280 | { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 281 | // if module is 0, the request comes from an old policy manager and we should load |
| 282 | // well known modules |
| 283 | if (module == 0) { |
| 284 | ALOGW("findSuitableHwDev_l() loading well know audio hw modules"); |
| 285 | for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) { |
| 286 | loadHwModule_l(audio_interfaces[i]); |
| 287 | } |
Eric Laurent | f1c04f9 | 2012-08-28 14:26:53 -0700 | [diff] [blame] | 288 | // then try to find a module supporting the requested device. |
| 289 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 290 | AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i); |
| 291 | audio_hw_device_t *dev = audioHwDevice->hwDevice(); |
| 292 | if ((dev->get_supported_devices != NULL) && |
| 293 | (dev->get_supported_devices(dev) & devices) == devices) |
| 294 | return audioHwDevice; |
| 295 | } |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 296 | } else { |
| 297 | // check a match for the requested module handle |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 298 | AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module); |
| 299 | if (audioHwDevice != NULL) { |
| 300 | return audioHwDevice; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 303 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 304 | return NULL; |
| 305 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 306 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 307 | void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 308 | { |
| 309 | const size_t SIZE = 256; |
| 310 | char buffer[SIZE]; |
| 311 | String8 result; |
| 312 | |
| 313 | result.append("Clients:\n"); |
| 314 | for (size_t i = 0; i < mClients.size(); ++i) { |
Glenn Kasten | 77c1119 | 2012-01-25 14:27:41 -0800 | [diff] [blame] | 315 | sp<Client> client = mClients.valueAt(i).promote(); |
| 316 | if (client != 0) { |
| 317 | snprintf(buffer, SIZE, " pid: %d\n", client->pid()); |
| 318 | result.append(buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 321 | |
Eric Laurent | d1b28d4 | 2013-09-18 18:47:13 -0700 | [diff] [blame] | 322 | result.append("Notification Clients:\n"); |
| 323 | for (size_t i = 0; i < mNotificationClients.size(); ++i) { |
| 324 | snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i)); |
| 325 | result.append(buffer); |
| 326 | } |
| 327 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 328 | result.append("Global session refs:\n"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 329 | result.append(" session pid count\n"); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 330 | for (size_t i = 0; i < mAudioSessionRefs.size(); i++) { |
| 331 | AudioSessionRef *r = mAudioSessionRefs[i]; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 332 | snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 333 | result.append(buffer); |
| 334 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 335 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 339 | void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 340 | { |
| 341 | const size_t SIZE = 256; |
| 342 | char buffer[SIZE]; |
| 343 | String8 result; |
Glenn Kasten | a4454b4 | 2012-01-04 11:02:33 -0800 | [diff] [blame] | 344 | hardware_call_state hardwareStatus = mHardwareStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 345 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 346 | snprintf(buffer, SIZE, "Hardware status: %d\n" |
| 347 | "Standby Time mSec: %u\n", |
| 348 | hardwareStatus, |
| 349 | (uint32_t)(mStandbyTimeInNsecs / 1000000)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 350 | result.append(buffer); |
| 351 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 354 | void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 355 | { |
| 356 | const size_t SIZE = 256; |
| 357 | char buffer[SIZE]; |
| 358 | String8 result; |
| 359 | snprintf(buffer, SIZE, "Permission Denial: " |
| 360 | "can't dump AudioFlinger from pid=%d, uid=%d\n", |
| 361 | IPCThreadState::self()->getCallingPid(), |
| 362 | IPCThreadState::self()->getCallingUid()); |
| 363 | result.append(buffer); |
| 364 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 367 | bool AudioFlinger::dumpTryLock(Mutex& mutex) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 368 | { |
| 369 | bool locked = false; |
| 370 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 371 | if (mutex.tryLock() == NO_ERROR) { |
| 372 | locked = true; |
| 373 | break; |
| 374 | } |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 375 | usleep(kDumpLockSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 376 | } |
| 377 | return locked; |
| 378 | } |
| 379 | |
| 380 | status_t AudioFlinger::dump(int fd, const Vector<String16>& args) |
| 381 | { |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 382 | if (!dumpAllowed()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 383 | dumpPermissionDenial(fd, args); |
| 384 | } else { |
| 385 | // get state of hardware lock |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 386 | bool hardwareLocked = dumpTryLock(mHardwareLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 387 | if (!hardwareLocked) { |
| 388 | String8 result(kHardwareLockedString); |
| 389 | write(fd, result.string(), result.size()); |
| 390 | } else { |
| 391 | mHardwareLock.unlock(); |
| 392 | } |
| 393 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 394 | bool locked = dumpTryLock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 395 | |
| 396 | // failed to lock - AudioFlinger is probably deadlocked |
| 397 | if (!locked) { |
| 398 | String8 result(kDeadlockedString); |
| 399 | write(fd, result.string(), result.size()); |
| 400 | } |
| 401 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 402 | bool clientLocked = dumpTryLock(mClientLock); |
| 403 | if (!clientLocked) { |
| 404 | String8 result(kClientLockedString); |
| 405 | write(fd, result.string(), result.size()); |
| 406 | } |
Marco Nelissen | d89eadd | 2014-10-07 13:28:44 -0700 | [diff] [blame] | 407 | |
| 408 | EffectDumpEffects(fd); |
| 409 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 410 | dumpClients(fd, args); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 411 | if (clientLocked) { |
| 412 | mClientLock.unlock(); |
| 413 | } |
| 414 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 415 | dumpInternals(fd, args); |
| 416 | |
| 417 | // dump playback threads |
| 418 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 419 | mPlaybackThreads.valueAt(i)->dump(fd, args); |
| 420 | } |
| 421 | |
| 422 | // dump record threads |
| 423 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 424 | mRecordThreads.valueAt(i)->dump(fd, args); |
| 425 | } |
| 426 | |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 427 | // dump orphan effect chains |
| 428 | if (mOrphanEffectChains.size() != 0) { |
| 429 | write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n")); |
| 430 | for (size_t i = 0; i < mOrphanEffectChains.size(); i++) { |
| 431 | mOrphanEffectChains.valueAt(i)->dump(fd, args); |
| 432 | } |
| 433 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 434 | // dump all hardware devs |
| 435 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 436 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 437 | dev->dump(dev, fd); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 438 | } |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 439 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 440 | #ifdef TEE_SINK |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 441 | // dump the serially shared record tee sink |
| 442 | if (mRecordTeeSource != 0) { |
| 443 | dumpTee(fd, mRecordTeeSource); |
| 444 | } |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 445 | #endif |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 446 | |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 447 | if (locked) { |
| 448 | mLock.unlock(); |
| 449 | } |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 450 | |
| 451 | // append a copy of media.log here by forwarding fd to it, but don't attempt |
| 452 | // to lookup the service if it's not running, as it will block for a second |
| 453 | if (mLogMemoryDealer != 0) { |
| 454 | sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log")); |
| 455 | if (binder != 0) { |
Elliott Hughes | 8b5f642 | 2014-05-22 01:22:06 -0700 | [diff] [blame] | 456 | dprintf(fd, "\nmedia.log:\n"); |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 457 | Vector<String16> args; |
| 458 | binder->dump(fd, args); |
| 459 | } |
| 460 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 461 | } |
| 462 | return NO_ERROR; |
| 463 | } |
| 464 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 465 | sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid) |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 466 | { |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 467 | Mutex::Autolock _cl(mClientLock); |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 468 | // If pid is already in the mClients wp<> map, then use that entry |
| 469 | // (for which promote() is always != 0), otherwise create a new entry and Client. |
| 470 | sp<Client> client = mClients.valueFor(pid).promote(); |
| 471 | if (client == 0) { |
| 472 | client = new Client(this, pid); |
| 473 | mClients.add(pid, client); |
| 474 | } |
| 475 | |
| 476 | return client; |
| 477 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 478 | |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 479 | sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name) |
| 480 | { |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 481 | // If there is no memory allocated for logs, return a dummy writer that does nothing |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 482 | if (mLogMemoryDealer == 0) { |
| 483 | return new NBLog::Writer(); |
| 484 | } |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 485 | sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log")); |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 486 | // Similarly if we can't contact the media.log service, also return a dummy writer |
| 487 | if (binder == 0) { |
| 488 | return new NBLog::Writer(); |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 489 | } |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 490 | sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder)); |
| 491 | sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size)); |
| 492 | // If allocation fails, consult the vector of previously unregistered writers |
| 493 | // and garbage-collect one or more them until an allocation succeeds |
| 494 | if (shared == 0) { |
| 495 | Mutex::Autolock _l(mUnregisteredWritersLock); |
| 496 | for (size_t count = mUnregisteredWriters.size(); count > 0; count--) { |
| 497 | { |
| 498 | // Pick the oldest stale writer to garbage-collect |
| 499 | sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory()); |
| 500 | mUnregisteredWriters.removeAt(0); |
| 501 | mediaLogService->unregisterWriter(iMemory); |
| 502 | // Now the media.log remote reference to IMemory is gone. When our last local |
| 503 | // reference to IMemory also drops to zero at end of this block, |
| 504 | // the IMemory destructor will deallocate the region from mLogMemoryDealer. |
| 505 | } |
| 506 | // Re-attempt the allocation |
| 507 | shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size)); |
| 508 | if (shared != 0) { |
| 509 | goto success; |
| 510 | } |
| 511 | } |
| 512 | // Even after garbage-collecting all old writers, there is still not enough memory, |
| 513 | // so return a dummy writer |
| 514 | return new NBLog::Writer(); |
| 515 | } |
| 516 | success: |
| 517 | mediaLogService->registerWriter(shared, size, name); |
| 518 | return new NBLog::Writer(size, shared); |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer) |
| 522 | { |
Glenn Kasten | 685ef09 | 2013-02-04 08:15:34 -0800 | [diff] [blame] | 523 | if (writer == 0) { |
| 524 | return; |
| 525 | } |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 526 | sp<IMemory> iMemory(writer->getIMemory()); |
| 527 | if (iMemory == 0) { |
| 528 | return; |
| 529 | } |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 530 | // Rather than removing the writer immediately, append it to a queue of old writers to |
| 531 | // be garbage-collected later. This allows us to continue to view old logs for a while. |
| 532 | Mutex::Autolock _l(mUnregisteredWritersLock); |
| 533 | mUnregisteredWriters.push(writer); |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 534 | } |
| 535 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 536 | // IAudioFlinger interface |
| 537 | |
| 538 | |
| 539 | sp<IAudioTrack> AudioFlinger::createTrack( |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 540 | audio_stream_type_t streamType, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 541 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 542 | audio_format_t format, |
Glenn Kasten | 254af18 | 2012-07-03 14:59:05 -0700 | [diff] [blame] | 543 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 544 | size_t *frameCount, |
Glenn Kasten | e0b0717 | 2012-11-06 15:03:34 -0800 | [diff] [blame] | 545 | IAudioFlinger::track_flags_t *flags, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 546 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 547 | audio_io_handle_t output, |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 548 | pid_t tid, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 549 | int *sessionId, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 550 | int clientUid, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 551 | status_t *status) |
| 552 | { |
| 553 | sp<PlaybackThread::Track> track; |
| 554 | sp<TrackHandle> trackHandle; |
| 555 | sp<Client> client; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 556 | status_t lStatus; |
| 557 | int lSessionId; |
| 558 | |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 559 | // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC, |
| 560 | // but if someone uses binder directly they could bypass that and cause us to crash |
| 561 | if (uint32_t(streamType) >= AUDIO_STREAM_CNT) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 562 | ALOGE("createTrack() invalid stream type %d", streamType); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 563 | lStatus = BAD_VALUE; |
| 564 | goto Exit; |
| 565 | } |
| 566 | |
Glenn Kasten | 53b5d09 | 2014-02-05 10:00:23 -0800 | [diff] [blame] | 567 | // further sample rate checks are performed by createTrack_l() depending on the thread type |
| 568 | if (sampleRate == 0) { |
| 569 | ALOGE("createTrack() invalid sample rate %u", sampleRate); |
| 570 | lStatus = BAD_VALUE; |
| 571 | goto Exit; |
| 572 | } |
| 573 | |
| 574 | // further channel mask checks are performed by createTrack_l() depending on the thread type |
| 575 | if (!audio_is_output_channel(channelMask)) { |
| 576 | ALOGE("createTrack() invalid channel mask %#x", channelMask); |
| 577 | lStatus = BAD_VALUE; |
| 578 | goto Exit; |
| 579 | } |
| 580 | |
Glenn Kasten | c4b88a8 | 2014-04-30 16:54:30 -0700 | [diff] [blame] | 581 | // further format checks are performed by createTrack_l() depending on the thread type |
| 582 | if (!audio_is_valid_format(format)) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 583 | ALOGE("createTrack() invalid format %#x", format); |
Glenn Kasten | 60a8392 | 2012-06-21 12:56:37 -0700 | [diff] [blame] | 584 | lStatus = BAD_VALUE; |
| 585 | goto Exit; |
| 586 | } |
| 587 | |
Glenn Kasten | 663c224 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 588 | if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) { |
| 589 | ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()"); |
| 590 | lStatus = BAD_VALUE; |
| 591 | goto Exit; |
| 592 | } |
| 593 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 594 | { |
| 595 | Mutex::Autolock _l(mLock); |
| 596 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 597 | if (thread == NULL) { |
Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 598 | ALOGE("no playback thread found for output handle %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 599 | lStatus = BAD_VALUE; |
| 600 | goto Exit; |
| 601 | } |
| 602 | |
Glenn Kasten | 8d6cc84 | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 603 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 604 | client = registerPid(pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 605 | |
Glenn Kasten | e848bd9 | 2014-03-13 15:00:32 -0700 | [diff] [blame] | 606 | PlaybackThread *effectThread = NULL; |
Glenn Kasten | aea7ea0 | 2013-06-26 09:25:47 -0700 | [diff] [blame] | 607 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) { |
Glenn Kasten | 570f633 | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 608 | lSessionId = *sessionId; |
Eric Laurent | f436fdc | 2012-05-24 11:07:14 -0700 | [diff] [blame] | 609 | // check if an effect chain with the same session ID is present on another |
| 610 | // output thread and move it here. |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 611 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 612 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 613 | if (mPlaybackThreads.keyAt(i) != output) { |
Glenn Kasten | 570f633 | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 614 | uint32_t sessions = t->hasAudioSession(lSessionId); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 615 | if (sessions & PlaybackThread::EFFECT_SESSION) { |
| 616 | effectThread = t.get(); |
Eric Laurent | f436fdc | 2012-05-24 11:07:14 -0700 | [diff] [blame] | 617 | break; |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 618 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 619 | } |
| 620 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 621 | } else { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 622 | // if no audio session id is provided, create one here |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 623 | lSessionId = nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 624 | if (sessionId != NULL) { |
| 625 | *sessionId = lSessionId; |
| 626 | } |
| 627 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 628 | ALOGV("createTrack() lSessionId: %d", lSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 629 | |
| 630 | track = thread->createTrack_l(client, streamType, sampleRate, format, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 631 | channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 632 | LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0)); |
Glenn Kasten | 2fc1473 | 2013-08-05 14:58:14 -0700 | [diff] [blame] | 633 | // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 634 | |
| 635 | // move effect chain to this output thread if an effect on same session was waiting |
| 636 | // for a track to be created |
| 637 | if (lStatus == NO_ERROR && effectThread != NULL) { |
Glenn Kasten | 2fc1473 | 2013-08-05 14:58:14 -0700 | [diff] [blame] | 638 | // no risk of deadlock because AudioFlinger::mLock is held |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 639 | Mutex::Autolock _dl(thread->mLock); |
| 640 | Mutex::Autolock _sl(effectThread->mLock); |
| 641 | moveEffectChain_l(lSessionId, effectThread, thread, true); |
| 642 | } |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 643 | |
| 644 | // Look for sync events awaiting for a session to be used. |
Mark Salyzyn | 3ab368e | 2014-04-15 14:55:53 -0700 | [diff] [blame] | 645 | for (size_t i = 0; i < mPendingSyncEvents.size(); i++) { |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 646 | if (mPendingSyncEvents[i]->triggerSession() == lSessionId) { |
| 647 | if (thread->isValidSyncEvent(mPendingSyncEvents[i])) { |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 648 | if (lStatus == NO_ERROR) { |
Glenn Kasten | d23eedc | 2012-08-02 13:35:47 -0700 | [diff] [blame] | 649 | (void) track->setSyncEvent(mPendingSyncEvents[i]); |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 650 | } else { |
| 651 | mPendingSyncEvents[i]->cancel(); |
| 652 | } |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 653 | mPendingSyncEvents.removeAt(i); |
| 654 | i--; |
| 655 | } |
| 656 | } |
| 657 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 658 | |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 659 | setAudioHwSyncForSession_l(thread, (audio_session_t)lSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 660 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 661 | |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 662 | if (lStatus != NO_ERROR) { |
| 663 | // remove local strong reference to Client before deleting the Track so that the |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 664 | // Client destructor is called by the TrackBase destructor with mClientLock held |
Eric Laurent | fe1a94e | 2014-05-26 16:03:08 -0700 | [diff] [blame] | 665 | // Don't hold mClientLock when releasing the reference on the track as the |
| 666 | // destructor will acquire it. |
| 667 | { |
| 668 | Mutex::Autolock _cl(mClientLock); |
| 669 | client.clear(); |
| 670 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 671 | track.clear(); |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 672 | goto Exit; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 675 | // return handle to client |
| 676 | trackHandle = new TrackHandle(track); |
| 677 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 678 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 679 | *status = lStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 680 | return trackHandle; |
| 681 | } |
| 682 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 683 | uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 684 | { |
| 685 | Mutex::Autolock _l(mLock); |
| 686 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 687 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 688 | ALOGW("sampleRate() unknown thread %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 689 | return 0; |
| 690 | } |
| 691 | return thread->sampleRate(); |
| 692 | } |
| 693 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 694 | audio_format_t AudioFlinger::format(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 695 | { |
| 696 | Mutex::Autolock _l(mLock); |
| 697 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 698 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 699 | ALOGW("format() unknown thread %d", output); |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 700 | return AUDIO_FORMAT_INVALID; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 701 | } |
| 702 | return thread->format(); |
| 703 | } |
| 704 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 705 | size_t AudioFlinger::frameCount(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 706 | { |
| 707 | Mutex::Autolock _l(mLock); |
| 708 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 709 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 710 | ALOGW("frameCount() unknown thread %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 711 | return 0; |
| 712 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 713 | // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers; |
| 714 | // should examine all callers and fix them to handle smaller counts |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 715 | return thread->frameCount(); |
| 716 | } |
| 717 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 718 | uint32_t AudioFlinger::latency(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 719 | { |
| 720 | Mutex::Autolock _l(mLock); |
| 721 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 722 | if (thread == NULL) { |
Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 723 | ALOGW("latency(): no playback thread found for output handle %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 724 | return 0; |
| 725 | } |
| 726 | return thread->latency(); |
| 727 | } |
| 728 | |
| 729 | status_t AudioFlinger::setMasterVolume(float value) |
| 730 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 731 | status_t ret = initCheck(); |
| 732 | if (ret != NO_ERROR) { |
| 733 | return ret; |
| 734 | } |
| 735 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 736 | // check calling permissions |
| 737 | if (!settingsAllowed()) { |
| 738 | return PERMISSION_DENIED; |
| 739 | } |
| 740 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 741 | Mutex::Autolock _l(mLock); |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 742 | mMasterVolume = value; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 743 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 744 | // Set master volume in the HALs which support it. |
| 745 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 746 | AutoMutex lock(mHardwareLock); |
| 747 | AudioHwDevice *dev = mAudioHwDevs.valueAt(i); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 748 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 749 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
| 750 | if (dev->canSetMasterVolume()) { |
| 751 | dev->hwDevice()->set_master_volume(dev->hwDevice(), value); |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 752 | } |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 753 | mHardwareStatus = AUDIO_HW_IDLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 754 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 755 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 756 | // Now set the master volume in each playback thread. Playback threads |
| 757 | // assigned to HALs which do not have master volume support will apply |
| 758 | // master volume during the mix operation. Threads with HALs which do |
| 759 | // support master volume will simply ignore the setting. |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 760 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 761 | if (mPlaybackThreads.valueAt(i)->isDuplicating()) { |
| 762 | continue; |
| 763 | } |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 764 | mPlaybackThreads.valueAt(i)->setMasterVolume(value); |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 765 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 766 | |
| 767 | return NO_ERROR; |
| 768 | } |
| 769 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 770 | status_t AudioFlinger::setMode(audio_mode_t mode) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 771 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 772 | status_t ret = initCheck(); |
| 773 | if (ret != NO_ERROR) { |
| 774 | return ret; |
| 775 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 776 | |
| 777 | // check calling permissions |
| 778 | if (!settingsAllowed()) { |
| 779 | return PERMISSION_DENIED; |
| 780 | } |
Glenn Kasten | 930f4ca | 2012-01-06 16:47:31 -0800 | [diff] [blame] | 781 | if (uint32_t(mode) >= AUDIO_MODE_CNT) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 782 | ALOGW("Illegal value: setMode(%d)", mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 783 | return BAD_VALUE; |
| 784 | } |
| 785 | |
| 786 | { // scope for the lock |
| 787 | AutoMutex lock(mHardwareLock); |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 788 | audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 789 | mHardwareStatus = AUDIO_HW_SET_MODE; |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 790 | ret = dev->set_mode(dev, mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 791 | mHardwareStatus = AUDIO_HW_IDLE; |
| 792 | } |
| 793 | |
| 794 | if (NO_ERROR == ret) { |
| 795 | Mutex::Autolock _l(mLock); |
| 796 | mMode = mode; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 797 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 798 | mPlaybackThreads.valueAt(i)->setMode(mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | return ret; |
| 802 | } |
| 803 | |
| 804 | status_t AudioFlinger::setMicMute(bool state) |
| 805 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 806 | status_t ret = initCheck(); |
| 807 | if (ret != NO_ERROR) { |
| 808 | return ret; |
| 809 | } |
| 810 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 811 | // check calling permissions |
| 812 | if (!settingsAllowed()) { |
| 813 | return PERMISSION_DENIED; |
| 814 | } |
| 815 | |
| 816 | AutoMutex lock(mHardwareLock); |
| 817 | mHardwareStatus = AUDIO_HW_SET_MIC_MUTE; |
Eric Laurent | 2f035f5 | 2014-09-14 12:11:52 -0700 | [diff] [blame] | 818 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 819 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
| 820 | status_t result = dev->set_mic_mute(dev, state); |
| 821 | if (result != NO_ERROR) { |
| 822 | ret = result; |
| 823 | } |
| 824 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 825 | mHardwareStatus = AUDIO_HW_IDLE; |
| 826 | return ret; |
| 827 | } |
| 828 | |
| 829 | bool AudioFlinger::getMicMute() const |
| 830 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 831 | status_t ret = initCheck(); |
| 832 | if (ret != NO_ERROR) { |
| 833 | return false; |
| 834 | } |
Ricardo Garcia | 3e91b5d | 2015-02-19 10:54:52 -0800 | [diff] [blame] | 835 | bool mute = true; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 836 | bool state = AUDIO_MODE_INVALID; |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 837 | AutoMutex lock(mHardwareLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 838 | mHardwareStatus = AUDIO_HW_GET_MIC_MUTE; |
Ricardo Garcia | 3e91b5d | 2015-02-19 10:54:52 -0800 | [diff] [blame] | 839 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 840 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
| 841 | status_t result = dev->get_mic_mute(dev, &state); |
| 842 | if (result == NO_ERROR) { |
| 843 | mute = mute && state; |
| 844 | } |
| 845 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 846 | mHardwareStatus = AUDIO_HW_IDLE; |
Ricardo Garcia | 3e91b5d | 2015-02-19 10:54:52 -0800 | [diff] [blame] | 847 | |
| 848 | return mute; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | status_t AudioFlinger::setMasterMute(bool muted) |
| 852 | { |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 853 | status_t ret = initCheck(); |
| 854 | if (ret != NO_ERROR) { |
| 855 | return ret; |
| 856 | } |
| 857 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 858 | // check calling permissions |
| 859 | if (!settingsAllowed()) { |
| 860 | return PERMISSION_DENIED; |
| 861 | } |
| 862 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 863 | Mutex::Autolock _l(mLock); |
| 864 | mMasterMute = muted; |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 865 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 866 | // Set master mute in the HALs which support it. |
| 867 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 868 | AutoMutex lock(mHardwareLock); |
| 869 | AudioHwDevice *dev = mAudioHwDevs.valueAt(i); |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 870 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 871 | mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE; |
| 872 | if (dev->canSetMasterMute()) { |
| 873 | dev->hwDevice()->set_master_mute(dev->hwDevice(), muted); |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 874 | } |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 875 | mHardwareStatus = AUDIO_HW_IDLE; |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 876 | } |
| 877 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 878 | // Now set the master mute in each playback thread. Playback threads |
| 879 | // assigned to HALs which do not have master mute support will apply master |
| 880 | // mute during the mix operation. Threads with HALs which do support master |
| 881 | // mute will simply ignore the setting. |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 882 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 883 | if (mPlaybackThreads.valueAt(i)->isDuplicating()) { |
| 884 | continue; |
| 885 | } |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 886 | mPlaybackThreads.valueAt(i)->setMasterMute(muted); |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 887 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 888 | |
| 889 | return NO_ERROR; |
| 890 | } |
| 891 | |
| 892 | float AudioFlinger::masterVolume() const |
| 893 | { |
Glenn Kasten | 9806710 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 894 | Mutex::Autolock _l(mLock); |
| 895 | return masterVolume_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | bool AudioFlinger::masterMute() const |
| 899 | { |
Glenn Kasten | 9806710 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 900 | Mutex::Autolock _l(mLock); |
| 901 | return masterMute_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 902 | } |
| 903 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 904 | float AudioFlinger::masterVolume_l() const |
| 905 | { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 906 | return mMasterVolume; |
| 907 | } |
| 908 | |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 909 | bool AudioFlinger::masterMute_l() const |
| 910 | { |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 911 | return mMasterMute; |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 912 | } |
| 913 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 914 | status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const |
| 915 | { |
| 916 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
| 917 | ALOGW("setStreamVolume() invalid stream %d", stream); |
| 918 | return BAD_VALUE; |
| 919 | } |
| 920 | pid_t caller = IPCThreadState::self()->getCallingPid(); |
| 921 | if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) { |
| 922 | ALOGW("setStreamVolume() pid %d cannot use internal stream type %d", caller, stream); |
| 923 | return PERMISSION_DENIED; |
| 924 | } |
| 925 | |
| 926 | return NO_ERROR; |
| 927 | } |
| 928 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 929 | status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value, |
| 930 | audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 931 | { |
| 932 | // check calling permissions |
| 933 | if (!settingsAllowed()) { |
| 934 | return PERMISSION_DENIED; |
| 935 | } |
| 936 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 937 | status_t status = checkStreamType(stream); |
| 938 | if (status != NO_ERROR) { |
| 939 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 940 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 941 | ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 942 | |
| 943 | AutoMutex lock(mLock); |
| 944 | PlaybackThread *thread = NULL; |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 945 | if (output != AUDIO_IO_HANDLE_NONE) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 946 | thread = checkPlaybackThread_l(output); |
| 947 | if (thread == NULL) { |
| 948 | return BAD_VALUE; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | mStreamTypes[stream].volume = value; |
| 953 | |
| 954 | if (thread == NULL) { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 955 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 956 | mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 957 | } |
| 958 | } else { |
| 959 | thread->setStreamVolume(stream, value); |
| 960 | } |
| 961 | |
| 962 | return NO_ERROR; |
| 963 | } |
| 964 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 965 | status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 966 | { |
| 967 | // check calling permissions |
| 968 | if (!settingsAllowed()) { |
| 969 | return PERMISSION_DENIED; |
| 970 | } |
| 971 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 972 | status_t status = checkStreamType(stream); |
| 973 | if (status != NO_ERROR) { |
| 974 | return status; |
| 975 | } |
| 976 | ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH"); |
| 977 | |
| 978 | if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 979 | ALOGE("setStreamMute() invalid stream %d", stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 980 | return BAD_VALUE; |
| 981 | } |
| 982 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 983 | AutoMutex lock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 984 | mStreamTypes[stream].mute = muted; |
Mark Salyzyn | 3ab368e | 2014-04-15 14:55:53 -0700 | [diff] [blame] | 985 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 986 | mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 987 | |
| 988 | return NO_ERROR; |
| 989 | } |
| 990 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 991 | 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] | 992 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 993 | status_t status = checkStreamType(stream); |
| 994 | if (status != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 995 | return 0.0f; |
| 996 | } |
| 997 | |
| 998 | AutoMutex lock(mLock); |
| 999 | float volume; |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 1000 | if (output != AUDIO_IO_HANDLE_NONE) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1001 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 1002 | if (thread == NULL) { |
| 1003 | return 0.0f; |
| 1004 | } |
| 1005 | volume = thread->streamVolume(stream); |
| 1006 | } else { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1007 | volume = streamVolume_l(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | return volume; |
| 1011 | } |
| 1012 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1013 | bool AudioFlinger::streamMute(audio_stream_type_t stream) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1014 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1015 | status_t status = checkStreamType(stream); |
| 1016 | if (status != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1017 | return true; |
| 1018 | } |
| 1019 | |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1020 | AutoMutex lock(mLock); |
| 1021 | return streamMute_l(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 1024 | |
| 1025 | void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs) |
| 1026 | { |
| 1027 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 1028 | mRecordThreads.valueAt(i)->setParameters(keyValuePairs); |
| 1029 | } |
| 1030 | } |
| 1031 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1032 | status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1033 | { |
Glenn Kasten | 827e5f1 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 1034 | ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d", |
| 1035 | ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1036 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1037 | // check calling permissions |
| 1038 | if (!settingsAllowed()) { |
| 1039 | return PERMISSION_DENIED; |
| 1040 | } |
| 1041 | |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 1042 | // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface |
| 1043 | if (ioHandle == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1044 | Mutex::Autolock _l(mLock); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1045 | status_t final_result = NO_ERROR; |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1046 | { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1047 | AutoMutex lock(mHardwareLock); |
| 1048 | mHardwareStatus = AUDIO_HW_SET_PARAMETER; |
| 1049 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 1050 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
| 1051 | status_t result = dev->set_parameters(dev, keyValuePairs.string()); |
| 1052 | final_result = result ?: final_result; |
| 1053 | } |
| 1054 | mHardwareStatus = AUDIO_HW_IDLE; |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1055 | } |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1056 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 1057 | AudioParameter param = AudioParameter(keyValuePairs); |
| 1058 | String8 value; |
| 1059 | if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) { |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 1060 | bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF); |
| 1061 | if (mBtNrecIsOff != btNrecIsOff) { |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1062 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 1063 | sp<RecordThread> thread = mRecordThreads.valueAt(i); |
Eric Laurent | f1c04f9 | 2012-08-28 14:26:53 -0700 | [diff] [blame] | 1064 | audio_devices_t device = thread->inDevice(); |
Glenn Kasten | 510a3d6 | 2012-07-16 14:24:34 -0700 | [diff] [blame] | 1065 | bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff; |
| 1066 | // collect all of the thread's session IDs |
| 1067 | KeyedVector<int, bool> ids = thread->sessionIds(); |
| 1068 | // suspend effects associated with those session IDs |
| 1069 | for (size_t j = 0; j < ids.size(); ++j) { |
| 1070 | int sessionId = ids.keyAt(j); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1071 | thread->setEffectSuspended(FX_IID_AEC, |
| 1072 | suspend, |
Glenn Kasten | 510a3d6 | 2012-07-16 14:24:34 -0700 | [diff] [blame] | 1073 | sessionId); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1074 | thread->setEffectSuspended(FX_IID_NS, |
| 1075 | suspend, |
Glenn Kasten | 510a3d6 | 2012-07-16 14:24:34 -0700 | [diff] [blame] | 1076 | sessionId); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1077 | } |
| 1078 | } |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 1079 | mBtNrecIsOff = btNrecIsOff; |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1080 | } |
| 1081 | } |
Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 1082 | String8 screenState; |
| 1083 | if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) { |
| 1084 | bool isOff = screenState == "off"; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1085 | if (isOff != (AudioFlinger::mScreenState & 1)) { |
| 1086 | AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff; |
Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 1087 | } |
| 1088 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1089 | return final_result; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | // hold a strong ref on thread in case closeOutput() or closeInput() is called |
| 1093 | // and the thread is exited once the lock is released |
| 1094 | sp<ThreadBase> thread; |
| 1095 | { |
| 1096 | Mutex::Autolock _l(mLock); |
| 1097 | thread = checkPlaybackThread_l(ioHandle); |
Glenn Kasten | d5903ec | 2012-03-18 10:33:27 -0700 | [diff] [blame] | 1098 | if (thread == 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1099 | thread = checkRecordThread_l(ioHandle); |
Glenn Kasten | 7fc9a6f | 2012-01-10 10:46:34 -0800 | [diff] [blame] | 1100 | } else if (thread == primaryPlaybackThread_l()) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1101 | // indicate output device change to all input threads for pre processing |
| 1102 | AudioParameter param = AudioParameter(keyValuePairs); |
| 1103 | int value; |
Eric Laurent | 89d94e7 | 2012-03-16 20:37:59 -0700 | [diff] [blame] | 1104 | if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) && |
| 1105 | (value != 0)) { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 1106 | broacastParametersToRecordThreads_l(keyValuePairs); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1107 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1108 | } |
| 1109 | } |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 1110 | if (thread != 0) { |
| 1111 | return thread->setParameters(keyValuePairs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1112 | } |
| 1113 | return BAD_VALUE; |
| 1114 | } |
| 1115 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1116 | String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1117 | { |
Glenn Kasten | 827e5f1 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 1118 | ALOGVV("getParameters() io %d, keys %s, calling pid %d", |
| 1119 | ioHandle, keys.string(), IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1120 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1121 | Mutex::Autolock _l(mLock); |
| 1122 | |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 1123 | if (ioHandle == AUDIO_IO_HANDLE_NONE) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1124 | String8 out_s8; |
| 1125 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1126 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1127 | char *s; |
| 1128 | { |
| 1129 | AutoMutex lock(mHardwareLock); |
| 1130 | mHardwareStatus = AUDIO_HW_GET_PARAMETER; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1131 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1132 | s = dev->get_parameters(dev, keys.string()); |
| 1133 | mHardwareStatus = AUDIO_HW_IDLE; |
| 1134 | } |
John Grossman | ef7740b | 2012-02-09 11:28:36 -0800 | [diff] [blame] | 1135 | out_s8 += String8(s ? s : ""); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1136 | free(s); |
| 1137 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1138 | return out_s8; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1141 | PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle); |
| 1142 | if (playbackThread != NULL) { |
| 1143 | return playbackThread->getParameters(keys); |
| 1144 | } |
| 1145 | RecordThread *recordThread = checkRecordThread_l(ioHandle); |
| 1146 | if (recordThread != NULL) { |
| 1147 | return recordThread->getParameters(keys); |
| 1148 | } |
| 1149 | return String8(""); |
| 1150 | } |
| 1151 | |
Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 1152 | size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format, |
| 1153 | audio_channel_mask_t channelMask) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1154 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1155 | status_t ret = initCheck(); |
| 1156 | if (ret != NO_ERROR) { |
| 1157 | return 0; |
| 1158 | } |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 1159 | if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) { |
| 1160 | return 0; |
| 1161 | } |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1162 | |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 1163 | AutoMutex lock(mHardwareLock); |
| 1164 | mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE; |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 1165 | audio_config_t config, proposed; |
| 1166 | memset(&proposed, 0, sizeof(proposed)); |
| 1167 | proposed.sample_rate = sampleRate; |
| 1168 | proposed.channel_mask = channelMask; |
| 1169 | proposed.format = format; |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1170 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1171 | audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice(); |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 1172 | size_t frames; |
| 1173 | for (;;) { |
| 1174 | // Note: config is currently a const parameter for get_input_buffer_size() |
| 1175 | // but we use a copy from proposed in case config changes from the call. |
| 1176 | config = proposed; |
| 1177 | frames = dev->get_input_buffer_size(dev, &config); |
| 1178 | if (frames != 0) { |
| 1179 | break; // hal success, config is the result |
| 1180 | } |
| 1181 | // change one parameter of the configuration each iteration to a more "common" value |
| 1182 | // to see if the device will support it. |
| 1183 | if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) { |
| 1184 | proposed.format = AUDIO_FORMAT_PCM_16_BIT; |
| 1185 | } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as |
| 1186 | proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw? |
| 1187 | } else { |
| 1188 | ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, " |
| 1189 | "format %#x, channelMask 0x%X", |
| 1190 | sampleRate, format, channelMask); |
| 1191 | break; // retries failed, break out of loop with frames == 0. |
| 1192 | } |
| 1193 | } |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 1194 | mHardwareStatus = AUDIO_HW_IDLE; |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 1195 | if (frames > 0 && config.sample_rate != sampleRate) { |
| 1196 | frames = destinationFramesPossible(frames, sampleRate, config.sample_rate); |
| 1197 | } |
| 1198 | return frames; // may be converted to bytes at the Java level. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 1201 | uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1202 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1203 | Mutex::Autolock _l(mLock); |
| 1204 | |
| 1205 | RecordThread *recordThread = checkRecordThread_l(ioHandle); |
| 1206 | if (recordThread != NULL) { |
| 1207 | return recordThread->getInputFramesLost(); |
| 1208 | } |
| 1209 | return 0; |
| 1210 | } |
| 1211 | |
| 1212 | status_t AudioFlinger::setVoiceVolume(float value) |
| 1213 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1214 | status_t ret = initCheck(); |
| 1215 | if (ret != NO_ERROR) { |
| 1216 | return ret; |
| 1217 | } |
| 1218 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1219 | // check calling permissions |
| 1220 | if (!settingsAllowed()) { |
| 1221 | return PERMISSION_DENIED; |
| 1222 | } |
| 1223 | |
| 1224 | AutoMutex lock(mHardwareLock); |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1225 | audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice(); |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1226 | mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME; |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1227 | ret = dev->set_voice_volume(dev, value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1228 | mHardwareStatus = AUDIO_HW_IDLE; |
| 1229 | |
| 1230 | return ret; |
| 1231 | } |
| 1232 | |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1233 | status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1234 | audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1235 | { |
| 1236 | status_t status; |
| 1237 | |
| 1238 | Mutex::Autolock _l(mLock); |
| 1239 | |
| 1240 | PlaybackThread *playbackThread = checkPlaybackThread_l(output); |
| 1241 | if (playbackThread != NULL) { |
| 1242 | return playbackThread->getRenderPosition(halFrames, dspFrames); |
| 1243 | } |
| 1244 | |
| 1245 | return BAD_VALUE; |
| 1246 | } |
| 1247 | |
| 1248 | void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client) |
| 1249 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1250 | Mutex::Autolock _l(mLock); |
Eric Laurent | 44622db | 2014-08-01 19:00:33 -0700 | [diff] [blame] | 1251 | if (client == 0) { |
| 1252 | return; |
| 1253 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1254 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1255 | { |
| 1256 | Mutex::Autolock _cl(mClientLock); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1257 | if (mNotificationClients.indexOfKey(pid) < 0) { |
| 1258 | sp<NotificationClient> notificationClient = new NotificationClient(this, |
| 1259 | client, |
| 1260 | pid); |
| 1261 | ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1262 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1263 | mNotificationClients.add(pid, notificationClient); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1264 | |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 1265 | sp<IBinder> binder = IInterface::asBinder(client); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1266 | binder->linkToDeath(notificationClient); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1267 | } |
| 1268 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1269 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1270 | // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the |
Eric Laurent | fe1a94e | 2014-05-26 16:03:08 -0700 | [diff] [blame] | 1271 | // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock. |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1272 | // the config change is always sent from playback or record threads to avoid deadlock |
| 1273 | // with AudioSystem::gLock |
| 1274 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 1275 | mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED, pid); |
| 1276 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1277 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1278 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 1279 | mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED, pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | void AudioFlinger::removeNotificationClient(pid_t pid) |
| 1284 | { |
| 1285 | Mutex::Autolock _l(mLock); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1286 | { |
| 1287 | Mutex::Autolock _cl(mClientLock); |
| 1288 | mNotificationClients.removeItem(pid); |
| 1289 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1290 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1291 | ALOGV("%d died, releasing its sessions", pid); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1292 | size_t num = mAudioSessionRefs.size(); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1293 | bool removed = false; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1294 | for (size_t i = 0; i< num; ) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1295 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 1296 | ALOGV(" pid %d @ %d", ref->mPid, i); |
| 1297 | if (ref->mPid == pid) { |
| 1298 | ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1299 | mAudioSessionRefs.removeAt(i); |
| 1300 | delete ref; |
| 1301 | removed = true; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1302 | num--; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1303 | } else { |
| 1304 | i++; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1305 | } |
| 1306 | } |
| 1307 | if (removed) { |
| 1308 | purgeStaleEffects_l(); |
| 1309 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1310 | } |
| 1311 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 1312 | void AudioFlinger::ioConfigChanged(audio_io_config_event event, |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1313 | const sp<AudioIoDescriptor>& ioDesc, |
| 1314 | pid_t pid) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1315 | { |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1316 | Mutex::Autolock _l(mClientLock); |
| 1317 | size_t size = mNotificationClients.size(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1318 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1319 | if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) { |
| 1320 | mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc); |
| 1321 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1322 | } |
| 1323 | } |
| 1324 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1325 | // removeClient_l() must be called with AudioFlinger::mClientLock held |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1326 | void AudioFlinger::removeClient_l(pid_t pid) |
| 1327 | { |
Glenn Kasten | 827e5f1 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 1328 | ALOGV("removeClient_l() pid %d, calling pid %d", pid, |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 1329 | IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1330 | mClients.removeItem(pid); |
| 1331 | } |
| 1332 | |
Eric Laurent | 717e128 | 2012-06-29 16:36:52 -0700 | [diff] [blame] | 1333 | // getEffectThread_l() must be called with AudioFlinger::mLock held |
| 1334 | sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId) |
| 1335 | { |
| 1336 | sp<PlaybackThread> thread; |
| 1337 | |
| 1338 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 1339 | if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) { |
| 1340 | ALOG_ASSERT(thread == 0); |
| 1341 | thread = mPlaybackThreads.valueAt(i); |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | return thread; |
| 1346 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1347 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1348 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1349 | |
| 1350 | // ---------------------------------------------------------------------------- |
| 1351 | |
| 1352 | AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid) |
| 1353 | : RefBase(), |
| 1354 | mAudioFlinger(audioFlinger), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1355 | mPid(pid), |
| 1356 | mTimedTrackCount(0) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1357 | { |
Eric Laurent | aba407f | 2015-08-20 16:18:53 -0700 | [diff] [blame^] | 1358 | size_t heapSize = kClientSharedHeapSizeBytes; |
| 1359 | // Increase heap size on non low ram devices to limit risk of reconnection failure for |
| 1360 | // invalidated tracks |
| 1361 | if (!audioFlinger->isLowRamDevice()) { |
| 1362 | heapSize *= kClientSharedHeapSizeMultiplier; |
| 1363 | } |
| 1364 | mMemoryDealer = new MemoryDealer(heapSize, "AudioFlinger::Client"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1367 | // Client destructor must be called with AudioFlinger::mClientLock held |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1368 | AudioFlinger::Client::~Client() |
| 1369 | { |
| 1370 | mAudioFlinger->removeClient_l(mPid); |
| 1371 | } |
| 1372 | |
Glenn Kasten | 435dbe6 | 2012-01-30 10:15:48 -0800 | [diff] [blame] | 1373 | sp<MemoryDealer> AudioFlinger::Client::heap() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1374 | { |
| 1375 | return mMemoryDealer; |
| 1376 | } |
| 1377 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1378 | // Reserve one of the limited slots for a timed audio track associated |
| 1379 | // with this client |
| 1380 | bool AudioFlinger::Client::reserveTimedTrack() |
| 1381 | { |
| 1382 | const int kMaxTimedTracksPerClient = 4; |
| 1383 | |
| 1384 | Mutex::Autolock _l(mTimedTrackLock); |
| 1385 | |
| 1386 | if (mTimedTrackCount >= kMaxTimedTracksPerClient) { |
| 1387 | ALOGW("can not create timed track - pid %d has exceeded the limit", |
| 1388 | mPid); |
| 1389 | return false; |
| 1390 | } |
| 1391 | |
| 1392 | mTimedTrackCount++; |
| 1393 | return true; |
| 1394 | } |
| 1395 | |
| 1396 | // Release a slot for a timed audio track |
| 1397 | void AudioFlinger::Client::releaseTimedTrack() |
| 1398 | { |
| 1399 | Mutex::Autolock _l(mTimedTrackLock); |
| 1400 | mTimedTrackCount--; |
| 1401 | } |
| 1402 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1403 | // ---------------------------------------------------------------------------- |
| 1404 | |
| 1405 | AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger, |
| 1406 | const sp<IAudioFlingerClient>& client, |
| 1407 | pid_t pid) |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 1408 | : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1409 | { |
| 1410 | } |
| 1411 | |
| 1412 | AudioFlinger::NotificationClient::~NotificationClient() |
| 1413 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1414 | } |
| 1415 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1416 | void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1417 | { |
| 1418 | sp<NotificationClient> keep(this); |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 1419 | mAudioFlinger->removeNotificationClient(mPid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1420 | } |
| 1421 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1422 | |
| 1423 | // ---------------------------------------------------------------------------- |
| 1424 | |
Jeff Brown | 893a564 | 2013-08-16 20:19:26 -0700 | [diff] [blame] | 1425 | static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) { |
| 1426 | return audio_is_remote_submix_device(inDevice); |
| 1427 | } |
| 1428 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1429 | sp<IAudioRecord> AudioFlinger::openRecord( |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1430 | audio_io_handle_t input, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1431 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 1432 | audio_format_t format, |
Glenn Kasten | 254af18 | 2012-07-03 14:59:05 -0700 | [diff] [blame] | 1433 | audio_channel_mask_t channelMask, |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 1434 | const String16& opPackageName, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1435 | size_t *frameCount, |
Glenn Kasten | eeca326 | 2013-07-31 16:12:48 -0700 | [diff] [blame] | 1436 | IAudioFlinger::track_flags_t *flags, |
Glenn Kasten | 1879fff | 2012-07-11 15:36:59 -0700 | [diff] [blame] | 1437 | pid_t tid, |
Jean-Michel Trivi | 4cb6683 | 2015-05-01 18:34:17 -0700 | [diff] [blame] | 1438 | int clientUid, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1439 | int *sessionId, |
Glenn Kasten | 7df8c0b | 2014-07-03 12:23:29 -0700 | [diff] [blame] | 1440 | size_t *notificationFrames, |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 1441 | sp<IMemory>& cblk, |
| 1442 | sp<IMemory>& buffers, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1443 | status_t *status) |
| 1444 | { |
| 1445 | sp<RecordThread::RecordTrack> recordTrack; |
| 1446 | sp<RecordHandle> recordHandle; |
| 1447 | sp<Client> client; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1448 | status_t lStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1449 | int lSessionId; |
| 1450 | |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 1451 | cblk.clear(); |
| 1452 | buffers.clear(); |
| 1453 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1454 | // check calling permissions |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 1455 | if (!recordingAllowed(opPackageName)) { |
Glenn Kasten | e93cf2c | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 1456 | ALOGE("openRecord() permission denied: recording not allowed"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1457 | lStatus = PERMISSION_DENIED; |
| 1458 | goto Exit; |
| 1459 | } |
| 1460 | |
Glenn Kasten | 53b5d09 | 2014-02-05 10:00:23 -0800 | [diff] [blame] | 1461 | // further sample rate checks are performed by createRecordTrack_l() |
| 1462 | if (sampleRate == 0) { |
| 1463 | ALOGE("openRecord() invalid sample rate %u", sampleRate); |
| 1464 | lStatus = BAD_VALUE; |
| 1465 | goto Exit; |
| 1466 | } |
| 1467 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 1468 | // we don't yet support anything other than linear PCM |
| 1469 | if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1470 | ALOGE("openRecord() invalid format %#x", format); |
Glenn Kasten | 291bb6d | 2013-07-16 17:23:39 -0700 | [diff] [blame] | 1471 | lStatus = BAD_VALUE; |
| 1472 | goto Exit; |
| 1473 | } |
| 1474 | |
Glenn Kasten | 53b5d09 | 2014-02-05 10:00:23 -0800 | [diff] [blame] | 1475 | // further channel mask checks are performed by createRecordTrack_l() |
| 1476 | if (!audio_is_input_channel(channelMask)) { |
| 1477 | ALOGE("openRecord() invalid channel mask %#x", channelMask); |
| 1478 | lStatus = BAD_VALUE; |
| 1479 | goto Exit; |
| 1480 | } |
| 1481 | |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 1482 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1483 | Mutex::Autolock _l(mLock); |
Glenn Kasten | e848bd9 | 2014-03-13 15:00:32 -0700 | [diff] [blame] | 1484 | RecordThread *thread = checkRecordThread_l(input); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1485 | if (thread == NULL) { |
Glenn Kasten | e93cf2c | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 1486 | ALOGE("openRecord() checkRecordThread_l failed"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1487 | lStatus = BAD_VALUE; |
| 1488 | goto Exit; |
| 1489 | } |
| 1490 | |
Glenn Kasten | 8d6cc84 | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 1491 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1492 | client = registerPid(pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1493 | |
Glenn Kasten | aea7ea0 | 2013-06-26 09:25:47 -0700 | [diff] [blame] | 1494 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1495 | lSessionId = *sessionId; |
| 1496 | } else { |
Glenn Kasten | 570f633 | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 1497 | // if no audio session id is provided, create one here |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1498 | lSessionId = nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1499 | if (sessionId != NULL) { |
| 1500 | *sessionId = lSessionId; |
| 1501 | } |
| 1502 | } |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 1503 | ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input); |
Glenn Kasten | 570f633 | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 1504 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1505 | // TODO: the uid should be passed in as a parameter to openRecord |
Glenn Kasten | 1879fff | 2012-07-11 15:36:59 -0700 | [diff] [blame] | 1506 | recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask, |
Glenn Kasten | 7df8c0b | 2014-07-03 12:23:29 -0700 | [diff] [blame] | 1507 | frameCount, lSessionId, notificationFrames, |
Jean-Michel Trivi | 4cb6683 | 2015-05-01 18:34:17 -0700 | [diff] [blame] | 1508 | clientUid, flags, tid, &lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 1509 | LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0)); |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 1510 | |
| 1511 | if (lStatus == NO_ERROR) { |
| 1512 | // Check if one effect chain was awaiting for an AudioRecord to be created on this |
| 1513 | // session and move it to this thread. |
| 1514 | sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)lSessionId); |
| 1515 | if (chain != 0) { |
| 1516 | Mutex::Autolock _l(thread->mLock); |
| 1517 | thread->addEffectChain_l(chain); |
| 1518 | } |
| 1519 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1520 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 1521 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1522 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 1523 | // remove local strong reference to Client before deleting the RecordTrack so that the |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1524 | // Client destructor is called by the TrackBase destructor with mClientLock held |
Eric Laurent | fe1a94e | 2014-05-26 16:03:08 -0700 | [diff] [blame] | 1525 | // Don't hold mClientLock when releasing the reference on the track as the |
| 1526 | // destructor will acquire it. |
| 1527 | { |
| 1528 | Mutex::Autolock _cl(mClientLock); |
| 1529 | client.clear(); |
| 1530 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1531 | recordTrack.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1532 | goto Exit; |
| 1533 | } |
| 1534 | |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 1535 | cblk = recordTrack->getCblk(); |
| 1536 | buffers = recordTrack->getBuffers(); |
| 1537 | |
Glenn Kasten | 2fc1473 | 2013-08-05 14:58:14 -0700 | [diff] [blame] | 1538 | // return handle to client |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1539 | recordHandle = new RecordHandle(recordTrack); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1540 | |
| 1541 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1542 | *status = lStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1543 | return recordHandle; |
| 1544 | } |
| 1545 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1546 | |
| 1547 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1548 | // ---------------------------------------------------------------------------- |
| 1549 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1550 | audio_module_handle_t AudioFlinger::loadHwModule(const char *name) |
| 1551 | { |
Eric Laurent | 44622db | 2014-08-01 19:00:33 -0700 | [diff] [blame] | 1552 | if (name == NULL) { |
| 1553 | return 0; |
| 1554 | } |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1555 | if (!settingsAllowed()) { |
| 1556 | return 0; |
| 1557 | } |
| 1558 | Mutex::Autolock _l(mLock); |
| 1559 | return loadHwModule_l(name); |
| 1560 | } |
| 1561 | |
| 1562 | // loadHwModule_l() must be called with AudioFlinger::mLock held |
| 1563 | audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name) |
| 1564 | { |
| 1565 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 1566 | if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) { |
| 1567 | ALOGW("loadHwModule() module %s already loaded", name); |
| 1568 | return mAudioHwDevs.keyAt(i); |
| 1569 | } |
| 1570 | } |
| 1571 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1572 | audio_hw_device_t *dev; |
| 1573 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 1574 | int rc = load_audio_interface(name, &dev); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1575 | if (rc) { |
| 1576 | ALOGI("loadHwModule() error %d loading module %s ", rc, name); |
| 1577 | return 0; |
| 1578 | } |
| 1579 | |
| 1580 | mHardwareStatus = AUDIO_HW_INIT; |
| 1581 | rc = dev->init_check(dev); |
| 1582 | mHardwareStatus = AUDIO_HW_IDLE; |
| 1583 | if (rc) { |
| 1584 | ALOGI("loadHwModule() init check error %d for module %s ", rc, name); |
| 1585 | return 0; |
| 1586 | } |
| 1587 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1588 | // Check and cache this HAL's level of support for master mute and master |
| 1589 | // volume. If this is the first HAL opened, and it supports the get |
| 1590 | // methods, use the initial values provided by the HAL as the current |
| 1591 | // master mute and volume settings. |
| 1592 | |
| 1593 | AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0); |
| 1594 | { // scope for auto-lock pattern |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1595 | AutoMutex lock(mHardwareLock); |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1596 | |
| 1597 | if (0 == mAudioHwDevs.size()) { |
| 1598 | mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME; |
| 1599 | if (NULL != dev->get_master_volume) { |
| 1600 | float mv; |
| 1601 | if (OK == dev->get_master_volume(dev, &mv)) { |
| 1602 | mMasterVolume = mv; |
| 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE; |
| 1607 | if (NULL != dev->get_master_mute) { |
| 1608 | bool mm; |
| 1609 | if (OK == dev->get_master_mute(dev, &mm)) { |
| 1610 | mMasterMute = mm; |
| 1611 | } |
| 1612 | } |
| 1613 | } |
| 1614 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1615 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1616 | if ((NULL != dev->set_master_volume) && |
| 1617 | (OK == dev->set_master_volume(dev, mMasterVolume))) { |
| 1618 | flags = static_cast<AudioHwDevice::Flags>(flags | |
| 1619 | AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME); |
| 1620 | } |
| 1621 | |
| 1622 | mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE; |
| 1623 | if ((NULL != dev->set_master_mute) && |
| 1624 | (OK == dev->set_master_mute(dev, mMasterMute))) { |
| 1625 | flags = static_cast<AudioHwDevice::Flags>(flags | |
| 1626 | AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE); |
| 1627 | } |
| 1628 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1629 | mHardwareStatus = AUDIO_HW_IDLE; |
| 1630 | } |
| 1631 | |
| 1632 | audio_module_handle_t handle = nextUniqueId(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1633 | mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags)); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1634 | |
| 1635 | ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d", |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 1636 | name, dev->common.module->name, dev->common.module->id, handle); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1637 | |
| 1638 | return handle; |
| 1639 | |
| 1640 | } |
| 1641 | |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 1642 | // ---------------------------------------------------------------------------- |
| 1643 | |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 1644 | uint32_t AudioFlinger::getPrimaryOutputSamplingRate() |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 1645 | { |
| 1646 | Mutex::Autolock _l(mLock); |
| 1647 | PlaybackThread *thread = primaryPlaybackThread_l(); |
| 1648 | return thread != NULL ? thread->sampleRate() : 0; |
| 1649 | } |
| 1650 | |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 1651 | size_t AudioFlinger::getPrimaryOutputFrameCount() |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 1652 | { |
| 1653 | Mutex::Autolock _l(mLock); |
| 1654 | PlaybackThread *thread = primaryPlaybackThread_l(); |
| 1655 | return thread != NULL ? thread->frameCountHAL() : 0; |
| 1656 | } |
| 1657 | |
| 1658 | // ---------------------------------------------------------------------------- |
| 1659 | |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 1660 | status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice) |
| 1661 | { |
| 1662 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 1663 | if (uid != AID_SYSTEM) { |
| 1664 | return PERMISSION_DENIED; |
| 1665 | } |
| 1666 | Mutex::Autolock _l(mLock); |
| 1667 | if (mIsDeviceTypeKnown) { |
| 1668 | return INVALID_OPERATION; |
| 1669 | } |
| 1670 | mIsLowRamDevice = isLowRamDevice; |
| 1671 | mIsDeviceTypeKnown = true; |
| 1672 | return NO_ERROR; |
| 1673 | } |
| 1674 | |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1675 | audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId) |
| 1676 | { |
| 1677 | Mutex::Autolock _l(mLock); |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 1678 | |
| 1679 | ssize_t index = mHwAvSyncIds.indexOfKey(sessionId); |
| 1680 | if (index >= 0) { |
| 1681 | ALOGV("getAudioHwSyncForSession found ID %d for session %d", |
| 1682 | mHwAvSyncIds.valueAt(index), sessionId); |
| 1683 | return mHwAvSyncIds.valueAt(index); |
| 1684 | } |
| 1685 | |
| 1686 | audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice(); |
| 1687 | if (dev == NULL) { |
| 1688 | return AUDIO_HW_SYNC_INVALID; |
| 1689 | } |
| 1690 | char *reply = dev->get_parameters(dev, AUDIO_PARAMETER_HW_AV_SYNC); |
| 1691 | AudioParameter param = AudioParameter(String8(reply)); |
| 1692 | free(reply); |
| 1693 | |
| 1694 | int value; |
| 1695 | if (param.getInt(String8(AUDIO_PARAMETER_HW_AV_SYNC), value) != NO_ERROR) { |
| 1696 | ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId); |
| 1697 | return AUDIO_HW_SYNC_INVALID; |
| 1698 | } |
| 1699 | |
| 1700 | // allow only one session for a given HW A/V sync ID. |
| 1701 | for (size_t i = 0; i < mHwAvSyncIds.size(); i++) { |
| 1702 | if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) { |
| 1703 | ALOGV("getAudioHwSyncForSession removing ID %d for session %d", |
| 1704 | value, mHwAvSyncIds.keyAt(i)); |
| 1705 | mHwAvSyncIds.removeItemsAt(i); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1706 | break; |
| 1707 | } |
| 1708 | } |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 1709 | |
| 1710 | mHwAvSyncIds.add(sessionId, value); |
| 1711 | |
| 1712 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 1713 | sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i); |
| 1714 | uint32_t sessions = thread->hasAudioSession(sessionId); |
| 1715 | if (sessions & PlaybackThread::TRACK_SESSION) { |
| 1716 | AudioParameter param = AudioParameter(); |
| 1717 | param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value); |
| 1718 | thread->setParameters(param.toString()); |
| 1719 | break; |
| 1720 | } |
| 1721 | } |
| 1722 | |
| 1723 | ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId); |
| 1724 | return (audio_hw_sync_t)value; |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1725 | } |
| 1726 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1727 | status_t AudioFlinger::systemReady() |
| 1728 | { |
| 1729 | Mutex::Autolock _l(mLock); |
| 1730 | ALOGI("%s", __FUNCTION__); |
| 1731 | if (mSystemReady) { |
| 1732 | ALOGW("%s called twice", __FUNCTION__); |
| 1733 | return NO_ERROR; |
| 1734 | } |
| 1735 | mSystemReady = true; |
| 1736 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 1737 | ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get(); |
| 1738 | thread->systemReady(); |
| 1739 | } |
| 1740 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 1741 | ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get(); |
| 1742 | thread->systemReady(); |
| 1743 | } |
| 1744 | return NO_ERROR; |
| 1745 | } |
| 1746 | |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 1747 | // setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held |
| 1748 | void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId) |
| 1749 | { |
| 1750 | ssize_t index = mHwAvSyncIds.indexOfKey(sessionId); |
| 1751 | if (index >= 0) { |
| 1752 | audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index); |
| 1753 | ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId); |
| 1754 | AudioParameter param = AudioParameter(); |
| 1755 | param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), syncId); |
| 1756 | thread->setParameters(param.toString()); |
| 1757 | } |
| 1758 | } |
| 1759 | |
| 1760 | |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 1761 | // ---------------------------------------------------------------------------- |
| 1762 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1763 | |
| 1764 | sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1765 | audio_io_handle_t *output, |
| 1766 | audio_config_t *config, |
| 1767 | audio_devices_t devices, |
| 1768 | const String8& address, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1769 | audio_output_flags_t flags) |
| 1770 | { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1771 | AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1772 | if (outHwDev == NULL) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1773 | return 0; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | audio_hw_device_t *hwDevHal = outHwDev->hwDevice(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1777 | if (*output == AUDIO_IO_HANDLE_NONE) { |
| 1778 | *output = nextUniqueId(); |
| 1779 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1780 | |
| 1781 | mHardwareStatus = AUDIO_HW_OUTPUT_OPEN; |
| 1782 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1783 | // FOR TESTING ONLY: |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1784 | // This if statement allows overriding the audio policy settings |
| 1785 | // and forcing a specific format or channel mask to the HAL/Sink device for testing. |
| 1786 | if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) { |
| 1787 | // Check only for Normal Mixing mode |
| 1788 | if (kEnableExtendedPrecision) { |
| 1789 | // Specify format (uncomment one below to choose) |
| 1790 | //config->format = AUDIO_FORMAT_PCM_FLOAT; |
| 1791 | //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED; |
| 1792 | //config->format = AUDIO_FORMAT_PCM_32_BIT; |
| 1793 | //config->format = AUDIO_FORMAT_PCM_8_24_BIT; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1794 | // ALOGV("openOutput_l() upgrading format to %#08x", config->format); |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1795 | } |
| 1796 | if (kEnableExtendedChannels) { |
| 1797 | // Specify channel mask (uncomment one below to choose) |
| 1798 | //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch |
| 1799 | //config->channel_mask = audio_channel_mask_from_representation_and_bits( |
| 1800 | // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example |
| 1801 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1802 | } |
| 1803 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 1804 | AudioStreamOut *outputStream = NULL; |
| 1805 | status_t status = outHwDev->openOutputStream( |
| 1806 | &outputStream, |
| 1807 | *output, |
| 1808 | devices, |
| 1809 | flags, |
| 1810 | config, |
| 1811 | address.string()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1812 | |
| 1813 | mHardwareStatus = AUDIO_HW_IDLE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1814 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 1815 | if (status == NO_ERROR) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1816 | |
| 1817 | PlaybackThread *thread; |
| 1818 | if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1819 | thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1820 | ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1821 | } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) |
| 1822 | || !isValidPcmSinkFormat(config->format) |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1823 | || !isValidPcmSinkChannelMask(config->channel_mask)) { |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1824 | thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1825 | ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1826 | } else { |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1827 | thread = new MixerThread(this, outputStream, *output, devices, mSystemReady); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1828 | ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1829 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1830 | mPlaybackThreads.add(*output, thread); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1831 | return thread; |
| 1832 | } |
| 1833 | |
| 1834 | return 0; |
| 1835 | } |
| 1836 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1837 | status_t AudioFlinger::openOutput(audio_module_handle_t module, |
| 1838 | audio_io_handle_t *output, |
| 1839 | audio_config_t *config, |
| 1840 | audio_devices_t *devices, |
| 1841 | const String8& address, |
| 1842 | uint32_t *latencyMs, |
| 1843 | audio_output_flags_t flags) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1844 | { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 1845 | ALOGI("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x", |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1846 | module, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1847 | (devices != NULL) ? *devices : 0, |
| 1848 | config->sample_rate, |
| 1849 | config->format, |
| 1850 | config->channel_mask, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1851 | flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1852 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1853 | if (*devices == AUDIO_DEVICE_NONE) { |
| 1854 | return BAD_VALUE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1855 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1856 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1857 | Mutex::Autolock _l(mLock); |
| 1858 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1859 | sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1860 | if (thread != 0) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1861 | *latencyMs = thread->latency(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1862 | |
| 1863 | // notify client processes of the new output creation |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 1864 | thread->ioConfigChanged(AUDIO_OUTPUT_OPENED); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1865 | |
| 1866 | // the first primary output opened designates the primary hw device |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 1867 | if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1868 | ALOGI("Using module %d has the primary audio interface", module); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1869 | mPrimaryHardwareDev = thread->getOutput()->audioHwDev; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1870 | |
| 1871 | AutoMutex lock(mHardwareLock); |
| 1872 | mHardwareStatus = AUDIO_HW_SET_MODE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1873 | mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1874 | mHardwareStatus = AUDIO_HW_IDLE; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1875 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1876 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1877 | } |
| 1878 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1879 | return NO_INIT; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1880 | } |
| 1881 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1882 | audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1, |
| 1883 | audio_io_handle_t output2) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1884 | { |
| 1885 | Mutex::Autolock _l(mLock); |
| 1886 | MixerThread *thread1 = checkMixerThread_l(output1); |
| 1887 | MixerThread *thread2 = checkMixerThread_l(output2); |
| 1888 | |
| 1889 | if (thread1 == NULL || thread2 == NULL) { |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 1890 | ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1, |
| 1891 | output2); |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 1892 | return AUDIO_IO_HANDLE_NONE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1893 | } |
| 1894 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1895 | audio_io_handle_t id = nextUniqueId(); |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1896 | DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1897 | thread->addOutputTrack(thread2); |
| 1898 | mPlaybackThreads.add(id, thread); |
| 1899 | // notify client processes of the new output creation |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 1900 | thread->ioConfigChanged(AUDIO_OUTPUT_OPENED); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1901 | return id; |
| 1902 | } |
| 1903 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1904 | status_t AudioFlinger::closeOutput(audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1905 | { |
Glenn Kasten | d96c572 | 2012-04-25 13:44:49 -0700 | [diff] [blame] | 1906 | return closeOutput_nonvirtual(output); |
| 1907 | } |
| 1908 | |
| 1909 | status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output) |
| 1910 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1911 | // keep strong reference on the playback thread so that |
| 1912 | // it is not destroyed while exit() is executed |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1913 | sp<PlaybackThread> thread; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1914 | { |
| 1915 | Mutex::Autolock _l(mLock); |
| 1916 | thread = checkPlaybackThread_l(output); |
| 1917 | if (thread == NULL) { |
| 1918 | return BAD_VALUE; |
| 1919 | } |
| 1920 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1921 | ALOGV("closeOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1922 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1923 | if (thread->type() == ThreadBase::MIXER) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1924 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 1925 | if (mPlaybackThreads.valueAt(i)->isDuplicating()) { |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 1926 | DuplicatingThread *dupThread = |
| 1927 | (DuplicatingThread *)mPlaybackThreads.valueAt(i).get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1928 | dupThread->removeOutputTrack((MixerThread *)thread.get()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1929 | } |
| 1930 | } |
| 1931 | } |
| 1932 | |
| 1933 | |
| 1934 | mPlaybackThreads.removeItem(output); |
| 1935 | // save all effects to the default thread |
| 1936 | if (mPlaybackThreads.size()) { |
| 1937 | PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0)); |
| 1938 | if (dstThread != NULL) { |
| 1939 | // audioflinger lock is held here so the acquisition order of thread locks does not |
| 1940 | // matter |
| 1941 | Mutex::Autolock _dl(dstThread->mLock); |
| 1942 | Mutex::Autolock _sl(thread->mLock); |
| 1943 | Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l(); |
| 1944 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 1945 | moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1946 | } |
| 1947 | } |
| 1948 | } |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 1949 | const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor(); |
| 1950 | ioDesc->mIoHandle = output; |
| 1951 | ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1952 | } |
| 1953 | thread->exit(); |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 1954 | // The thread entity (active unit of execution) is no longer running here, |
| 1955 | // but the ThreadBase container still exists. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1956 | |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 1957 | if (!thread->isDuplicating()) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1958 | closeOutputFinish(thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1959 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1960 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1961 | return NO_ERROR; |
| 1962 | } |
| 1963 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1964 | void AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread) |
| 1965 | { |
| 1966 | AudioStreamOut *out = thread->clearOutput(); |
| 1967 | ALOG_ASSERT(out != NULL, "out shouldn't be NULL"); |
| 1968 | // from now on thread->mOutput is NULL |
| 1969 | out->hwDev()->close_output_stream(out->hwDev(), out->stream); |
| 1970 | delete out; |
| 1971 | } |
| 1972 | |
| 1973 | void AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread) |
| 1974 | { |
| 1975 | mPlaybackThreads.removeItem(thread->mId); |
| 1976 | thread->exit(); |
| 1977 | closeOutputFinish(thread); |
| 1978 | } |
| 1979 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1980 | status_t AudioFlinger::suspendOutput(audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1981 | { |
| 1982 | Mutex::Autolock _l(mLock); |
| 1983 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 1984 | |
| 1985 | if (thread == NULL) { |
| 1986 | return BAD_VALUE; |
| 1987 | } |
| 1988 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1989 | ALOGV("suspendOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1990 | thread->suspend(); |
| 1991 | |
| 1992 | return NO_ERROR; |
| 1993 | } |
| 1994 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1995 | status_t AudioFlinger::restoreOutput(audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1996 | { |
| 1997 | Mutex::Autolock _l(mLock); |
| 1998 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 1999 | |
| 2000 | if (thread == NULL) { |
| 2001 | return BAD_VALUE; |
| 2002 | } |
| 2003 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2004 | ALOGV("restoreOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2005 | |
| 2006 | thread->restore(); |
| 2007 | |
| 2008 | return NO_ERROR; |
| 2009 | } |
| 2010 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2011 | status_t AudioFlinger::openInput(audio_module_handle_t module, |
| 2012 | audio_io_handle_t *input, |
| 2013 | audio_config_t *config, |
Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2014 | audio_devices_t *devices, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2015 | const String8& address, |
| 2016 | audio_source_t source, |
Glenn Kasten | ec40d28 | 2014-07-15 15:31:26 -0700 | [diff] [blame] | 2017 | audio_input_flags_t flags) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2018 | { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2019 | Mutex::Autolock _l(mLock); |
| 2020 | |
Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2021 | if (*devices == AUDIO_DEVICE_NONE) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2022 | return BAD_VALUE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2023 | } |
| 2024 | |
Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2025 | sp<RecordThread> thread = openInput_l(module, input, config, *devices, address, source, flags); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2026 | |
| 2027 | if (thread != 0) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2028 | // notify client processes of the new input creation |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2029 | thread->ioConfigChanged(AUDIO_INPUT_OPENED); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2030 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2031 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2032 | return NO_INIT; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2033 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2034 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2035 | sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2036 | audio_io_handle_t *input, |
| 2037 | audio_config_t *config, |
Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2038 | audio_devices_t devices, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2039 | const String8& address, |
| 2040 | audio_source_t source, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2041 | audio_input_flags_t flags) |
| 2042 | { |
Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2043 | AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices); |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 2044 | if (inHwDev == NULL) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2045 | *input = AUDIO_IO_HANDLE_NONE; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2046 | return 0; |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 2047 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2048 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2049 | if (*input == AUDIO_IO_HANDLE_NONE) { |
| 2050 | *input = nextUniqueId(); |
| 2051 | } |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 2052 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2053 | audio_config_t halconfig = *config; |
| 2054 | audio_hw_device_t *inHwHal = inHwDev->hwDevice(); |
Glenn Kasten | 3255095 | 2013-08-06 10:45:10 -0700 | [diff] [blame] | 2055 | audio_stream_in_t *inStream = NULL; |
Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2056 | status_t status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2057 | &inStream, flags, address.string(), source); |
| 2058 | ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d" |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 2059 | ", Format %#x, Channels %x, flags %#x, status %d addr %s", |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2060 | inStream, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2061 | halconfig.sample_rate, |
| 2062 | halconfig.format, |
| 2063 | halconfig.channel_mask, |
Glenn Kasten | ec40d28 | 2014-07-15 15:31:26 -0700 | [diff] [blame] | 2064 | flags, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 2065 | status, address.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2066 | |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 2067 | // If the input could not be opened with the requested parameters and we can handle the |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 2068 | // conversion internally, try to open again with the proposed parameters. |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 2069 | if (status == BAD_VALUE && |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 2070 | audio_is_linear_pcm(config->format) && |
| 2071 | audio_is_linear_pcm(halconfig.format) && |
| 2072 | (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) && |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2073 | (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_2) && |
| 2074 | (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_2)) { |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 2075 | // FIXME describe the change proposed by HAL (save old values so we can log them here) |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2076 | ALOGV("openInput_l() reopening with proposed sampling rate and channel mask"); |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 2077 | inStream = NULL; |
Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2078 | status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2079 | &inStream, flags, address.string(), source); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 2080 | // FIXME log this new status; HAL should not propose any further changes |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2081 | } |
| 2082 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 2083 | if (status == NO_ERROR && inStream != NULL) { |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 2084 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 2085 | #ifdef TEE_SINK |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 2086 | // Try to re-use most recently used Pipe to archive a copy of input for dumpsys, |
| 2087 | // or (re-)create if current Pipe is idle and does not match the new format |
| 2088 | sp<NBAIO_Sink> teeSink; |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 2089 | enum { |
| 2090 | TEE_SINK_NO, // don't copy input |
| 2091 | TEE_SINK_NEW, // copy input using a new pipe |
| 2092 | TEE_SINK_OLD, // copy input using an existing pipe |
| 2093 | } kind; |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 2094 | NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate, |
| 2095 | audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2096 | if (!mTeeSinkInputEnabled) { |
| 2097 | kind = TEE_SINK_NO; |
Glenn Kasten | 6e0d67d | 2014-01-31 09:41:08 -0800 | [diff] [blame] | 2098 | } else if (!Format_isValid(format)) { |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 2099 | kind = TEE_SINK_NO; |
| 2100 | } else if (mRecordTeeSink == 0) { |
| 2101 | kind = TEE_SINK_NEW; |
| 2102 | } else if (mRecordTeeSink->getStrongCount() != 1) { |
| 2103 | kind = TEE_SINK_NO; |
Glenn Kasten | f66b422 | 2014-02-20 10:23:28 -0800 | [diff] [blame] | 2104 | } else if (Format_isEqual(format, mRecordTeeSink->format())) { |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 2105 | kind = TEE_SINK_OLD; |
| 2106 | } else { |
| 2107 | kind = TEE_SINK_NEW; |
| 2108 | } |
| 2109 | switch (kind) { |
| 2110 | case TEE_SINK_NEW: { |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2111 | Pipe *pipe = new Pipe(mTeeSinkInputFrames, format); |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 2112 | size_t numCounterOffers = 0; |
| 2113 | const NBAIO_Format offers[1] = {format}; |
| 2114 | ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); |
| 2115 | ALOG_ASSERT(index == 0); |
| 2116 | PipeReader *pipeReader = new PipeReader(*pipe); |
| 2117 | numCounterOffers = 0; |
| 2118 | index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); |
| 2119 | ALOG_ASSERT(index == 0); |
| 2120 | mRecordTeeSink = pipe; |
| 2121 | mRecordTeeSource = pipeReader; |
| 2122 | teeSink = pipe; |
| 2123 | } |
| 2124 | break; |
| 2125 | case TEE_SINK_OLD: |
| 2126 | teeSink = mRecordTeeSink; |
| 2127 | break; |
| 2128 | case TEE_SINK_NO: |
| 2129 | default: |
| 2130 | break; |
| 2131 | } |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 2132 | #endif |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2133 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2134 | AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2135 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2136 | // Start record thread |
Glenn Kasten | 34af026 | 2013-07-30 11:52:39 -0700 | [diff] [blame] | 2137 | // RecordThread requires both input and output device indication to forward to audio |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2138 | // pre processing modules |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2139 | sp<RecordThread> thread = new RecordThread(this, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2140 | inputStream, |
| 2141 | *input, |
Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 2142 | primaryOutputDevice_l(), |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 2143 | devices, |
| 2144 | mSystemReady |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 2145 | #ifdef TEE_SINK |
| 2146 | , teeSink |
| 2147 | #endif |
| 2148 | ); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2149 | mRecordThreads.add(*input, thread); |
| 2150 | ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2151 | return thread; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2152 | } |
| 2153 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2154 | *input = AUDIO_IO_HANDLE_NONE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2155 | return 0; |
| 2156 | } |
| 2157 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2158 | status_t AudioFlinger::closeInput(audio_io_handle_t input) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2159 | { |
Glenn Kasten | d96c572 | 2012-04-25 13:44:49 -0700 | [diff] [blame] | 2160 | return closeInput_nonvirtual(input); |
| 2161 | } |
| 2162 | |
| 2163 | status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input) |
| 2164 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2165 | // keep strong reference on the record thread so that |
| 2166 | // it is not destroyed while exit() is executed |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 2167 | sp<RecordThread> thread; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2168 | { |
| 2169 | Mutex::Autolock _l(mLock); |
| 2170 | thread = checkRecordThread_l(input); |
Glenn Kasten | d5903ec | 2012-03-18 10:33:27 -0700 | [diff] [blame] | 2171 | if (thread == 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2172 | return BAD_VALUE; |
| 2173 | } |
| 2174 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2175 | ALOGV("closeInput() %d", input); |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 2176 | |
| 2177 | // If we still have effect chains, it means that a client still holds a handle |
| 2178 | // on at least one effect. We must either move the chain to an existing thread with the |
| 2179 | // same session ID or put it aside in case a new record thread is opened for a |
| 2180 | // new capture on the same session |
| 2181 | sp<EffectChain> chain; |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2182 | { |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2183 | Mutex::Autolock _sl(thread->mLock); |
| 2184 | Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l(); |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 2185 | // Note: maximum one chain per record thread |
| 2186 | if (effectChains.size() != 0) { |
| 2187 | chain = effectChains[0]; |
| 2188 | } |
| 2189 | } |
| 2190 | if (chain != 0) { |
| 2191 | // first check if a record thread is already opened with a client on the same session. |
| 2192 | // This should only happen in case of overlap between one thread tear down and the |
| 2193 | // creation of its replacement |
| 2194 | size_t i; |
| 2195 | for (i = 0; i < mRecordThreads.size(); i++) { |
| 2196 | sp<RecordThread> t = mRecordThreads.valueAt(i); |
| 2197 | if (t == thread) { |
| 2198 | continue; |
| 2199 | } |
| 2200 | if (t->hasAudioSession(chain->sessionId()) != 0) { |
| 2201 | Mutex::Autolock _l(t->mLock); |
| 2202 | ALOGV("closeInput() found thread %d for effect session %d", |
| 2203 | t->id(), chain->sessionId()); |
| 2204 | t->addEffectChain_l(chain); |
| 2205 | break; |
| 2206 | } |
| 2207 | } |
| 2208 | // put the chain aside if we could not find a record thread with the same session id. |
| 2209 | if (i == mRecordThreads.size()) { |
| 2210 | putOrphanEffectChain_l(chain); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2211 | } |
| 2212 | } |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2213 | const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor(); |
| 2214 | ioDesc->mIoHandle = input; |
| 2215 | ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2216 | mRecordThreads.removeItem(input); |
| 2217 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2218 | // FIXME: calling thread->exit() without mLock held should not be needed anymore now that |
| 2219 | // we have a different lock for notification client |
| 2220 | closeInputFinish(thread); |
| 2221 | return NO_ERROR; |
| 2222 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2223 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2224 | void AudioFlinger::closeInputFinish(sp<RecordThread> thread) |
| 2225 | { |
| 2226 | thread->exit(); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2227 | AudioStreamIn *in = thread->clearInput(); |
Glenn Kasten | 5798d4e | 2012-03-08 12:18:35 -0800 | [diff] [blame] | 2228 | ALOG_ASSERT(in != NULL, "in shouldn't be NULL"); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2229 | // from now on thread->mInput is NULL |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 2230 | in->hwDev()->close_input_stream(in->hwDev(), in->stream); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2231 | delete in; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2232 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2233 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2234 | void AudioFlinger::closeInputInternal_l(sp<RecordThread> thread) |
| 2235 | { |
| 2236 | mRecordThreads.removeItem(thread->mId); |
| 2237 | closeInputFinish(thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2238 | } |
| 2239 | |
Glenn Kasten | d2304db | 2014-02-03 07:40:31 -0800 | [diff] [blame] | 2240 | status_t AudioFlinger::invalidateStream(audio_stream_type_t stream) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2241 | { |
| 2242 | Mutex::Autolock _l(mLock); |
Glenn Kasten | d2304db | 2014-02-03 07:40:31 -0800 | [diff] [blame] | 2243 | ALOGV("invalidateStream() stream %d", stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2244 | |
| 2245 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2246 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
Eric Laurent | 2216785 | 2012-06-20 12:26:32 -0700 | [diff] [blame] | 2247 | thread->invalidateTracks(stream); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2248 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2249 | |
| 2250 | return NO_ERROR; |
| 2251 | } |
| 2252 | |
| 2253 | |
Eric Laurent | de3f839 | 2014-07-27 18:38:22 -0700 | [diff] [blame] | 2254 | audio_unique_id_t AudioFlinger::newAudioUniqueId() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2255 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2256 | return nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2257 | } |
| 2258 | |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 2259 | void AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid) |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2260 | { |
| 2261 | Mutex::Autolock _l(mLock); |
Glenn Kasten | bb00192 | 2012-02-03 11:10:26 -0800 | [diff] [blame] | 2262 | pid_t caller = IPCThreadState::self()->getCallingPid(); |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 2263 | ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid); |
| 2264 | if (pid != -1 && (caller == getpid_cached)) { |
| 2265 | caller = pid; |
| 2266 | } |
Eric Laurent | d1b28d4 | 2013-09-18 18:47:13 -0700 | [diff] [blame] | 2267 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 2268 | { |
| 2269 | Mutex::Autolock _cl(mClientLock); |
| 2270 | // Ignore requests received from processes not known as notification client. The request |
| 2271 | // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be |
| 2272 | // called from a different pid leaving a stale session reference. Also we don't know how |
| 2273 | // to clear this reference if the client process dies. |
| 2274 | if (mNotificationClients.indexOfKey(caller) < 0) { |
| 2275 | ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession); |
| 2276 | return; |
| 2277 | } |
Eric Laurent | d1b28d4 | 2013-09-18 18:47:13 -0700 | [diff] [blame] | 2278 | } |
| 2279 | |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 2280 | size_t num = mAudioSessionRefs.size(); |
| 2281 | for (size_t i = 0; i< num; i++) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2282 | AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 2283 | if (ref->mSessionid == audioSession && ref->mPid == caller) { |
| 2284 | ref->mCnt++; |
| 2285 | ALOGV(" incremented refcount to %d", ref->mCnt); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2286 | return; |
| 2287 | } |
| 2288 | } |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 2289 | mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller)); |
| 2290 | ALOGV(" added new entry for %d", audioSession); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2291 | } |
| 2292 | |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 2293 | void AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid) |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2294 | { |
| 2295 | Mutex::Autolock _l(mLock); |
Glenn Kasten | bb00192 | 2012-02-03 11:10:26 -0800 | [diff] [blame] | 2296 | pid_t caller = IPCThreadState::self()->getCallingPid(); |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 2297 | ALOGV("releasing %d from %d for %d", audioSession, caller, pid); |
| 2298 | if (pid != -1 && (caller == getpid_cached)) { |
| 2299 | caller = pid; |
| 2300 | } |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 2301 | size_t num = mAudioSessionRefs.size(); |
| 2302 | for (size_t i = 0; i< num; i++) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2303 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 2304 | if (ref->mSessionid == audioSession && ref->mPid == caller) { |
| 2305 | ref->mCnt--; |
| 2306 | ALOGV(" decremented refcount to %d", ref->mCnt); |
| 2307 | if (ref->mCnt == 0) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2308 | mAudioSessionRefs.removeAt(i); |
| 2309 | delete ref; |
| 2310 | purgeStaleEffects_l(); |
| 2311 | } |
| 2312 | return; |
| 2313 | } |
| 2314 | } |
Eric Laurent | d1b28d4 | 2013-09-18 18:47:13 -0700 | [diff] [blame] | 2315 | // If the caller is mediaserver it is likely that the session being released was acquired |
| 2316 | // on behalf of a process not in notification clients and we ignore the warning. |
| 2317 | ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2318 | } |
| 2319 | |
| 2320 | void AudioFlinger::purgeStaleEffects_l() { |
| 2321 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2322 | ALOGV("purging stale effects"); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2323 | |
| 2324 | Vector< sp<EffectChain> > chains; |
| 2325 | |
| 2326 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2327 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 2328 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 2329 | sp<EffectChain> ec = t->mEffectChains[j]; |
Marco Nelissen | 0270b18 | 2011-08-12 14:14:39 -0700 | [diff] [blame] | 2330 | if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) { |
| 2331 | chains.push(ec); |
| 2332 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2333 | } |
| 2334 | } |
| 2335 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 2336 | sp<RecordThread> t = mRecordThreads.valueAt(i); |
| 2337 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 2338 | sp<EffectChain> ec = t->mEffectChains[j]; |
| 2339 | chains.push(ec); |
| 2340 | } |
| 2341 | } |
| 2342 | |
| 2343 | for (size_t i = 0; i < chains.size(); i++) { |
| 2344 | sp<EffectChain> ec = chains[i]; |
| 2345 | int sessionid = ec->sessionId(); |
| 2346 | sp<ThreadBase> t = ec->mThread.promote(); |
| 2347 | if (t == 0) { |
| 2348 | continue; |
| 2349 | } |
| 2350 | size_t numsessionrefs = mAudioSessionRefs.size(); |
| 2351 | bool found = false; |
| 2352 | for (size_t k = 0; k < numsessionrefs; k++) { |
| 2353 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(k); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 2354 | if (ref->mSessionid == sessionid) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2355 | ALOGV(" session %d still exists for %d with %d refs", |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 2356 | sessionid, ref->mPid, ref->mCnt); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2357 | found = true; |
| 2358 | break; |
| 2359 | } |
| 2360 | } |
| 2361 | if (!found) { |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 2362 | Mutex::Autolock _l(t->mLock); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2363 | // remove all effects from the chain |
| 2364 | while (ec->mEffects.size()) { |
| 2365 | sp<EffectModule> effect = ec->mEffects[0]; |
| 2366 | effect->unPin(); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2367 | t->removeEffect_l(effect); |
Eric Laurent | a5f44eb | 2012-06-25 11:38:29 -0700 | [diff] [blame] | 2368 | if (effect->purgeHandles()) { |
| 2369 | t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId()); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2370 | } |
| 2371 | AudioSystem::unregisterEffect(effect->id()); |
| 2372 | } |
| 2373 | } |
| 2374 | } |
| 2375 | return; |
| 2376 | } |
| 2377 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2378 | // checkPlaybackThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2379 | AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2380 | { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 2381 | return mPlaybackThreads.valueFor(output).get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2382 | } |
| 2383 | |
| 2384 | // checkMixerThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2385 | AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2386 | { |
| 2387 | PlaybackThread *thread = checkPlaybackThread_l(output); |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 2388 | return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | // checkRecordThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2392 | AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2393 | { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 2394 | return mRecordThreads.valueFor(input).get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2395 | } |
| 2396 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2397 | uint32_t AudioFlinger::nextUniqueId() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2398 | { |
Glenn Kasten | bcefec3 | 2014-01-17 12:09:05 -0800 | [diff] [blame] | 2399 | return (uint32_t) android_atomic_inc(&mNextUniqueId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2400 | } |
| 2401 | |
Glenn Kasten | 02fe1bf | 2012-02-24 15:42:17 -0800 | [diff] [blame] | 2402 | AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2403 | { |
| 2404 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2405 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 2406 | if(thread->isDuplicating()) { |
| 2407 | continue; |
| 2408 | } |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2409 | AudioStreamOut *output = thread->getOutput(); |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 2410 | if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2411 | return thread; |
| 2412 | } |
| 2413 | } |
| 2414 | return NULL; |
| 2415 | } |
| 2416 | |
Glenn Kasten | bb4350d | 2012-07-03 15:56:38 -0700 | [diff] [blame] | 2417 | audio_devices_t AudioFlinger::primaryOutputDevice_l() const |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2418 | { |
| 2419 | PlaybackThread *thread = primaryPlaybackThread_l(); |
| 2420 | |
| 2421 | if (thread == NULL) { |
| 2422 | return 0; |
| 2423 | } |
| 2424 | |
Eric Laurent | f1c04f9 | 2012-08-28 14:26:53 -0700 | [diff] [blame] | 2425 | return thread->outDevice(); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2426 | } |
| 2427 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 2428 | sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type, |
| 2429 | int triggerSession, |
| 2430 | int listenerSession, |
| 2431 | sync_event_callback_t callBack, |
Eric Laurent | 8ea16e4 | 2014-02-20 16:26:11 -0800 | [diff] [blame] | 2432 | wp<RefBase> cookie) |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 2433 | { |
| 2434 | Mutex::Autolock _l(mLock); |
| 2435 | |
| 2436 | sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie); |
| 2437 | status_t playStatus = NAME_NOT_FOUND; |
| 2438 | status_t recStatus = NAME_NOT_FOUND; |
| 2439 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2440 | playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event); |
| 2441 | if (playStatus == NO_ERROR) { |
| 2442 | return event; |
| 2443 | } |
| 2444 | } |
| 2445 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 2446 | recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event); |
| 2447 | if (recStatus == NO_ERROR) { |
| 2448 | return event; |
| 2449 | } |
| 2450 | } |
| 2451 | if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) { |
| 2452 | mPendingSyncEvents.add(event); |
| 2453 | } else { |
| 2454 | ALOGV("createSyncEvent() invalid event %d", event->type()); |
| 2455 | event.clear(); |
| 2456 | } |
| 2457 | return event; |
| 2458 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2459 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2460 | // ---------------------------------------------------------------------------- |
| 2461 | // Effect management |
| 2462 | // ---------------------------------------------------------------------------- |
| 2463 | |
| 2464 | |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 2465 | status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2466 | { |
| 2467 | Mutex::Autolock _l(mLock); |
| 2468 | return EffectQueryNumberEffects(numEffects); |
| 2469 | } |
| 2470 | |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 2471 | status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2472 | { |
| 2473 | Mutex::Autolock _l(mLock); |
| 2474 | return EffectQueryEffect(index, descriptor); |
| 2475 | } |
| 2476 | |
Glenn Kasten | 5e92a78 | 2012-01-30 07:40:52 -0800 | [diff] [blame] | 2477 | status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid, |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 2478 | effect_descriptor_t *descriptor) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2479 | { |
| 2480 | Mutex::Autolock _l(mLock); |
| 2481 | return EffectGetDescriptor(pUuid, descriptor); |
| 2482 | } |
| 2483 | |
| 2484 | |
Glenn Kasten | 8d6cc84 | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 2485 | sp<IEffect> AudioFlinger::createEffect( |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2486 | effect_descriptor_t *pDesc, |
| 2487 | const sp<IEffectClient>& effectClient, |
| 2488 | int32_t priority, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2489 | audio_io_handle_t io, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2490 | int sessionId, |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 2491 | const String16& opPackageName, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2492 | status_t *status, |
| 2493 | int *id, |
| 2494 | int *enabled) |
| 2495 | { |
| 2496 | status_t lStatus = NO_ERROR; |
| 2497 | sp<EffectHandle> handle; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2498 | effect_descriptor_t desc; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2499 | |
Glenn Kasten | 8d6cc84 | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 2500 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 2501 | ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d", |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2502 | pid, effectClient.get(), priority, sessionId, io); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2503 | |
| 2504 | if (pDesc == NULL) { |
| 2505 | lStatus = BAD_VALUE; |
| 2506 | goto Exit; |
| 2507 | } |
| 2508 | |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 2509 | // check audio settings permission for global effects |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2510 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 2511 | lStatus = PERMISSION_DENIED; |
| 2512 | goto Exit; |
| 2513 | } |
| 2514 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2515 | // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 2516 | // 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] | 2517 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 2518 | lStatus = PERMISSION_DENIED; |
| 2519 | goto Exit; |
| 2520 | } |
| 2521 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2522 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2523 | if (!EffectIsNullUuid(&pDesc->uuid)) { |
| 2524 | // if uuid is specified, request effect descriptor |
| 2525 | lStatus = EffectGetDescriptor(&pDesc->uuid, &desc); |
| 2526 | if (lStatus < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2527 | ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2528 | goto Exit; |
| 2529 | } |
| 2530 | } else { |
| 2531 | // if uuid is not specified, look for an available implementation |
| 2532 | // of the required type in effect factory |
| 2533 | if (EffectIsNullUuid(&pDesc->type)) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2534 | ALOGW("createEffect() no effect type"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2535 | lStatus = BAD_VALUE; |
| 2536 | goto Exit; |
| 2537 | } |
| 2538 | uint32_t numEffects = 0; |
| 2539 | effect_descriptor_t d; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2540 | d.flags = 0; // prevent compiler warning |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2541 | bool found = false; |
| 2542 | |
| 2543 | lStatus = EffectQueryNumberEffects(&numEffects); |
| 2544 | if (lStatus < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2545 | ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2546 | goto Exit; |
| 2547 | } |
| 2548 | for (uint32_t i = 0; i < numEffects; i++) { |
| 2549 | lStatus = EffectQueryEffect(i, &desc); |
| 2550 | if (lStatus < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2551 | ALOGW("createEffect() error %d from EffectQueryEffect", lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2552 | continue; |
| 2553 | } |
| 2554 | if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) { |
| 2555 | // If matching type found save effect descriptor. If the session is |
| 2556 | // 0 and the effect is not auxiliary, continue enumeration in case |
| 2557 | // an auxiliary version of this effect type is available |
| 2558 | found = true; |
Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 2559 | d = desc; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2560 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX || |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2561 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 2562 | break; |
| 2563 | } |
| 2564 | } |
| 2565 | } |
| 2566 | if (!found) { |
| 2567 | lStatus = BAD_VALUE; |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2568 | ALOGW("createEffect() effect not found"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2569 | goto Exit; |
| 2570 | } |
| 2571 | // For same effect type, chose auxiliary version over insert version if |
| 2572 | // connect to output mix (Compliance to OpenSL ES) |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2573 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2574 | (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) { |
Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 2575 | desc = d; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2576 | } |
| 2577 | } |
| 2578 | |
| 2579 | // 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] | 2580 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2581 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 2582 | lStatus = INVALID_OPERATION; |
| 2583 | goto Exit; |
| 2584 | } |
| 2585 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2586 | // check recording permission for visualizer |
| 2587 | if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) && |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 2588 | !recordingAllowed(opPackageName)) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2589 | lStatus = PERMISSION_DENIED; |
| 2590 | goto Exit; |
| 2591 | } |
| 2592 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2593 | // return effect descriptor |
Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 2594 | *pDesc = desc; |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 2595 | if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | eb3c337 | 2013-09-25 12:25:29 -0700 | [diff] [blame] | 2596 | // if the output returned by getOutputForEffect() is removed before we lock the |
| 2597 | // mutex below, the call to checkPlaybackThread_l(io) below will detect it |
| 2598 | // and we will exit safely |
| 2599 | io = AudioSystem::getOutputForEffect(&desc); |
| 2600 | ALOGV("createEffect got output %d", io); |
| 2601 | } |
| 2602 | |
| 2603 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2604 | |
| 2605 | // If output is not specified try to find a matching audio session ID in one of the |
| 2606 | // output threads. |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 2607 | // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX |
| 2608 | // because of code checking output when entering the function. |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2609 | // Note: io is never 0 when creating an effect on an input |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 2610 | if (io == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2611 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
| 2612 | // output must be specified by AudioPolicyManager when using session |
| 2613 | // AUDIO_SESSION_OUTPUT_STAGE |
| 2614 | lStatus = BAD_VALUE; |
| 2615 | goto Exit; |
| 2616 | } |
Eric Laurent | eb3c337 | 2013-09-25 12:25:29 -0700 | [diff] [blame] | 2617 | // look for the thread where the specified audio session is present |
| 2618 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2619 | if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { |
| 2620 | io = mPlaybackThreads.keyAt(i); |
| 2621 | break; |
| 2622 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2623 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2624 | if (io == 0) { |
Eric Laurent | eb3c337 | 2013-09-25 12:25:29 -0700 | [diff] [blame] | 2625 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 2626 | if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { |
| 2627 | io = mRecordThreads.keyAt(i); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 2628 | break; |
| 2629 | } |
| 2630 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2631 | } |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 2632 | // If no output thread contains the requested session ID, default to |
| 2633 | // first output. The effect chain will be moved to the correct output |
| 2634 | // thread when a track with the same session ID is created |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 2635 | if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2636 | io = mPlaybackThreads.keyAt(0); |
| 2637 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2638 | ALOGV("createEffect() got io %d for effect %s", io, desc.name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2639 | } |
| 2640 | ThreadBase *thread = checkRecordThread_l(io); |
| 2641 | if (thread == NULL) { |
| 2642 | thread = checkPlaybackThread_l(io); |
| 2643 | if (thread == NULL) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 2644 | ALOGE("createEffect() unknown output thread"); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2645 | lStatus = BAD_VALUE; |
| 2646 | goto Exit; |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 2647 | } |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2648 | } else { |
| 2649 | // Check if one effect chain was awaiting for an effect to be created on this |
| 2650 | // session and used it instead of creating a new one. |
| 2651 | sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)sessionId); |
| 2652 | if (chain != 0) { |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 2653 | Mutex::Autolock _l(thread->mLock); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2654 | thread->addEffectChain_l(chain); |
| 2655 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2656 | } |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 2657 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 2658 | sp<Client> client = registerPid(pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2659 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2660 | // create effect on selected output thread |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2661 | handle = thread->createEffect_l(client, effectClient, priority, sessionId, |
| 2662 | &desc, enabled, &lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2663 | if (handle != 0 && id != NULL) { |
| 2664 | *id = handle->id(); |
| 2665 | } |
Eric Laurent | fe1a94e | 2014-05-26 16:03:08 -0700 | [diff] [blame] | 2666 | if (handle == 0) { |
| 2667 | // remove local strong reference to Client with mClientLock held |
| 2668 | Mutex::Autolock _cl(mClientLock); |
| 2669 | client.clear(); |
| 2670 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2671 | } |
| 2672 | |
| 2673 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 2674 | *status = lStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2675 | return handle; |
| 2676 | } |
| 2677 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2678 | status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput, |
| 2679 | audio_io_handle_t dstOutput) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2680 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2681 | ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2682 | sessionId, srcOutput, dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2683 | Mutex::Autolock _l(mLock); |
| 2684 | if (srcOutput == dstOutput) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2685 | ALOGW("moveEffects() same dst and src outputs %d", dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2686 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2687 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2688 | PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput); |
| 2689 | if (srcThread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2690 | ALOGW("moveEffects() bad srcOutput %d", srcOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2691 | return BAD_VALUE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2692 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2693 | PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput); |
| 2694 | if (dstThread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2695 | ALOGW("moveEffects() bad dstOutput %d", dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2696 | return BAD_VALUE; |
| 2697 | } |
| 2698 | |
| 2699 | Mutex::Autolock _dl(dstThread->mLock); |
| 2700 | Mutex::Autolock _sl(srcThread->mLock); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2701 | return moveEffectChain_l(sessionId, srcThread, dstThread, false); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2702 | } |
| 2703 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2704 | // moveEffectChain_l must be called with both srcThread and dstThread mLocks held |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2705 | status_t AudioFlinger::moveEffectChain_l(int sessionId, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2706 | AudioFlinger::PlaybackThread *srcThread, |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2707 | AudioFlinger::PlaybackThread *dstThread, |
| 2708 | bool reRegister) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2709 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2710 | ALOGV("moveEffectChain_l() session %d from thread %p to thread %p", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2711 | sessionId, srcThread, dstThread); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2712 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2713 | sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2714 | if (chain == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2715 | 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] | 2716 | sessionId, srcThread); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2717 | return INVALID_OPERATION; |
| 2718 | } |
| 2719 | |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 2720 | // Check whether the destination thread has a channel count of FCC_2, which is |
| 2721 | // currently required for (most) effects. Prevent moving the effect chain here rather |
| 2722 | // than disabling the addEffect_l() call in dstThread below. |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 2723 | if ((dstThread->type() == ThreadBase::MIXER || dstThread->isDuplicating()) && |
Eric Laurent | b10352f | 2014-11-03 16:25:39 -0800 | [diff] [blame] | 2724 | dstThread->mChannelCount != FCC_2) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 2725 | ALOGW("moveEffectChain_l() effect chain failed because" |
| 2726 | " destination thread %p channel count(%u) != %u", |
| 2727 | dstThread, dstThread->mChannelCount, FCC_2); |
| 2728 | return INVALID_OPERATION; |
| 2729 | } |
| 2730 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2731 | // 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] | 2732 | // 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] | 2733 | // 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] | 2734 | // removed. |
| 2735 | srcThread->removeEffectChain_l(chain); |
| 2736 | |
| 2737 | // transfer all effects one by one so that new effect chain is created on new thread with |
| 2738 | // correct buffer sizes and audio parameters and effect engines reconfigured accordingly |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2739 | sp<EffectChain> dstChain; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2740 | uint32_t strategy = 0; // prevent compiler warning |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2741 | sp<EffectModule> effect = chain->getEffectFromId_l(0); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2742 | Vector< sp<EffectModule> > removed; |
| 2743 | status_t status = NO_ERROR; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2744 | while (effect != 0) { |
| 2745 | srcThread->removeEffect_l(effect); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2746 | removed.add(effect); |
| 2747 | status = dstThread->addEffect_l(effect); |
| 2748 | if (status != NO_ERROR) { |
| 2749 | break; |
| 2750 | } |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 2751 | // removeEffect_l() has stopped the effect if it was active so it must be restarted |
| 2752 | if (effect->state() == EffectModule::ACTIVE || |
| 2753 | effect->state() == EffectModule::STOPPING) { |
| 2754 | effect->start(); |
| 2755 | } |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2756 | // if the move request is not received from audio policy manager, the effect must be |
| 2757 | // re-registered with the new strategy and output |
| 2758 | if (dstChain == 0) { |
| 2759 | dstChain = effect->chain().promote(); |
| 2760 | if (dstChain == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2761 | ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get()); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2762 | status = NO_INIT; |
| 2763 | break; |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2764 | } |
| 2765 | strategy = dstChain->strategy(); |
| 2766 | } |
| 2767 | if (reRegister) { |
| 2768 | AudioSystem::unregisterEffect(effect->id()); |
| 2769 | AudioSystem::registerEffect(&effect->desc(), |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2770 | dstThread->id(), |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2771 | strategy, |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2772 | sessionId, |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2773 | effect->id()); |
Eric Laurent | d72b7c0 | 2013-10-12 16:17:46 -0700 | [diff] [blame] | 2774 | AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled()); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2775 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2776 | effect = chain->getEffectFromId_l(0); |
| 2777 | } |
| 2778 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2779 | if (status != NO_ERROR) { |
| 2780 | for (size_t i = 0; i < removed.size(); i++) { |
| 2781 | srcThread->addEffect_l(removed[i]); |
| 2782 | if (dstChain != 0 && reRegister) { |
| 2783 | AudioSystem::unregisterEffect(removed[i]->id()); |
| 2784 | AudioSystem::registerEffect(&removed[i]->desc(), |
| 2785 | srcThread->id(), |
| 2786 | strategy, |
| 2787 | sessionId, |
| 2788 | removed[i]->id()); |
Eric Laurent | d72b7c0 | 2013-10-12 16:17:46 -0700 | [diff] [blame] | 2789 | AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled()); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2790 | } |
| 2791 | } |
| 2792 | } |
| 2793 | |
| 2794 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2795 | } |
| 2796 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2797 | bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l() |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 2798 | { |
| 2799 | if (mGlobalEffectEnableTime != 0 && |
| 2800 | ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) { |
| 2801 | return true; |
| 2802 | } |
| 2803 | |
| 2804 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2805 | sp<EffectChain> ec = |
| 2806 | mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2807 | if (ec != 0 && ec->isNonOffloadableEnabled()) { |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 2808 | return true; |
| 2809 | } |
| 2810 | } |
| 2811 | return false; |
| 2812 | } |
| 2813 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2814 | void AudioFlinger::onNonOffloadableGlobalEffectEnable() |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 2815 | { |
| 2816 | Mutex::Autolock _l(mLock); |
| 2817 | |
| 2818 | mGlobalEffectEnableTime = systemTime(); |
| 2819 | |
| 2820 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2821 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 2822 | if (t->mType == ThreadBase::OFFLOAD) { |
| 2823 | t->invalidateTracks(AUDIO_STREAM_MUSIC); |
| 2824 | } |
| 2825 | } |
| 2826 | |
| 2827 | } |
| 2828 | |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2829 | status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain) |
| 2830 | { |
| 2831 | audio_session_t session = (audio_session_t)chain->sessionId(); |
| 2832 | ssize_t index = mOrphanEffectChains.indexOfKey(session); |
| 2833 | ALOGV("putOrphanEffectChain_l session %d index %d", session, index); |
| 2834 | if (index >= 0) { |
| 2835 | ALOGW("putOrphanEffectChain_l chain for session %d already present", session); |
| 2836 | return ALREADY_EXISTS; |
| 2837 | } |
| 2838 | mOrphanEffectChains.add(session, chain); |
| 2839 | return NO_ERROR; |
| 2840 | } |
| 2841 | |
| 2842 | sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session) |
| 2843 | { |
| 2844 | sp<EffectChain> chain; |
| 2845 | ssize_t index = mOrphanEffectChains.indexOfKey(session); |
| 2846 | ALOGV("getOrphanEffectChain_l session %d index %d", session, index); |
| 2847 | if (index >= 0) { |
| 2848 | chain = mOrphanEffectChains.valueAt(index); |
| 2849 | mOrphanEffectChains.removeItemsAt(index); |
| 2850 | } |
| 2851 | return chain; |
| 2852 | } |
| 2853 | |
| 2854 | bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect) |
| 2855 | { |
| 2856 | Mutex::Autolock _l(mLock); |
| 2857 | audio_session_t session = (audio_session_t)effect->sessionId(); |
| 2858 | ssize_t index = mOrphanEffectChains.indexOfKey(session); |
| 2859 | ALOGV("updateOrphanEffectChains session %d index %d", session, index); |
| 2860 | if (index >= 0) { |
| 2861 | sp<EffectChain> chain = mOrphanEffectChains.valueAt(index); |
| 2862 | if (chain->removeEffect_l(effect) == 0) { |
| 2863 | ALOGV("updateOrphanEffectChains removing effect chain at index %d", index); |
| 2864 | mOrphanEffectChains.removeItemsAt(index); |
| 2865 | } |
| 2866 | return true; |
| 2867 | } |
| 2868 | return false; |
| 2869 | } |
| 2870 | |
| 2871 | |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2872 | struct Entry { |
Glenn Kasten | 2f55e76 | 2015-03-05 16:05:08 -0800 | [diff] [blame] | 2873 | #define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21 |
| 2874 | char mFileName[TEE_MAX_FILENAME]; |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2875 | }; |
| 2876 | |
| 2877 | int comparEntry(const void *p1, const void *p2) |
| 2878 | { |
Glenn Kasten | 2f55e76 | 2015-03-05 16:05:08 -0800 | [diff] [blame] | 2879 | return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2880 | } |
| 2881 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 2882 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2883 | void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2884 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2885 | NBAIO_Source *teeSource = source.get(); |
| 2886 | if (teeSource != NULL) { |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2887 | // .wav rotation |
| 2888 | // There is a benign race condition if 2 threads call this simultaneously. |
| 2889 | // They would both traverse the directory, but the result would simply be |
| 2890 | // failures at unlink() which are ignored. It's also unlikely since |
| 2891 | // normally dumpsys is only done by bugreport or from the command line. |
| 2892 | char teePath[32+256]; |
| 2893 | strcpy(teePath, "/data/misc/media"); |
| 2894 | size_t teePathLen = strlen(teePath); |
| 2895 | DIR *dir = opendir(teePath); |
| 2896 | teePath[teePathLen++] = '/'; |
| 2897 | if (dir != NULL) { |
Glenn Kasten | 2f55e76 | 2015-03-05 16:05:08 -0800 | [diff] [blame] | 2898 | #define TEE_MAX_SORT 20 // number of entries to sort |
| 2899 | #define TEE_MAX_KEEP 10 // number of entries to keep |
| 2900 | struct Entry entries[TEE_MAX_SORT]; |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2901 | size_t entryCount = 0; |
Glenn Kasten | 2f55e76 | 2015-03-05 16:05:08 -0800 | [diff] [blame] | 2902 | while (entryCount < TEE_MAX_SORT) { |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2903 | struct dirent de; |
| 2904 | struct dirent *result = NULL; |
| 2905 | int rc = readdir_r(dir, &de, &result); |
| 2906 | if (rc != 0) { |
| 2907 | ALOGW("readdir_r failed %d", rc); |
| 2908 | break; |
| 2909 | } |
| 2910 | if (result == NULL) { |
| 2911 | break; |
| 2912 | } |
| 2913 | if (result != &de) { |
| 2914 | ALOGW("readdir_r returned unexpected result %p != %p", result, &de); |
| 2915 | break; |
| 2916 | } |
| 2917 | // ignore non .wav file entries |
| 2918 | size_t nameLen = strlen(de.d_name); |
Glenn Kasten | 2f55e76 | 2015-03-05 16:05:08 -0800 | [diff] [blame] | 2919 | if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME || |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2920 | strcmp(&de.d_name[nameLen - 4], ".wav")) { |
| 2921 | continue; |
| 2922 | } |
Glenn Kasten | 2f55e76 | 2015-03-05 16:05:08 -0800 | [diff] [blame] | 2923 | strcpy(entries[entryCount++].mFileName, de.d_name); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2924 | } |
| 2925 | (void) closedir(dir); |
Glenn Kasten | 2f55e76 | 2015-03-05 16:05:08 -0800 | [diff] [blame] | 2926 | if (entryCount > TEE_MAX_KEEP) { |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2927 | qsort(entries, entryCount, sizeof(Entry), comparEntry); |
Glenn Kasten | 2f55e76 | 2015-03-05 16:05:08 -0800 | [diff] [blame] | 2928 | for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) { |
| 2929 | strcpy(&teePath[teePathLen], entries[i].mFileName); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2930 | (void) unlink(teePath); |
| 2931 | } |
| 2932 | } |
| 2933 | } else { |
| 2934 | if (fd >= 0) { |
Elliott Hughes | 8b5f642 | 2014-05-22 01:22:06 -0700 | [diff] [blame] | 2935 | dprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno)); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2936 | } |
| 2937 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2938 | char teeTime[16]; |
| 2939 | struct timeval tv; |
| 2940 | gettimeofday(&tv, NULL); |
| 2941 | struct tm tm; |
| 2942 | localtime_r(&tv.tv_sec, &tm); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2943 | strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm); |
| 2944 | snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id); |
| 2945 | // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd |
| 2946 | int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2947 | if (teeFd >= 0) { |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 2948 | // FIXME use libsndfile |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2949 | char wavHeader[44]; |
| 2950 | memcpy(wavHeader, |
| 2951 | "RIFF\0\0\0\0WAVEfmt \20\0\0\0\1\0\2\0\104\254\0\0\0\0\0\0\4\0\20\0data\0\0\0\0", |
| 2952 | sizeof(wavHeader)); |
| 2953 | NBAIO_Format format = teeSource->format(); |
| 2954 | unsigned channelCount = Format_channelCount(format); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2955 | uint32_t sampleRate = Format_sampleRate(format); |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 2956 | size_t frameSize = Format_frameSize(format); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2957 | wavHeader[22] = channelCount; // number of channels |
| 2958 | wavHeader[24] = sampleRate; // sample rate |
| 2959 | wavHeader[25] = sampleRate >> 8; |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 2960 | wavHeader[32] = frameSize; // block alignment |
| 2961 | wavHeader[33] = frameSize >> 8; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2962 | write(teeFd, wavHeader, sizeof(wavHeader)); |
| 2963 | size_t total = 0; |
| 2964 | bool firstRead = true; |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 2965 | #define TEE_SINK_READ 1024 // frames per I/O operation |
| 2966 | void *buffer = malloc(TEE_SINK_READ * frameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2967 | for (;;) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2968 | size_t count = TEE_SINK_READ; |
| 2969 | ssize_t actual = teeSource->read(buffer, count, |
| 2970 | AudioBufferProvider::kInvalidPTS); |
| 2971 | bool wasFirstRead = firstRead; |
| 2972 | firstRead = false; |
| 2973 | if (actual <= 0) { |
| 2974 | if (actual == (ssize_t) OVERRUN && wasFirstRead) { |
| 2975 | continue; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2976 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2977 | break; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2978 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2979 | ALOG_ASSERT(actual <= (ssize_t)count); |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 2980 | write(teeFd, buffer, actual * frameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2981 | total += actual; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2982 | } |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 2983 | free(buffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2984 | lseek(teeFd, (off_t) 4, SEEK_SET); |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 2985 | uint32_t temp = 44 + total * frameSize - 8; |
| 2986 | // FIXME not big-endian safe |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2987 | write(teeFd, &temp, sizeof(temp)); |
| 2988 | lseek(teeFd, (off_t) 40, SEEK_SET); |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 2989 | temp = total * frameSize; |
| 2990 | // FIXME not big-endian safe |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2991 | write(teeFd, &temp, sizeof(temp)); |
| 2992 | close(teeFd); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2993 | if (fd >= 0) { |
Elliott Hughes | 8b5f642 | 2014-05-22 01:22:06 -0700 | [diff] [blame] | 2994 | dprintf(fd, "tee copied to %s\n", teePath); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2995 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2996 | } else { |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2997 | if (fd >= 0) { |
Elliott Hughes | 8b5f642 | 2014-05-22 01:22:06 -0700 | [diff] [blame] | 2998 | dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno)); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2999 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 3000 | } |
| 3001 | } |
| 3002 | } |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3003 | #endif |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 3004 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3005 | // ---------------------------------------------------------------------------- |
| 3006 | |
| 3007 | status_t AudioFlinger::onTransact( |
| 3008 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 3009 | { |
| 3010 | return BnAudioFlinger::onTransact(code, data, reply, flags); |
| 3011 | } |
| 3012 | |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 3013 | } // namespace android |