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