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