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