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