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