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