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