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> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 34 | #include <utils/String16.h> |
| 35 | #include <utils/threads.h> |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 36 | #include <utils/Atomic.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 37 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 38 | #include <cutils/bitops.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 39 | #include <cutils/properties.h> |
| 40 | |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 41 | #include <system/audio.h> |
Dima Zavin | 7394a4f | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 42 | #include <hardware/audio.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 43 | |
| 44 | #include "AudioMixer.h" |
| 45 | #include "AudioFlinger.h" |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 46 | #include "ServiceUtilities.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 47 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 48 | #include <media/AudioResamplerPublic.h> |
| 49 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 50 | #include <media/EffectsFactoryApi.h> |
Eric Laurent | 6d8b694 | 2011-06-24 07:01:31 -0700 | [diff] [blame] | 51 | #include <audio_effects/effect_visualizer.h> |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 52 | #include <audio_effects/effect_ns.h> |
| 53 | #include <audio_effects/effect_aec.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 54 | |
Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 55 | #include <audio_utils/primitives.h> |
| 56 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 57 | #include <powermanager/PowerManager.h> |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 58 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 59 | #include <common_time/cc_helper.h> |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 60 | |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 61 | #include <media/IMediaLogService.h> |
| 62 | |
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> |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 66 | #include <private/android_filesystem_config.h> |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 67 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 68 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 69 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 70 | // Note: the following macro is used for extremely verbose logging message. In |
| 71 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 72 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 73 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 74 | // turned on. Do not uncomment the #def below unless you really know what you |
| 75 | // are doing and want to see all of the extremely verbose messages. |
| 76 | //#define VERY_VERY_VERBOSE_LOGGING |
| 77 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 78 | #define ALOGVV ALOGV |
| 79 | #else |
| 80 | #define ALOGVV(a...) do { } while(0) |
| 81 | #endif |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 82 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 83 | namespace android { |
| 84 | |
Glenn Kasten | ec1d6b5 | 2011-12-12 09:04:45 -0800 | [diff] [blame] | 85 | static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n"; |
| 86 | static const char kHardwareLockedString[] = "Hardware lock is taken\n"; |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 87 | static const char kClientLockedString[] = "Client lock is taken\n"; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 88 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 89 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 90 | nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs; |
Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 91 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 92 | uint32_t AudioFlinger::mScreenState; |
Glenn Kasten | 3ed2920 | 2012-08-07 15:24:44 -0700 | [diff] [blame] | 93 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 94 | #ifdef TEE_SINK |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 95 | bool AudioFlinger::mTeeSinkInputEnabled = false; |
| 96 | bool AudioFlinger::mTeeSinkOutputEnabled = false; |
| 97 | bool AudioFlinger::mTeeSinkTrackEnabled = false; |
| 98 | |
| 99 | size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault; |
| 100 | size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault; |
| 101 | size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault; |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 102 | #endif |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 103 | |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 104 | // In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off |
| 105 | // we define a minimum time during which a global effect is considered enabled. |
| 106 | static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200); |
| 107 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 108 | // ---------------------------------------------------------------------------- |
| 109 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 110 | const char *formatToString(audio_format_t format) { |
aarti jadhav-gaikwad | 2829edc | 2014-06-18 15:25:26 +0530 | [diff] [blame] | 111 | switch (format & AUDIO_FORMAT_MAIN_MASK) { |
| 112 | case AUDIO_FORMAT_PCM: |
| 113 | switch (format) { |
| 114 | case AUDIO_FORMAT_PCM_16_BIT: return "pcm16"; |
| 115 | case AUDIO_FORMAT_PCM_8_BIT: return "pcm8"; |
| 116 | case AUDIO_FORMAT_PCM_32_BIT: return "pcm32"; |
| 117 | case AUDIO_FORMAT_PCM_8_24_BIT: return "pcm8.24"; |
| 118 | case AUDIO_FORMAT_PCM_FLOAT: return "pcmfloat"; |
| 119 | case AUDIO_FORMAT_PCM_24_BIT_PACKED: return "pcm24"; |
| 120 | default: |
| 121 | break; |
| 122 | } |
| 123 | break; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 124 | case AUDIO_FORMAT_MP3: return "mp3"; |
| 125 | case AUDIO_FORMAT_AMR_NB: return "amr-nb"; |
| 126 | case AUDIO_FORMAT_AMR_WB: return "amr-wb"; |
| 127 | case AUDIO_FORMAT_AAC: return "aac"; |
| 128 | case AUDIO_FORMAT_HE_AAC_V1: return "he-aac-v1"; |
| 129 | case AUDIO_FORMAT_HE_AAC_V2: return "he-aac-v2"; |
| 130 | case AUDIO_FORMAT_VORBIS: return "vorbis"; |
aarti jadhav-gaikwad | 2829edc | 2014-06-18 15:25:26 +0530 | [diff] [blame] | 131 | case AUDIO_FORMAT_OPUS: return "opus"; |
| 132 | case AUDIO_FORMAT_AC3: return "ac-3"; |
| 133 | case AUDIO_FORMAT_E_AC3: return "e-ac-3"; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 134 | default: |
| 135 | break; |
| 136 | } |
| 137 | return "unknown"; |
| 138 | } |
| 139 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 140 | static int load_audio_interface(const char *if_name, audio_hw_device_t **dev) |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 141 | { |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 142 | const hw_module_t *mod; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 143 | int rc; |
| 144 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 145 | rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod); |
| 146 | ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__, |
| 147 | AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); |
| 148 | if (rc) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 149 | goto out; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 150 | } |
| 151 | rc = audio_hw_device_open(mod, dev); |
| 152 | ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__, |
| 153 | AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); |
| 154 | if (rc) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 155 | goto out; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 156 | } |
Eric Laurent | 5806b35 | 2014-05-22 12:23:26 -0700 | [diff] [blame] | 157 | if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) { |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 158 | ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version); |
| 159 | rc = BAD_VALUE; |
| 160 | goto out; |
| 161 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 162 | return 0; |
| 163 | |
| 164 | out: |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 165 | *dev = NULL; |
| 166 | return rc; |
| 167 | } |
| 168 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 169 | // ---------------------------------------------------------------------------- |
| 170 | |
| 171 | AudioFlinger::AudioFlinger() |
| 172 | : BnAudioFlinger(), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 173 | mPrimaryHardwareDev(NULL), |
Glenn Kasten | 9ea65d0 | 2014-01-17 10:21:24 -0800 | [diff] [blame] | 174 | mAudioHwDevs(NULL), |
Glenn Kasten | 7d6c35b | 2012-07-02 12:45:10 -0700 | [diff] [blame] | 175 | mHardwareStatus(AUDIO_HW_IDLE), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 176 | mMasterVolume(1.0f), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 177 | mMasterMute(false), |
| 178 | mNextUniqueId(1), |
| 179 | mMode(AUDIO_MODE_INVALID), |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 180 | mBtNrecIsOff(false), |
| 181 | mIsLowRamDevice(true), |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 182 | mIsDeviceTypeKnown(false), |
Glenn Kasten | 4ea00a2 | 2014-06-02 08:29:22 -0700 | [diff] [blame] | 183 | mGlobalEffectEnableTime(0), |
| 184 | mPrimaryOutputSampleRate(0) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 185 | { |
Glenn Kasten | 949a926 | 2013-04-16 12:35:20 -0700 | [diff] [blame] | 186 | getpid_cached = getpid(); |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 187 | char value[PROPERTY_VALUE_MAX]; |
| 188 | bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1); |
| 189 | if (doLog) { |
Glenn Kasten | b187de1 | 2014-12-30 08:18:15 -0800 | [diff] [blame] | 190 | mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters", |
| 191 | MemoryHeapBase::READ_ONLY); |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 192 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 193 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 194 | #ifdef TEE_SINK |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 195 | (void) property_get("ro.debuggable", value, "0"); |
| 196 | int debuggable = atoi(value); |
| 197 | int teeEnabled = 0; |
| 198 | if (debuggable) { |
| 199 | (void) property_get("af.tee", value, "0"); |
| 200 | teeEnabled = atoi(value); |
| 201 | } |
Glenn Kasten | f66b422 | 2014-02-20 10:23:28 -0800 | [diff] [blame] | 202 | // FIXME symbolic constants here |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 203 | if (teeEnabled & 1) { |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 204 | mTeeSinkInputEnabled = true; |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 205 | } |
| 206 | if (teeEnabled & 2) { |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 207 | mTeeSinkOutputEnabled = true; |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 208 | } |
| 209 | if (teeEnabled & 4) { |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 210 | mTeeSinkTrackEnabled = true; |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 211 | } |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 212 | #endif |
Dima Zavin | 5a61d2f | 2011-04-19 19:04:32 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void AudioFlinger::onFirstRef() |
| 216 | { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 217 | int rc = 0; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 218 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 219 | Mutex::Autolock _l(mLock); |
| 220 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 221 | /* TODO: move all this work into an Init() function */ |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 222 | char val_str[PROPERTY_VALUE_MAX] = { 0 }; |
| 223 | if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) { |
| 224 | uint32_t int_val; |
| 225 | if (1 == sscanf(val_str, "%u", &int_val)) { |
| 226 | mStandbyTimeInNsecs = milliseconds(int_val); |
| 227 | ALOGI("Using %u mSec as standby time.", int_val); |
| 228 | } else { |
| 229 | mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs; |
| 230 | ALOGI("Using default %u mSec as standby time.", |
| 231 | (uint32_t)(mStandbyTimeInNsecs / 1000000)); |
| 232 | } |
| 233 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 234 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 235 | mPatchPanel = new PatchPanel(this); |
| 236 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 237 | mMode = AUDIO_MODE_NORMAL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | AudioFlinger::~AudioFlinger() |
| 241 | { |
| 242 | while (!mRecordThreads.isEmpty()) { |
Glenn Kasten | c3ae93f | 2012-07-30 10:59:30 -0700 | [diff] [blame] | 243 | // closeInput_nonvirtual() will remove specified entry from mRecordThreads |
Glenn Kasten | d96c572 | 2012-04-25 13:44:49 -0700 | [diff] [blame] | 244 | closeInput_nonvirtual(mRecordThreads.keyAt(0)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 245 | } |
| 246 | while (!mPlaybackThreads.isEmpty()) { |
Glenn Kasten | c3ae93f | 2012-07-30 10:59:30 -0700 | [diff] [blame] | 247 | // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads |
Glenn Kasten | d96c572 | 2012-04-25 13:44:49 -0700 | [diff] [blame] | 248 | closeOutput_nonvirtual(mPlaybackThreads.keyAt(0)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 249 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 250 | |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 251 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 252 | // no mHardwareLock needed, as there are no other references to this |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 253 | audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice()); |
| 254 | delete mAudioHwDevs.valueAt(i); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 255 | } |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 256 | |
| 257 | // Tell media.log service about any old writers that still need to be unregistered |
| 258 | sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log")); |
| 259 | if (binder != 0) { |
| 260 | sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder)); |
| 261 | for (size_t count = mUnregisteredWriters.size(); count > 0; count--) { |
| 262 | sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory()); |
| 263 | mUnregisteredWriters.pop(); |
| 264 | mediaLogService->unregisterWriter(iMemory); |
| 265 | } |
| 266 | } |
| 267 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 270 | static const char * const audio_interfaces[] = { |
| 271 | AUDIO_HARDWARE_MODULE_ID_PRIMARY, |
| 272 | AUDIO_HARDWARE_MODULE_ID_A2DP, |
| 273 | AUDIO_HARDWARE_MODULE_ID_USB, |
| 274 | }; |
| 275 | #define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0]))) |
| 276 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 277 | AudioHwDevice* AudioFlinger::findSuitableHwDev_l( |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 278 | audio_module_handle_t module, |
| 279 | audio_devices_t devices) |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 280 | { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 281 | // if module is 0, the request comes from an old policy manager and we should load |
| 282 | // well known modules |
| 283 | if (module == 0) { |
| 284 | ALOGW("findSuitableHwDev_l() loading well know audio hw modules"); |
| 285 | for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) { |
| 286 | loadHwModule_l(audio_interfaces[i]); |
| 287 | } |
Eric Laurent | f1c04f9 | 2012-08-28 14:26:53 -0700 | [diff] [blame] | 288 | // then try to find a module supporting the requested device. |
| 289 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 290 | AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i); |
| 291 | audio_hw_device_t *dev = audioHwDevice->hwDevice(); |
| 292 | if ((dev->get_supported_devices != NULL) && |
| 293 | (dev->get_supported_devices(dev) & devices) == devices) |
| 294 | return audioHwDevice; |
| 295 | } |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 296 | } else { |
| 297 | // check a match for the requested module handle |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 298 | AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module); |
| 299 | if (audioHwDevice != NULL) { |
| 300 | return audioHwDevice; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 303 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 304 | return NULL; |
| 305 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 306 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 307 | void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 308 | { |
| 309 | const size_t SIZE = 256; |
| 310 | char buffer[SIZE]; |
| 311 | String8 result; |
| 312 | |
| 313 | result.append("Clients:\n"); |
| 314 | for (size_t i = 0; i < mClients.size(); ++i) { |
Glenn Kasten | 77c1119 | 2012-01-25 14:27:41 -0800 | [diff] [blame] | 315 | sp<Client> client = mClients.valueAt(i).promote(); |
| 316 | if (client != 0) { |
| 317 | snprintf(buffer, SIZE, " pid: %d\n", client->pid()); |
| 318 | result.append(buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 321 | |
Eric Laurent | d1b28d4 | 2013-09-18 18:47:13 -0700 | [diff] [blame] | 322 | result.append("Notification Clients:\n"); |
| 323 | for (size_t i = 0; i < mNotificationClients.size(); ++i) { |
| 324 | snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i)); |
| 325 | result.append(buffer); |
| 326 | } |
| 327 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 328 | result.append("Global session refs:\n"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 329 | result.append(" session pid count\n"); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 330 | for (size_t i = 0; i < mAudioSessionRefs.size(); i++) { |
| 331 | AudioSessionRef *r = mAudioSessionRefs[i]; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 332 | 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] | 333 | result.append(buffer); |
| 334 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 335 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 339 | void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 340 | { |
| 341 | const size_t SIZE = 256; |
| 342 | char buffer[SIZE]; |
| 343 | String8 result; |
Glenn Kasten | a4454b4 | 2012-01-04 11:02:33 -0800 | [diff] [blame] | 344 | hardware_call_state hardwareStatus = mHardwareStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 345 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 346 | snprintf(buffer, SIZE, "Hardware status: %d\n" |
| 347 | "Standby Time mSec: %u\n", |
| 348 | hardwareStatus, |
| 349 | (uint32_t)(mStandbyTimeInNsecs / 1000000)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 350 | result.append(buffer); |
| 351 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 354 | void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 355 | { |
| 356 | const size_t SIZE = 256; |
| 357 | char buffer[SIZE]; |
| 358 | String8 result; |
| 359 | snprintf(buffer, SIZE, "Permission Denial: " |
| 360 | "can't dump AudioFlinger from pid=%d, uid=%d\n", |
| 361 | IPCThreadState::self()->getCallingPid(), |
| 362 | IPCThreadState::self()->getCallingUid()); |
| 363 | result.append(buffer); |
| 364 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 367 | bool AudioFlinger::dumpTryLock(Mutex& mutex) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 368 | { |
| 369 | bool locked = false; |
| 370 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 371 | if (mutex.tryLock() == NO_ERROR) { |
| 372 | locked = true; |
| 373 | break; |
| 374 | } |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 375 | usleep(kDumpLockSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 376 | } |
| 377 | return locked; |
| 378 | } |
| 379 | |
| 380 | status_t AudioFlinger::dump(int fd, const Vector<String16>& args) |
| 381 | { |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 382 | if (!dumpAllowed()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 383 | dumpPermissionDenial(fd, args); |
| 384 | } else { |
| 385 | // get state of hardware lock |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 386 | bool hardwareLocked = dumpTryLock(mHardwareLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 387 | if (!hardwareLocked) { |
| 388 | String8 result(kHardwareLockedString); |
| 389 | write(fd, result.string(), result.size()); |
| 390 | } else { |
| 391 | mHardwareLock.unlock(); |
| 392 | } |
| 393 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 394 | bool locked = dumpTryLock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 395 | |
| 396 | // failed to lock - AudioFlinger is probably deadlocked |
| 397 | if (!locked) { |
| 398 | String8 result(kDeadlockedString); |
| 399 | write(fd, result.string(), result.size()); |
| 400 | } |
| 401 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 402 | bool clientLocked = dumpTryLock(mClientLock); |
| 403 | if (!clientLocked) { |
| 404 | String8 result(kClientLockedString); |
| 405 | write(fd, result.string(), result.size()); |
| 406 | } |
Marco Nelissen | d89eadd | 2014-10-07 13:28:44 -0700 | [diff] [blame] | 407 | |
| 408 | EffectDumpEffects(fd); |
| 409 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 410 | dumpClients(fd, args); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 411 | if (clientLocked) { |
| 412 | mClientLock.unlock(); |
| 413 | } |
| 414 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 415 | dumpInternals(fd, args); |
| 416 | |
| 417 | // dump playback threads |
| 418 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 419 | mPlaybackThreads.valueAt(i)->dump(fd, args); |
| 420 | } |
| 421 | |
| 422 | // dump record threads |
| 423 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 424 | mRecordThreads.valueAt(i)->dump(fd, args); |
| 425 | } |
| 426 | |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 427 | // dump orphan effect chains |
| 428 | if (mOrphanEffectChains.size() != 0) { |
| 429 | write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n")); |
| 430 | for (size_t i = 0; i < mOrphanEffectChains.size(); i++) { |
| 431 | mOrphanEffectChains.valueAt(i)->dump(fd, args); |
| 432 | } |
| 433 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 434 | // dump all hardware devs |
| 435 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 436 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 437 | dev->dump(dev, fd); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 438 | } |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 439 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 440 | #ifdef TEE_SINK |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 441 | // dump the serially shared record tee sink |
| 442 | if (mRecordTeeSource != 0) { |
| 443 | dumpTee(fd, mRecordTeeSource); |
| 444 | } |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 445 | #endif |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 446 | |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 447 | if (locked) { |
| 448 | mLock.unlock(); |
| 449 | } |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 450 | |
| 451 | // append a copy of media.log here by forwarding fd to it, but don't attempt |
| 452 | // to lookup the service if it's not running, as it will block for a second |
| 453 | if (mLogMemoryDealer != 0) { |
| 454 | sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log")); |
| 455 | if (binder != 0) { |
Elliott Hughes | 8b5f642 | 2014-05-22 01:22:06 -0700 | [diff] [blame] | 456 | dprintf(fd, "\nmedia.log:\n"); |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 457 | Vector<String16> args; |
| 458 | binder->dump(fd, args); |
| 459 | } |
| 460 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 461 | } |
| 462 | return NO_ERROR; |
| 463 | } |
| 464 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 465 | sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid) |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 466 | { |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 467 | Mutex::Autolock _cl(mClientLock); |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 468 | // If pid is already in the mClients wp<> map, then use that entry |
| 469 | // (for which promote() is always != 0), otherwise create a new entry and Client. |
| 470 | sp<Client> client = mClients.valueFor(pid).promote(); |
| 471 | if (client == 0) { |
| 472 | client = new Client(this, pid); |
| 473 | mClients.add(pid, client); |
| 474 | } |
| 475 | |
| 476 | return client; |
| 477 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 478 | |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 479 | sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name) |
| 480 | { |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 481 | // If there is no memory allocated for logs, return a dummy writer that does nothing |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 482 | if (mLogMemoryDealer == 0) { |
| 483 | return new NBLog::Writer(); |
| 484 | } |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 485 | sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log")); |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 486 | // Similarly if we can't contact the media.log service, also return a dummy writer |
| 487 | if (binder == 0) { |
| 488 | return new NBLog::Writer(); |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 489 | } |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 490 | sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder)); |
| 491 | sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size)); |
| 492 | // If allocation fails, consult the vector of previously unregistered writers |
| 493 | // and garbage-collect one or more them until an allocation succeeds |
| 494 | if (shared == 0) { |
| 495 | Mutex::Autolock _l(mUnregisteredWritersLock); |
| 496 | for (size_t count = mUnregisteredWriters.size(); count > 0; count--) { |
| 497 | { |
| 498 | // Pick the oldest stale writer to garbage-collect |
| 499 | sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory()); |
| 500 | mUnregisteredWriters.removeAt(0); |
| 501 | mediaLogService->unregisterWriter(iMemory); |
| 502 | // Now the media.log remote reference to IMemory is gone. When our last local |
| 503 | // reference to IMemory also drops to zero at end of this block, |
| 504 | // the IMemory destructor will deallocate the region from mLogMemoryDealer. |
| 505 | } |
| 506 | // Re-attempt the allocation |
| 507 | shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size)); |
| 508 | if (shared != 0) { |
| 509 | goto success; |
| 510 | } |
| 511 | } |
| 512 | // Even after garbage-collecting all old writers, there is still not enough memory, |
| 513 | // so return a dummy writer |
| 514 | return new NBLog::Writer(); |
| 515 | } |
| 516 | success: |
| 517 | mediaLogService->registerWriter(shared, size, name); |
| 518 | return new NBLog::Writer(size, shared); |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer) |
| 522 | { |
Glenn Kasten | 685ef09 | 2013-02-04 08:15:34 -0800 | [diff] [blame] | 523 | if (writer == 0) { |
| 524 | return; |
| 525 | } |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 526 | sp<IMemory> iMemory(writer->getIMemory()); |
| 527 | if (iMemory == 0) { |
| 528 | return; |
| 529 | } |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 530 | // Rather than removing the writer immediately, append it to a queue of old writers to |
| 531 | // be garbage-collected later. This allows us to continue to view old logs for a while. |
| 532 | Mutex::Autolock _l(mUnregisteredWritersLock); |
| 533 | mUnregisteredWriters.push(writer); |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 534 | } |
| 535 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 536 | // IAudioFlinger interface |
| 537 | |
| 538 | |
| 539 | sp<IAudioTrack> AudioFlinger::createTrack( |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 540 | audio_stream_type_t streamType, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 541 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 542 | audio_format_t format, |
Glenn Kasten | 254af18 | 2012-07-03 14:59:05 -0700 | [diff] [blame] | 543 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 544 | size_t *frameCount, |
Glenn Kasten | e0b0717 | 2012-11-06 15:03:34 -0800 | [diff] [blame] | 545 | IAudioFlinger::track_flags_t *flags, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 546 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 547 | audio_io_handle_t output, |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 548 | pid_t tid, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 549 | int *sessionId, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 550 | int clientUid, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 551 | status_t *status) |
| 552 | { |
| 553 | sp<PlaybackThread::Track> track; |
| 554 | sp<TrackHandle> trackHandle; |
| 555 | sp<Client> client; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 556 | status_t lStatus; |
| 557 | int lSessionId; |
| 558 | |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 559 | // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC, |
| 560 | // but if someone uses binder directly they could bypass that and cause us to crash |
| 561 | if (uint32_t(streamType) >= AUDIO_STREAM_CNT) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 562 | ALOGE("createTrack() invalid stream type %d", streamType); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 563 | lStatus = BAD_VALUE; |
| 564 | goto Exit; |
| 565 | } |
| 566 | |
Glenn Kasten | 53b5d09 | 2014-02-05 10:00:23 -0800 | [diff] [blame] | 567 | // further sample rate checks are performed by createTrack_l() depending on the thread type |
| 568 | if (sampleRate == 0) { |
| 569 | ALOGE("createTrack() invalid sample rate %u", sampleRate); |
| 570 | lStatus = BAD_VALUE; |
| 571 | goto Exit; |
| 572 | } |
| 573 | |
| 574 | // further channel mask checks are performed by createTrack_l() depending on the thread type |
| 575 | if (!audio_is_output_channel(channelMask)) { |
| 576 | ALOGE("createTrack() invalid channel mask %#x", channelMask); |
| 577 | lStatus = BAD_VALUE; |
| 578 | goto Exit; |
| 579 | } |
| 580 | |
Glenn Kasten | c4b88a8 | 2014-04-30 16:54:30 -0700 | [diff] [blame] | 581 | // further format checks are performed by createTrack_l() depending on the thread type |
| 582 | if (!audio_is_valid_format(format)) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 583 | ALOGE("createTrack() invalid format %#x", format); |
Glenn Kasten | 60a8392 | 2012-06-21 12:56:37 -0700 | [diff] [blame] | 584 | lStatus = BAD_VALUE; |
| 585 | goto Exit; |
| 586 | } |
| 587 | |
Glenn Kasten | 663c224 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 588 | if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) { |
| 589 | ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()"); |
| 590 | lStatus = BAD_VALUE; |
| 591 | goto Exit; |
| 592 | } |
| 593 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 594 | { |
| 595 | Mutex::Autolock _l(mLock); |
| 596 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 597 | if (thread == NULL) { |
Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 598 | ALOGE("no playback thread found for output handle %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 599 | lStatus = BAD_VALUE; |
| 600 | goto Exit; |
| 601 | } |
| 602 | |
Glenn Kasten | 8d6cc84 | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 603 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 604 | client = registerPid(pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 605 | |
Glenn Kasten | e848bd9 | 2014-03-13 15:00:32 -0700 | [diff] [blame] | 606 | PlaybackThread *effectThread = NULL; |
Glenn Kasten | aea7ea0 | 2013-06-26 09:25:47 -0700 | [diff] [blame] | 607 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) { |
Glenn Kasten | 570f633 | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 608 | lSessionId = *sessionId; |
Eric Laurent | f436fdc | 2012-05-24 11:07:14 -0700 | [diff] [blame] | 609 | // check if an effect chain with the same session ID is present on another |
| 610 | // output thread and move it here. |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 611 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 612 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 613 | if (mPlaybackThreads.keyAt(i) != output) { |
Glenn Kasten | 570f633 | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 614 | uint32_t sessions = t->hasAudioSession(lSessionId); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 615 | if (sessions & PlaybackThread::EFFECT_SESSION) { |
| 616 | effectThread = t.get(); |
Eric Laurent | f436fdc | 2012-05-24 11:07:14 -0700 | [diff] [blame] | 617 | break; |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 618 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 619 | } |
| 620 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 621 | } else { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 622 | // if no audio session id is provided, create one here |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 623 | lSessionId = nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 624 | if (sessionId != NULL) { |
| 625 | *sessionId = lSessionId; |
| 626 | } |
| 627 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 628 | ALOGV("createTrack() lSessionId: %d", lSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 629 | |
| 630 | track = thread->createTrack_l(client, streamType, sampleRate, format, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 631 | channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 632 | LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0)); |
Glenn Kasten | 2fc1473 | 2013-08-05 14:58:14 -0700 | [diff] [blame] | 633 | // 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] | 634 | |
| 635 | // move effect chain to this output thread if an effect on same session was waiting |
| 636 | // for a track to be created |
| 637 | if (lStatus == NO_ERROR && effectThread != NULL) { |
Glenn Kasten | 2fc1473 | 2013-08-05 14:58:14 -0700 | [diff] [blame] | 638 | // no risk of deadlock because AudioFlinger::mLock is held |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 639 | Mutex::Autolock _dl(thread->mLock); |
| 640 | Mutex::Autolock _sl(effectThread->mLock); |
| 641 | moveEffectChain_l(lSessionId, effectThread, thread, true); |
| 642 | } |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 643 | |
| 644 | // Look for sync events awaiting for a session to be used. |
Mark Salyzyn | 3ab368e | 2014-04-15 14:55:53 -0700 | [diff] [blame] | 645 | for (size_t i = 0; i < mPendingSyncEvents.size(); i++) { |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 646 | if (mPendingSyncEvents[i]->triggerSession() == lSessionId) { |
| 647 | if (thread->isValidSyncEvent(mPendingSyncEvents[i])) { |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 648 | if (lStatus == NO_ERROR) { |
Glenn Kasten | d23eedc | 2012-08-02 13:35:47 -0700 | [diff] [blame] | 649 | (void) track->setSyncEvent(mPendingSyncEvents[i]); |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 650 | } else { |
| 651 | mPendingSyncEvents[i]->cancel(); |
| 652 | } |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 653 | mPendingSyncEvents.removeAt(i); |
| 654 | i--; |
| 655 | } |
| 656 | } |
| 657 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 658 | |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 659 | setAudioHwSyncForSession_l(thread, (audio_session_t)lSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 660 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 661 | |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 662 | if (lStatus != NO_ERROR) { |
| 663 | // 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] | 664 | // Client destructor is called by the TrackBase destructor with mClientLock held |
Eric Laurent | fe1a94e | 2014-05-26 16:03:08 -0700 | [diff] [blame] | 665 | // Don't hold mClientLock when releasing the reference on the track as the |
| 666 | // destructor will acquire it. |
| 667 | { |
| 668 | Mutex::Autolock _cl(mClientLock); |
| 669 | client.clear(); |
| 670 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 671 | track.clear(); |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 672 | goto Exit; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 675 | // return handle to client |
| 676 | trackHandle = new TrackHandle(track); |
| 677 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 678 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 679 | *status = lStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 680 | return trackHandle; |
| 681 | } |
| 682 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 683 | uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 684 | { |
| 685 | Mutex::Autolock _l(mLock); |
| 686 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 687 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 688 | ALOGW("sampleRate() unknown thread %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 689 | return 0; |
| 690 | } |
| 691 | return thread->sampleRate(); |
| 692 | } |
| 693 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 694 | audio_format_t AudioFlinger::format(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 695 | { |
| 696 | Mutex::Autolock _l(mLock); |
| 697 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 698 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 699 | ALOGW("format() unknown thread %d", output); |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 700 | return AUDIO_FORMAT_INVALID; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 701 | } |
| 702 | return thread->format(); |
| 703 | } |
| 704 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 705 | size_t AudioFlinger::frameCount(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 706 | { |
| 707 | Mutex::Autolock _l(mLock); |
| 708 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 709 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 710 | ALOGW("frameCount() unknown thread %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 711 | return 0; |
| 712 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 713 | // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers; |
| 714 | // should examine all callers and fix them to handle smaller counts |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 715 | return thread->frameCount(); |
| 716 | } |
| 717 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 718 | uint32_t AudioFlinger::latency(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 719 | { |
| 720 | Mutex::Autolock _l(mLock); |
| 721 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 722 | if (thread == NULL) { |
Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 723 | ALOGW("latency(): no playback thread found for output handle %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 724 | return 0; |
| 725 | } |
| 726 | return thread->latency(); |
| 727 | } |
| 728 | |
| 729 | status_t AudioFlinger::setMasterVolume(float value) |
| 730 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 731 | status_t ret = initCheck(); |
| 732 | if (ret != NO_ERROR) { |
| 733 | return ret; |
| 734 | } |
| 735 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 736 | // check calling permissions |
| 737 | if (!settingsAllowed()) { |
| 738 | return PERMISSION_DENIED; |
| 739 | } |
| 740 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 741 | Mutex::Autolock _l(mLock); |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 742 | mMasterVolume = value; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 743 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 744 | // Set master volume in the HALs which support it. |
| 745 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 746 | AutoMutex lock(mHardwareLock); |
| 747 | AudioHwDevice *dev = mAudioHwDevs.valueAt(i); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 748 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 749 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
| 750 | if (dev->canSetMasterVolume()) { |
| 751 | dev->hwDevice()->set_master_volume(dev->hwDevice(), value); |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 752 | } |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 753 | mHardwareStatus = AUDIO_HW_IDLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 754 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 755 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 756 | // Now set the master volume in each playback thread. Playback threads |
| 757 | // assigned to HALs which do not have master volume support will apply |
| 758 | // master volume during the mix operation. Threads with HALs which do |
| 759 | // support master volume will simply ignore the setting. |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 760 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 761 | mPlaybackThreads.valueAt(i)->setMasterVolume(value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 762 | |
| 763 | return NO_ERROR; |
| 764 | } |
| 765 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 766 | status_t AudioFlinger::setMode(audio_mode_t mode) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 767 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 768 | status_t ret = initCheck(); |
| 769 | if (ret != NO_ERROR) { |
| 770 | return ret; |
| 771 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 772 | |
| 773 | // check calling permissions |
| 774 | if (!settingsAllowed()) { |
| 775 | return PERMISSION_DENIED; |
| 776 | } |
Glenn Kasten | 930f4ca | 2012-01-06 16:47:31 -0800 | [diff] [blame] | 777 | if (uint32_t(mode) >= AUDIO_MODE_CNT) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 778 | ALOGW("Illegal value: setMode(%d)", mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 779 | return BAD_VALUE; |
| 780 | } |
| 781 | |
| 782 | { // scope for the lock |
| 783 | AutoMutex lock(mHardwareLock); |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 784 | audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 785 | mHardwareStatus = AUDIO_HW_SET_MODE; |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 786 | ret = dev->set_mode(dev, mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 787 | mHardwareStatus = AUDIO_HW_IDLE; |
| 788 | } |
| 789 | |
| 790 | if (NO_ERROR == ret) { |
| 791 | Mutex::Autolock _l(mLock); |
| 792 | mMode = mode; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 793 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 794 | mPlaybackThreads.valueAt(i)->setMode(mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | return ret; |
| 798 | } |
| 799 | |
| 800 | status_t AudioFlinger::setMicMute(bool state) |
| 801 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 802 | status_t ret = initCheck(); |
| 803 | if (ret != NO_ERROR) { |
| 804 | return ret; |
| 805 | } |
| 806 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 807 | // check calling permissions |
| 808 | if (!settingsAllowed()) { |
| 809 | return PERMISSION_DENIED; |
| 810 | } |
| 811 | |
| 812 | AutoMutex lock(mHardwareLock); |
| 813 | mHardwareStatus = AUDIO_HW_SET_MIC_MUTE; |
Eric Laurent | 2f035f5 | 2014-09-14 12:11:52 -0700 | [diff] [blame] | 814 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 815 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
| 816 | status_t result = dev->set_mic_mute(dev, state); |
| 817 | if (result != NO_ERROR) { |
| 818 | ret = result; |
| 819 | } |
| 820 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 821 | mHardwareStatus = AUDIO_HW_IDLE; |
| 822 | return ret; |
| 823 | } |
| 824 | |
| 825 | bool AudioFlinger::getMicMute() const |
| 826 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 827 | status_t ret = initCheck(); |
| 828 | if (ret != NO_ERROR) { |
| 829 | return false; |
| 830 | } |
Ricardo Garcia | 3e91b5d | 2015-02-19 10:54:52 -0800 | [diff] [blame] | 831 | bool mute = true; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 832 | bool state = AUDIO_MODE_INVALID; |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 833 | AutoMutex lock(mHardwareLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 834 | mHardwareStatus = AUDIO_HW_GET_MIC_MUTE; |
Ricardo Garcia | 3e91b5d | 2015-02-19 10:54:52 -0800 | [diff] [blame] | 835 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 836 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
| 837 | status_t result = dev->get_mic_mute(dev, &state); |
| 838 | if (result == NO_ERROR) { |
| 839 | mute = mute && state; |
| 840 | } |
| 841 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 842 | mHardwareStatus = AUDIO_HW_IDLE; |
Ricardo Garcia | 3e91b5d | 2015-02-19 10:54:52 -0800 | [diff] [blame] | 843 | |
| 844 | return mute; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | status_t AudioFlinger::setMasterMute(bool muted) |
| 848 | { |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 849 | status_t ret = initCheck(); |
| 850 | if (ret != NO_ERROR) { |
| 851 | return ret; |
| 852 | } |
| 853 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 854 | // check calling permissions |
| 855 | if (!settingsAllowed()) { |
| 856 | return PERMISSION_DENIED; |
| 857 | } |
| 858 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 859 | Mutex::Autolock _l(mLock); |
| 860 | mMasterMute = muted; |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 861 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 862 | // Set master mute in the HALs which support it. |
| 863 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 864 | AutoMutex lock(mHardwareLock); |
| 865 | AudioHwDevice *dev = mAudioHwDevs.valueAt(i); |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 866 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 867 | mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE; |
| 868 | if (dev->canSetMasterMute()) { |
| 869 | dev->hwDevice()->set_master_mute(dev->hwDevice(), muted); |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 870 | } |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 871 | mHardwareStatus = AUDIO_HW_IDLE; |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 872 | } |
| 873 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 874 | // Now set the master mute in each playback thread. Playback threads |
| 875 | // assigned to HALs which do not have master mute support will apply master |
| 876 | // mute during the mix operation. Threads with HALs which do support master |
| 877 | // mute will simply ignore the setting. |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 878 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 879 | mPlaybackThreads.valueAt(i)->setMasterMute(muted); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 880 | |
| 881 | return NO_ERROR; |
| 882 | } |
| 883 | |
| 884 | float AudioFlinger::masterVolume() const |
| 885 | { |
Glenn Kasten | 9806710 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 886 | Mutex::Autolock _l(mLock); |
| 887 | return masterVolume_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | bool AudioFlinger::masterMute() const |
| 891 | { |
Glenn Kasten | 9806710 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 892 | Mutex::Autolock _l(mLock); |
| 893 | return masterMute_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 894 | } |
| 895 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 896 | float AudioFlinger::masterVolume_l() const |
| 897 | { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 898 | return mMasterVolume; |
| 899 | } |
| 900 | |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 901 | bool AudioFlinger::masterMute_l() const |
| 902 | { |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 903 | return mMasterMute; |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 904 | } |
| 905 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 906 | status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const |
| 907 | { |
| 908 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
| 909 | ALOGW("setStreamVolume() invalid stream %d", stream); |
| 910 | return BAD_VALUE; |
| 911 | } |
| 912 | pid_t caller = IPCThreadState::self()->getCallingPid(); |
| 913 | if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) { |
| 914 | ALOGW("setStreamVolume() pid %d cannot use internal stream type %d", caller, stream); |
| 915 | return PERMISSION_DENIED; |
| 916 | } |
| 917 | |
| 918 | return NO_ERROR; |
| 919 | } |
| 920 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 921 | status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value, |
| 922 | audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 923 | { |
| 924 | // check calling permissions |
| 925 | if (!settingsAllowed()) { |
| 926 | return PERMISSION_DENIED; |
| 927 | } |
| 928 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 929 | status_t status = checkStreamType(stream); |
| 930 | if (status != NO_ERROR) { |
| 931 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 932 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 933 | 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] | 934 | |
| 935 | AutoMutex lock(mLock); |
| 936 | PlaybackThread *thread = NULL; |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 937 | if (output != AUDIO_IO_HANDLE_NONE) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 938 | thread = checkPlaybackThread_l(output); |
| 939 | if (thread == NULL) { |
| 940 | return BAD_VALUE; |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | mStreamTypes[stream].volume = value; |
| 945 | |
| 946 | if (thread == NULL) { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 947 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 948 | mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 949 | } |
| 950 | } else { |
| 951 | thread->setStreamVolume(stream, value); |
| 952 | } |
| 953 | |
| 954 | return NO_ERROR; |
| 955 | } |
| 956 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 957 | status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 958 | { |
| 959 | // check calling permissions |
| 960 | if (!settingsAllowed()) { |
| 961 | return PERMISSION_DENIED; |
| 962 | } |
| 963 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 964 | status_t status = checkStreamType(stream); |
| 965 | if (status != NO_ERROR) { |
| 966 | return status; |
| 967 | } |
| 968 | ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH"); |
| 969 | |
| 970 | if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 971 | ALOGE("setStreamMute() invalid stream %d", stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 972 | return BAD_VALUE; |
| 973 | } |
| 974 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 975 | AutoMutex lock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 976 | mStreamTypes[stream].mute = muted; |
Mark Salyzyn | 3ab368e | 2014-04-15 14:55:53 -0700 | [diff] [blame] | 977 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 978 | mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 979 | |
| 980 | return NO_ERROR; |
| 981 | } |
| 982 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 983 | 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] | 984 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 985 | status_t status = checkStreamType(stream); |
| 986 | if (status != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 987 | return 0.0f; |
| 988 | } |
| 989 | |
| 990 | AutoMutex lock(mLock); |
| 991 | float volume; |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 992 | if (output != AUDIO_IO_HANDLE_NONE) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 993 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 994 | if (thread == NULL) { |
| 995 | return 0.0f; |
| 996 | } |
| 997 | volume = thread->streamVolume(stream); |
| 998 | } else { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 999 | volume = streamVolume_l(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | return volume; |
| 1003 | } |
| 1004 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1005 | bool AudioFlinger::streamMute(audio_stream_type_t stream) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1006 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1007 | status_t status = checkStreamType(stream); |
| 1008 | if (status != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1009 | return true; |
| 1010 | } |
| 1011 | |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1012 | AutoMutex lock(mLock); |
| 1013 | return streamMute_l(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 1016 | |
| 1017 | void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs) |
| 1018 | { |
| 1019 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 1020 | mRecordThreads.valueAt(i)->setParameters(keyValuePairs); |
| 1021 | } |
| 1022 | } |
| 1023 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1024 | status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1025 | { |
Glenn Kasten | 827e5f1 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 1026 | ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d", |
| 1027 | ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1028 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1029 | // check calling permissions |
| 1030 | if (!settingsAllowed()) { |
| 1031 | return PERMISSION_DENIED; |
| 1032 | } |
| 1033 | |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 1034 | // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface |
| 1035 | if (ioHandle == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1036 | Mutex::Autolock _l(mLock); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1037 | status_t final_result = NO_ERROR; |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1038 | { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1039 | AutoMutex lock(mHardwareLock); |
| 1040 | mHardwareStatus = AUDIO_HW_SET_PARAMETER; |
| 1041 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 1042 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
| 1043 | status_t result = dev->set_parameters(dev, keyValuePairs.string()); |
| 1044 | final_result = result ?: final_result; |
| 1045 | } |
| 1046 | mHardwareStatus = AUDIO_HW_IDLE; |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1047 | } |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1048 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 1049 | AudioParameter param = AudioParameter(keyValuePairs); |
| 1050 | String8 value; |
| 1051 | if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) { |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 1052 | bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF); |
| 1053 | if (mBtNrecIsOff != btNrecIsOff) { |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1054 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 1055 | sp<RecordThread> thread = mRecordThreads.valueAt(i); |
Eric Laurent | f1c04f9 | 2012-08-28 14:26:53 -0700 | [diff] [blame] | 1056 | audio_devices_t device = thread->inDevice(); |
Glenn Kasten | 510a3d6 | 2012-07-16 14:24:34 -0700 | [diff] [blame] | 1057 | bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff; |
| 1058 | // collect all of the thread's session IDs |
| 1059 | KeyedVector<int, bool> ids = thread->sessionIds(); |
| 1060 | // suspend effects associated with those session IDs |
| 1061 | for (size_t j = 0; j < ids.size(); ++j) { |
| 1062 | int sessionId = ids.keyAt(j); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1063 | thread->setEffectSuspended(FX_IID_AEC, |
| 1064 | suspend, |
Glenn Kasten | 510a3d6 | 2012-07-16 14:24:34 -0700 | [diff] [blame] | 1065 | sessionId); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1066 | thread->setEffectSuspended(FX_IID_NS, |
| 1067 | suspend, |
Glenn Kasten | 510a3d6 | 2012-07-16 14:24:34 -0700 | [diff] [blame] | 1068 | sessionId); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1069 | } |
| 1070 | } |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 1071 | mBtNrecIsOff = btNrecIsOff; |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1072 | } |
| 1073 | } |
Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 1074 | String8 screenState; |
| 1075 | if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) { |
| 1076 | bool isOff = screenState == "off"; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1077 | if (isOff != (AudioFlinger::mScreenState & 1)) { |
| 1078 | AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff; |
Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 1079 | } |
| 1080 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1081 | return final_result; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | // hold a strong ref on thread in case closeOutput() or closeInput() is called |
| 1085 | // and the thread is exited once the lock is released |
| 1086 | sp<ThreadBase> thread; |
| 1087 | { |
| 1088 | Mutex::Autolock _l(mLock); |
| 1089 | thread = checkPlaybackThread_l(ioHandle); |
Glenn Kasten | d5903ec | 2012-03-18 10:33:27 -0700 | [diff] [blame] | 1090 | if (thread == 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1091 | thread = checkRecordThread_l(ioHandle); |
Glenn Kasten | 7fc9a6f | 2012-01-10 10:46:34 -0800 | [diff] [blame] | 1092 | } else if (thread == primaryPlaybackThread_l()) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1093 | // indicate output device change to all input threads for pre processing |
| 1094 | AudioParameter param = AudioParameter(keyValuePairs); |
| 1095 | int value; |
Eric Laurent | 89d94e7 | 2012-03-16 20:37:59 -0700 | [diff] [blame] | 1096 | if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) && |
| 1097 | (value != 0)) { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 1098 | broacastParametersToRecordThreads_l(keyValuePairs); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1099 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1100 | } |
| 1101 | } |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 1102 | if (thread != 0) { |
| 1103 | return thread->setParameters(keyValuePairs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1104 | } |
| 1105 | return BAD_VALUE; |
| 1106 | } |
| 1107 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1108 | String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1109 | { |
Glenn Kasten | 827e5f1 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 1110 | ALOGVV("getParameters() io %d, keys %s, calling pid %d", |
| 1111 | ioHandle, keys.string(), IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1112 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1113 | Mutex::Autolock _l(mLock); |
| 1114 | |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 1115 | if (ioHandle == AUDIO_IO_HANDLE_NONE) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1116 | String8 out_s8; |
| 1117 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1118 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1119 | char *s; |
| 1120 | { |
| 1121 | AutoMutex lock(mHardwareLock); |
| 1122 | mHardwareStatus = AUDIO_HW_GET_PARAMETER; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1123 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1124 | s = dev->get_parameters(dev, keys.string()); |
| 1125 | mHardwareStatus = AUDIO_HW_IDLE; |
| 1126 | } |
John Grossman | ef7740b | 2012-02-09 11:28:36 -0800 | [diff] [blame] | 1127 | out_s8 += String8(s ? s : ""); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1128 | free(s); |
| 1129 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1130 | return out_s8; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1133 | PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle); |
| 1134 | if (playbackThread != NULL) { |
| 1135 | return playbackThread->getParameters(keys); |
| 1136 | } |
| 1137 | RecordThread *recordThread = checkRecordThread_l(ioHandle); |
| 1138 | if (recordThread != NULL) { |
| 1139 | return recordThread->getParameters(keys); |
| 1140 | } |
| 1141 | return String8(""); |
| 1142 | } |
| 1143 | |
Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 1144 | size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format, |
| 1145 | audio_channel_mask_t channelMask) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1146 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1147 | status_t ret = initCheck(); |
| 1148 | if (ret != NO_ERROR) { |
| 1149 | return 0; |
| 1150 | } |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 1151 | if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) { |
| 1152 | return 0; |
| 1153 | } |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1154 | |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 1155 | AutoMutex lock(mHardwareLock); |
| 1156 | mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE; |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 1157 | audio_config_t config, proposed; |
| 1158 | memset(&proposed, 0, sizeof(proposed)); |
| 1159 | proposed.sample_rate = sampleRate; |
| 1160 | proposed.channel_mask = channelMask; |
| 1161 | proposed.format = format; |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1162 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1163 | audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice(); |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 1164 | size_t frames; |
| 1165 | for (;;) { |
| 1166 | // Note: config is currently a const parameter for get_input_buffer_size() |
| 1167 | // but we use a copy from proposed in case config changes from the call. |
| 1168 | config = proposed; |
| 1169 | frames = dev->get_input_buffer_size(dev, &config); |
| 1170 | if (frames != 0) { |
| 1171 | break; // hal success, config is the result |
| 1172 | } |
| 1173 | // change one parameter of the configuration each iteration to a more "common" value |
| 1174 | // to see if the device will support it. |
| 1175 | if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) { |
| 1176 | proposed.format = AUDIO_FORMAT_PCM_16_BIT; |
| 1177 | } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as |
| 1178 | proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw? |
| 1179 | } else { |
| 1180 | ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, " |
| 1181 | "format %#x, channelMask 0x%X", |
| 1182 | sampleRate, format, channelMask); |
| 1183 | break; // retries failed, break out of loop with frames == 0. |
| 1184 | } |
| 1185 | } |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 1186 | mHardwareStatus = AUDIO_HW_IDLE; |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 1187 | if (frames > 0 && config.sample_rate != sampleRate) { |
| 1188 | frames = destinationFramesPossible(frames, sampleRate, config.sample_rate); |
| 1189 | } |
| 1190 | return frames; // may be converted to bytes at the Java level. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 1193 | uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1194 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1195 | Mutex::Autolock _l(mLock); |
| 1196 | |
| 1197 | RecordThread *recordThread = checkRecordThread_l(ioHandle); |
| 1198 | if (recordThread != NULL) { |
| 1199 | return recordThread->getInputFramesLost(); |
| 1200 | } |
| 1201 | return 0; |
| 1202 | } |
| 1203 | |
| 1204 | status_t AudioFlinger::setVoiceVolume(float value) |
| 1205 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1206 | status_t ret = initCheck(); |
| 1207 | if (ret != NO_ERROR) { |
| 1208 | return ret; |
| 1209 | } |
| 1210 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1211 | // check calling permissions |
| 1212 | if (!settingsAllowed()) { |
| 1213 | return PERMISSION_DENIED; |
| 1214 | } |
| 1215 | |
| 1216 | AutoMutex lock(mHardwareLock); |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1217 | audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice(); |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1218 | mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME; |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1219 | ret = dev->set_voice_volume(dev, value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1220 | mHardwareStatus = AUDIO_HW_IDLE; |
| 1221 | |
| 1222 | return ret; |
| 1223 | } |
| 1224 | |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1225 | status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1226 | audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1227 | { |
| 1228 | status_t status; |
| 1229 | |
| 1230 | Mutex::Autolock _l(mLock); |
| 1231 | |
| 1232 | PlaybackThread *playbackThread = checkPlaybackThread_l(output); |
| 1233 | if (playbackThread != NULL) { |
| 1234 | return playbackThread->getRenderPosition(halFrames, dspFrames); |
| 1235 | } |
| 1236 | |
| 1237 | return BAD_VALUE; |
| 1238 | } |
| 1239 | |
| 1240 | void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client) |
| 1241 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1242 | Mutex::Autolock _l(mLock); |
Eric Laurent | 44622db | 2014-08-01 19:00:33 -0700 | [diff] [blame] | 1243 | if (client == 0) { |
| 1244 | return; |
| 1245 | } |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1246 | bool clientAdded = false; |
| 1247 | { |
| 1248 | Mutex::Autolock _cl(mClientLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1249 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1250 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
| 1251 | if (mNotificationClients.indexOfKey(pid) < 0) { |
| 1252 | sp<NotificationClient> notificationClient = new NotificationClient(this, |
| 1253 | client, |
| 1254 | pid); |
| 1255 | ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1256 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1257 | mNotificationClients.add(pid, notificationClient); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1258 | |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 1259 | sp<IBinder> binder = IInterface::asBinder(client); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1260 | binder->linkToDeath(notificationClient); |
| 1261 | clientAdded = true; |
| 1262 | } |
| 1263 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1264 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1265 | // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the |
Eric Laurent | fe1a94e | 2014-05-26 16:03:08 -0700 | [diff] [blame] | 1266 | // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock. |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1267 | if (clientAdded) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1268 | // the config change is always sent from playback or record threads to avoid deadlock |
| 1269 | // with AudioSystem::gLock |
| 1270 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | 896adcd | 2012-09-13 11:18:23 -0700 | [diff] [blame] | 1271 | mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
Eric Laurent | 896adcd | 2012-09-13 11:18:23 -0700 | [diff] [blame] | 1275 | mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1276 | } |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | void AudioFlinger::removeNotificationClient(pid_t pid) |
| 1281 | { |
| 1282 | Mutex::Autolock _l(mLock); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1283 | { |
| 1284 | Mutex::Autolock _cl(mClientLock); |
| 1285 | mNotificationClients.removeItem(pid); |
| 1286 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1287 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1288 | ALOGV("%d died, releasing its sessions", pid); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1289 | size_t num = mAudioSessionRefs.size(); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1290 | bool removed = false; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1291 | for (size_t i = 0; i< num; ) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1292 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 1293 | ALOGV(" pid %d @ %d", ref->mPid, i); |
| 1294 | if (ref->mPid == pid) { |
| 1295 | ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1296 | mAudioSessionRefs.removeAt(i); |
| 1297 | delete ref; |
| 1298 | removed = true; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1299 | num--; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1300 | } else { |
| 1301 | i++; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1302 | } |
| 1303 | } |
| 1304 | if (removed) { |
| 1305 | purgeStaleEffects_l(); |
| 1306 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1309 | void AudioFlinger::audioConfigChanged(int event, audio_io_handle_t ioHandle, const void *param2) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1310 | { |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1311 | Mutex::Autolock _l(mClientLock); |
| 1312 | size_t size = mNotificationClients.size(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1313 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1314 | mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 1315 | ioHandle, |
| 1316 | param2); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1317 | } |
| 1318 | } |
| 1319 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1320 | // removeClient_l() must be called with AudioFlinger::mClientLock held |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1321 | void AudioFlinger::removeClient_l(pid_t pid) |
| 1322 | { |
Glenn Kasten | 827e5f1 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 1323 | ALOGV("removeClient_l() pid %d, calling pid %d", pid, |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 1324 | IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1325 | mClients.removeItem(pid); |
| 1326 | } |
| 1327 | |
Eric Laurent | 717e128 | 2012-06-29 16:36:52 -0700 | [diff] [blame] | 1328 | // getEffectThread_l() must be called with AudioFlinger::mLock held |
| 1329 | sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId) |
| 1330 | { |
| 1331 | sp<PlaybackThread> thread; |
| 1332 | |
| 1333 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 1334 | if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) { |
| 1335 | ALOG_ASSERT(thread == 0); |
| 1336 | thread = mPlaybackThreads.valueAt(i); |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | return thread; |
| 1341 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1342 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1343 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1344 | |
| 1345 | // ---------------------------------------------------------------------------- |
| 1346 | |
| 1347 | AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid) |
| 1348 | : RefBase(), |
| 1349 | mAudioFlinger(audioFlinger), |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 1350 | // FIXME should be a "k" constant not hard-coded, in .h or ro. property, see 4 lines below |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1351 | mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1352 | mPid(pid), |
| 1353 | mTimedTrackCount(0) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1354 | { |
| 1355 | // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer |
| 1356 | } |
| 1357 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1358 | // Client destructor must be called with AudioFlinger::mClientLock held |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1359 | AudioFlinger::Client::~Client() |
| 1360 | { |
| 1361 | mAudioFlinger->removeClient_l(mPid); |
| 1362 | } |
| 1363 | |
Glenn Kasten | 435dbe6 | 2012-01-30 10:15:48 -0800 | [diff] [blame] | 1364 | sp<MemoryDealer> AudioFlinger::Client::heap() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1365 | { |
| 1366 | return mMemoryDealer; |
| 1367 | } |
| 1368 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1369 | // Reserve one of the limited slots for a timed audio track associated |
| 1370 | // with this client |
| 1371 | bool AudioFlinger::Client::reserveTimedTrack() |
| 1372 | { |
| 1373 | const int kMaxTimedTracksPerClient = 4; |
| 1374 | |
| 1375 | Mutex::Autolock _l(mTimedTrackLock); |
| 1376 | |
| 1377 | if (mTimedTrackCount >= kMaxTimedTracksPerClient) { |
| 1378 | ALOGW("can not create timed track - pid %d has exceeded the limit", |
| 1379 | mPid); |
| 1380 | return false; |
| 1381 | } |
| 1382 | |
| 1383 | mTimedTrackCount++; |
| 1384 | return true; |
| 1385 | } |
| 1386 | |
| 1387 | // Release a slot for a timed audio track |
| 1388 | void AudioFlinger::Client::releaseTimedTrack() |
| 1389 | { |
| 1390 | Mutex::Autolock _l(mTimedTrackLock); |
| 1391 | mTimedTrackCount--; |
| 1392 | } |
| 1393 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1394 | // ---------------------------------------------------------------------------- |
| 1395 | |
| 1396 | AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger, |
| 1397 | const sp<IAudioFlingerClient>& client, |
| 1398 | pid_t pid) |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 1399 | : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1400 | { |
| 1401 | } |
| 1402 | |
| 1403 | AudioFlinger::NotificationClient::~NotificationClient() |
| 1404 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1405 | } |
| 1406 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1407 | void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1408 | { |
| 1409 | sp<NotificationClient> keep(this); |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 1410 | mAudioFlinger->removeNotificationClient(mPid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1413 | |
| 1414 | // ---------------------------------------------------------------------------- |
| 1415 | |
Jeff Brown | 893a564 | 2013-08-16 20:19:26 -0700 | [diff] [blame] | 1416 | static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) { |
| 1417 | return audio_is_remote_submix_device(inDevice); |
| 1418 | } |
| 1419 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1420 | sp<IAudioRecord> AudioFlinger::openRecord( |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1421 | audio_io_handle_t input, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1422 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 1423 | audio_format_t format, |
Glenn Kasten | 254af18 | 2012-07-03 14:59:05 -0700 | [diff] [blame] | 1424 | audio_channel_mask_t channelMask, |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 1425 | const String16& opPackageName, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1426 | size_t *frameCount, |
Glenn Kasten | eeca326 | 2013-07-31 16:12:48 -0700 | [diff] [blame] | 1427 | IAudioFlinger::track_flags_t *flags, |
Glenn Kasten | 1879fff | 2012-07-11 15:36:59 -0700 | [diff] [blame] | 1428 | pid_t tid, |
Jean-Michel Trivi | 4cb6683 | 2015-05-01 18:34:17 -0700 | [diff] [blame^] | 1429 | int clientUid, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1430 | int *sessionId, |
Glenn Kasten | 7df8c0b | 2014-07-03 12:23:29 -0700 | [diff] [blame] | 1431 | size_t *notificationFrames, |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 1432 | sp<IMemory>& cblk, |
| 1433 | sp<IMemory>& buffers, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1434 | status_t *status) |
| 1435 | { |
| 1436 | sp<RecordThread::RecordTrack> recordTrack; |
| 1437 | sp<RecordHandle> recordHandle; |
| 1438 | sp<Client> client; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1439 | status_t lStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1440 | int lSessionId; |
| 1441 | |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 1442 | cblk.clear(); |
| 1443 | buffers.clear(); |
| 1444 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1445 | // check calling permissions |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 1446 | if (!recordingAllowed(opPackageName)) { |
Glenn Kasten | e93cf2c | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 1447 | ALOGE("openRecord() permission denied: recording not allowed"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1448 | lStatus = PERMISSION_DENIED; |
| 1449 | goto Exit; |
| 1450 | } |
| 1451 | |
Glenn Kasten | 53b5d09 | 2014-02-05 10:00:23 -0800 | [diff] [blame] | 1452 | // further sample rate checks are performed by createRecordTrack_l() |
| 1453 | if (sampleRate == 0) { |
| 1454 | ALOGE("openRecord() invalid sample rate %u", sampleRate); |
| 1455 | lStatus = BAD_VALUE; |
| 1456 | goto Exit; |
| 1457 | } |
| 1458 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 1459 | // we don't yet support anything other than linear PCM |
| 1460 | if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1461 | ALOGE("openRecord() invalid format %#x", format); |
Glenn Kasten | 291bb6d | 2013-07-16 17:23:39 -0700 | [diff] [blame] | 1462 | lStatus = BAD_VALUE; |
| 1463 | goto Exit; |
| 1464 | } |
| 1465 | |
Glenn Kasten | 53b5d09 | 2014-02-05 10:00:23 -0800 | [diff] [blame] | 1466 | // further channel mask checks are performed by createRecordTrack_l() |
| 1467 | if (!audio_is_input_channel(channelMask)) { |
| 1468 | ALOGE("openRecord() invalid channel mask %#x", channelMask); |
| 1469 | lStatus = BAD_VALUE; |
| 1470 | goto Exit; |
| 1471 | } |
| 1472 | |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 1473 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1474 | Mutex::Autolock _l(mLock); |
Glenn Kasten | e848bd9 | 2014-03-13 15:00:32 -0700 | [diff] [blame] | 1475 | RecordThread *thread = checkRecordThread_l(input); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1476 | if (thread == NULL) { |
Glenn Kasten | e93cf2c | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 1477 | ALOGE("openRecord() checkRecordThread_l failed"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1478 | lStatus = BAD_VALUE; |
| 1479 | goto Exit; |
| 1480 | } |
| 1481 | |
Glenn Kasten | 8d6cc84 | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 1482 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1483 | client = registerPid(pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1484 | |
Glenn Kasten | aea7ea0 | 2013-06-26 09:25:47 -0700 | [diff] [blame] | 1485 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1486 | lSessionId = *sessionId; |
| 1487 | } else { |
Glenn Kasten | 570f633 | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 1488 | // if no audio session id is provided, create one here |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1489 | lSessionId = nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1490 | if (sessionId != NULL) { |
| 1491 | *sessionId = lSessionId; |
| 1492 | } |
| 1493 | } |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 1494 | ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input); |
Glenn Kasten | 570f633 | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 1495 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1496 | // TODO: the uid should be passed in as a parameter to openRecord |
Glenn Kasten | 1879fff | 2012-07-11 15:36:59 -0700 | [diff] [blame] | 1497 | recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask, |
Glenn Kasten | 7df8c0b | 2014-07-03 12:23:29 -0700 | [diff] [blame] | 1498 | frameCount, lSessionId, notificationFrames, |
Jean-Michel Trivi | 4cb6683 | 2015-05-01 18:34:17 -0700 | [diff] [blame^] | 1499 | clientUid, flags, tid, &lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 1500 | LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0)); |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 1501 | |
| 1502 | if (lStatus == NO_ERROR) { |
| 1503 | // Check if one effect chain was awaiting for an AudioRecord to be created on this |
| 1504 | // session and move it to this thread. |
| 1505 | sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)lSessionId); |
| 1506 | if (chain != 0) { |
| 1507 | Mutex::Autolock _l(thread->mLock); |
| 1508 | thread->addEffectChain_l(chain); |
| 1509 | } |
| 1510 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1511 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 1512 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1513 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 1514 | // 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] | 1515 | // Client destructor is called by the TrackBase destructor with mClientLock held |
Eric Laurent | fe1a94e | 2014-05-26 16:03:08 -0700 | [diff] [blame] | 1516 | // Don't hold mClientLock when releasing the reference on the track as the |
| 1517 | // destructor will acquire it. |
| 1518 | { |
| 1519 | Mutex::Autolock _cl(mClientLock); |
| 1520 | client.clear(); |
| 1521 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1522 | recordTrack.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1523 | goto Exit; |
| 1524 | } |
| 1525 | |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 1526 | cblk = recordTrack->getCblk(); |
| 1527 | buffers = recordTrack->getBuffers(); |
| 1528 | |
Glenn Kasten | 2fc1473 | 2013-08-05 14:58:14 -0700 | [diff] [blame] | 1529 | // return handle to client |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1530 | recordHandle = new RecordHandle(recordTrack); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1531 | |
| 1532 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1533 | *status = lStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1534 | return recordHandle; |
| 1535 | } |
| 1536 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1537 | |
| 1538 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1539 | // ---------------------------------------------------------------------------- |
| 1540 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1541 | audio_module_handle_t AudioFlinger::loadHwModule(const char *name) |
| 1542 | { |
Eric Laurent | 44622db | 2014-08-01 19:00:33 -0700 | [diff] [blame] | 1543 | if (name == NULL) { |
| 1544 | return 0; |
| 1545 | } |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1546 | if (!settingsAllowed()) { |
| 1547 | return 0; |
| 1548 | } |
| 1549 | Mutex::Autolock _l(mLock); |
| 1550 | return loadHwModule_l(name); |
| 1551 | } |
| 1552 | |
| 1553 | // loadHwModule_l() must be called with AudioFlinger::mLock held |
| 1554 | audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name) |
| 1555 | { |
| 1556 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 1557 | if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) { |
| 1558 | ALOGW("loadHwModule() module %s already loaded", name); |
| 1559 | return mAudioHwDevs.keyAt(i); |
| 1560 | } |
| 1561 | } |
| 1562 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1563 | audio_hw_device_t *dev; |
| 1564 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 1565 | int rc = load_audio_interface(name, &dev); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1566 | if (rc) { |
| 1567 | ALOGI("loadHwModule() error %d loading module %s ", rc, name); |
| 1568 | return 0; |
| 1569 | } |
| 1570 | |
| 1571 | mHardwareStatus = AUDIO_HW_INIT; |
| 1572 | rc = dev->init_check(dev); |
| 1573 | mHardwareStatus = AUDIO_HW_IDLE; |
| 1574 | if (rc) { |
| 1575 | ALOGI("loadHwModule() init check error %d for module %s ", rc, name); |
| 1576 | return 0; |
| 1577 | } |
| 1578 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1579 | // Check and cache this HAL's level of support for master mute and master |
| 1580 | // volume. If this is the first HAL opened, and it supports the get |
| 1581 | // methods, use the initial values provided by the HAL as the current |
| 1582 | // master mute and volume settings. |
| 1583 | |
| 1584 | AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0); |
| 1585 | { // scope for auto-lock pattern |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1586 | AutoMutex lock(mHardwareLock); |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1587 | |
| 1588 | if (0 == mAudioHwDevs.size()) { |
| 1589 | mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME; |
| 1590 | if (NULL != dev->get_master_volume) { |
| 1591 | float mv; |
| 1592 | if (OK == dev->get_master_volume(dev, &mv)) { |
| 1593 | mMasterVolume = mv; |
| 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE; |
| 1598 | if (NULL != dev->get_master_mute) { |
| 1599 | bool mm; |
| 1600 | if (OK == dev->get_master_mute(dev, &mm)) { |
| 1601 | mMasterMute = mm; |
| 1602 | } |
| 1603 | } |
| 1604 | } |
| 1605 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1606 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1607 | if ((NULL != dev->set_master_volume) && |
| 1608 | (OK == dev->set_master_volume(dev, mMasterVolume))) { |
| 1609 | flags = static_cast<AudioHwDevice::Flags>(flags | |
| 1610 | AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME); |
| 1611 | } |
| 1612 | |
| 1613 | mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE; |
| 1614 | if ((NULL != dev->set_master_mute) && |
| 1615 | (OK == dev->set_master_mute(dev, mMasterMute))) { |
| 1616 | flags = static_cast<AudioHwDevice::Flags>(flags | |
| 1617 | AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE); |
| 1618 | } |
| 1619 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1620 | mHardwareStatus = AUDIO_HW_IDLE; |
| 1621 | } |
| 1622 | |
| 1623 | audio_module_handle_t handle = nextUniqueId(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1624 | mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags)); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1625 | |
| 1626 | ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d", |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 1627 | name, dev->common.module->name, dev->common.module->id, handle); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1628 | |
| 1629 | return handle; |
| 1630 | |
| 1631 | } |
| 1632 | |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 1633 | // ---------------------------------------------------------------------------- |
| 1634 | |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 1635 | uint32_t AudioFlinger::getPrimaryOutputSamplingRate() |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 1636 | { |
| 1637 | Mutex::Autolock _l(mLock); |
| 1638 | PlaybackThread *thread = primaryPlaybackThread_l(); |
| 1639 | return thread != NULL ? thread->sampleRate() : 0; |
| 1640 | } |
| 1641 | |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 1642 | size_t AudioFlinger::getPrimaryOutputFrameCount() |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 1643 | { |
| 1644 | Mutex::Autolock _l(mLock); |
| 1645 | PlaybackThread *thread = primaryPlaybackThread_l(); |
| 1646 | return thread != NULL ? thread->frameCountHAL() : 0; |
| 1647 | } |
| 1648 | |
| 1649 | // ---------------------------------------------------------------------------- |
| 1650 | |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 1651 | status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice) |
| 1652 | { |
| 1653 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 1654 | if (uid != AID_SYSTEM) { |
| 1655 | return PERMISSION_DENIED; |
| 1656 | } |
| 1657 | Mutex::Autolock _l(mLock); |
| 1658 | if (mIsDeviceTypeKnown) { |
| 1659 | return INVALID_OPERATION; |
| 1660 | } |
| 1661 | mIsLowRamDevice = isLowRamDevice; |
| 1662 | mIsDeviceTypeKnown = true; |
| 1663 | return NO_ERROR; |
| 1664 | } |
| 1665 | |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1666 | audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId) |
| 1667 | { |
| 1668 | Mutex::Autolock _l(mLock); |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 1669 | |
| 1670 | ssize_t index = mHwAvSyncIds.indexOfKey(sessionId); |
| 1671 | if (index >= 0) { |
| 1672 | ALOGV("getAudioHwSyncForSession found ID %d for session %d", |
| 1673 | mHwAvSyncIds.valueAt(index), sessionId); |
| 1674 | return mHwAvSyncIds.valueAt(index); |
| 1675 | } |
| 1676 | |
| 1677 | audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice(); |
| 1678 | if (dev == NULL) { |
| 1679 | return AUDIO_HW_SYNC_INVALID; |
| 1680 | } |
| 1681 | char *reply = dev->get_parameters(dev, AUDIO_PARAMETER_HW_AV_SYNC); |
| 1682 | AudioParameter param = AudioParameter(String8(reply)); |
| 1683 | free(reply); |
| 1684 | |
| 1685 | int value; |
| 1686 | if (param.getInt(String8(AUDIO_PARAMETER_HW_AV_SYNC), value) != NO_ERROR) { |
| 1687 | ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId); |
| 1688 | return AUDIO_HW_SYNC_INVALID; |
| 1689 | } |
| 1690 | |
| 1691 | // allow only one session for a given HW A/V sync ID. |
| 1692 | for (size_t i = 0; i < mHwAvSyncIds.size(); i++) { |
| 1693 | if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) { |
| 1694 | ALOGV("getAudioHwSyncForSession removing ID %d for session %d", |
| 1695 | value, mHwAvSyncIds.keyAt(i)); |
| 1696 | mHwAvSyncIds.removeItemsAt(i); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1697 | break; |
| 1698 | } |
| 1699 | } |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 1700 | |
| 1701 | mHwAvSyncIds.add(sessionId, value); |
| 1702 | |
| 1703 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 1704 | sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i); |
| 1705 | uint32_t sessions = thread->hasAudioSession(sessionId); |
| 1706 | if (sessions & PlaybackThread::TRACK_SESSION) { |
| 1707 | AudioParameter param = AudioParameter(); |
| 1708 | param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value); |
| 1709 | thread->setParameters(param.toString()); |
| 1710 | break; |
| 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId); |
| 1715 | return (audio_hw_sync_t)value; |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1716 | } |
| 1717 | |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 1718 | // setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held |
| 1719 | void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId) |
| 1720 | { |
| 1721 | ssize_t index = mHwAvSyncIds.indexOfKey(sessionId); |
| 1722 | if (index >= 0) { |
| 1723 | audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index); |
| 1724 | ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId); |
| 1725 | AudioParameter param = AudioParameter(); |
| 1726 | param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), syncId); |
| 1727 | thread->setParameters(param.toString()); |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 1732 | // ---------------------------------------------------------------------------- |
| 1733 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1734 | |
| 1735 | sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1736 | audio_io_handle_t *output, |
| 1737 | audio_config_t *config, |
| 1738 | audio_devices_t devices, |
| 1739 | const String8& address, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1740 | audio_output_flags_t flags) |
| 1741 | { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1742 | AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1743 | if (outHwDev == NULL) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1744 | return 0; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | audio_hw_device_t *hwDevHal = outHwDev->hwDevice(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1748 | if (*output == AUDIO_IO_HANDLE_NONE) { |
| 1749 | *output = nextUniqueId(); |
| 1750 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1751 | |
| 1752 | mHardwareStatus = AUDIO_HW_OUTPUT_OPEN; |
| 1753 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1754 | // FOR TESTING ONLY: |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1755 | // This if statement allows overriding the audio policy settings |
| 1756 | // and forcing a specific format or channel mask to the HAL/Sink device for testing. |
| 1757 | if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) { |
| 1758 | // Check only for Normal Mixing mode |
| 1759 | if (kEnableExtendedPrecision) { |
| 1760 | // Specify format (uncomment one below to choose) |
| 1761 | //config->format = AUDIO_FORMAT_PCM_FLOAT; |
| 1762 | //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED; |
| 1763 | //config->format = AUDIO_FORMAT_PCM_32_BIT; |
| 1764 | //config->format = AUDIO_FORMAT_PCM_8_24_BIT; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1765 | // ALOGV("openOutput_l() upgrading format to %#08x", config->format); |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1766 | } |
| 1767 | if (kEnableExtendedChannels) { |
| 1768 | // Specify channel mask (uncomment one below to choose) |
| 1769 | //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch |
| 1770 | //config->channel_mask = audio_channel_mask_from_representation_and_bits( |
| 1771 | // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example |
| 1772 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1773 | } |
| 1774 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 1775 | AudioStreamOut *outputStream = NULL; |
| 1776 | status_t status = outHwDev->openOutputStream( |
| 1777 | &outputStream, |
| 1778 | *output, |
| 1779 | devices, |
| 1780 | flags, |
| 1781 | config, |
| 1782 | address.string()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1783 | |
| 1784 | mHardwareStatus = AUDIO_HW_IDLE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1785 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 1786 | if (status == NO_ERROR) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1787 | |
| 1788 | PlaybackThread *thread; |
| 1789 | if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1790 | thread = new OffloadThread(this, outputStream, *output, devices); |
| 1791 | ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1792 | } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) |
| 1793 | || !isValidPcmSinkFormat(config->format) |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1794 | || !isValidPcmSinkChannelMask(config->channel_mask)) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1795 | thread = new DirectOutputThread(this, outputStream, *output, devices); |
| 1796 | ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1797 | } else { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1798 | thread = new MixerThread(this, outputStream, *output, devices); |
| 1799 | ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1800 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1801 | mPlaybackThreads.add(*output, thread); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1802 | return thread; |
| 1803 | } |
| 1804 | |
| 1805 | return 0; |
| 1806 | } |
| 1807 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1808 | status_t AudioFlinger::openOutput(audio_module_handle_t module, |
| 1809 | audio_io_handle_t *output, |
| 1810 | audio_config_t *config, |
| 1811 | audio_devices_t *devices, |
| 1812 | const String8& address, |
| 1813 | uint32_t *latencyMs, |
| 1814 | audio_output_flags_t flags) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1815 | { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 1816 | ALOGI("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x", |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1817 | module, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1818 | (devices != NULL) ? *devices : 0, |
| 1819 | config->sample_rate, |
| 1820 | config->format, |
| 1821 | config->channel_mask, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1822 | flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1823 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1824 | if (*devices == AUDIO_DEVICE_NONE) { |
| 1825 | return BAD_VALUE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1826 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1827 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1828 | Mutex::Autolock _l(mLock); |
| 1829 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1830 | sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1831 | if (thread != 0) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1832 | *latencyMs = thread->latency(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1833 | |
| 1834 | // notify client processes of the new output creation |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1835 | thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1836 | |
| 1837 | // the first primary output opened designates the primary hw device |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 1838 | if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1839 | ALOGI("Using module %d has the primary audio interface", module); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1840 | mPrimaryHardwareDev = thread->getOutput()->audioHwDev; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1841 | |
| 1842 | AutoMutex lock(mHardwareLock); |
| 1843 | mHardwareStatus = AUDIO_HW_SET_MODE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1844 | mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1845 | mHardwareStatus = AUDIO_HW_IDLE; |
Glenn Kasten | 4ea00a2 | 2014-06-02 08:29:22 -0700 | [diff] [blame] | 1846 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1847 | mPrimaryOutputSampleRate = config->sample_rate; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1848 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1849 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1850 | } |
| 1851 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1852 | return NO_INIT; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1853 | } |
| 1854 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1855 | audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1, |
| 1856 | audio_io_handle_t output2) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1857 | { |
| 1858 | Mutex::Autolock _l(mLock); |
| 1859 | MixerThread *thread1 = checkMixerThread_l(output1); |
| 1860 | MixerThread *thread2 = checkMixerThread_l(output2); |
| 1861 | |
| 1862 | if (thread1 == NULL || thread2 == NULL) { |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 1863 | ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1, |
| 1864 | output2); |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 1865 | return AUDIO_IO_HANDLE_NONE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1866 | } |
| 1867 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1868 | audio_io_handle_t id = nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1869 | DuplicatingThread *thread = new DuplicatingThread(this, thread1, id); |
| 1870 | thread->addOutputTrack(thread2); |
| 1871 | mPlaybackThreads.add(id, thread); |
| 1872 | // notify client processes of the new output creation |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1873 | thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1874 | return id; |
| 1875 | } |
| 1876 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1877 | status_t AudioFlinger::closeOutput(audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1878 | { |
Glenn Kasten | d96c572 | 2012-04-25 13:44:49 -0700 | [diff] [blame] | 1879 | return closeOutput_nonvirtual(output); |
| 1880 | } |
| 1881 | |
| 1882 | status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output) |
| 1883 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1884 | // keep strong reference on the playback thread so that |
| 1885 | // it is not destroyed while exit() is executed |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1886 | sp<PlaybackThread> thread; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1887 | { |
| 1888 | Mutex::Autolock _l(mLock); |
| 1889 | thread = checkPlaybackThread_l(output); |
| 1890 | if (thread == NULL) { |
| 1891 | return BAD_VALUE; |
| 1892 | } |
| 1893 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1894 | ALOGV("closeOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1895 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1896 | if (thread->type() == ThreadBase::MIXER) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1897 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1898 | if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) { |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 1899 | DuplicatingThread *dupThread = |
| 1900 | (DuplicatingThread *)mPlaybackThreads.valueAt(i).get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1901 | dupThread->removeOutputTrack((MixerThread *)thread.get()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1902 | |
| 1903 | } |
| 1904 | } |
| 1905 | } |
| 1906 | |
| 1907 | |
| 1908 | mPlaybackThreads.removeItem(output); |
| 1909 | // save all effects to the default thread |
| 1910 | if (mPlaybackThreads.size()) { |
| 1911 | PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0)); |
| 1912 | if (dstThread != NULL) { |
| 1913 | // audioflinger lock is held here so the acquisition order of thread locks does not |
| 1914 | // matter |
| 1915 | Mutex::Autolock _dl(dstThread->mLock); |
| 1916 | Mutex::Autolock _sl(thread->mLock); |
| 1917 | Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l(); |
| 1918 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 1919 | moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1920 | } |
| 1921 | } |
| 1922 | } |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1923 | audioConfigChanged(AudioSystem::OUTPUT_CLOSED, output, NULL); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1924 | } |
| 1925 | thread->exit(); |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 1926 | // The thread entity (active unit of execution) is no longer running here, |
| 1927 | // but the ThreadBase container still exists. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1928 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1929 | if (thread->type() != ThreadBase::DUPLICATING) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1930 | closeOutputFinish(thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1931 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1932 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1933 | return NO_ERROR; |
| 1934 | } |
| 1935 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1936 | void AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread) |
| 1937 | { |
| 1938 | AudioStreamOut *out = thread->clearOutput(); |
| 1939 | ALOG_ASSERT(out != NULL, "out shouldn't be NULL"); |
| 1940 | // from now on thread->mOutput is NULL |
| 1941 | out->hwDev()->close_output_stream(out->hwDev(), out->stream); |
| 1942 | delete out; |
| 1943 | } |
| 1944 | |
| 1945 | void AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread) |
| 1946 | { |
| 1947 | mPlaybackThreads.removeItem(thread->mId); |
| 1948 | thread->exit(); |
| 1949 | closeOutputFinish(thread); |
| 1950 | } |
| 1951 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1952 | status_t AudioFlinger::suspendOutput(audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1953 | { |
| 1954 | Mutex::Autolock _l(mLock); |
| 1955 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 1956 | |
| 1957 | if (thread == NULL) { |
| 1958 | return BAD_VALUE; |
| 1959 | } |
| 1960 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1961 | ALOGV("suspendOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1962 | thread->suspend(); |
| 1963 | |
| 1964 | return NO_ERROR; |
| 1965 | } |
| 1966 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1967 | status_t AudioFlinger::restoreOutput(audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1968 | { |
| 1969 | Mutex::Autolock _l(mLock); |
| 1970 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 1971 | |
| 1972 | if (thread == NULL) { |
| 1973 | return BAD_VALUE; |
| 1974 | } |
| 1975 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1976 | ALOGV("restoreOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1977 | |
| 1978 | thread->restore(); |
| 1979 | |
| 1980 | return NO_ERROR; |
| 1981 | } |
| 1982 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1983 | status_t AudioFlinger::openInput(audio_module_handle_t module, |
| 1984 | audio_io_handle_t *input, |
| 1985 | audio_config_t *config, |
Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 1986 | audio_devices_t *devices, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1987 | const String8& address, |
| 1988 | audio_source_t source, |
Glenn Kasten | ec40d28 | 2014-07-15 15:31:26 -0700 | [diff] [blame] | 1989 | audio_input_flags_t flags) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1990 | { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1991 | Mutex::Autolock _l(mLock); |
| 1992 | |
Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 1993 | if (*devices == AUDIO_DEVICE_NONE) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1994 | return BAD_VALUE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1995 | } |
| 1996 | |
Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 1997 | sp<RecordThread> thread = openInput_l(module, input, config, *devices, address, source, flags); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1998 | |
| 1999 | if (thread != 0) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2000 | // notify client processes of the new input creation |
| 2001 | thread->audioConfigChanged(AudioSystem::INPUT_OPENED); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2002 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2003 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2004 | return NO_INIT; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2005 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2006 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2007 | sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2008 | audio_io_handle_t *input, |
| 2009 | audio_config_t *config, |
Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2010 | audio_devices_t devices, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2011 | const String8& address, |
| 2012 | audio_source_t source, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2013 | audio_input_flags_t flags) |
| 2014 | { |
Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2015 | AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices); |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 2016 | if (inHwDev == NULL) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2017 | *input = AUDIO_IO_HANDLE_NONE; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2018 | return 0; |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 2019 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2020 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2021 | if (*input == AUDIO_IO_HANDLE_NONE) { |
| 2022 | *input = nextUniqueId(); |
| 2023 | } |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 2024 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2025 | audio_config_t halconfig = *config; |
| 2026 | audio_hw_device_t *inHwHal = inHwDev->hwDevice(); |
Glenn Kasten | 3255095 | 2013-08-06 10:45:10 -0700 | [diff] [blame] | 2027 | audio_stream_in_t *inStream = NULL; |
Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2028 | status_t status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2029 | &inStream, flags, address.string(), source); |
| 2030 | ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d" |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 2031 | ", Format %#x, Channels %x, flags %#x, status %d addr %s", |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2032 | inStream, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2033 | halconfig.sample_rate, |
| 2034 | halconfig.format, |
| 2035 | halconfig.channel_mask, |
Glenn Kasten | ec40d28 | 2014-07-15 15:31:26 -0700 | [diff] [blame] | 2036 | flags, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 2037 | status, address.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2038 | |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 2039 | // 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] | 2040 | // conversion internally, try to open again with the proposed parameters. |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 2041 | if (status == BAD_VALUE && |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 2042 | audio_is_linear_pcm(config->format) && |
| 2043 | audio_is_linear_pcm(halconfig.format) && |
| 2044 | (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) && |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2045 | (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_2) && |
| 2046 | (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_2)) { |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 2047 | // 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] | 2048 | ALOGV("openInput_l() reopening with proposed sampling rate and channel mask"); |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 2049 | inStream = NULL; |
Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2050 | status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2051 | &inStream, flags, address.string(), source); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 2052 | // FIXME log this new status; HAL should not propose any further changes |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2053 | } |
| 2054 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 2055 | if (status == NO_ERROR && inStream != NULL) { |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 2056 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 2057 | #ifdef TEE_SINK |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 2058 | // Try to re-use most recently used Pipe to archive a copy of input for dumpsys, |
| 2059 | // or (re-)create if current Pipe is idle and does not match the new format |
| 2060 | sp<NBAIO_Sink> teeSink; |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 2061 | enum { |
| 2062 | TEE_SINK_NO, // don't copy input |
| 2063 | TEE_SINK_NEW, // copy input using a new pipe |
| 2064 | TEE_SINK_OLD, // copy input using an existing pipe |
| 2065 | } kind; |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 2066 | NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate, |
| 2067 | audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2068 | if (!mTeeSinkInputEnabled) { |
| 2069 | kind = TEE_SINK_NO; |
Glenn Kasten | 6e0d67d | 2014-01-31 09:41:08 -0800 | [diff] [blame] | 2070 | } else if (!Format_isValid(format)) { |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 2071 | kind = TEE_SINK_NO; |
| 2072 | } else if (mRecordTeeSink == 0) { |
| 2073 | kind = TEE_SINK_NEW; |
| 2074 | } else if (mRecordTeeSink->getStrongCount() != 1) { |
| 2075 | kind = TEE_SINK_NO; |
Glenn Kasten | f66b422 | 2014-02-20 10:23:28 -0800 | [diff] [blame] | 2076 | } else if (Format_isEqual(format, mRecordTeeSink->format())) { |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 2077 | kind = TEE_SINK_OLD; |
| 2078 | } else { |
| 2079 | kind = TEE_SINK_NEW; |
| 2080 | } |
| 2081 | switch (kind) { |
| 2082 | case TEE_SINK_NEW: { |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2083 | Pipe *pipe = new Pipe(mTeeSinkInputFrames, format); |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 2084 | size_t numCounterOffers = 0; |
| 2085 | const NBAIO_Format offers[1] = {format}; |
| 2086 | ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); |
| 2087 | ALOG_ASSERT(index == 0); |
| 2088 | PipeReader *pipeReader = new PipeReader(*pipe); |
| 2089 | numCounterOffers = 0; |
| 2090 | index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); |
| 2091 | ALOG_ASSERT(index == 0); |
| 2092 | mRecordTeeSink = pipe; |
| 2093 | mRecordTeeSource = pipeReader; |
| 2094 | teeSink = pipe; |
| 2095 | } |
| 2096 | break; |
| 2097 | case TEE_SINK_OLD: |
| 2098 | teeSink = mRecordTeeSink; |
| 2099 | break; |
| 2100 | case TEE_SINK_NO: |
| 2101 | default: |
| 2102 | break; |
| 2103 | } |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 2104 | #endif |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2105 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2106 | AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2107 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2108 | // Start record thread |
Glenn Kasten | 34af026 | 2013-07-30 11:52:39 -0700 | [diff] [blame] | 2109 | // RecordThread requires both input and output device indication to forward to audio |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2110 | // pre processing modules |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2111 | sp<RecordThread> thread = new RecordThread(this, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2112 | inputStream, |
| 2113 | *input, |
Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 2114 | primaryOutputDevice_l(), |
Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2115 | devices |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 2116 | #ifdef TEE_SINK |
| 2117 | , teeSink |
| 2118 | #endif |
| 2119 | ); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2120 | mRecordThreads.add(*input, thread); |
| 2121 | ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2122 | return thread; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2123 | } |
| 2124 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2125 | *input = AUDIO_IO_HANDLE_NONE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2126 | return 0; |
| 2127 | } |
| 2128 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2129 | status_t AudioFlinger::closeInput(audio_io_handle_t input) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2130 | { |
Glenn Kasten | d96c572 | 2012-04-25 13:44:49 -0700 | [diff] [blame] | 2131 | return closeInput_nonvirtual(input); |
| 2132 | } |
| 2133 | |
| 2134 | status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input) |
| 2135 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2136 | // keep strong reference on the record thread so that |
| 2137 | // it is not destroyed while exit() is executed |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 2138 | sp<RecordThread> thread; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2139 | { |
| 2140 | Mutex::Autolock _l(mLock); |
| 2141 | thread = checkRecordThread_l(input); |
Glenn Kasten | d5903ec | 2012-03-18 10:33:27 -0700 | [diff] [blame] | 2142 | if (thread == 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2143 | return BAD_VALUE; |
| 2144 | } |
| 2145 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2146 | ALOGV("closeInput() %d", input); |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 2147 | |
| 2148 | // If we still have effect chains, it means that a client still holds a handle |
| 2149 | // on at least one effect. We must either move the chain to an existing thread with the |
| 2150 | // same session ID or put it aside in case a new record thread is opened for a |
| 2151 | // new capture on the same session |
| 2152 | sp<EffectChain> chain; |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2153 | { |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2154 | Mutex::Autolock _sl(thread->mLock); |
| 2155 | Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l(); |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 2156 | // Note: maximum one chain per record thread |
| 2157 | if (effectChains.size() != 0) { |
| 2158 | chain = effectChains[0]; |
| 2159 | } |
| 2160 | } |
| 2161 | if (chain != 0) { |
| 2162 | // first check if a record thread is already opened with a client on the same session. |
| 2163 | // This should only happen in case of overlap between one thread tear down and the |
| 2164 | // creation of its replacement |
| 2165 | size_t i; |
| 2166 | for (i = 0; i < mRecordThreads.size(); i++) { |
| 2167 | sp<RecordThread> t = mRecordThreads.valueAt(i); |
| 2168 | if (t == thread) { |
| 2169 | continue; |
| 2170 | } |
| 2171 | if (t->hasAudioSession(chain->sessionId()) != 0) { |
| 2172 | Mutex::Autolock _l(t->mLock); |
| 2173 | ALOGV("closeInput() found thread %d for effect session %d", |
| 2174 | t->id(), chain->sessionId()); |
| 2175 | t->addEffectChain_l(chain); |
| 2176 | break; |
| 2177 | } |
| 2178 | } |
| 2179 | // put the chain aside if we could not find a record thread with the same session id. |
| 2180 | if (i == mRecordThreads.size()) { |
| 2181 | putOrphanEffectChain_l(chain); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2182 | } |
| 2183 | } |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 2184 | audioConfigChanged(AudioSystem::INPUT_CLOSED, input, NULL); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2185 | mRecordThreads.removeItem(input); |
| 2186 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2187 | // FIXME: calling thread->exit() without mLock held should not be needed anymore now that |
| 2188 | // we have a different lock for notification client |
| 2189 | closeInputFinish(thread); |
| 2190 | return NO_ERROR; |
| 2191 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2192 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2193 | void AudioFlinger::closeInputFinish(sp<RecordThread> thread) |
| 2194 | { |
| 2195 | thread->exit(); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2196 | AudioStreamIn *in = thread->clearInput(); |
Glenn Kasten | 5798d4e | 2012-03-08 12:18:35 -0800 | [diff] [blame] | 2197 | ALOG_ASSERT(in != NULL, "in shouldn't be NULL"); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2198 | // from now on thread->mInput is NULL |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 2199 | in->hwDev()->close_input_stream(in->hwDev(), in->stream); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2200 | delete in; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2201 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2202 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2203 | void AudioFlinger::closeInputInternal_l(sp<RecordThread> thread) |
| 2204 | { |
| 2205 | mRecordThreads.removeItem(thread->mId); |
| 2206 | closeInputFinish(thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2207 | } |
| 2208 | |
Glenn Kasten | d2304db | 2014-02-03 07:40:31 -0800 | [diff] [blame] | 2209 | status_t AudioFlinger::invalidateStream(audio_stream_type_t stream) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2210 | { |
| 2211 | Mutex::Autolock _l(mLock); |
Glenn Kasten | d2304db | 2014-02-03 07:40:31 -0800 | [diff] [blame] | 2212 | ALOGV("invalidateStream() stream %d", stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2213 | |
| 2214 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2215 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
Eric Laurent | 2216785 | 2012-06-20 12:26:32 -0700 | [diff] [blame] | 2216 | thread->invalidateTracks(stream); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2217 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2218 | |
| 2219 | return NO_ERROR; |
| 2220 | } |
| 2221 | |
| 2222 | |
Eric Laurent | de3f839 | 2014-07-27 18:38:22 -0700 | [diff] [blame] | 2223 | audio_unique_id_t AudioFlinger::newAudioUniqueId() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2224 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2225 | return nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2226 | } |
| 2227 | |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 2228 | void AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid) |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2229 | { |
| 2230 | Mutex::Autolock _l(mLock); |
Glenn Kasten | bb00192 | 2012-02-03 11:10:26 -0800 | [diff] [blame] | 2231 | pid_t caller = IPCThreadState::self()->getCallingPid(); |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 2232 | ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid); |
| 2233 | if (pid != -1 && (caller == getpid_cached)) { |
| 2234 | caller = pid; |
| 2235 | } |
Eric Laurent | d1b28d4 | 2013-09-18 18:47:13 -0700 | [diff] [blame] | 2236 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 2237 | { |
| 2238 | Mutex::Autolock _cl(mClientLock); |
| 2239 | // Ignore requests received from processes not known as notification client. The request |
| 2240 | // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be |
| 2241 | // called from a different pid leaving a stale session reference. Also we don't know how |
| 2242 | // to clear this reference if the client process dies. |
| 2243 | if (mNotificationClients.indexOfKey(caller) < 0) { |
| 2244 | ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession); |
| 2245 | return; |
| 2246 | } |
Eric Laurent | d1b28d4 | 2013-09-18 18:47:13 -0700 | [diff] [blame] | 2247 | } |
| 2248 | |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 2249 | size_t num = mAudioSessionRefs.size(); |
| 2250 | for (size_t i = 0; i< num; i++) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2251 | AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 2252 | if (ref->mSessionid == audioSession && ref->mPid == caller) { |
| 2253 | ref->mCnt++; |
| 2254 | ALOGV(" incremented refcount to %d", ref->mCnt); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2255 | return; |
| 2256 | } |
| 2257 | } |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 2258 | mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller)); |
| 2259 | ALOGV(" added new entry for %d", audioSession); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2260 | } |
| 2261 | |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 2262 | void AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid) |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2263 | { |
| 2264 | Mutex::Autolock _l(mLock); |
Glenn Kasten | bb00192 | 2012-02-03 11:10:26 -0800 | [diff] [blame] | 2265 | pid_t caller = IPCThreadState::self()->getCallingPid(); |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 2266 | ALOGV("releasing %d from %d for %d", audioSession, caller, pid); |
| 2267 | if (pid != -1 && (caller == getpid_cached)) { |
| 2268 | caller = pid; |
| 2269 | } |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 2270 | size_t num = mAudioSessionRefs.size(); |
| 2271 | for (size_t i = 0; i< num; i++) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2272 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 2273 | if (ref->mSessionid == audioSession && ref->mPid == caller) { |
| 2274 | ref->mCnt--; |
| 2275 | ALOGV(" decremented refcount to %d", ref->mCnt); |
| 2276 | if (ref->mCnt == 0) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2277 | mAudioSessionRefs.removeAt(i); |
| 2278 | delete ref; |
| 2279 | purgeStaleEffects_l(); |
| 2280 | } |
| 2281 | return; |
| 2282 | } |
| 2283 | } |
Eric Laurent | d1b28d4 | 2013-09-18 18:47:13 -0700 | [diff] [blame] | 2284 | // If the caller is mediaserver it is likely that the session being released was acquired |
| 2285 | // on behalf of a process not in notification clients and we ignore the warning. |
| 2286 | ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2287 | } |
| 2288 | |
| 2289 | void AudioFlinger::purgeStaleEffects_l() { |
| 2290 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2291 | ALOGV("purging stale effects"); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2292 | |
| 2293 | Vector< sp<EffectChain> > chains; |
| 2294 | |
| 2295 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2296 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 2297 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 2298 | sp<EffectChain> ec = t->mEffectChains[j]; |
Marco Nelissen | 0270b18 | 2011-08-12 14:14:39 -0700 | [diff] [blame] | 2299 | if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) { |
| 2300 | chains.push(ec); |
| 2301 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2302 | } |
| 2303 | } |
| 2304 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 2305 | sp<RecordThread> t = mRecordThreads.valueAt(i); |
| 2306 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 2307 | sp<EffectChain> ec = t->mEffectChains[j]; |
| 2308 | chains.push(ec); |
| 2309 | } |
| 2310 | } |
| 2311 | |
| 2312 | for (size_t i = 0; i < chains.size(); i++) { |
| 2313 | sp<EffectChain> ec = chains[i]; |
| 2314 | int sessionid = ec->sessionId(); |
| 2315 | sp<ThreadBase> t = ec->mThread.promote(); |
| 2316 | if (t == 0) { |
| 2317 | continue; |
| 2318 | } |
| 2319 | size_t numsessionrefs = mAudioSessionRefs.size(); |
| 2320 | bool found = false; |
| 2321 | for (size_t k = 0; k < numsessionrefs; k++) { |
| 2322 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(k); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 2323 | if (ref->mSessionid == sessionid) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2324 | ALOGV(" session %d still exists for %d with %d refs", |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 2325 | sessionid, ref->mPid, ref->mCnt); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2326 | found = true; |
| 2327 | break; |
| 2328 | } |
| 2329 | } |
| 2330 | if (!found) { |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 2331 | Mutex::Autolock _l(t->mLock); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2332 | // remove all effects from the chain |
| 2333 | while (ec->mEffects.size()) { |
| 2334 | sp<EffectModule> effect = ec->mEffects[0]; |
| 2335 | effect->unPin(); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2336 | t->removeEffect_l(effect); |
Eric Laurent | a5f44eb | 2012-06-25 11:38:29 -0700 | [diff] [blame] | 2337 | if (effect->purgeHandles()) { |
| 2338 | t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId()); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2339 | } |
| 2340 | AudioSystem::unregisterEffect(effect->id()); |
| 2341 | } |
| 2342 | } |
| 2343 | } |
| 2344 | return; |
| 2345 | } |
| 2346 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2347 | // checkPlaybackThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2348 | AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2349 | { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 2350 | return mPlaybackThreads.valueFor(output).get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2351 | } |
| 2352 | |
| 2353 | // checkMixerThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2354 | AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2355 | { |
| 2356 | PlaybackThread *thread = checkPlaybackThread_l(output); |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 2357 | return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2358 | } |
| 2359 | |
| 2360 | // checkRecordThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2361 | AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2362 | { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 2363 | return mRecordThreads.valueFor(input).get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2364 | } |
| 2365 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2366 | uint32_t AudioFlinger::nextUniqueId() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2367 | { |
Glenn Kasten | bcefec3 | 2014-01-17 12:09:05 -0800 | [diff] [blame] | 2368 | return (uint32_t) android_atomic_inc(&mNextUniqueId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2369 | } |
| 2370 | |
Glenn Kasten | 02fe1bf | 2012-02-24 15:42:17 -0800 | [diff] [blame] | 2371 | AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2372 | { |
| 2373 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2374 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2375 | AudioStreamOut *output = thread->getOutput(); |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 2376 | if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2377 | return thread; |
| 2378 | } |
| 2379 | } |
| 2380 | return NULL; |
| 2381 | } |
| 2382 | |
Glenn Kasten | bb4350d | 2012-07-03 15:56:38 -0700 | [diff] [blame] | 2383 | audio_devices_t AudioFlinger::primaryOutputDevice_l() const |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2384 | { |
| 2385 | PlaybackThread *thread = primaryPlaybackThread_l(); |
| 2386 | |
| 2387 | if (thread == NULL) { |
| 2388 | return 0; |
| 2389 | } |
| 2390 | |
Eric Laurent | f1c04f9 | 2012-08-28 14:26:53 -0700 | [diff] [blame] | 2391 | return thread->outDevice(); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2392 | } |
| 2393 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 2394 | sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type, |
| 2395 | int triggerSession, |
| 2396 | int listenerSession, |
| 2397 | sync_event_callback_t callBack, |
Eric Laurent | 8ea16e4 | 2014-02-20 16:26:11 -0800 | [diff] [blame] | 2398 | wp<RefBase> cookie) |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 2399 | { |
| 2400 | Mutex::Autolock _l(mLock); |
| 2401 | |
| 2402 | sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie); |
| 2403 | status_t playStatus = NAME_NOT_FOUND; |
| 2404 | status_t recStatus = NAME_NOT_FOUND; |
| 2405 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2406 | playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event); |
| 2407 | if (playStatus == NO_ERROR) { |
| 2408 | return event; |
| 2409 | } |
| 2410 | } |
| 2411 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 2412 | recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event); |
| 2413 | if (recStatus == NO_ERROR) { |
| 2414 | return event; |
| 2415 | } |
| 2416 | } |
| 2417 | if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) { |
| 2418 | mPendingSyncEvents.add(event); |
| 2419 | } else { |
| 2420 | ALOGV("createSyncEvent() invalid event %d", event->type()); |
| 2421 | event.clear(); |
| 2422 | } |
| 2423 | return event; |
| 2424 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2425 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2426 | // ---------------------------------------------------------------------------- |
| 2427 | // Effect management |
| 2428 | // ---------------------------------------------------------------------------- |
| 2429 | |
| 2430 | |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 2431 | status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2432 | { |
| 2433 | Mutex::Autolock _l(mLock); |
| 2434 | return EffectQueryNumberEffects(numEffects); |
| 2435 | } |
| 2436 | |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 2437 | status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2438 | { |
| 2439 | Mutex::Autolock _l(mLock); |
| 2440 | return EffectQueryEffect(index, descriptor); |
| 2441 | } |
| 2442 | |
Glenn Kasten | 5e92a78 | 2012-01-30 07:40:52 -0800 | [diff] [blame] | 2443 | status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid, |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 2444 | effect_descriptor_t *descriptor) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2445 | { |
| 2446 | Mutex::Autolock _l(mLock); |
| 2447 | return EffectGetDescriptor(pUuid, descriptor); |
| 2448 | } |
| 2449 | |
| 2450 | |
Glenn Kasten | 8d6cc84 | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 2451 | sp<IEffect> AudioFlinger::createEffect( |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2452 | effect_descriptor_t *pDesc, |
| 2453 | const sp<IEffectClient>& effectClient, |
| 2454 | int32_t priority, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2455 | audio_io_handle_t io, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2456 | int sessionId, |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 2457 | const String16& opPackageName, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2458 | status_t *status, |
| 2459 | int *id, |
| 2460 | int *enabled) |
| 2461 | { |
| 2462 | status_t lStatus = NO_ERROR; |
| 2463 | sp<EffectHandle> handle; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2464 | effect_descriptor_t desc; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2465 | |
Glenn Kasten | 8d6cc84 | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 2466 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 2467 | ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d", |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2468 | pid, effectClient.get(), priority, sessionId, io); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2469 | |
| 2470 | if (pDesc == NULL) { |
| 2471 | lStatus = BAD_VALUE; |
| 2472 | goto Exit; |
| 2473 | } |
| 2474 | |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 2475 | // check audio settings permission for global effects |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2476 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 2477 | lStatus = PERMISSION_DENIED; |
| 2478 | goto Exit; |
| 2479 | } |
| 2480 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2481 | // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 2482 | // that can only be created by audio policy manager (running in same process) |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 2483 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 2484 | lStatus = PERMISSION_DENIED; |
| 2485 | goto Exit; |
| 2486 | } |
| 2487 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2488 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2489 | if (!EffectIsNullUuid(&pDesc->uuid)) { |
| 2490 | // if uuid is specified, request effect descriptor |
| 2491 | lStatus = EffectGetDescriptor(&pDesc->uuid, &desc); |
| 2492 | if (lStatus < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2493 | ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2494 | goto Exit; |
| 2495 | } |
| 2496 | } else { |
| 2497 | // if uuid is not specified, look for an available implementation |
| 2498 | // of the required type in effect factory |
| 2499 | if (EffectIsNullUuid(&pDesc->type)) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2500 | ALOGW("createEffect() no effect type"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2501 | lStatus = BAD_VALUE; |
| 2502 | goto Exit; |
| 2503 | } |
| 2504 | uint32_t numEffects = 0; |
| 2505 | effect_descriptor_t d; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2506 | d.flags = 0; // prevent compiler warning |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2507 | bool found = false; |
| 2508 | |
| 2509 | lStatus = EffectQueryNumberEffects(&numEffects); |
| 2510 | if (lStatus < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2511 | ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2512 | goto Exit; |
| 2513 | } |
| 2514 | for (uint32_t i = 0; i < numEffects; i++) { |
| 2515 | lStatus = EffectQueryEffect(i, &desc); |
| 2516 | if (lStatus < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2517 | ALOGW("createEffect() error %d from EffectQueryEffect", lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2518 | continue; |
| 2519 | } |
| 2520 | if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) { |
| 2521 | // If matching type found save effect descriptor. If the session is |
| 2522 | // 0 and the effect is not auxiliary, continue enumeration in case |
| 2523 | // an auxiliary version of this effect type is available |
| 2524 | found = true; |
Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 2525 | d = desc; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2526 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX || |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2527 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 2528 | break; |
| 2529 | } |
| 2530 | } |
| 2531 | } |
| 2532 | if (!found) { |
| 2533 | lStatus = BAD_VALUE; |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2534 | ALOGW("createEffect() effect not found"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2535 | goto Exit; |
| 2536 | } |
| 2537 | // For same effect type, chose auxiliary version over insert version if |
| 2538 | // connect to output mix (Compliance to OpenSL ES) |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2539 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2540 | (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) { |
Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 2541 | desc = d; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2542 | } |
| 2543 | } |
| 2544 | |
| 2545 | // 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] | 2546 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2547 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 2548 | lStatus = INVALID_OPERATION; |
| 2549 | goto Exit; |
| 2550 | } |
| 2551 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2552 | // check recording permission for visualizer |
| 2553 | if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) && |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 2554 | !recordingAllowed(opPackageName)) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2555 | lStatus = PERMISSION_DENIED; |
| 2556 | goto Exit; |
| 2557 | } |
| 2558 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2559 | // return effect descriptor |
Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 2560 | *pDesc = desc; |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 2561 | if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | eb3c337 | 2013-09-25 12:25:29 -0700 | [diff] [blame] | 2562 | // if the output returned by getOutputForEffect() is removed before we lock the |
| 2563 | // mutex below, the call to checkPlaybackThread_l(io) below will detect it |
| 2564 | // and we will exit safely |
| 2565 | io = AudioSystem::getOutputForEffect(&desc); |
| 2566 | ALOGV("createEffect got output %d", io); |
| 2567 | } |
| 2568 | |
| 2569 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2570 | |
| 2571 | // If output is not specified try to find a matching audio session ID in one of the |
| 2572 | // output threads. |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 2573 | // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX |
| 2574 | // because of code checking output when entering the function. |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2575 | // Note: io is never 0 when creating an effect on an input |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 2576 | if (io == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2577 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
| 2578 | // output must be specified by AudioPolicyManager when using session |
| 2579 | // AUDIO_SESSION_OUTPUT_STAGE |
| 2580 | lStatus = BAD_VALUE; |
| 2581 | goto Exit; |
| 2582 | } |
Eric Laurent | eb3c337 | 2013-09-25 12:25:29 -0700 | [diff] [blame] | 2583 | // look for the thread where the specified audio session is present |
| 2584 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2585 | if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { |
| 2586 | io = mPlaybackThreads.keyAt(i); |
| 2587 | break; |
| 2588 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2589 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2590 | if (io == 0) { |
Eric Laurent | eb3c337 | 2013-09-25 12:25:29 -0700 | [diff] [blame] | 2591 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 2592 | if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { |
| 2593 | io = mRecordThreads.keyAt(i); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 2594 | break; |
| 2595 | } |
| 2596 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2597 | } |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 2598 | // If no output thread contains the requested session ID, default to |
| 2599 | // first output. The effect chain will be moved to the correct output |
| 2600 | // thread when a track with the same session ID is created |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 2601 | if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2602 | io = mPlaybackThreads.keyAt(0); |
| 2603 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2604 | ALOGV("createEffect() got io %d for effect %s", io, desc.name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2605 | } |
| 2606 | ThreadBase *thread = checkRecordThread_l(io); |
| 2607 | if (thread == NULL) { |
| 2608 | thread = checkPlaybackThread_l(io); |
| 2609 | if (thread == NULL) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 2610 | ALOGE("createEffect() unknown output thread"); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2611 | lStatus = BAD_VALUE; |
| 2612 | goto Exit; |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 2613 | } |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2614 | } else { |
| 2615 | // Check if one effect chain was awaiting for an effect to be created on this |
| 2616 | // session and used it instead of creating a new one. |
| 2617 | sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)sessionId); |
| 2618 | if (chain != 0) { |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 2619 | Mutex::Autolock _l(thread->mLock); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2620 | thread->addEffectChain_l(chain); |
| 2621 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2622 | } |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 2623 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 2624 | sp<Client> client = registerPid(pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2625 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2626 | // create effect on selected output thread |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2627 | handle = thread->createEffect_l(client, effectClient, priority, sessionId, |
| 2628 | &desc, enabled, &lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2629 | if (handle != 0 && id != NULL) { |
| 2630 | *id = handle->id(); |
| 2631 | } |
Eric Laurent | fe1a94e | 2014-05-26 16:03:08 -0700 | [diff] [blame] | 2632 | if (handle == 0) { |
| 2633 | // remove local strong reference to Client with mClientLock held |
| 2634 | Mutex::Autolock _cl(mClientLock); |
| 2635 | client.clear(); |
| 2636 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2637 | } |
| 2638 | |
| 2639 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 2640 | *status = lStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2641 | return handle; |
| 2642 | } |
| 2643 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2644 | status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput, |
| 2645 | audio_io_handle_t dstOutput) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2646 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2647 | ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2648 | sessionId, srcOutput, dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2649 | Mutex::Autolock _l(mLock); |
| 2650 | if (srcOutput == dstOutput) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2651 | ALOGW("moveEffects() same dst and src outputs %d", dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2652 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2653 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2654 | PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput); |
| 2655 | if (srcThread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2656 | ALOGW("moveEffects() bad srcOutput %d", srcOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2657 | return BAD_VALUE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2658 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2659 | PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput); |
| 2660 | if (dstThread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2661 | ALOGW("moveEffects() bad dstOutput %d", dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2662 | return BAD_VALUE; |
| 2663 | } |
| 2664 | |
| 2665 | Mutex::Autolock _dl(dstThread->mLock); |
| 2666 | Mutex::Autolock _sl(srcThread->mLock); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2667 | return moveEffectChain_l(sessionId, srcThread, dstThread, false); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2668 | } |
| 2669 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2670 | // moveEffectChain_l must be called with both srcThread and dstThread mLocks held |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2671 | status_t AudioFlinger::moveEffectChain_l(int sessionId, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2672 | AudioFlinger::PlaybackThread *srcThread, |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2673 | AudioFlinger::PlaybackThread *dstThread, |
| 2674 | bool reRegister) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2675 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2676 | ALOGV("moveEffectChain_l() session %d from thread %p to thread %p", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2677 | sessionId, srcThread, dstThread); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2678 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2679 | sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2680 | if (chain == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2681 | 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] | 2682 | sessionId, srcThread); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2683 | return INVALID_OPERATION; |
| 2684 | } |
| 2685 | |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 2686 | // Check whether the destination thread has a channel count of FCC_2, which is |
| 2687 | // currently required for (most) effects. Prevent moving the effect chain here rather |
| 2688 | // than disabling the addEffect_l() call in dstThread below. |
Eric Laurent | b10352f | 2014-11-03 16:25:39 -0800 | [diff] [blame] | 2689 | if ((dstThread->type() == ThreadBase::MIXER || dstThread->type() == ThreadBase::DUPLICATING) && |
| 2690 | dstThread->mChannelCount != FCC_2) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 2691 | ALOGW("moveEffectChain_l() effect chain failed because" |
| 2692 | " destination thread %p channel count(%u) != %u", |
| 2693 | dstThread, dstThread->mChannelCount, FCC_2); |
| 2694 | return INVALID_OPERATION; |
| 2695 | } |
| 2696 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2697 | // 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] | 2698 | // 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] | 2699 | // 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] | 2700 | // removed. |
| 2701 | srcThread->removeEffectChain_l(chain); |
| 2702 | |
| 2703 | // transfer all effects one by one so that new effect chain is created on new thread with |
| 2704 | // correct buffer sizes and audio parameters and effect engines reconfigured accordingly |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2705 | sp<EffectChain> dstChain; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2706 | uint32_t strategy = 0; // prevent compiler warning |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2707 | sp<EffectModule> effect = chain->getEffectFromId_l(0); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2708 | Vector< sp<EffectModule> > removed; |
| 2709 | status_t status = NO_ERROR; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2710 | while (effect != 0) { |
| 2711 | srcThread->removeEffect_l(effect); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2712 | removed.add(effect); |
| 2713 | status = dstThread->addEffect_l(effect); |
| 2714 | if (status != NO_ERROR) { |
| 2715 | break; |
| 2716 | } |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 2717 | // removeEffect_l() has stopped the effect if it was active so it must be restarted |
| 2718 | if (effect->state() == EffectModule::ACTIVE || |
| 2719 | effect->state() == EffectModule::STOPPING) { |
| 2720 | effect->start(); |
| 2721 | } |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2722 | // if the move request is not received from audio policy manager, the effect must be |
| 2723 | // re-registered with the new strategy and output |
| 2724 | if (dstChain == 0) { |
| 2725 | dstChain = effect->chain().promote(); |
| 2726 | if (dstChain == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2727 | ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get()); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2728 | status = NO_INIT; |
| 2729 | break; |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2730 | } |
| 2731 | strategy = dstChain->strategy(); |
| 2732 | } |
| 2733 | if (reRegister) { |
| 2734 | AudioSystem::unregisterEffect(effect->id()); |
| 2735 | AudioSystem::registerEffect(&effect->desc(), |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2736 | dstThread->id(), |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2737 | strategy, |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2738 | sessionId, |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2739 | effect->id()); |
Eric Laurent | d72b7c0 | 2013-10-12 16:17:46 -0700 | [diff] [blame] | 2740 | AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled()); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2741 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2742 | effect = chain->getEffectFromId_l(0); |
| 2743 | } |
| 2744 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2745 | if (status != NO_ERROR) { |
| 2746 | for (size_t i = 0; i < removed.size(); i++) { |
| 2747 | srcThread->addEffect_l(removed[i]); |
| 2748 | if (dstChain != 0 && reRegister) { |
| 2749 | AudioSystem::unregisterEffect(removed[i]->id()); |
| 2750 | AudioSystem::registerEffect(&removed[i]->desc(), |
| 2751 | srcThread->id(), |
| 2752 | strategy, |
| 2753 | sessionId, |
| 2754 | removed[i]->id()); |
Eric Laurent | d72b7c0 | 2013-10-12 16:17:46 -0700 | [diff] [blame] | 2755 | AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled()); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2756 | } |
| 2757 | } |
| 2758 | } |
| 2759 | |
| 2760 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2761 | } |
| 2762 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2763 | bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l() |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 2764 | { |
| 2765 | if (mGlobalEffectEnableTime != 0 && |
| 2766 | ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) { |
| 2767 | return true; |
| 2768 | } |
| 2769 | |
| 2770 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2771 | sp<EffectChain> ec = |
| 2772 | mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2773 | if (ec != 0 && ec->isNonOffloadableEnabled()) { |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 2774 | return true; |
| 2775 | } |
| 2776 | } |
| 2777 | return false; |
| 2778 | } |
| 2779 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2780 | void AudioFlinger::onNonOffloadableGlobalEffectEnable() |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 2781 | { |
| 2782 | Mutex::Autolock _l(mLock); |
| 2783 | |
| 2784 | mGlobalEffectEnableTime = systemTime(); |
| 2785 | |
| 2786 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2787 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 2788 | if (t->mType == ThreadBase::OFFLOAD) { |
| 2789 | t->invalidateTracks(AUDIO_STREAM_MUSIC); |
| 2790 | } |
| 2791 | } |
| 2792 | |
| 2793 | } |
| 2794 | |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2795 | status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain) |
| 2796 | { |
| 2797 | audio_session_t session = (audio_session_t)chain->sessionId(); |
| 2798 | ssize_t index = mOrphanEffectChains.indexOfKey(session); |
| 2799 | ALOGV("putOrphanEffectChain_l session %d index %d", session, index); |
| 2800 | if (index >= 0) { |
| 2801 | ALOGW("putOrphanEffectChain_l chain for session %d already present", session); |
| 2802 | return ALREADY_EXISTS; |
| 2803 | } |
| 2804 | mOrphanEffectChains.add(session, chain); |
| 2805 | return NO_ERROR; |
| 2806 | } |
| 2807 | |
| 2808 | sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session) |
| 2809 | { |
| 2810 | sp<EffectChain> chain; |
| 2811 | ssize_t index = mOrphanEffectChains.indexOfKey(session); |
| 2812 | ALOGV("getOrphanEffectChain_l session %d index %d", session, index); |
| 2813 | if (index >= 0) { |
| 2814 | chain = mOrphanEffectChains.valueAt(index); |
| 2815 | mOrphanEffectChains.removeItemsAt(index); |
| 2816 | } |
| 2817 | return chain; |
| 2818 | } |
| 2819 | |
| 2820 | bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect) |
| 2821 | { |
| 2822 | Mutex::Autolock _l(mLock); |
| 2823 | audio_session_t session = (audio_session_t)effect->sessionId(); |
| 2824 | ssize_t index = mOrphanEffectChains.indexOfKey(session); |
| 2825 | ALOGV("updateOrphanEffectChains session %d index %d", session, index); |
| 2826 | if (index >= 0) { |
| 2827 | sp<EffectChain> chain = mOrphanEffectChains.valueAt(index); |
| 2828 | if (chain->removeEffect_l(effect) == 0) { |
| 2829 | ALOGV("updateOrphanEffectChains removing effect chain at index %d", index); |
| 2830 | mOrphanEffectChains.removeItemsAt(index); |
| 2831 | } |
| 2832 | return true; |
| 2833 | } |
| 2834 | return false; |
| 2835 | } |
| 2836 | |
| 2837 | |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2838 | struct Entry { |
Glenn Kasten | 2f55e76 | 2015-03-05 16:05:08 -0800 | [diff] [blame] | 2839 | #define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21 |
| 2840 | char mFileName[TEE_MAX_FILENAME]; |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2841 | }; |
| 2842 | |
| 2843 | int comparEntry(const void *p1, const void *p2) |
| 2844 | { |
Glenn Kasten | 2f55e76 | 2015-03-05 16:05:08 -0800 | [diff] [blame] | 2845 | return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2846 | } |
| 2847 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 2848 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2849 | void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2850 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2851 | NBAIO_Source *teeSource = source.get(); |
| 2852 | if (teeSource != NULL) { |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2853 | // .wav rotation |
| 2854 | // There is a benign race condition if 2 threads call this simultaneously. |
| 2855 | // They would both traverse the directory, but the result would simply be |
| 2856 | // failures at unlink() which are ignored. It's also unlikely since |
| 2857 | // normally dumpsys is only done by bugreport or from the command line. |
| 2858 | char teePath[32+256]; |
| 2859 | strcpy(teePath, "/data/misc/media"); |
| 2860 | size_t teePathLen = strlen(teePath); |
| 2861 | DIR *dir = opendir(teePath); |
| 2862 | teePath[teePathLen++] = '/'; |
| 2863 | if (dir != NULL) { |
Glenn Kasten | 2f55e76 | 2015-03-05 16:05:08 -0800 | [diff] [blame] | 2864 | #define TEE_MAX_SORT 20 // number of entries to sort |
| 2865 | #define TEE_MAX_KEEP 10 // number of entries to keep |
| 2866 | struct Entry entries[TEE_MAX_SORT]; |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2867 | size_t entryCount = 0; |
Glenn Kasten | 2f55e76 | 2015-03-05 16:05:08 -0800 | [diff] [blame] | 2868 | while (entryCount < TEE_MAX_SORT) { |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2869 | struct dirent de; |
| 2870 | struct dirent *result = NULL; |
| 2871 | int rc = readdir_r(dir, &de, &result); |
| 2872 | if (rc != 0) { |
| 2873 | ALOGW("readdir_r failed %d", rc); |
| 2874 | break; |
| 2875 | } |
| 2876 | if (result == NULL) { |
| 2877 | break; |
| 2878 | } |
| 2879 | if (result != &de) { |
| 2880 | ALOGW("readdir_r returned unexpected result %p != %p", result, &de); |
| 2881 | break; |
| 2882 | } |
| 2883 | // ignore non .wav file entries |
| 2884 | size_t nameLen = strlen(de.d_name); |
Glenn Kasten | 2f55e76 | 2015-03-05 16:05:08 -0800 | [diff] [blame] | 2885 | if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME || |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2886 | strcmp(&de.d_name[nameLen - 4], ".wav")) { |
| 2887 | continue; |
| 2888 | } |
Glenn Kasten | 2f55e76 | 2015-03-05 16:05:08 -0800 | [diff] [blame] | 2889 | strcpy(entries[entryCount++].mFileName, de.d_name); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2890 | } |
| 2891 | (void) closedir(dir); |
Glenn Kasten | 2f55e76 | 2015-03-05 16:05:08 -0800 | [diff] [blame] | 2892 | if (entryCount > TEE_MAX_KEEP) { |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2893 | qsort(entries, entryCount, sizeof(Entry), comparEntry); |
Glenn Kasten | 2f55e76 | 2015-03-05 16:05:08 -0800 | [diff] [blame] | 2894 | for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) { |
| 2895 | strcpy(&teePath[teePathLen], entries[i].mFileName); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2896 | (void) unlink(teePath); |
| 2897 | } |
| 2898 | } |
| 2899 | } else { |
| 2900 | if (fd >= 0) { |
Elliott Hughes | 8b5f642 | 2014-05-22 01:22:06 -0700 | [diff] [blame] | 2901 | dprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno)); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2902 | } |
| 2903 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2904 | char teeTime[16]; |
| 2905 | struct timeval tv; |
| 2906 | gettimeofday(&tv, NULL); |
| 2907 | struct tm tm; |
| 2908 | localtime_r(&tv.tv_sec, &tm); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2909 | strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm); |
| 2910 | snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id); |
| 2911 | // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd |
| 2912 | int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2913 | if (teeFd >= 0) { |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 2914 | // FIXME use libsndfile |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2915 | char wavHeader[44]; |
| 2916 | memcpy(wavHeader, |
| 2917 | "RIFF\0\0\0\0WAVEfmt \20\0\0\0\1\0\2\0\104\254\0\0\0\0\0\0\4\0\20\0data\0\0\0\0", |
| 2918 | sizeof(wavHeader)); |
| 2919 | NBAIO_Format format = teeSource->format(); |
| 2920 | unsigned channelCount = Format_channelCount(format); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2921 | uint32_t sampleRate = Format_sampleRate(format); |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 2922 | size_t frameSize = Format_frameSize(format); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2923 | wavHeader[22] = channelCount; // number of channels |
| 2924 | wavHeader[24] = sampleRate; // sample rate |
| 2925 | wavHeader[25] = sampleRate >> 8; |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 2926 | wavHeader[32] = frameSize; // block alignment |
| 2927 | wavHeader[33] = frameSize >> 8; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2928 | write(teeFd, wavHeader, sizeof(wavHeader)); |
| 2929 | size_t total = 0; |
| 2930 | bool firstRead = true; |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 2931 | #define TEE_SINK_READ 1024 // frames per I/O operation |
| 2932 | void *buffer = malloc(TEE_SINK_READ * frameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2933 | for (;;) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2934 | size_t count = TEE_SINK_READ; |
| 2935 | ssize_t actual = teeSource->read(buffer, count, |
| 2936 | AudioBufferProvider::kInvalidPTS); |
| 2937 | bool wasFirstRead = firstRead; |
| 2938 | firstRead = false; |
| 2939 | if (actual <= 0) { |
| 2940 | if (actual == (ssize_t) OVERRUN && wasFirstRead) { |
| 2941 | continue; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2942 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2943 | break; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2944 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2945 | ALOG_ASSERT(actual <= (ssize_t)count); |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 2946 | write(teeFd, buffer, actual * frameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2947 | total += actual; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2948 | } |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 2949 | free(buffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2950 | lseek(teeFd, (off_t) 4, SEEK_SET); |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 2951 | uint32_t temp = 44 + total * frameSize - 8; |
| 2952 | // FIXME not big-endian safe |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2953 | write(teeFd, &temp, sizeof(temp)); |
| 2954 | lseek(teeFd, (off_t) 40, SEEK_SET); |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 2955 | temp = total * frameSize; |
| 2956 | // FIXME not big-endian safe |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2957 | write(teeFd, &temp, sizeof(temp)); |
| 2958 | close(teeFd); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2959 | if (fd >= 0) { |
Elliott Hughes | 8b5f642 | 2014-05-22 01:22:06 -0700 | [diff] [blame] | 2960 | dprintf(fd, "tee copied to %s\n", teePath); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2961 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2962 | } else { |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2963 | if (fd >= 0) { |
Elliott Hughes | 8b5f642 | 2014-05-22 01:22:06 -0700 | [diff] [blame] | 2964 | dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno)); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2965 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2966 | } |
| 2967 | } |
| 2968 | } |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 2969 | #endif |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2970 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2971 | // ---------------------------------------------------------------------------- |
| 2972 | |
| 2973 | status_t AudioFlinger::onTransact( |
| 2974 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 2975 | { |
| 2976 | return BnAudioFlinger::onTransact(code, data, reply, flags); |
| 2977 | } |
| 2978 | |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 2979 | } // namespace android |