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