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