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