The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2008, 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 | //#define LOG_NDEBUG 0 |
| 19 | #define LOG_TAG "AudioRecord" |
| 20 | |
Mark Salyzyn | 34fb296 | 2014-06-18 16:30:56 -0700 | [diff] [blame] | 21 | #include <inttypes.h> |
Chih-Hung Hsieh | ffe3558 | 2018-09-13 13:59:28 -0700 | [diff] [blame] | 22 | #include <android-base/macros.h> |
Andy Hung | b58f2c5 | 2022-01-10 00:00:15 -0800 | [diff] [blame] | 23 | #include <android-base/stringprintf.h> |
Glenn Kasten | 868a6a3 | 2012-06-21 16:22:09 -0700 | [diff] [blame] | 24 | #include <sys/resource.h> |
Mark Salyzyn | 34fb296 | 2014-06-18 16:30:56 -0700 | [diff] [blame] | 25 | |
jiabin | b00edc3 | 2021-08-16 16:27:54 +0000 | [diff] [blame] | 26 | #include <audio_utils/format.h> |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 27 | #include <audiomanager/AudioManager.h> |
| 28 | #include <audiomanager/IAudioManager.h> |
| 29 | #include <binder/Binder.h> |
Glenn Kasten | 868a6a3 | 2012-06-21 16:22:09 -0700 | [diff] [blame] | 30 | #include <binder/IPCThreadState.h> |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 31 | #include <binder/IServiceManager.h> |
Glenn Kasten | 868a6a3 | 2012-06-21 16:22:09 -0700 | [diff] [blame] | 32 | #include <media/AudioRecord.h> |
Glenn Kasten | 868a6a3 | 2012-06-21 16:22:09 -0700 | [diff] [blame] | 33 | #include <utils/Log.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | #include <private/media/AudioTrackShared.h> |
Suren Baghdasaryan | 7435e7d | 2018-12-19 17:09:28 -0800 | [diff] [blame] | 35 | #include <processgroup/sched_policy.h> |
Glenn Kasten | 1ab85ec | 2013-05-31 09:18:43 -0700 | [diff] [blame] | 36 | #include <media/IAudioFlinger.h> |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 37 | #include <media/MediaMetricsItem.h> |
Ray Essick | 734d186 | 2018-01-09 10:42:14 -0800 | [diff] [blame] | 38 | #include <media/TypeConverter.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 40 | #define WAIT_PERIOD_MS 10 |
| 41 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | namespace android { |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 43 | |
Andy Hung | b58f2c5 | 2022-01-10 00:00:15 -0800 | [diff] [blame] | 44 | using ::android::base::StringPrintf; |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 45 | using android::content::AttributionSourceState; |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 46 | using aidl_utils::statusTFromBinderStatus; |
| 47 | |
Chia-chi Yeh | 15304d6 | 2010-06-22 08:01:13 +0800 | [diff] [blame] | 48 | // --------------------------------------------------------------------------- |
| 49 | |
| 50 | // static |
| 51 | status_t AudioRecord::getMinFrameCount( |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 52 | size_t* frameCount, |
Chia-chi Yeh | 15304d6 | 2010-06-22 08:01:13 +0800 | [diff] [blame] | 53 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 54 | audio_format_t format, |
Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 55 | audio_channel_mask_t channelMask) |
Chia-chi Yeh | 15304d6 | 2010-06-22 08:01:13 +0800 | [diff] [blame] | 56 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 57 | if (frameCount == NULL) { |
| 58 | return BAD_VALUE; |
| 59 | } |
Glenn Kasten | 04cd018 | 2012-06-25 11:49:27 -0700 | [diff] [blame] | 60 | |
Glenn Kasten | b42f318 | 2014-02-24 13:42:58 -0800 | [diff] [blame] | 61 | size_t size; |
Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 62 | status_t status = AudioSystem::getInputBufferSize(sampleRate, format, channelMask, &size); |
| 63 | if (status != NO_ERROR) { |
Andy Hung | 6b1c612 | 2018-09-12 19:09:07 -0700 | [diff] [blame] | 64 | ALOGE("%s(): AudioSystem could not query the input buffer size for" |
| 65 | " sampleRate %u, format %#x, channelMask %#x; status %d", |
| 66 | __func__, sampleRate, format, channelMask, status); |
Glenn Kasten | b42f318 | 2014-02-24 13:42:58 -0800 | [diff] [blame] | 67 | return status; |
Chia-chi Yeh | 15304d6 | 2010-06-22 08:01:13 +0800 | [diff] [blame] | 68 | } |
| 69 | |
Glenn Kasten | b42f318 | 2014-02-24 13:42:58 -0800 | [diff] [blame] | 70 | // We double the size of input buffer for ping pong use of record buffer. |
Dean Wheatley | d883e30 | 2023-10-20 06:11:43 +1100 | [diff] [blame] | 71 | const auto frameSize = audio_bytes_per_frame( |
| 72 | audio_channel_count_from_in_mask(channelMask), format); |
| 73 | if (frameSize == 0 || ((*frameCount = (size * 2) / frameSize) == 0)) { |
Andy Hung | 6b1c612 | 2018-09-12 19:09:07 -0700 | [diff] [blame] | 74 | ALOGE("%s(): Unsupported configuration: sampleRate %u, format %#x, channelMask %#x", |
| 75 | __func__, sampleRate, format, channelMask); |
Chia-chi Yeh | 15304d6 | 2010-06-22 08:01:13 +0800 | [diff] [blame] | 76 | return BAD_VALUE; |
| 77 | } |
| 78 | |
Chia-chi Yeh | 15304d6 | 2010-06-22 08:01:13 +0800 | [diff] [blame] | 79 | return NO_ERROR; |
| 80 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | |
| 82 | // --------------------------------------------------------------------------- |
| 83 | |
Ray Essick | 734d186 | 2018-01-09 10:42:14 -0800 | [diff] [blame] | 84 | void AudioRecord::MediaMetrics::gather(const AudioRecord *record) |
| 85 | { |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 86 | #define MM_PREFIX "android.media.audiorecord." // avoid cut-n-paste errors. |
Ray Essick | 734d186 | 2018-01-09 10:42:14 -0800 | [diff] [blame] | 87 | |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 88 | // Java API 28 entries, do not change. |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 89 | mMetricsItem->setCString(MM_PREFIX "encoding", toString(record->mFormat).c_str()); |
| 90 | mMetricsItem->setCString(MM_PREFIX "source", toString(record->mAttributes.source).c_str()); |
| 91 | mMetricsItem->setInt32(MM_PREFIX "latency", (int32_t)record->mLatency); // bad estimate. |
| 92 | mMetricsItem->setInt32(MM_PREFIX "samplerate", (int32_t)record->mSampleRate); |
| 93 | mMetricsItem->setInt32(MM_PREFIX "channels", (int32_t)record->mChannelCount); |
Ray Essick | 734d186 | 2018-01-09 10:42:14 -0800 | [diff] [blame] | 94 | |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 95 | // Non-API entries, these can change. |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 96 | mMetricsItem->setInt32(MM_PREFIX "portId", (int32_t)record->mPortId); |
| 97 | mMetricsItem->setInt32(MM_PREFIX "frameCount", (int32_t)record->mFrameCount); |
| 98 | mMetricsItem->setCString(MM_PREFIX "attributes", toString(record->mAttributes).c_str()); |
| 99 | mMetricsItem->setInt64(MM_PREFIX "channelMask", (int64_t)record->mChannelMask); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 100 | |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 101 | // log total duration recording, including anything currently running. |
| 102 | int64_t activeNs = 0; |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 103 | if (mStartedNs != 0) { |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 104 | activeNs = systemTime() - mStartedNs; |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 105 | } |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 106 | mMetricsItem->setDouble(MM_PREFIX "durationMs", (mDurationNs + activeNs) * 1e-6); |
| 107 | mMetricsItem->setInt64(MM_PREFIX "startCount", (int64_t)mCount); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 108 | |
| 109 | if (mLastError != NO_ERROR) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 110 | mMetricsItem->setInt32(MM_PREFIX "lastError.code", (int32_t)mLastError); |
| 111 | mMetricsItem->setCString(MM_PREFIX "lastError.at", mLastErrorFunc.c_str()); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 112 | } |
Andy Hung | 1a9c21b | 2021-02-25 20:43:18 -0800 | [diff] [blame] | 113 | mMetricsItem->setCString(MM_PREFIX "logSessionId", record->mLogSessionId.c_str()); |
Ray Essick | 8839430 | 2018-01-24 14:52:05 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 116 | static const char *stateToString(bool active) { |
| 117 | return active ? "ACTIVE" : "STOPPED"; |
| 118 | } |
| 119 | |
Ray Essick | 8839430 | 2018-01-24 14:52:05 -0800 | [diff] [blame] | 120 | // hand the user a snapshot of the metrics. |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 121 | status_t AudioRecord::getMetrics(mediametrics::Item * &item) |
Ray Essick | 8839430 | 2018-01-24 14:52:05 -0800 | [diff] [blame] | 122 | { |
| 123 | mMediaMetrics.gather(this); |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 124 | mediametrics::Item *tmp = mMediaMetrics.dup(); |
Ray Essick | 8839430 | 2018-01-24 14:52:05 -0800 | [diff] [blame] | 125 | if (tmp == nullptr) { |
| 126 | return BAD_VALUE; |
| 127 | } |
| 128 | item = tmp; |
| 129 | return NO_ERROR; |
Ray Essick | 734d186 | 2018-01-09 10:42:14 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 132 | AudioRecord::AudioRecord(const AttributionSourceState &client) |
| 133 | : mActive(false), mStatus(NO_INIT), mClientAttributionSource(client), |
| 134 | mSessionId(AUDIO_SESSION_ALLOCATE), mPreviousPriority(ANDROID_PRIORITY_NORMAL), |
| 135 | mPreviousSchedulingGroup(SP_DEFAULT), mSelectedDeviceId(AUDIO_PORT_HANDLE_NONE), |
| 136 | mRoutedDeviceId(AUDIO_PORT_HANDLE_NONE), mSelectedMicDirection(MIC_DIRECTION_UNSPECIFIED), |
Paul McLean | 366b643 | 2019-02-25 10:35:51 -0700 | [diff] [blame] | 137 | mSelectedMicFieldDimension(MIC_FIELD_DIMENSION_DEFAULT) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | { |
| 139 | } |
| 140 | |
| 141 | AudioRecord::AudioRecord( |
Glenn Kasten | eba51fb | 2012-01-23 13:58:49 -0800 | [diff] [blame] | 142 | audio_source_t inputSource, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 144 | audio_format_t format, |
Glenn Kasten | 624a7fc | 2012-06-21 16:24:00 -0700 | [diff] [blame] | 145 | audio_channel_mask_t channelMask, |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 146 | const AttributionSourceState& client, |
Glenn Kasten | bce50bf | 2014-02-27 15:29:51 -0800 | [diff] [blame] | 147 | size_t frameCount, |
Atneya Nair | b249d27 | 2021-11-29 18:06:46 -0500 | [diff] [blame] | 148 | const wp<IAudioRecordCallback>& callback, |
| 149 | uint32_t notificationFrames, |
| 150 | audio_session_t sessionId, |
| 151 | transfer_type transferType, |
| 152 | audio_input_flags_t flags, |
| 153 | const audio_attributes_t* pAttributes, |
| 154 | audio_port_handle_t selectedDeviceId, |
| 155 | audio_microphone_direction_t selectedMicDirection, |
| 156 | float microphoneFieldDimension) |
| 157 | : mActive(false), |
| 158 | mStatus(NO_INIT), |
| 159 | mClientAttributionSource(client), |
| 160 | mSessionId(AUDIO_SESSION_ALLOCATE), |
| 161 | mPreviousPriority(ANDROID_PRIORITY_NORMAL), |
| 162 | mPreviousSchedulingGroup(SP_DEFAULT), |
| 163 | mProxy(nullptr) |
| 164 | { |
| 165 | uid_t uid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(mClientAttributionSource.uid)); |
| 166 | pid_t pid = VALUE_OR_FATAL(aidl2legacy_int32_t_pid_t(mClientAttributionSource.pid)); |
| 167 | (void)set(inputSource, sampleRate, format, channelMask, frameCount, callback, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 168 | notificationFrames, false /*threadCanCallJava*/, sessionId, transferType, flags, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 169 | uid, pid, pAttributes, selectedDeviceId, selectedMicDirection, |
| 170 | microphoneFieldDimension); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | AudioRecord::~AudioRecord() |
| 174 | { |
Ray Essick | 734d186 | 2018-01-09 10:42:14 -0800 | [diff] [blame] | 175 | mMediaMetrics.gather(this); |
| 176 | |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 177 | mediametrics::LogItem(mMetricsId) |
| 178 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_DTOR) |
Phil Burk | d3813f3 | 2020-04-23 16:26:15 -0700 | [diff] [blame] | 179 | .set(AMEDIAMETRICS_PROP_CALLERNAME, |
| 180 | mCallerName.empty() |
Andy Hung | a6b2703 | 2020-04-27 10:34:24 -0700 | [diff] [blame] | 181 | ? AMEDIAMETRICS_PROP_CALLERNAME_VALUE_UNKNOWN |
Phil Burk | d3813f3 | 2020-04-23 16:26:15 -0700 | [diff] [blame] | 182 | : mCallerName.c_str()) |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 183 | .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)mStatus) |
| 184 | .record(); |
| 185 | |
Phil Burk | 7a9577c | 2021-03-12 20:12:11 +0000 | [diff] [blame] | 186 | stopAndJoinCallbacks(); // checks mStatus |
| 187 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | if (mStatus == NO_ERROR) { |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 189 | IInterface::asBinder(mAudioRecord)->unlinkToDeath(mDeathNotifier, this); |
Glenn Kasten | 089e872 | 2014-02-24 15:12:48 -0800 | [diff] [blame] | 190 | mAudioRecord.clear(); |
Eric Laurent | 3bcffa1 | 2014-06-12 18:38:45 -0700 | [diff] [blame] | 191 | mCblkMemory.clear(); |
| 192 | mBufferMemory.clear(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | IPCThreadState::self()->flushCommands(); |
Andy Hung | 6b1c612 | 2018-09-12 19:09:07 -0700 | [diff] [blame] | 194 | ALOGV("%s(%d): releasing session id %d", |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 195 | __func__, mPortId, mSessionId); |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 196 | pid_t pid = VALUE_OR_FATAL(aidl2legacy_int32_t_pid_t(mClientAttributionSource.pid)); |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 197 | AudioSystem::releaseAudioSessionId(mSessionId, pid); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Phil Burk | 7a9577c | 2021-03-12 20:12:11 +0000 | [diff] [blame] | 201 | void AudioRecord::stopAndJoinCallbacks() { |
| 202 | // Prevent nullptr crash if it did not open properly. |
| 203 | if (mStatus != NO_ERROR) return; |
| 204 | |
| 205 | // Make sure that callback function exits in the case where |
| 206 | // it is looping on buffer empty condition in obtainBuffer(). |
| 207 | // Otherwise the callback thread will never exit. |
| 208 | stop(); |
| 209 | if (mAudioRecordThread != 0) { |
Phil Burk | 7a9577c | 2021-03-12 20:12:11 +0000 | [diff] [blame] | 210 | mAudioRecordThread->requestExit(); // see comment in AudioRecord.h |
Andy Hung | 21f819c | 2021-12-02 10:51:40 -0800 | [diff] [blame] | 211 | mProxy->interrupt(); |
Phil Burk | 7a9577c | 2021-03-12 20:12:11 +0000 | [diff] [blame] | 212 | mAudioRecordThread->requestExitAndWait(); |
| 213 | mAudioRecordThread.clear(); |
| 214 | } |
zhenjun.zhang | 116df6a | 2021-12-08 09:17:13 +0800 | [diff] [blame] | 215 | |
| 216 | AutoMutex lock(mLock); |
Phil Burk | 7a9577c | 2021-03-12 20:12:11 +0000 | [diff] [blame] | 217 | if (mDeviceCallback != 0 && mInput != AUDIO_IO_HANDLE_NONE) { |
| 218 | // This may not stop all of these device callbacks! |
| 219 | // TODO: Add some sort of protection. |
| 220 | AudioSystem::removeAudioDeviceCallback(this, mInput, mPortId); |
zhenjun.zhang | 116df6a | 2021-12-08 09:17:13 +0800 | [diff] [blame] | 221 | mDeviceCallback.clear(); |
Phil Burk | 7a9577c | 2021-03-12 20:12:11 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | status_t AudioRecord::set( |
Glenn Kasten | eba51fb | 2012-01-23 13:58:49 -0800 | [diff] [blame] | 225 | audio_source_t inputSource, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 227 | audio_format_t format, |
Glenn Kasten | 624a7fc | 2012-06-21 16:24:00 -0700 | [diff] [blame] | 228 | audio_channel_mask_t channelMask, |
Glenn Kasten | bce50bf | 2014-02-27 15:29:51 -0800 | [diff] [blame] | 229 | size_t frameCount, |
Atneya Nair | b249d27 | 2021-11-29 18:06:46 -0500 | [diff] [blame] | 230 | const wp<IAudioRecordCallback>& callback, |
Glenn Kasten | 838b3d8 | 2014-02-27 15:30:41 -0800 | [diff] [blame] | 231 | uint32_t notificationFrames, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 232 | bool threadCanCallJava, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 233 | audio_session_t sessionId, |
Glenn Kasten | 27f7b2a | 2013-07-31 16:10:22 -0700 | [diff] [blame] | 234 | transfer_type transferType, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 235 | audio_input_flags_t flags, |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 236 | uid_t uid, |
Jean-Michel Trivi | 4cb6683 | 2015-05-01 18:34:17 -0700 | [diff] [blame] | 237 | pid_t pid, |
jiabin | fec2f93 | 2017-10-24 10:10:53 -0700 | [diff] [blame] | 238 | const audio_attributes_t* pAttributes, |
Paul McLean | 366b643 | 2019-02-25 10:35:51 -0700 | [diff] [blame] | 239 | audio_port_handle_t selectedDeviceId, |
| 240 | audio_microphone_direction_t selectedMicDirection, |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 241 | float microphoneFieldDimension, |
| 242 | int32_t maxSharedAudioHistoryMs) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | { |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 244 | status_t status = NO_ERROR; |
Atneya Nair | c2f0db7 | 2022-02-28 16:00:51 -0500 | [diff] [blame] | 245 | LOG_ALWAYS_FATAL_IF(mInitialized, "%s: should not be called twice", __func__); |
| 246 | mInitialized = true; |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 247 | // Note mPortId is not valid until the track is created, so omit mPortId in ALOG for set. |
Andy Hung | 6b1c612 | 2018-09-12 19:09:07 -0700 | [diff] [blame] | 248 | ALOGV("%s(): inputSource %d, sampleRate %u, format %#x, channelMask %#x, frameCount %zu, " |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 249 | "notificationFrames %u, sessionId %d, transferType %d, flags %#x, attributionSource %s" |
Jean-Michel Trivi | 4cb6683 | 2015-05-01 18:34:17 -0700 | [diff] [blame] | 250 | "uid %d, pid %d", |
Andy Hung | 6b1c612 | 2018-09-12 19:09:07 -0700 | [diff] [blame] | 251 | __func__, |
Glenn Kasten | bce50bf | 2014-02-27 15:29:51 -0800 | [diff] [blame] | 252 | inputSource, sampleRate, format, channelMask, frameCount, notificationFrames, |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 253 | sessionId, transferType, flags, mClientAttributionSource.toString().c_str(), uid, pid); |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 254 | |
| 255 | // TODO b/182392553: refactor or remove |
| 256 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 257 | pid_t myPid = getpid(); |
| 258 | pid_t adjPid = pid; |
| 259 | if (pid == -1 || (callingPid != myPid)) { |
| 260 | adjPid = callingPid; |
| 261 | } |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 262 | mClientAttributionSource.pid = VALUE_OR_FATAL(legacy2aidl_pid_t_int32_t(adjPid)); |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 263 | |
| 264 | uid_t adjUid = uid; |
| 265 | if (uid == -1 || (callingPid != myPid)) { |
| 266 | adjUid = IPCThreadState::self()->getCallingUid(); |
| 267 | } |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 268 | mClientAttributionSource.uid = VALUE_OR_FATAL(legacy2aidl_uid_t_int32_t(adjUid)); |
Glenn Kasten | 86f0466 | 2014-02-24 15:13:05 -0800 | [diff] [blame] | 269 | |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 270 | mTracker.reset(new RecordingActivityTracker()); |
| 271 | |
jiabin | fec2f93 | 2017-10-24 10:10:53 -0700 | [diff] [blame] | 272 | mSelectedDeviceId = selectedDeviceId; |
Paul McLean | 366b643 | 2019-02-25 10:35:51 -0700 | [diff] [blame] | 273 | mSelectedMicDirection = selectedMicDirection; |
| 274 | mSelectedMicFieldDimension = microphoneFieldDimension; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 275 | mMaxSharedAudioHistoryMs = maxSharedAudioHistoryMs; |
jiabin | fec2f93 | 2017-10-24 10:10:53 -0700 | [diff] [blame] | 276 | |
Andy Hung | b58f2c5 | 2022-01-10 00:00:15 -0800 | [diff] [blame] | 277 | std::string errorMessage; |
| 278 | // Copy the state variables early so they are available for error reporting. |
Atneya Nair | b249d27 | 2021-11-29 18:06:46 -0500 | [diff] [blame] | 279 | if (pAttributes == nullptr) { |
Eric Laurent | d17c850 | 2019-10-24 15:58:35 -0700 | [diff] [blame] | 280 | mAttributes = AUDIO_ATTRIBUTES_INITIALIZER; |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 281 | mAttributes.source = inputSource; |
Eric Laurent | d17c850 | 2019-10-24 15:58:35 -0700 | [diff] [blame] | 282 | if (inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION |
| 283 | || inputSource == AUDIO_SOURCE_CAMCORDER) { |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 284 | mAttributes.flags = static_cast<audio_flags_mask_t>( |
| 285 | mAttributes.flags | AUDIO_FLAG_CAPTURE_PRIVATE); |
Eric Laurent | d17c850 | 2019-10-24 15:58:35 -0700 | [diff] [blame] | 286 | } |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 287 | } else { |
| 288 | // stream type shouldn't be looked at, this track has audio attributes |
| 289 | memcpy(&mAttributes, pAttributes, sizeof(audio_attributes_t)); |
Andy Hung | b58f2c5 | 2022-01-10 00:00:15 -0800 | [diff] [blame] | 290 | ALOGV("%s: Building AudioRecord with attributes: source=%d flags=0x%x tags=[%s]", |
Andy Hung | 6b1c612 | 2018-09-12 19:09:07 -0700 | [diff] [blame] | 291 | __func__, mAttributes.source, mAttributes.flags, mAttributes.tags); |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 292 | } |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 293 | mSampleRate = sampleRate; |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 294 | if (format == AUDIO_FORMAT_DEFAULT) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 295 | format = AUDIO_FORMAT_PCM_16_BIT; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | } |
Atneya Nair | 497fff1 | 2022-01-18 16:23:04 -0500 | [diff] [blame] | 297 | if (!audio_is_linear_pcm(format)) { |
| 298 | // Compressed capture requires direct |
| 299 | flags = (audio_input_flags_t) (flags | AUDIO_INPUT_FLAG_DIRECT); |
| 300 | ALOGI("%s(): Format %#x is not linear pcm. Setting DIRECT, using flags %#x", __func__, |
| 301 | format, flags); |
| 302 | } |
Glenn Kasten | 2e46646 | 2013-01-10 14:26:24 -0800 | [diff] [blame] | 303 | mFormat = format; |
Glenn Kasten | a42ff00 | 2012-11-14 12:47:55 -0800 | [diff] [blame] | 304 | mChannelMask = channelMask; |
Andy Hung | b58f2c5 | 2022-01-10 00:00:15 -0800 | [diff] [blame] | 305 | mSessionId = sessionId; |
| 306 | ALOGV("%s: mSessionId %d", __func__, mSessionId); |
| 307 | mOrigFlags = mFlags = flags; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 308 | |
Andy Hung | b58f2c5 | 2022-01-10 00:00:15 -0800 | [diff] [blame] | 309 | mTransfer = transferType; |
| 310 | switch (mTransfer) { |
| 311 | case TRANSFER_DEFAULT: |
Atneya Nair | 2eedae3 | 2022-02-14 16:42:23 -0500 | [diff] [blame] | 312 | if (callback == nullptr || threadCanCallJava) { |
Andy Hung | b58f2c5 | 2022-01-10 00:00:15 -0800 | [diff] [blame] | 313 | mTransfer = TRANSFER_SYNC; |
| 314 | } else { |
| 315 | mTransfer = TRANSFER_CALLBACK; |
| 316 | } |
| 317 | break; |
| 318 | case TRANSFER_CALLBACK: |
Atneya Nair | 2eedae3 | 2022-02-14 16:42:23 -0500 | [diff] [blame] | 319 | if (callback == nullptr) { |
Andy Hung | b58f2c5 | 2022-01-10 00:00:15 -0800 | [diff] [blame] | 320 | errorMessage = StringPrintf( |
| 321 | "%s: Transfer type TRANSFER_CALLBACK but callback == nullptr", __func__); |
| 322 | status = BAD_VALUE; |
| 323 | goto error; |
| 324 | } |
| 325 | break; |
| 326 | case TRANSFER_OBTAIN: |
| 327 | case TRANSFER_SYNC: |
| 328 | break; |
| 329 | default: |
| 330 | errorMessage = StringPrintf("%s: Invalid transfer type %d", __func__, mTransfer); |
| 331 | status = BAD_VALUE; |
| 332 | goto error; |
| 333 | } |
| 334 | |
| 335 | // invariant that mAudioRecord != 0 is true only after set() returns successfully |
| 336 | if (mAudioRecord != 0) { |
| 337 | errorMessage = StringPrintf("%s: Track already in use", __func__); |
| 338 | status = INVALID_OPERATION; |
| 339 | goto error; |
| 340 | } |
| 341 | |
Atneya Nair | 497fff1 | 2022-01-18 16:23:04 -0500 | [diff] [blame] | 342 | if (!audio_is_valid_format(mFormat)) { |
| 343 | errorMessage = StringPrintf("%s: Format %#x is not valid", __func__, mFormat); |
Andy Hung | b58f2c5 | 2022-01-10 00:00:15 -0800 | [diff] [blame] | 344 | status = BAD_VALUE; |
| 345 | goto error; |
| 346 | } |
| 347 | |
| 348 | if (!audio_is_input_channel(mChannelMask)) { |
| 349 | errorMessage = StringPrintf("%s: Invalid channel mask %#x", __func__, mChannelMask); |
| 350 | status = BAD_VALUE; |
| 351 | goto error; |
| 352 | } |
| 353 | |
| 354 | mChannelCount = audio_channel_count_from_in_mask(mChannelMask); |
Dean Wheatley | d883e30 | 2023-10-20 06:11:43 +1100 | [diff] [blame] | 355 | mFrameSize = audio_bytes_per_frame(mChannelCount, mFormat); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 356 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 357 | // mFrameCount is initialized in createRecord_l |
Glenn Kasten | b3b2e23 | 2014-02-05 11:10:26 -0800 | [diff] [blame] | 358 | mReqFrameCount = frameCount; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | |
Glenn Kasten | 4a0efb7 | 2013-08-08 15:20:53 -0700 | [diff] [blame] | 360 | mNotificationFramesReq = notificationFrames; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 361 | // mNotificationFramesAct is initialized in createRecord_l |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | |
Atneya Nair | 2eedae3 | 2022-02-14 16:42:23 -0500 | [diff] [blame] | 363 | mCallback = callback; |
Atneya Nair | b249d27 | 2021-11-29 18:06:46 -0500 | [diff] [blame] | 364 | if (mCallback != nullptr) { |
Andy Hung | ca35367 | 2019-03-06 11:54:38 -0800 | [diff] [blame] | 365 | mAudioRecordThread = new AudioRecordThread(*this); |
Glenn Kasten | 68337ed | 2012-07-12 09:05:58 -0700 | [diff] [blame] | 366 | mAudioRecordThread->run("AudioRecord", ANDROID_PRIORITY_AUDIO); |
Glenn Kasten | bfd3184 | 2015-03-20 09:01:44 -0700 | [diff] [blame] | 367 | // thread begins in paused state, and will not reference us until start() |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | } |
| 369 | |
Glenn Kasten | 8797070 | 2014-02-27 15:31:39 -0800 | [diff] [blame] | 370 | // create the IAudioRecord |
Francois Gaffie | 24a9fb0 | 2019-01-18 17:51:34 +0100 | [diff] [blame] | 371 | { |
| 372 | AutoMutex lock(mLock); |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 373 | status = createRecord_l(0 /*epoch*/); |
Francois Gaffie | 24a9fb0 | 2019-01-18 17:51:34 +0100 | [diff] [blame] | 374 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 376 | ALOGV("%s(%d): status %d", __func__, mPortId, status); |
Andy Hung | 6b1c612 | 2018-09-12 19:09:07 -0700 | [diff] [blame] | 377 | |
Glenn Kasten | 8797070 | 2014-02-27 15:31:39 -0800 | [diff] [blame] | 378 | if (status != NO_ERROR) { |
| 379 | if (mAudioRecordThread != 0) { |
| 380 | mAudioRecordThread->requestExit(); // see comment in AudioRecord.h |
| 381 | mAudioRecordThread->requestExitAndWait(); |
| 382 | mAudioRecordThread.clear(); |
| 383 | } |
Andy Hung | b58f2c5 | 2022-01-10 00:00:15 -0800 | [diff] [blame] | 384 | // bypass error message to avoid logging twice (createRecord_l logs the error). |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 385 | goto exit; |
Glenn Kasten | 8797070 | 2014-02-27 15:31:39 -0800 | [diff] [blame] | 386 | } |
| 387 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | // TODO: add audio hardware input latency here |
Andy Hung | 1396926 | 2017-09-11 17:24:21 -0700 | [diff] [blame] | 389 | mLatency = (1000LL * mFrameCount) / mSampleRate; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | mMarkerPosition = 0; |
Jean-Michel Trivi | 7d56324 | 2009-03-24 19:48:58 -0700 | [diff] [blame] | 391 | mMarkerReached = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | mNewPosition = 0; |
| 393 | mUpdatePeriod = 0; |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 394 | AudioSystem::acquireAudioSessionId(mSessionId, adjPid, adjUid); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 395 | mSequence = 1; |
| 396 | mObservedSequence = mSequence; |
| 397 | mInOverrun = false; |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 398 | mFramesRead = 0; |
| 399 | mFramesReadServerOffset = 0; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | |
Andy Hung | b58f2c5 | 2022-01-10 00:00:15 -0800 | [diff] [blame] | 401 | error: |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 402 | if (status != NO_ERROR) { |
| 403 | mMediaMetrics.markError(status, __FUNCTION__); |
Andy Hung | b58f2c5 | 2022-01-10 00:00:15 -0800 | [diff] [blame] | 404 | ALOGE_IF(!errorMessage.empty(), "%s", errorMessage.c_str()); |
| 405 | reportError(status, AMEDIAMETRICS_PROP_EVENT_VALUE_CREATE, errorMessage.c_str()); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 406 | } |
Andy Hung | b58f2c5 | 2022-01-10 00:00:15 -0800 | [diff] [blame] | 407 | exit: |
| 408 | mStatus = status; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 409 | return status; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | } |
| 411 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | // ------------------------------------------------------------------------- |
| 413 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 414 | status_t AudioRecord::start(AudioSystem::sync_event_t event, audio_session_t triggerSession) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | { |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 416 | const int64_t beginNs = systemTime(); |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 417 | ALOGV("%s(%d): sync event %d trigger session %d", __func__, mPortId, event, triggerSession); |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 418 | AutoMutex lock(mLock); |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 419 | |
| 420 | status_t status = NO_ERROR; |
Andy Hung | 06a730b | 2020-04-09 13:28:31 -0700 | [diff] [blame] | 421 | mediametrics::Defer defer([&] { |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 422 | mediametrics::LogItem(mMetricsId) |
Andy Hung | a6b2703 | 2020-04-27 10:34:24 -0700 | [diff] [blame] | 423 | .set(AMEDIAMETRICS_PROP_CALLERNAME, |
| 424 | mCallerName.empty() |
| 425 | ? AMEDIAMETRICS_PROP_CALLERNAME_VALUE_UNKNOWN |
| 426 | : mCallerName.c_str()) |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 427 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_START) |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 428 | .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(systemTime() - beginNs)) |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 429 | .set(AMEDIAMETRICS_PROP_STATE, stateToString(mActive)) |
| 430 | .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)status) |
| 431 | .record(); }); |
| 432 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 433 | if (mActive) { |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 434 | return status; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 435 | } |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 436 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 437 | // discard data in buffer |
| 438 | const uint32_t framesFlushed = mProxy->flush(); |
| 439 | mFramesReadServerOffset -= mFramesRead + framesFlushed; |
| 440 | mFramesRead = 0; |
| 441 | mProxy->clearTimestamp(); // timestamp is invalid until next server push |
Andy Hung | 7efe2b8 | 2020-03-31 10:24:42 -0700 | [diff] [blame] | 442 | mPreviousTimestamp.clear(); |
| 443 | mTimestampRetrogradePositionReported = false; |
| 444 | mTimestampRetrogradeTimeReported = false; |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 445 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 446 | // reset current position as seen by client to 0 |
| 447 | mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition()); |
Glenn Kasten | 5e1f79b | 2014-01-14 15:45:21 -0800 | [diff] [blame] | 448 | // force refresh of remaining frames by processAudioBuffer() as last |
| 449 | // read before stop could be partial. |
| 450 | mRefreshRemaining = true; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 451 | |
| 452 | mNewPosition = mProxy->getPosition() + mUpdatePeriod; |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 453 | int32_t flags = android_atomic_acquire_load(&mCblk->mFlags); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 454 | |
Andy Hung | 3da23b2 | 2015-10-23 18:11:28 -0700 | [diff] [blame] | 455 | // we reactivate markers (mMarkerPosition != 0) as the position is reset to 0. |
| 456 | // This is legacy behavior. This is not done in stop() to avoid a race condition |
| 457 | // where the last marker event is issued twice. |
| 458 | mMarkerReached = false; |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 459 | // mActive is checked by restoreRecord_l |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 460 | mActive = true; |
| 461 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 462 | if (!(flags & CBLK_INVALID)) { |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 463 | status = statusTFromBinderStatus(mAudioRecord->start(event, triggerSession)); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 464 | if (status == DEAD_OBJECT) { |
| 465 | flags |= CBLK_INVALID; |
| 466 | } |
| 467 | } |
| 468 | if (flags & CBLK_INVALID) { |
| 469 | status = restoreRecord_l("start"); |
| 470 | } |
| 471 | |
Paul McLean | 366b643 | 2019-02-25 10:35:51 -0700 | [diff] [blame] | 472 | // Call these directly because we are already holding the lock. |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 473 | mAudioRecord->setPreferredMicrophoneDirection(mSelectedMicDirection); |
| 474 | mAudioRecord->setPreferredMicrophoneFieldDimension(mSelectedMicFieldDimension); |
Paul McLean | 366b643 | 2019-02-25 10:35:51 -0700 | [diff] [blame] | 475 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 476 | if (status != NO_ERROR) { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 477 | mActive = false; |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 478 | ALOGE("%s(%d): status %d", __func__, mPortId, status); |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 479 | mMediaMetrics.markError(status, __FUNCTION__); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 480 | } else { |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 481 | mTracker->recordingStarted(); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 482 | sp<AudioRecordThread> t = mAudioRecordThread; |
| 483 | if (t != 0) { |
| 484 | t->resume(); |
Eric Laurent | 6100d2d | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 485 | } else { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 486 | mPreviousPriority = getpriority(PRIO_PROCESS, 0); |
| 487 | get_sched_policy(0, &mPreviousSchedulingGroup); |
| 488 | androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO); |
Eric Laurent | 6100d2d | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 489 | } |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 490 | |
| 491 | // we've successfully started, log that time |
| 492 | mMediaMetrics.logStart(systemTime()); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 494 | return status; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | } |
| 496 | |
Glenn Kasten | d64cd23 | 2012-02-21 10:21:23 -0800 | [diff] [blame] | 497 | void AudioRecord::stop() |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | { |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 499 | const int64_t beginNs = systemTime(); |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 500 | AutoMutex lock(mLock); |
Andy Hung | 06a730b | 2020-04-09 13:28:31 -0700 | [diff] [blame] | 501 | mediametrics::Defer defer([&] { |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 502 | mediametrics::LogItem(mMetricsId) |
| 503 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_STOP) |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 504 | .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(systemTime() - beginNs)) |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 505 | .set(AMEDIAMETRICS_PROP_STATE, stateToString(mActive)) |
| 506 | .record(); }); |
| 507 | |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 508 | ALOGV("%s(%d): mActive:%d\n", __func__, mPortId, mActive); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 509 | if (!mActive) { |
| 510 | return; |
| 511 | } |
| 512 | |
| 513 | mActive = false; |
| 514 | mProxy->interrupt(); |
| 515 | mAudioRecord->stop(); |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 516 | mTracker->recordingStopped(); |
Andy Hung | 3da23b2 | 2015-10-23 18:11:28 -0700 | [diff] [blame] | 517 | |
| 518 | // Note: legacy handling - stop does not clear record marker and |
| 519 | // periodic update position; we update those on start(). |
| 520 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 521 | sp<AudioRecordThread> t = mAudioRecordThread; |
| 522 | if (t != 0) { |
| 523 | t->pause(); |
| 524 | } else { |
| 525 | setpriority(PRIO_PROCESS, 0, mPreviousPriority); |
| 526 | set_sched_policy(0, mPreviousSchedulingGroup); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 527 | } |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 528 | |
| 529 | // we've successfully started, log that time |
| 530 | mMediaMetrics.logStop(systemTime()); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | bool AudioRecord::stopped() const |
| 534 | { |
Glenn Kasten | 68337ed | 2012-07-12 09:05:58 -0700 | [diff] [blame] | 535 | AutoMutex lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | return !mActive; |
| 537 | } |
| 538 | |
| 539 | status_t AudioRecord::setMarkerPosition(uint32_t marker) |
| 540 | { |
Atneya Nair | b249d27 | 2021-11-29 18:06:46 -0500 | [diff] [blame] | 541 | AutoMutex lock(mLock); |
Glenn Kasten | 2b2165c | 2014-01-13 08:53:36 -0800 | [diff] [blame] | 542 | // The only purpose of setting marker position is to get a callback |
Atneya Nair | 2eedae3 | 2022-02-14 16:42:23 -0500 | [diff] [blame] | 543 | if (mCallback == nullptr) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 544 | return INVALID_OPERATION; |
| 545 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | |
| 547 | mMarkerPosition = marker; |
Jean-Michel Trivi | 7d56324 | 2009-03-24 19:48:58 -0700 | [diff] [blame] | 548 | mMarkerReached = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | |
Andy Hung | 803b3e5 | 2015-03-18 23:26:55 -0700 | [diff] [blame] | 550 | sp<AudioRecordThread> t = mAudioRecordThread; |
| 551 | if (t != 0) { |
| 552 | t->wake(); |
| 553 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 554 | return NO_ERROR; |
| 555 | } |
| 556 | |
Robert Wu | 310037a | 2022-09-06 21:48:18 +0000 | [diff] [blame] | 557 | uint32_t AudioRecord::getHalSampleRate() const |
| 558 | { |
| 559 | return mHalSampleRate; |
| 560 | } |
| 561 | |
| 562 | uint32_t AudioRecord::getHalChannelCount() const |
| 563 | { |
| 564 | return mHalChannelCount; |
| 565 | } |
| 566 | |
| 567 | audio_format_t AudioRecord::getHalFormat() const |
| 568 | { |
| 569 | return mHalFormat; |
| 570 | } |
| 571 | |
Glenn Kasten | 606ee61 | 2012-02-24 16:33:14 -0800 | [diff] [blame] | 572 | status_t AudioRecord::getMarkerPosition(uint32_t *marker) const |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 573 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 574 | if (marker == NULL) { |
| 575 | return BAD_VALUE; |
| 576 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 577 | |
Glenn Kasten | 955e781 | 2012-02-21 10:32:45 -0800 | [diff] [blame] | 578 | AutoMutex lock(mLock); |
Andy Hung | 90e8a97 | 2015-11-09 16:42:40 -0800 | [diff] [blame] | 579 | mMarkerPosition.getValue(marker); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 580 | |
| 581 | return NO_ERROR; |
| 582 | } |
| 583 | |
| 584 | status_t AudioRecord::setPositionUpdatePeriod(uint32_t updatePeriod) |
| 585 | { |
Atneya Nair | b249d27 | 2021-11-29 18:06:46 -0500 | [diff] [blame] | 586 | AutoMutex lock(mLock); |
Glenn Kasten | 2b2165c | 2014-01-13 08:53:36 -0800 | [diff] [blame] | 587 | // The only purpose of setting position update period is to get a callback |
Atneya Nair | 2eedae3 | 2022-02-14 16:42:23 -0500 | [diff] [blame] | 588 | if (mCallback == nullptr) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 589 | return INVALID_OPERATION; |
| 590 | } |
Glenn Kasten | 955e781 | 2012-02-21 10:32:45 -0800 | [diff] [blame] | 591 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 592 | mNewPosition = mProxy->getPosition() + updatePeriod; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 593 | mUpdatePeriod = updatePeriod; |
| 594 | |
Andy Hung | 803b3e5 | 2015-03-18 23:26:55 -0700 | [diff] [blame] | 595 | sp<AudioRecordThread> t = mAudioRecordThread; |
| 596 | if (t != 0) { |
| 597 | t->wake(); |
| 598 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 599 | return NO_ERROR; |
| 600 | } |
| 601 | |
Glenn Kasten | 606ee61 | 2012-02-24 16:33:14 -0800 | [diff] [blame] | 602 | status_t AudioRecord::getPositionUpdatePeriod(uint32_t *updatePeriod) const |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 603 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 604 | if (updatePeriod == NULL) { |
| 605 | return BAD_VALUE; |
| 606 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 607 | |
Glenn Kasten | 955e781 | 2012-02-21 10:32:45 -0800 | [diff] [blame] | 608 | AutoMutex lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 609 | *updatePeriod = mUpdatePeriod; |
| 610 | |
| 611 | return NO_ERROR; |
| 612 | } |
| 613 | |
Glenn Kasten | 606ee61 | 2012-02-24 16:33:14 -0800 | [diff] [blame] | 614 | status_t AudioRecord::getPosition(uint32_t *position) const |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 615 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 616 | if (position == NULL) { |
| 617 | return BAD_VALUE; |
| 618 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 620 | AutoMutex lock(mLock); |
Andy Hung | 90e8a97 | 2015-11-09 16:42:40 -0800 | [diff] [blame] | 621 | mProxy->getPosition().getValue(position); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 622 | |
| 623 | return NO_ERROR; |
| 624 | } |
| 625 | |
Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 626 | uint32_t AudioRecord::getInputFramesLost() const |
Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 627 | { |
Glenn Kasten | bf04a5d | 2012-07-12 09:07:11 -0700 | [diff] [blame] | 628 | // no need to check mActive, because if inactive this will return 0, which is what we want |
Glenn Kasten | 32860f7 | 2015-03-20 08:55:18 -0700 | [diff] [blame] | 629 | return AudioSystem::getInputFramesLost(getInputPrivate()); |
Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 630 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 631 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 632 | status_t AudioRecord::getTimestamp(ExtendedTimestamp *timestamp) |
| 633 | { |
| 634 | if (timestamp == nullptr) { |
| 635 | return BAD_VALUE; |
| 636 | } |
| 637 | AutoMutex lock(mLock); |
| 638 | status_t status = mProxy->getTimestamp(timestamp); |
| 639 | if (status == OK) { |
| 640 | timestamp->mPosition[ExtendedTimestamp::LOCATION_CLIENT] = mFramesRead; |
| 641 | timestamp->mTimeNs[ExtendedTimestamp::LOCATION_CLIENT] = 0; |
Atneya Nair | 497fff1 | 2022-01-18 16:23:04 -0500 | [diff] [blame] | 642 | if (!audio_is_linear_pcm(mFormat)) { |
| 643 | // Don't do retrograde corrections or server offset if track is |
| 644 | // compressed |
| 645 | return OK; |
| 646 | } |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 647 | // server side frame offset in case AudioRecord has been restored. |
| 648 | for (int i = ExtendedTimestamp::LOCATION_SERVER; |
| 649 | i < ExtendedTimestamp::LOCATION_MAX; ++i) { |
| 650 | if (timestamp->mTimeNs[i] >= 0) { |
| 651 | timestamp->mPosition[i] += mFramesReadServerOffset; |
| 652 | } |
| 653 | } |
Andy Hung | 7efe2b8 | 2020-03-31 10:24:42 -0700 | [diff] [blame] | 654 | |
| 655 | bool timestampRetrogradeTimeReported = false; |
| 656 | bool timestampRetrogradePositionReported = false; |
| 657 | for (int i = 0; i < ExtendedTimestamp::LOCATION_MAX; ++i) { |
| 658 | if (timestamp->mTimeNs[i] >= 0 && mPreviousTimestamp.mTimeNs[i] >= 0) { |
| 659 | if (timestamp->mTimeNs[i] < mPreviousTimestamp.mTimeNs[i]) { |
| 660 | if (!mTimestampRetrogradeTimeReported) { |
| 661 | ALOGD("%s: retrograde time adjusting [%d] current:%lld to previous:%lld", |
| 662 | __func__, i, (long long)timestamp->mTimeNs[i], |
| 663 | (long long)mPreviousTimestamp.mTimeNs[i]); |
| 664 | timestampRetrogradeTimeReported = true; |
| 665 | } |
| 666 | timestamp->mTimeNs[i] = mPreviousTimestamp.mTimeNs[i]; |
| 667 | } |
| 668 | if (timestamp->mPosition[i] < mPreviousTimestamp.mPosition[i]) { |
| 669 | if (!mTimestampRetrogradePositionReported) { |
| 670 | ALOGD("%s: retrograde position" |
| 671 | " adjusting [%d] current:%lld to previous:%lld", |
| 672 | __func__, i, (long long)timestamp->mPosition[i], |
| 673 | (long long)mPreviousTimestamp.mPosition[i]); |
| 674 | timestampRetrogradePositionReported = true; |
| 675 | } |
| 676 | timestamp->mPosition[i] = mPreviousTimestamp.mPosition[i]; |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 | mPreviousTimestamp = *timestamp; |
| 681 | if (timestampRetrogradeTimeReported) { |
| 682 | mTimestampRetrogradeTimeReported = true; |
| 683 | } |
| 684 | if (timestampRetrogradePositionReported) { |
| 685 | mTimestampRetrogradePositionReported = true; |
| 686 | } |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 687 | } |
| 688 | return status; |
| 689 | } |
| 690 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 691 | // ---- Explicit Routing --------------------------------------------------- |
| 692 | status_t AudioRecord::setInputDevice(audio_port_handle_t deviceId) { |
| 693 | AutoMutex lock(mLock); |
Eric Laurent | 2f2c198 | 2021-06-02 14:03:11 +0200 | [diff] [blame] | 694 | ALOGV("%s(%d): deviceId=%d mSelectedDeviceId=%d", |
| 695 | __func__, mPortId, deviceId, mSelectedDeviceId); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 696 | if (mSelectedDeviceId != deviceId) { |
| 697 | mSelectedDeviceId = deviceId; |
Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 698 | if (mStatus == NO_ERROR) { |
| 699 | // stop capture so that audio policy manager does not reject the new instance start request |
| 700 | // as only one capture can be active at a time. |
| 701 | if (mAudioRecord != 0 && mActive) { |
| 702 | mAudioRecord->stop(); |
| 703 | } |
| 704 | android_atomic_or(CBLK_INVALID, &mCblk->mFlags); |
jiabin | fec2f93 | 2017-10-24 10:10:53 -0700 | [diff] [blame] | 705 | mProxy->interrupt(); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 706 | } |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 707 | } |
| 708 | return NO_ERROR; |
| 709 | } |
| 710 | |
| 711 | audio_port_handle_t AudioRecord::getInputDevice() { |
| 712 | AutoMutex lock(mLock); |
| 713 | return mSelectedDeviceId; |
| 714 | } |
| 715 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 716 | // must be called with mLock held |
| 717 | void AudioRecord::updateRoutedDeviceId_l() |
| 718 | { |
| 719 | // if the record is inactive, do not update actual device as the input stream maybe routed |
| 720 | // from a device not relevant to this client because of other active use cases. |
| 721 | if (!mActive) { |
| 722 | return; |
| 723 | } |
| 724 | if (mInput != AUDIO_IO_HANDLE_NONE) { |
| 725 | audio_port_handle_t deviceId = AudioSystem::getDeviceIdForIo(mInput); |
| 726 | if (deviceId != AUDIO_PORT_HANDLE_NONE) { |
| 727 | mRoutedDeviceId = deviceId; |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 732 | audio_port_handle_t AudioRecord::getRoutedDeviceId() { |
| 733 | AutoMutex lock(mLock); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 734 | updateRoutedDeviceId_l(); |
| 735 | return mRoutedDeviceId; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Eric Laurent | 724a181 | 2017-12-06 16:59:56 -0800 | [diff] [blame] | 738 | status_t AudioRecord::dump(int fd, const Vector<String16>& args __unused) const |
| 739 | { |
| 740 | String8 result; |
| 741 | |
| 742 | result.append(" AudioRecord::dump\n"); |
Andy Hung | 6b1c612 | 2018-09-12 19:09:07 -0700 | [diff] [blame] | 743 | result.appendFormat(" id(%d) status(%d), active(%d), session Id(%d)\n", |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 744 | mPortId, mStatus, mActive, mSessionId); |
Eric Laurent | 724a181 | 2017-12-06 16:59:56 -0800 | [diff] [blame] | 745 | result.appendFormat(" flags(%#x), req. flags(%#x), audio source(%d)\n", |
| 746 | mFlags, mOrigFlags, mAttributes.source); |
| 747 | result.appendFormat(" format(%#x), channel mask(%#x), channel count(%u), sample rate(%u)\n", |
| 748 | mFormat, mChannelMask, mChannelCount, mSampleRate); |
| 749 | result.appendFormat(" frame count(%zu), req. frame count(%zu)\n", |
| 750 | mFrameCount, mReqFrameCount); |
| 751 | result.appendFormat(" notif. frame count(%u), req. notif. frame count(%u)\n", |
| 752 | mNotificationFramesAct, mNotificationFramesReq); |
| 753 | result.appendFormat(" input(%d), latency(%u), selected device Id(%d), routed device Id(%d)\n", |
| 754 | mInput, mLatency, mSelectedDeviceId, mRoutedDeviceId); |
Paul McLean | 366b643 | 2019-02-25 10:35:51 -0700 | [diff] [blame] | 755 | result.appendFormat(" mic direction(%d) mic field dimension(%f)", |
| 756 | mSelectedMicDirection, mSelectedMicFieldDimension); |
Tomasz Wasilczyk | 8f36f6e | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 757 | ::write(fd, result.c_str(), result.size()); |
Eric Laurent | 724a181 | 2017-12-06 16:59:56 -0800 | [diff] [blame] | 758 | return NO_ERROR; |
| 759 | } |
| 760 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 761 | // ------------------------------------------------------------------------- |
Phil Burk | adbb75a | 2017-06-16 12:19:42 -0700 | [diff] [blame] | 762 | // TODO Move this macro to a common header file for enum to string conversion in audio framework. |
| 763 | #define MEDIA_CASE_ENUM(name) case name: return #name |
| 764 | const char * AudioRecord::convertTransferToText(transfer_type transferType) { |
| 765 | switch (transferType) { |
| 766 | MEDIA_CASE_ENUM(TRANSFER_DEFAULT); |
| 767 | MEDIA_CASE_ENUM(TRANSFER_CALLBACK); |
| 768 | MEDIA_CASE_ENUM(TRANSFER_OBTAIN); |
| 769 | MEDIA_CASE_ENUM(TRANSFER_SYNC); |
| 770 | default: |
| 771 | return "UNRECOGNIZED"; |
| 772 | } |
| 773 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 774 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 775 | // must be called with mLock held |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 776 | status_t AudioRecord::createRecord_l(const Modulo<uint32_t> &epoch) |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 777 | { |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 778 | const int64_t beginNs = systemTime(); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 779 | const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger(); |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 780 | IAudioFlinger::CreateRecordInput input; |
| 781 | IAudioFlinger::CreateRecordOutput output; |
Atneya Nair | b249d27 | 2021-11-29 18:06:46 -0500 | [diff] [blame] | 782 | [[maybe_unused]] audio_session_t originalSessionId; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 783 | void *iMemPointer; |
| 784 | audio_track_cblk_t* cblk; |
| 785 | status_t status; |
Eric Laurent | d52a28c | 2020-08-21 17:10:39 -0700 | [diff] [blame] | 786 | static const int32_t kMaxCreateAttempts = 3; |
| 787 | int32_t remainingAttempts = kMaxCreateAttempts; |
Andy Hung | b58f2c5 | 2022-01-10 00:00:15 -0800 | [diff] [blame] | 788 | std::string errorMessage; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 789 | |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 790 | if (audioFlinger == 0) { |
Andy Hung | b58f2c5 | 2022-01-10 00:00:15 -0800 | [diff] [blame] | 791 | errorMessage = StringPrintf("%s(%d): Could not get audioflinger", __func__, mPortId); |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 792 | status = NO_INIT; |
| 793 | goto exit; |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 794 | } |
| 795 | |
Glenn Kasten | 58883a0 | 2016-03-08 15:31:49 -0800 | [diff] [blame] | 796 | // mFlags (not mOrigFlags) is modified depending on whether fast request is accepted. |
| 797 | // After fast request is denied, we will request again if IAudioRecord is re-created. |
| 798 | |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 799 | // Now that we have a reference to an I/O handle and have not yet handed it off to AudioFlinger, |
| 800 | // we must release it ourselves if anything goes wrong. |
| 801 | |
Glenn Kasten | 3151427 | 2013-08-01 07:24:34 -0700 | [diff] [blame] | 802 | // Client can only express a preference for FAST. Server will perform additional tests. |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 803 | if (mFlags & AUDIO_INPUT_FLAG_FAST) { |
| 804 | bool useCaseAllowed = |
Phil Burk | 382d11a | 2017-05-22 16:07:37 -0700 | [diff] [blame] | 805 | // any of these use cases: |
Glenn Kasten | b7fbf7e | 2015-03-18 12:57:28 -0700 | [diff] [blame] | 806 | // use case 1: callback transfer mode |
| 807 | (mTransfer == TRANSFER_CALLBACK) || |
Phil Burk | 382d11a | 2017-05-22 16:07:37 -0700 | [diff] [blame] | 808 | // use case 2: blocking read mode |
| 809 | // The default buffer capacity at 48 kHz is 2048 frames, or ~42.6 ms. |
| 810 | // That's enough for double-buffering with our standard 20 ms rule of thumb for |
| 811 | // the minimum period of a non-SCHED_FIFO thread. |
| 812 | // This is needed so that AAudio apps can do a low latency non-blocking read from a |
| 813 | // callback running with SCHED_FIFO. |
| 814 | (mTransfer == TRANSFER_SYNC) || |
| 815 | // use case 3: obtain/release mode |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 816 | (mTransfer == TRANSFER_OBTAIN); |
Phil Burk | adbb75a | 2017-06-16 12:19:42 -0700 | [diff] [blame] | 817 | if (!useCaseAllowed) { |
Phil Burk | cc6ed2d | 2020-05-18 13:06:54 -0700 | [diff] [blame] | 818 | ALOGD("%s(%d): AUDIO_INPUT_FLAG_FAST denied, incompatible transfer = %s", |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 819 | __func__, mPortId, |
Phil Burk | adbb75a | 2017-06-16 12:19:42 -0700 | [diff] [blame] | 820 | convertTransferToText(mTransfer)); |
Glenn Kasten | 2854e51 | 2016-03-11 16:14:06 -0800 | [diff] [blame] | 821 | mFlags = (audio_input_flags_t) (mFlags & ~(AUDIO_INPUT_FLAG_FAST | |
| 822 | AUDIO_INPUT_FLAG_RAW)); |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 823 | } |
Glenn Kasten | c6ba823 | 2014-02-27 13:34:29 -0800 | [diff] [blame] | 824 | } |
| 825 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 826 | input.attr = mAttributes; |
| 827 | input.config.sample_rate = mSampleRate; |
| 828 | input.config.channel_mask = mChannelMask; |
| 829 | input.config.format = mFormat; |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 830 | input.clientInfo.attributionSource = mClientAttributionSource; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 831 | input.clientInfo.clientTid = -1; |
Eric Laurent | 0aa3c6e | 2017-12-08 18:13:22 +0000 | [diff] [blame] | 832 | if (mFlags & AUDIO_INPUT_FLAG_FAST) { |
| 833 | if (mAudioRecordThread != 0) { |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 834 | input.clientInfo.clientTid = mAudioRecordThread->getTid(); |
Eric Laurent | 0aa3c6e | 2017-12-08 18:13:22 +0000 | [diff] [blame] | 835 | } |
| 836 | } |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 837 | input.riid = mTracker->getRiid(); |
Eric Laurent | 0aa3c6e | 2017-12-08 18:13:22 +0000 | [diff] [blame] | 838 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 839 | input.flags = mFlags; |
| 840 | // The notification frame count is the period between callbacks, as suggested by the client |
| 841 | // but moderated by the server. For record, the calculations are done entirely on server side. |
| 842 | input.frameCount = mReqFrameCount; |
| 843 | input.notificationFrameCount = mNotificationFramesReq; |
| 844 | input.selectedDeviceId = mSelectedDeviceId; |
| 845 | input.sessionId = mSessionId; |
| 846 | originalSessionId = mSessionId; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 847 | input.maxSharedAudioHistoryMs = mMaxSharedAudioHistoryMs; |
Eric Laurent | 0aa3c6e | 2017-12-08 18:13:22 +0000 | [diff] [blame] | 848 | |
Eric Laurent | d52a28c | 2020-08-21 17:10:39 -0700 | [diff] [blame] | 849 | do { |
Ytai Ben-Tsvi | 4dfeb62 | 2020-11-02 12:47:30 -0800 | [diff] [blame] | 850 | media::CreateRecordResponse response; |
Ytai Ben-Tsvi | 16d8761 | 2020-11-03 16:32:36 -0800 | [diff] [blame] | 851 | status = audioFlinger->createRecord(VALUE_OR_FATAL(input.toAidl()), response); |
Ytai Ben-Tsvi | 4dfeb62 | 2020-11-02 12:47:30 -0800 | [diff] [blame] | 852 | output = VALUE_OR_FATAL(IAudioFlinger::CreateRecordOutput::fromAidl(response)); |
Eric Laurent | d52a28c | 2020-08-21 17:10:39 -0700 | [diff] [blame] | 853 | if (status == NO_ERROR) { |
| 854 | break; |
| 855 | } |
| 856 | if (status != FAILED_TRANSACTION || --remainingAttempts <= 0) { |
Andy Hung | b58f2c5 | 2022-01-10 00:00:15 -0800 | [diff] [blame] | 857 | errorMessage = StringPrintf( |
| 858 | "%s(%d): AudioFlinger could not create record track, status: %d", |
| 859 | __func__, mPortId, status); |
Eric Laurent | d52a28c | 2020-08-21 17:10:39 -0700 | [diff] [blame] | 860 | goto exit; |
| 861 | } |
| 862 | // FAILED_TRANSACTION happens under very specific conditions causing a state mismatch |
| 863 | // between audio policy manager and audio flinger during the input stream open sequence |
| 864 | // and can be recovered by retrying. |
| 865 | // Leave time for race condition to clear before retrying and randomize delay |
| 866 | // to reduce the probability of concurrent retries in locked steps. |
| 867 | usleep((20 + rand() % 30) * 10000); |
| 868 | } while (1); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 869 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 870 | ALOG_ASSERT(output.audioRecord != 0); |
Glenn Kasten | c08d20b | 2014-02-24 15:21:10 -0800 | [diff] [blame] | 871 | |
Glenn Kasten | 38e905b | 2014-01-13 10:21:48 -0800 | [diff] [blame] | 872 | // AudioFlinger now owns the reference to the I/O handle, |
| 873 | // so we are no longer responsible for releasing it. |
| 874 | |
Glenn Kasten | 2854e51 | 2016-03-11 16:14:06 -0800 | [diff] [blame] | 875 | mAwaitBoost = false; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 876 | if (output.flags & AUDIO_INPUT_FLAG_FAST) { |
Andy Hung | 6b1c612 | 2018-09-12 19:09:07 -0700 | [diff] [blame] | 877 | ALOGI("%s(%d): AUDIO_INPUT_FLAG_FAST successful; frameCount %zu -> %zu", |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 878 | __func__, mPortId, |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 879 | mReqFrameCount, output.frameCount); |
| 880 | mAwaitBoost = true; |
Glenn Kasten | 2854e51 | 2016-03-11 16:14:06 -0800 | [diff] [blame] | 881 | } |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 882 | mFlags = output.flags; |
| 883 | mRoutedDeviceId = output.selectedDeviceId; |
| 884 | mSessionId = output.sessionId; |
| 885 | mSampleRate = output.sampleRate; |
jiabin | b00edc3 | 2021-08-16 16:27:54 +0000 | [diff] [blame] | 886 | mServerConfig = output.serverConfig; |
| 887 | mServerFrameSize = audio_bytes_per_frame( |
| 888 | audio_channel_count_from_in_mask(mServerConfig.channel_mask), mServerConfig.format); |
| 889 | mServerSampleSize = audio_bytes_per_sample(mServerConfig.format); |
Robert Wu | 310037a | 2022-09-06 21:48:18 +0000 | [diff] [blame] | 890 | mHalSampleRate = output.halConfig.sample_rate; |
| 891 | mHalChannelCount = audio_channel_count_from_in_mask(output.halConfig.channel_mask); |
| 892 | mHalFormat = output.halConfig.format; |
Glenn Kasten | 2854e51 | 2016-03-11 16:14:06 -0800 | [diff] [blame] | 893 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 894 | if (output.cblk == 0) { |
Andy Hung | b58f2c5 | 2022-01-10 00:00:15 -0800 | [diff] [blame] | 895 | errorMessage = StringPrintf("%s(%d): Could not get control block", __func__, mPortId); |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 896 | status = NO_INIT; |
| 897 | goto exit; |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 898 | } |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 899 | // TODO: Using unsecurePointer() has some associated security pitfalls |
| 900 | // (see declaration for details). |
| 901 | // Either document why it is safe in this case or address the |
| 902 | // issue (e.g. by copying). |
| 903 | iMemPointer = output.cblk ->unsecurePointer(); |
Glenn Kasten | e93cf2c | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 904 | if (iMemPointer == NULL) { |
Andy Hung | b58f2c5 | 2022-01-10 00:00:15 -0800 | [diff] [blame] | 905 | errorMessage = StringPrintf( |
| 906 | "%s(%d): Could not get control block pointer", __func__, mPortId); |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 907 | status = NO_INIT; |
| 908 | goto exit; |
Glenn Kasten | e93cf2c | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 909 | } |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 910 | cblk = static_cast<audio_track_cblk_t*>(iMemPointer); |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 911 | |
| 912 | // Starting address of buffers in shared memory. |
| 913 | // The buffers are either immediately after the control block, |
| 914 | // or in a separate area at discretion of server. |
| 915 | void *buffers; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 916 | if (output.buffers == 0) { |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 917 | buffers = cblk + 1; |
| 918 | } else { |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 919 | // TODO: Using unsecurePointer() has some associated security pitfalls |
| 920 | // (see declaration for details). |
| 921 | // Either document why it is safe in this case or address the |
| 922 | // issue (e.g. by copying). |
| 923 | buffers = output.buffers->unsecurePointer(); |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 924 | if (buffers == NULL) { |
Andy Hung | b58f2c5 | 2022-01-10 00:00:15 -0800 | [diff] [blame] | 925 | errorMessage = StringPrintf( |
| 926 | "%s(%d): Could not get buffer pointer", __func__, mPortId); |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 927 | status = NO_INIT; |
| 928 | goto exit; |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | |
Glenn Kasten | 089e872 | 2014-02-24 15:12:48 -0800 | [diff] [blame] | 932 | // invariant that mAudioRecord != 0 is true only after set() returns successfully |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 933 | if (mAudioRecord != 0) { |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 934 | IInterface::asBinder(mAudioRecord)->unlinkToDeath(mDeathNotifier, this); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 935 | mDeathNotifier.clear(); |
| 936 | } |
Ytai Ben-Tsvi | 16d8761 | 2020-11-03 16:32:36 -0800 | [diff] [blame] | 937 | mAudioRecord = output.audioRecord; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 938 | mCblkMemory = output.cblk; |
| 939 | mBufferMemory = output.buffers; |
Eric Laurent | 3bcffa1 | 2014-06-12 18:38:45 -0700 | [diff] [blame] | 940 | IPCThreadState::self()->flushCommands(); |
| 941 | |
Glenn Kasten | b36a7a6 | 2012-11-12 15:46:10 -0800 | [diff] [blame] | 942 | mCblk = cblk; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 943 | // note that output.frameCount is the (possibly revised) value of mReqFrameCount |
| 944 | if (output.frameCount < mReqFrameCount || (mReqFrameCount == 0 && output.frameCount == 0)) { |
Andy Hung | 6b1c612 | 2018-09-12 19:09:07 -0700 | [diff] [blame] | 945 | ALOGW("%s(%d): Requested frameCount %zu but received frameCount %zu", |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 946 | __func__, output.portId, |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 947 | mReqFrameCount, output.frameCount); |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 948 | } |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 949 | |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 950 | // Make sure that application is notified with sufficient margin before overrun. |
| 951 | // The computation is done on server side. |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 952 | if (mNotificationFramesReq > 0 && output.notificationFrameCount != mNotificationFramesReq) { |
Andy Hung | 6b1c612 | 2018-09-12 19:09:07 -0700 | [diff] [blame] | 953 | ALOGW("%s(%d): Server adjusted notificationFrames from %u to %zu for frameCount %zu", |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 954 | __func__, output.portId, |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 955 | mNotificationFramesReq, output.notificationFrameCount, output.frameCount); |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 956 | } |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 957 | mNotificationFramesAct = (uint32_t)output.notificationFrameCount; |
Atneya Nair | 2eedae3 | 2022-02-14 16:42:23 -0500 | [diff] [blame] | 958 | if (mServerConfig.format != mFormat && mCallback != nullptr) { |
jiabin | b00edc3 | 2021-08-16 16:27:54 +0000 | [diff] [blame] | 959 | mFormatConversionBufRaw = std::make_unique<uint8_t[]>(mNotificationFramesAct * mFrameSize); |
| 960 | mFormatConversionBuffer.raw = mFormatConversionBufRaw.get(); |
| 961 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 962 | |
| 963 | //mInput != input includes the case where mInput == AUDIO_IO_HANDLE_NONE for first creation |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 964 | if (mDeviceCallback != 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 965 | if (mInput != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 966 | AudioSystem::removeAudioDeviceCallback(this, mInput, mPortId); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 967 | } |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 968 | AudioSystem::addAudioDeviceCallback(this, output.inputId, output.portId); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 971 | if (!mSharedAudioPackageName.empty()) { |
| 972 | mAudioRecord->shareAudioHistory(mSharedAudioPackageName, mSharedAudioStartMs); |
| 973 | } |
| 974 | |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 975 | mPortId = output.portId; |
Glenn Kasten | 045e739 | 2014-02-25 15:15:53 -0800 | [diff] [blame] | 976 | // We retain a copy of the I/O handle, but don't own the reference |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 977 | mInput = output.inputId; |
Glenn Kasten | a5ed48d | 2014-02-25 15:13:37 -0800 | [diff] [blame] | 978 | mRefreshRemaining = true; |
| 979 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 980 | mFrameCount = output.frameCount; |
Glenn Kasten | 11cb175 | 2014-02-24 15:16:59 -0800 | [diff] [blame] | 981 | // If IAudioRecord is re-created, don't let the requested frameCount |
| 982 | // decrease. This can confuse clients that cache frameCount(). |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 983 | if (mFrameCount > mReqFrameCount) { |
| 984 | mReqFrameCount = mFrameCount; |
Glenn Kasten | 11cb175 | 2014-02-24 15:16:59 -0800 | [diff] [blame] | 985 | } |
Glenn Kasten | b3b2e23 | 2014-02-05 11:10:26 -0800 | [diff] [blame] | 986 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 987 | // update proxy |
jiabin | b00edc3 | 2021-08-16 16:27:54 +0000 | [diff] [blame] | 988 | mProxy = new AudioRecordClientProxy(cblk, buffers, mFrameCount, mServerFrameSize); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 989 | mProxy->setEpoch(epoch); |
Glenn Kasten | 7cd9cf7 | 2013-08-01 07:22:02 -0700 | [diff] [blame] | 990 | mProxy->setMinimum(mNotificationFramesAct); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 991 | |
| 992 | mDeathNotifier = new DeathNotifier(this); |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 993 | IInterface::asBinder(mAudioRecord)->linkToDeath(mDeathNotifier, this); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 994 | |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 995 | mMetricsId = std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_RECORD) + std::to_string(mPortId); |
| 996 | mediametrics::LogItem(mMetricsId) |
| 997 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_CREATE) |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 998 | .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(systemTime() - beginNs)) |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 999 | // the following are immutable (at least until restore) |
Andy Hung | a629bd1 | 2020-06-05 16:03:53 -0700 | [diff] [blame] | 1000 | .set(AMEDIAMETRICS_PROP_FLAGS, toString(mFlags).c_str()) |
| 1001 | .set(AMEDIAMETRICS_PROP_ORIGINALFLAGS, toString(mOrigFlags).c_str()) |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 1002 | .set(AMEDIAMETRICS_PROP_SESSIONID, (int32_t)mSessionId) |
| 1003 | .set(AMEDIAMETRICS_PROP_TRACKID, mPortId) |
Andy Hung | 1a9c21b | 2021-02-25 20:43:18 -0800 | [diff] [blame] | 1004 | .set(AMEDIAMETRICS_PROP_LOGSESSIONID, mLogSessionId) |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 1005 | .set(AMEDIAMETRICS_PROP_SOURCE, toString(mAttributes.source).c_str()) |
| 1006 | .set(AMEDIAMETRICS_PROP_THREADID, (int32_t)output.inputId) |
| 1007 | .set(AMEDIAMETRICS_PROP_SELECTEDDEVICEID, (int32_t)mSelectedDeviceId) |
| 1008 | .set(AMEDIAMETRICS_PROP_ROUTEDDEVICEID, (int32_t)mRoutedDeviceId) |
| 1009 | .set(AMEDIAMETRICS_PROP_ENCODING, toString(mFormat).c_str()) |
| 1010 | .set(AMEDIAMETRICS_PROP_CHANNELMASK, (int32_t)mChannelMask) |
| 1011 | .set(AMEDIAMETRICS_PROP_FRAMECOUNT, (int32_t)mFrameCount) |
| 1012 | .set(AMEDIAMETRICS_PROP_SAMPLERATE, (int32_t)mSampleRate) |
| 1013 | // the following are NOT immutable |
| 1014 | .set(AMEDIAMETRICS_PROP_STATE, stateToString(mActive)) |
Andy Hung | 73dc2f9 | 2021-12-07 21:50:04 -0800 | [diff] [blame] | 1015 | .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)status) |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 1016 | .set(AMEDIAMETRICS_PROP_SELECTEDMICDIRECTION, (int32_t)mSelectedMicDirection) |
| 1017 | .set(AMEDIAMETRICS_PROP_SELECTEDMICFIELDDIRECTION, (double)mSelectedMicFieldDimension) |
| 1018 | .record(); |
| 1019 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1020 | exit: |
Andy Hung | b58f2c5 | 2022-01-10 00:00:15 -0800 | [diff] [blame] | 1021 | if (status != NO_ERROR) { |
| 1022 | ALOGE_IF(!errorMessage.empty(), "%s", errorMessage.c_str()); |
| 1023 | reportError(status, AMEDIAMETRICS_PROP_EVENT_VALUE_CREATE, errorMessage.c_str()); |
| 1024 | } |
| 1025 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1026 | mStatus = status; |
| 1027 | // sp<IAudioTrack> track destructor will cause releaseOutput() to be called by AudioFlinger |
Glenn Kasten | 38e905b | 2014-01-13 10:21:48 -0800 | [diff] [blame] | 1028 | return status; |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 1029 | } |
| 1030 | |
Andy Hung | b58f2c5 | 2022-01-10 00:00:15 -0800 | [diff] [blame] | 1031 | // Report error associated with the event and some configuration details. |
| 1032 | void AudioRecord::reportError(status_t status, const char *event, const char *message) const |
| 1033 | { |
| 1034 | if (status == NO_ERROR) return; |
| 1035 | // We report error on the native side because some callers do not come |
| 1036 | // from Java. |
| 1037 | // Ensure these variables are initialized in set(). |
| 1038 | mediametrics::LogItem(AMEDIAMETRICS_KEY_AUDIO_RECORD_ERROR) |
| 1039 | .set(AMEDIAMETRICS_PROP_EVENT, event) |
| 1040 | .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)status) |
| 1041 | .set(AMEDIAMETRICS_PROP_STATUSMESSAGE, message) |
| 1042 | .set(AMEDIAMETRICS_PROP_ORIGINALFLAGS, toString(mOrigFlags).c_str()) |
| 1043 | .set(AMEDIAMETRICS_PROP_SESSIONID, (int32_t)mSessionId) |
| 1044 | .set(AMEDIAMETRICS_PROP_SOURCE, toString(mAttributes.source).c_str()) |
| 1045 | .set(AMEDIAMETRICS_PROP_SELECTEDDEVICEID, (int32_t)mSelectedDeviceId) |
| 1046 | .set(AMEDIAMETRICS_PROP_ENCODING, toString(mFormat).c_str()) |
| 1047 | .set(AMEDIAMETRICS_PROP_CHANNELMASK, (int32_t)mChannelMask) |
| 1048 | .set(AMEDIAMETRICS_PROP_FRAMECOUNT, (int32_t)mFrameCount) |
| 1049 | .set(AMEDIAMETRICS_PROP_SAMPLERATE, (int32_t)mSampleRate) |
| 1050 | .record(); |
| 1051 | } |
| 1052 | |
Glenn Kasten | 551b535 | 2015-03-20 11:30:28 -0700 | [diff] [blame] | 1053 | status_t AudioRecord::obtainBuffer(Buffer* audioBuffer, int32_t waitCount, size_t *nonContig) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1054 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1055 | if (audioBuffer == NULL) { |
Glenn Kasten | 551b535 | 2015-03-20 11:30:28 -0700 | [diff] [blame] | 1056 | if (nonContig != NULL) { |
| 1057 | *nonContig = 0; |
| 1058 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1059 | return BAD_VALUE; |
| 1060 | } |
| 1061 | if (mTransfer != TRANSFER_OBTAIN) { |
| 1062 | audioBuffer->frameCount = 0; |
Atneya Nair | 0307927 | 2022-01-18 17:03:14 -0500 | [diff] [blame] | 1063 | audioBuffer->mSize = 0; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1064 | audioBuffer->raw = NULL; |
Glenn Kasten | 551b535 | 2015-03-20 11:30:28 -0700 | [diff] [blame] | 1065 | if (nonContig != NULL) { |
| 1066 | *nonContig = 0; |
| 1067 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1068 | return INVALID_OPERATION; |
| 1069 | } |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 1070 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1071 | const struct timespec *requested; |
Eric Laurent | df57699 | 2014-01-27 18:13:39 -0800 | [diff] [blame] | 1072 | struct timespec timeout; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1073 | if (waitCount == -1) { |
| 1074 | requested = &ClientProxy::kForever; |
| 1075 | } else if (waitCount == 0) { |
| 1076 | requested = &ClientProxy::kNonBlocking; |
| 1077 | } else if (waitCount > 0) { |
Chih-Hung Hsieh | bca7429 | 2018-08-10 16:06:07 -0700 | [diff] [blame] | 1078 | time_t ms = WAIT_PERIOD_MS * (time_t) waitCount; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1079 | timeout.tv_sec = ms / 1000; |
Chih-Hung Hsieh | bca7429 | 2018-08-10 16:06:07 -0700 | [diff] [blame] | 1080 | timeout.tv_nsec = (long) (ms % 1000) * 1000000; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1081 | requested = &timeout; |
| 1082 | } else { |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 1083 | ALOGE("%s(%d): invalid waitCount %d", __func__, mPortId, waitCount); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1084 | requested = NULL; |
| 1085 | } |
Glenn Kasten | 551b535 | 2015-03-20 11:30:28 -0700 | [diff] [blame] | 1086 | return obtainBuffer(audioBuffer, requested, NULL /*elapsed*/, nonContig); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1087 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1088 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1089 | status_t AudioRecord::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested, |
| 1090 | struct timespec *elapsed, size_t *nonContig) |
| 1091 | { |
| 1092 | // previous and new IAudioRecord sequence numbers are used to detect track re-creation |
| 1093 | uint32_t oldSequence = 0; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1094 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1095 | Proxy::Buffer buffer; |
| 1096 | status_t status = NO_ERROR; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1097 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1098 | static const int32_t kMaxTries = 5; |
| 1099 | int32_t tryCounter = kMaxTries; |
| 1100 | |
| 1101 | do { |
| 1102 | // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to |
| 1103 | // keep them from going away if another thread re-creates the track during obtainBuffer() |
| 1104 | sp<AudioRecordClientProxy> proxy; |
| 1105 | sp<IMemory> iMem; |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 1106 | sp<IMemory> bufferMem; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1107 | { |
| 1108 | // start of lock scope |
| 1109 | AutoMutex lock(mLock); |
| 1110 | |
Glenn Kasten | ba703d6 | 2019-12-13 11:13:42 -0800 | [diff] [blame] | 1111 | uint32_t newSequence = mSequence; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1112 | // did previous obtainBuffer() fail due to media server death or voluntary invalidation? |
| 1113 | if (status == DEAD_OBJECT) { |
| 1114 | // re-create track, unless someone else has already done so |
| 1115 | if (newSequence == oldSequence) { |
Atneya Nair | 497fff1 | 2022-01-18 16:23:04 -0500 | [diff] [blame] | 1116 | if (!audio_is_linear_pcm(mFormat)) { |
| 1117 | // If compressed capture, don't attempt to restore the track. |
| 1118 | // Return a DEAD_OBJECT error and let the caller recreate. |
| 1119 | tryCounter = 0; |
| 1120 | } else { |
| 1121 | status = restoreRecord_l("obtainBuffer"); |
| 1122 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1123 | if (status != NO_ERROR) { |
Glenn Kasten | d8a9d02 | 2014-01-14 15:46:38 -0800 | [diff] [blame] | 1124 | buffer.mFrameCount = 0; |
| 1125 | buffer.mRaw = NULL; |
| 1126 | buffer.mNonContig = 0; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1127 | break; |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1128 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1129 | } |
| 1130 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1131 | oldSequence = newSequence; |
| 1132 | |
| 1133 | // Keep the extra references |
| 1134 | proxy = mProxy; |
| 1135 | iMem = mCblkMemory; |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 1136 | bufferMem = mBufferMemory; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1137 | |
| 1138 | // Non-blocking if track is stopped |
| 1139 | if (!mActive) { |
| 1140 | requested = &ClientProxy::kNonBlocking; |
| 1141 | } |
| 1142 | |
| 1143 | } // end of lock scope |
| 1144 | |
| 1145 | buffer.mFrameCount = audioBuffer->frameCount; |
| 1146 | // FIXME starts the requested timeout and elapsed over from scratch |
| 1147 | status = proxy->obtainBuffer(&buffer, requested, elapsed); |
| 1148 | |
| 1149 | } while ((status == DEAD_OBJECT) && (tryCounter-- > 0)); |
| 1150 | |
| 1151 | audioBuffer->frameCount = buffer.mFrameCount; |
Atneya Nair | 0307927 | 2022-01-18 17:03:14 -0500 | [diff] [blame] | 1152 | audioBuffer->mSize = buffer.mFrameCount * mServerFrameSize; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1153 | audioBuffer->raw = buffer.mRaw; |
Glenn Kasten | ba703d6 | 2019-12-13 11:13:42 -0800 | [diff] [blame] | 1154 | audioBuffer->sequence = oldSequence; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1155 | if (nonContig != NULL) { |
| 1156 | *nonContig = buffer.mNonContig; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1157 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1158 | return status; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1159 | } |
| 1160 | |
Glenn Kasten | 5014c94 | 2015-03-20 10:06:37 -0700 | [diff] [blame] | 1161 | void AudioRecord::releaseBuffer(const Buffer* audioBuffer) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1162 | { |
Glenn Kasten | 17ea135 | 2015-03-20 08:54:17 -0700 | [diff] [blame] | 1163 | // FIXME add error checking on mode, by adding an internal version |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1164 | |
jiabin | b00edc3 | 2021-08-16 16:27:54 +0000 | [diff] [blame] | 1165 | size_t stepCount = audioBuffer->frameCount; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1166 | if (stepCount == 0) { |
| 1167 | return; |
| 1168 | } |
| 1169 | |
| 1170 | Proxy::Buffer buffer; |
| 1171 | buffer.mFrameCount = stepCount; |
| 1172 | buffer.mRaw = audioBuffer->raw; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 1173 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1174 | AutoMutex lock(mLock); |
Glenn Kasten | ba703d6 | 2019-12-13 11:13:42 -0800 | [diff] [blame] | 1175 | if (audioBuffer->sequence != mSequence) { |
| 1176 | // This Buffer came from a different IAudioRecord instance, so ignore the releaseBuffer |
| 1177 | ALOGD("%s is no-op due to IAudioRecord sequence mismatch %u != %u", |
| 1178 | __func__, audioBuffer->sequence, mSequence); |
| 1179 | return; |
| 1180 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1181 | mInOverrun = false; |
| 1182 | mProxy->releaseBuffer(&buffer); |
| 1183 | |
| 1184 | // the server does not automatically disable recorder on overrun, so no need to restart |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1185 | } |
| 1186 | |
Glenn Kasten | 32860f7 | 2015-03-20 08:55:18 -0700 | [diff] [blame] | 1187 | audio_io_handle_t AudioRecord::getInputPrivate() const |
Eric Laurent | 6100d2d | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 1188 | { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1189 | AutoMutex lock(mLock); |
Eric Laurent | d1a243e | 2011-07-26 20:32:28 -0700 | [diff] [blame] | 1190 | return mInput; |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1191 | } |
| 1192 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1193 | // ------------------------------------------------------------------------- |
| 1194 | |
Glenn Kasten | 3622cdf | 2015-03-20 10:58:21 -0700 | [diff] [blame] | 1195 | ssize_t AudioRecord::read(void* buffer, size_t userSize, bool blocking) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1196 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1197 | if (mTransfer != TRANSFER_SYNC) { |
| 1198 | return INVALID_OPERATION; |
| 1199 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1200 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1201 | if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) { |
Jiabin Huang | cec62a7 | 2020-07-28 22:33:43 +0000 | [diff] [blame] | 1202 | // Validation. user is most-likely passing an error code, and it would |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1203 | // make the return value ambiguous (actualSize vs error). |
Andy Hung | 6b1c612 | 2018-09-12 19:09:07 -0700 | [diff] [blame] | 1204 | ALOGE("%s(%d) (buffer=%p, size=%zu (%zu)", |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 1205 | __func__, mPortId, buffer, userSize, userSize); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1206 | return BAD_VALUE; |
| 1207 | } |
| 1208 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1209 | ssize_t read = 0; |
| 1210 | Buffer audioBuffer; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1211 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1212 | while (userSize >= mFrameSize) { |
| 1213 | audioBuffer.frameCount = userSize / mFrameSize; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1214 | |
Glenn Kasten | 3622cdf | 2015-03-20 10:58:21 -0700 | [diff] [blame] | 1215 | status_t err = obtainBuffer(&audioBuffer, |
| 1216 | blocking ? &ClientProxy::kForever : &ClientProxy::kNonBlocking); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1217 | if (err < 0) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1218 | if (read > 0) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1219 | break; |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 1220 | } |
Glenn Kasten | 0a2f151 | 2016-07-22 08:06:37 -0700 | [diff] [blame] | 1221 | if (err == TIMED_OUT || err == -EINTR) { |
| 1222 | err = WOULD_BLOCK; |
| 1223 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1224 | return ssize_t(err); |
| 1225 | } |
| 1226 | |
jiabin | b00edc3 | 2021-08-16 16:27:54 +0000 | [diff] [blame] | 1227 | size_t bytesRead = audioBuffer.frameCount * mFrameSize; |
Dean Wheatley | 1058702 | 2023-11-02 11:38:43 +1100 | [diff] [blame] | 1228 | if (audio_is_linear_pcm(mFormat)) { |
| 1229 | memcpy_by_audio_format(buffer, mFormat, audioBuffer.raw, mServerConfig.format, |
| 1230 | audioBuffer.mSize / mServerSampleSize); |
| 1231 | } else { |
| 1232 | memcpy(buffer, audioBuffer.raw, audioBuffer.mSize); |
| 1233 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1234 | buffer = ((char *) buffer) + bytesRead; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1235 | userSize -= bytesRead; |
| 1236 | read += bytesRead; |
| 1237 | |
| 1238 | releaseBuffer(&audioBuffer); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1239 | } |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1240 | if (read > 0) { |
| 1241 | mFramesRead += read / mFrameSize; |
| 1242 | // mFramesReadTime = systemTime(SYSTEM_TIME_MONOTONIC); // not provided at this time. |
| 1243 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1244 | return read; |
| 1245 | } |
| 1246 | |
| 1247 | // ------------------------------------------------------------------------- |
| 1248 | |
Glenn Kasten | 7c7be1e | 2013-12-19 16:34:04 -0800 | [diff] [blame] | 1249 | nsecs_t AudioRecord::processAudioBuffer() |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1250 | { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1251 | mLock.lock(); |
Atneya Nair | b249d27 | 2021-11-29 18:06:46 -0500 | [diff] [blame] | 1252 | const sp<IAudioRecordCallback> callback = mCallback.promote(); |
| 1253 | if (!callback) { |
| 1254 | mCallback = nullptr; |
Atneya Nair | e260f5a | 2022-05-03 17:02:20 -0400 | [diff] [blame] | 1255 | mLock.unlock(); |
Atneya Nair | b249d27 | 2021-11-29 18:06:46 -0500 | [diff] [blame] | 1256 | return NS_NEVER; |
| 1257 | } |
Glenn Kasten | 28f1351 | 2013-07-31 12:27:26 -0700 | [diff] [blame] | 1258 | if (mAwaitBoost) { |
| 1259 | mAwaitBoost = false; |
| 1260 | mLock.unlock(); |
| 1261 | static const int32_t kMaxTries = 5; |
| 1262 | int32_t tryCounter = kMaxTries; |
| 1263 | uint32_t pollUs = 10000; |
| 1264 | do { |
Glenn Kasten | 8255ba7 | 2016-08-23 13:54:23 -0700 | [diff] [blame] | 1265 | int policy = sched_getscheduler(0) & ~SCHED_RESET_ON_FORK; |
Glenn Kasten | 28f1351 | 2013-07-31 12:27:26 -0700 | [diff] [blame] | 1266 | if (policy == SCHED_FIFO || policy == SCHED_RR) { |
| 1267 | break; |
| 1268 | } |
| 1269 | usleep(pollUs); |
| 1270 | pollUs <<= 1; |
| 1271 | } while (tryCounter-- > 0); |
| 1272 | if (tryCounter < 0) { |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 1273 | ALOGE("%s(%d): did not receive expected priority boost on time", __func__, mPortId); |
Glenn Kasten | 28f1351 | 2013-07-31 12:27:26 -0700 | [diff] [blame] | 1274 | } |
| 1275 | // Run again immediately |
| 1276 | return 0; |
| 1277 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1278 | |
| 1279 | // Can only reference mCblk while locked |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 1280 | int32_t flags = android_atomic_and(~CBLK_OVERRUN, &mCblk->mFlags); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1281 | |
| 1282 | // Check for track invalidation |
| 1283 | if (flags & CBLK_INVALID) { |
| 1284 | (void) restoreRecord_l("processAudioBuffer"); |
| 1285 | mLock.unlock(); |
| 1286 | // Run again immediately, but with a new IAudioRecord |
| 1287 | return 0; |
Glenn Kasten | 955e781 | 2012-02-21 10:32:45 -0800 | [diff] [blame] | 1288 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1289 | |
| 1290 | bool active = mActive; |
| 1291 | |
| 1292 | // Manage overrun callback, must be done under lock to avoid race with releaseBuffer() |
| 1293 | bool newOverrun = false; |
| 1294 | if (flags & CBLK_OVERRUN) { |
| 1295 | if (!mInOverrun) { |
| 1296 | mInOverrun = true; |
| 1297 | newOverrun = true; |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | // Get current position of server |
Andy Hung | 90e8a97 | 2015-11-09 16:42:40 -0800 | [diff] [blame] | 1302 | Modulo<uint32_t> position(mProxy->getPosition()); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1303 | |
| 1304 | // Manage marker callback |
| 1305 | bool markerReached = false; |
Andy Hung | 90e8a97 | 2015-11-09 16:42:40 -0800 | [diff] [blame] | 1306 | Modulo<uint32_t> markerPosition(mMarkerPosition); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1307 | // FIXME fails for wraparound, need 64 bits |
Andy Hung | 90e8a97 | 2015-11-09 16:42:40 -0800 | [diff] [blame] | 1308 | if (!mMarkerReached && markerPosition.value() > 0 && position >= markerPosition) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1309 | mMarkerReached = markerReached = true; |
| 1310 | } |
| 1311 | |
| 1312 | // Determine the number of new position callback(s) that will be needed, while locked |
| 1313 | size_t newPosCount = 0; |
Andy Hung | 90e8a97 | 2015-11-09 16:42:40 -0800 | [diff] [blame] | 1314 | Modulo<uint32_t> newPosition(mNewPosition); |
Glenn Kasten | 955e781 | 2012-02-21 10:32:45 -0800 | [diff] [blame] | 1315 | uint32_t updatePeriod = mUpdatePeriod; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1316 | // FIXME fails for wraparound, need 64 bits |
| 1317 | if (updatePeriod > 0 && position >= newPosition) { |
Andy Hung | 90e8a97 | 2015-11-09 16:42:40 -0800 | [diff] [blame] | 1318 | newPosCount = ((position - newPosition).value() / updatePeriod) + 1; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1319 | mNewPosition += updatePeriod * newPosCount; |
| 1320 | } |
| 1321 | |
| 1322 | // Cache other fields that will be needed soon |
Glenn Kasten | 838b3d8 | 2014-02-27 15:30:41 -0800 | [diff] [blame] | 1323 | uint32_t notificationFrames = mNotificationFramesAct; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1324 | if (mRefreshRemaining) { |
| 1325 | mRefreshRemaining = false; |
| 1326 | mRemainingFrames = notificationFrames; |
| 1327 | mRetryOnPartialBuffer = false; |
| 1328 | } |
| 1329 | size_t misalignment = mProxy->getMisalignment(); |
Glenn Kasten | 8ff50e7 | 2014-01-14 15:44:30 -0800 | [diff] [blame] | 1330 | uint32_t sequence = mSequence; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1331 | |
| 1332 | // These fields don't need to be cached, because they are assigned only by set(): |
Atneya Nair | b249d27 | 2021-11-29 18:06:46 -0500 | [diff] [blame] | 1333 | // mTransfer, mCallback, mUserData, mSampleRate, mFrameSize |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1334 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1335 | mLock.unlock(); |
| 1336 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1337 | // perform callbacks while unlocked |
| 1338 | if (newOverrun) { |
Atneya Nair | b249d27 | 2021-11-29 18:06:46 -0500 | [diff] [blame] | 1339 | callback->onOverrun(); |
| 1340 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1341 | } |
| 1342 | if (markerReached) { |
Atneya Nair | b249d27 | 2021-11-29 18:06:46 -0500 | [diff] [blame] | 1343 | callback->onMarker(markerPosition.value()); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1344 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1345 | while (newPosCount > 0) { |
Atneya Nair | b249d27 | 2021-11-29 18:06:46 -0500 | [diff] [blame] | 1346 | callback->onNewPos(newPosition.value()); |
Glenn Kasten | 955e781 | 2012-02-21 10:32:45 -0800 | [diff] [blame] | 1347 | newPosition += updatePeriod; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1348 | newPosCount--; |
| 1349 | } |
| 1350 | if (mObservedSequence != sequence) { |
| 1351 | mObservedSequence = sequence; |
Atneya Nair | b249d27 | 2021-11-29 18:06:46 -0500 | [diff] [blame] | 1352 | callback->onNewIAudioRecord(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1353 | } |
| 1354 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1355 | // if inactive, then don't run me again until re-started |
| 1356 | if (!active) { |
| 1357 | return NS_INACTIVE; |
| 1358 | } |
| 1359 | |
| 1360 | // Compute the estimated time until the next timed event (position, markers) |
| 1361 | uint32_t minFrames = ~0; |
| 1362 | if (!markerReached && position < markerPosition) { |
Andy Hung | 90e8a97 | 2015-11-09 16:42:40 -0800 | [diff] [blame] | 1363 | minFrames = (markerPosition - position).value(); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1364 | } |
Andy Hung | fb7df2c | 2015-03-13 15:55:18 -0700 | [diff] [blame] | 1365 | if (updatePeriod > 0) { |
Andy Hung | 90e8a97 | 2015-11-09 16:42:40 -0800 | [diff] [blame] | 1366 | uint32_t remaining = (newPosition - position).value(); |
Andy Hung | fb7df2c | 2015-03-13 15:55:18 -0700 | [diff] [blame] | 1367 | if (remaining < minFrames) { |
| 1368 | minFrames = remaining; |
| 1369 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | // If > 0, poll periodically to recover from a stuck server. A good value is 2. |
| 1373 | static const uint32_t kPoll = 0; |
| 1374 | if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) { |
| 1375 | minFrames = kPoll * notificationFrames; |
| 1376 | } |
| 1377 | |
| 1378 | // Convert frame units to time units |
| 1379 | nsecs_t ns = NS_WHENEVER; |
| 1380 | if (minFrames != (uint32_t) ~0) { |
| 1381 | // This "fudge factor" avoids soaking CPU, and compensates for late progress by server |
| 1382 | static const nsecs_t kFudgeNs = 10000000LL; // 10 ms |
| 1383 | ns = ((minFrames * 1000000000LL) / mSampleRate) + kFudgeNs; |
| 1384 | } |
| 1385 | |
| 1386 | // If not supplying data by EVENT_MORE_DATA, then we're done |
| 1387 | if (mTransfer != TRANSFER_CALLBACK) { |
| 1388 | return ns; |
| 1389 | } |
| 1390 | |
| 1391 | struct timespec timeout; |
| 1392 | const struct timespec *requested = &ClientProxy::kForever; |
| 1393 | if (ns != NS_WHENEVER) { |
| 1394 | timeout.tv_sec = ns / 1000000000LL; |
| 1395 | timeout.tv_nsec = ns % 1000000000LL; |
Andy Hung | 6b1c612 | 2018-09-12 19:09:07 -0700 | [diff] [blame] | 1396 | ALOGV("%s(%d): timeout %ld.%03d", |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 1397 | __func__, mPortId, timeout.tv_sec, (int) timeout.tv_nsec / 1000000); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1398 | requested = &timeout; |
| 1399 | } |
| 1400 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1401 | size_t readFrames = 0; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1402 | while (mRemainingFrames > 0) { |
| 1403 | |
| 1404 | Buffer audioBuffer; |
| 1405 | audioBuffer.frameCount = mRemainingFrames; |
| 1406 | size_t nonContig; |
| 1407 | status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig); |
| 1408 | LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0), |
Andy Hung | 6b1c612 | 2018-09-12 19:09:07 -0700 | [diff] [blame] | 1409 | "%s(%d): obtainBuffer() err=%d frameCount=%zu", |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 1410 | __func__, mPortId, err, audioBuffer.frameCount); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1411 | requested = &ClientProxy::kNonBlocking; |
| 1412 | size_t avail = audioBuffer.frameCount + nonContig; |
Andy Hung | 6b1c612 | 2018-09-12 19:09:07 -0700 | [diff] [blame] | 1413 | ALOGV("%s(%d): obtainBuffer(%u) returned %zu = %zu + %zu err %d", |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 1414 | __func__, mPortId, mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1415 | if (err != NO_ERROR) { |
| 1416 | if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR) { |
| 1417 | break; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1418 | } |
Andy Hung | 6b1c612 | 2018-09-12 19:09:07 -0700 | [diff] [blame] | 1419 | ALOGE("%s(%d): Error %d obtaining an audio buffer, giving up.", |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 1420 | __func__, mPortId, err); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1421 | return NS_NEVER; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1422 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1423 | |
| 1424 | if (mRetryOnPartialBuffer) { |
| 1425 | mRetryOnPartialBuffer = false; |
| 1426 | if (avail < mRemainingFrames) { |
| 1427 | int64_t myns = ((mRemainingFrames - avail) * |
| 1428 | 1100000000LL) / mSampleRate; |
| 1429 | if (ns < 0 || myns < ns) { |
| 1430 | ns = myns; |
| 1431 | } |
| 1432 | return ns; |
| 1433 | } |
| 1434 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1435 | |
jiabin | b00edc3 | 2021-08-16 16:27:54 +0000 | [diff] [blame] | 1436 | Buffer* buffer = &audioBuffer; |
| 1437 | if (mServerConfig.format != mFormat) { |
| 1438 | buffer = &mFormatConversionBuffer; |
| 1439 | buffer->frameCount = audioBuffer.frameCount; |
Atneya Nair | 0307927 | 2022-01-18 17:03:14 -0500 | [diff] [blame] | 1440 | buffer->mSize = buffer->frameCount * mFrameSize; |
jiabin | b00edc3 | 2021-08-16 16:27:54 +0000 | [diff] [blame] | 1441 | buffer->sequence = audioBuffer.sequence; |
| 1442 | memcpy_by_audio_format(buffer->raw, mFormat, audioBuffer.raw, |
Atneya Nair | 0307927 | 2022-01-18 17:03:14 -0500 | [diff] [blame] | 1443 | mServerConfig.format, audioBuffer.size() / mServerSampleSize); |
jiabin | b00edc3 | 2021-08-16 16:27:54 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Atneya Nair | 0307927 | 2022-01-18 17:03:14 -0500 | [diff] [blame] | 1446 | const size_t reqSize = buffer->size(); |
Atneya Nair | b249d27 | 2021-11-29 18:06:46 -0500 | [diff] [blame] | 1447 | const size_t readSize = callback->onMoreData(*buffer); |
Atneya Nair | 0307927 | 2022-01-18 17:03:14 -0500 | [diff] [blame] | 1448 | buffer->mSize = readSize; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1449 | |
Jiabin Huang | cec62a7 | 2020-07-28 22:33:43 +0000 | [diff] [blame] | 1450 | // Validate on returned size |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1451 | if (ssize_t(readSize) < 0 || readSize > reqSize) { |
Andy Hung | 6b1c612 | 2018-09-12 19:09:07 -0700 | [diff] [blame] | 1452 | ALOGE("%s(%d): EVENT_MORE_DATA requested %zu bytes but callback returned %zd bytes", |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 1453 | __func__, mPortId, reqSize, ssize_t(readSize)); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1454 | return NS_NEVER; |
Eric Laurent | cd6725a | 2009-03-24 21:23:54 -0700 | [diff] [blame] | 1455 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1456 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1457 | if (readSize == 0) { |
| 1458 | // The callback is done consuming buffers |
| 1459 | // Keep this thread going to handle timed events and |
| 1460 | // still try to provide more data in intervals of WAIT_PERIOD_MS |
| 1461 | // but don't just loop and block the CPU, so wait |
| 1462 | return WAIT_PERIOD_MS * 1000000LL; |
| 1463 | } |
| 1464 | |
| 1465 | size_t releasedFrames = readSize / mFrameSize; |
| 1466 | audioBuffer.frameCount = releasedFrames; |
| 1467 | mRemainingFrames -= releasedFrames; |
| 1468 | if (misalignment >= releasedFrames) { |
| 1469 | misalignment -= releasedFrames; |
| 1470 | } else { |
| 1471 | misalignment = 0; |
| 1472 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1473 | |
| 1474 | releaseBuffer(&audioBuffer); |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1475 | readFrames += releasedFrames; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1476 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1477 | // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer |
| 1478 | // if callback doesn't like to accept the full chunk |
| 1479 | if (readSize < reqSize) { |
| 1480 | continue; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1481 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1482 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1483 | // There could be enough non-contiguous frames available to satisfy the remaining request |
| 1484 | if (mRemainingFrames <= nonContig) { |
| 1485 | continue; |
| 1486 | } |
| 1487 | |
| 1488 | #if 0 |
| 1489 | // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a |
| 1490 | // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA |
| 1491 | // that total to a sum == notificationFrames. |
| 1492 | if (0 < misalignment && misalignment <= mRemainingFrames) { |
| 1493 | mRemainingFrames = misalignment; |
| 1494 | return (mRemainingFrames * 1100000000LL) / mSampleRate; |
| 1495 | } |
| 1496 | #endif |
| 1497 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1498 | } |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1499 | if (readFrames > 0) { |
| 1500 | AutoMutex lock(mLock); |
| 1501 | mFramesRead += readFrames; |
| 1502 | // mFramesReadTime = systemTime(SYSTEM_TIME_MONOTONIC); // not provided at this time. |
| 1503 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1504 | mRemainingFrames = notificationFrames; |
| 1505 | mRetryOnPartialBuffer = true; |
| 1506 | |
| 1507 | // A lot has transpired since ns was calculated, so run again immediately and re-calculate |
| 1508 | return 0; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1509 | } |
| 1510 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1511 | status_t AudioRecord::restoreRecord_l(const char *from) |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1512 | { |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 1513 | status_t result = NO_ERROR; // logged: make sure to set this before returning. |
| 1514 | const int64_t beginNs = systemTime(); |
Andy Hung | 06a730b | 2020-04-09 13:28:31 -0700 | [diff] [blame] | 1515 | mediametrics::Defer defer([&] { |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 1516 | mediametrics::LogItem(mMetricsId) |
| 1517 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_RESTORE) |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 1518 | .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(systemTime() - beginNs)) |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 1519 | .set(AMEDIAMETRICS_PROP_STATE, stateToString(mActive)) |
| 1520 | .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result) |
| 1521 | .set(AMEDIAMETRICS_PROP_WHERE, from) |
| 1522 | .record(); }); |
| 1523 | |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 1524 | ALOGW("%s(%d): dead IAudioRecord, creating a new one from %s()", __func__, mPortId, from); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1525 | ++mSequence; |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1526 | |
Paul McLean | b8ca98e | 2018-04-16 12:47:19 -0700 | [diff] [blame] | 1527 | const int INITIAL_RETRIES = 3; |
| 1528 | int retries = INITIAL_RETRIES; |
| 1529 | retry: |
| 1530 | if (retries < INITIAL_RETRIES) { |
| 1531 | // refresh the audio configuration cache in this process to make sure we get new |
| 1532 | // input parameters and new IAudioRecord in createRecord_l() |
| 1533 | AudioSystem::clearAudioConfigCache(); |
| 1534 | } |
Glenn Kasten | 58883a0 | 2016-03-08 15:31:49 -0800 | [diff] [blame] | 1535 | mFlags = mOrigFlags; |
| 1536 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1537 | // if the new IAudioRecord is created, createRecord_l() will modify the |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 1538 | // following member variables: mAudioRecord, mCblkMemory, mCblk, mBufferMemory. |
Glenn Kasten | b36a7a6 | 2012-11-12 15:46:10 -0800 | [diff] [blame] | 1539 | // It will also delete the strong references on previous IAudioRecord and IMemory |
Andy Hung | 90e8a97 | 2015-11-09 16:42:40 -0800 | [diff] [blame] | 1540 | Modulo<uint32_t> position(mProxy->getPosition()); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1541 | mNewPosition = position + mUpdatePeriod; |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 1542 | result = createRecord_l(position); |
Paul McLean | b8ca98e | 2018-04-16 12:47:19 -0700 | [diff] [blame] | 1543 | |
Eric Laurent | 6ec546d | 2018-10-10 16:52:14 -0700 | [diff] [blame] | 1544 | if (result == NO_ERROR) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1545 | if (mActive) { |
| 1546 | // callback thread or sync event hasn't changed |
| 1547 | // FIXME this fails if we have a new AudioFlinger instance |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 1548 | result = statusTFromBinderStatus(mAudioRecord->start( |
| 1549 | AudioSystem::SYNC_EVENT_SAME, AUDIO_SESSION_NONE)); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1550 | } |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1551 | mFramesReadServerOffset = mFramesRead; // server resets to zero so we need an offset. |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1552 | } |
Paul McLean | b8ca98e | 2018-04-16 12:47:19 -0700 | [diff] [blame] | 1553 | |
| 1554 | if (result != NO_ERROR) { |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 1555 | ALOGW("%s(%d): failed status %d, retries %d", __func__, mPortId, result, retries); |
Paul McLean | b8ca98e | 2018-04-16 12:47:19 -0700 | [diff] [blame] | 1556 | if (--retries > 0) { |
Eric Laurent | 6ec546d | 2018-10-10 16:52:14 -0700 | [diff] [blame] | 1557 | // leave time for an eventual race condition to clear before retrying |
| 1558 | usleep(500000); |
Paul McLean | b8ca98e | 2018-04-16 12:47:19 -0700 | [diff] [blame] | 1559 | goto retry; |
| 1560 | } |
Eric Laurent | 6ec546d | 2018-10-10 16:52:14 -0700 | [diff] [blame] | 1561 | // if no retries left, set invalid bit to force restoring at next occasion |
| 1562 | // and avoid inconsistent active state on client and server sides |
| 1563 | if (mCblk != nullptr) { |
| 1564 | android_atomic_or(CBLK_INVALID, &mCblk->mFlags); |
| 1565 | } |
Glenn Kasten | b36a7a6 | 2012-11-12 15:46:10 -0800 | [diff] [blame] | 1566 | } |
| 1567 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1568 | return result; |
| 1569 | } |
| 1570 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1571 | status_t AudioRecord::addAudioDeviceCallback(const sp<AudioSystem::AudioDeviceCallback>& callback) |
| 1572 | { |
| 1573 | if (callback == 0) { |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 1574 | ALOGW("%s(%d): adding NULL callback!", __func__, mPortId); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1575 | return BAD_VALUE; |
| 1576 | } |
| 1577 | AutoMutex lock(mLock); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1578 | if (mDeviceCallback.unsafe_get() == callback.get()) { |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 1579 | ALOGW("%s(%d): adding same callback!", __func__, mPortId); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1580 | return INVALID_OPERATION; |
| 1581 | } |
| 1582 | status_t status = NO_ERROR; |
| 1583 | if (mInput != AUDIO_IO_HANDLE_NONE) { |
| 1584 | if (mDeviceCallback != 0) { |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 1585 | ALOGW("%s(%d): callback already present!", __func__, mPortId); |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 1586 | AudioSystem::removeAudioDeviceCallback(this, mInput, mPortId); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1587 | } |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 1588 | status = AudioSystem::addAudioDeviceCallback(this, mInput, mPortId); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1589 | } |
| 1590 | mDeviceCallback = callback; |
| 1591 | return status; |
| 1592 | } |
| 1593 | |
| 1594 | status_t AudioRecord::removeAudioDeviceCallback( |
| 1595 | const sp<AudioSystem::AudioDeviceCallback>& callback) |
| 1596 | { |
| 1597 | if (callback == 0) { |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 1598 | ALOGW("%s(%d): removing NULL callback!", __func__, mPortId); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1599 | return BAD_VALUE; |
| 1600 | } |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame] | 1601 | AutoMutex lock(mLock); |
| 1602 | if (mDeviceCallback.unsafe_get() != callback.get()) { |
| 1603 | ALOGW("%s(%d): removing different callback!", __func__, mPortId); |
| 1604 | return INVALID_OPERATION; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1605 | } |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame] | 1606 | mDeviceCallback.clear(); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1607 | if (mInput != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 1608 | AudioSystem::removeAudioDeviceCallback(this, mInput, mPortId); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1609 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1610 | return NO_ERROR; |
| 1611 | } |
| 1612 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1613 | void AudioRecord::onAudioDeviceUpdate(audio_io_handle_t audioIo, |
| 1614 | audio_port_handle_t deviceId) |
| 1615 | { |
| 1616 | sp<AudioSystem::AudioDeviceCallback> callback; |
| 1617 | { |
| 1618 | AutoMutex lock(mLock); |
| 1619 | if (audioIo != mInput) { |
| 1620 | return; |
| 1621 | } |
| 1622 | callback = mDeviceCallback.promote(); |
| 1623 | // only update device if the record is active as route changes due to other use cases are |
| 1624 | // irrelevant for this client |
| 1625 | if (mActive) { |
| 1626 | mRoutedDeviceId = deviceId; |
| 1627 | } |
| 1628 | } |
| 1629 | if (callback.get() != nullptr) { |
| 1630 | callback->onAudioDeviceUpdate(mInput, mRoutedDeviceId); |
| 1631 | } |
| 1632 | } |
| 1633 | |
jiabin | 653cc0a | 2018-01-17 17:54:10 -0800 | [diff] [blame] | 1634 | // ------------------------------------------------------------------------- |
| 1635 | |
Mikhail Naganov | 2a6a301 | 2023-02-13 11:45:03 -0800 | [diff] [blame] | 1636 | status_t AudioRecord::getActiveMicrophones(std::vector<media::MicrophoneInfoFw>* activeMicrophones) |
jiabin | 653cc0a | 2018-01-17 17:54:10 -0800 | [diff] [blame] | 1637 | { |
| 1638 | AutoMutex lock(mLock); |
Mikhail Naganov | 2a6a301 | 2023-02-13 11:45:03 -0800 | [diff] [blame] | 1639 | return statusTFromBinderStatus(mAudioRecord->getActiveMicrophones(activeMicrophones)); |
jiabin | 653cc0a | 2018-01-17 17:54:10 -0800 | [diff] [blame] | 1640 | } |
| 1641 | |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 1642 | status_t AudioRecord::setPreferredMicrophoneDirection(audio_microphone_direction_t direction) |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 1643 | { |
| 1644 | AutoMutex lock(mLock); |
Paul McLean | 366b643 | 2019-02-25 10:35:51 -0700 | [diff] [blame] | 1645 | if (mSelectedMicDirection == direction) { |
| 1646 | // NOP |
| 1647 | return OK; |
| 1648 | } |
| 1649 | |
| 1650 | mSelectedMicDirection = direction; |
| 1651 | if (mAudioRecord == 0) { |
| 1652 | // the internal AudioRecord hasn't be created yet, so just stash the attribute. |
| 1653 | return OK; |
| 1654 | } else { |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 1655 | return statusTFromBinderStatus(mAudioRecord->setPreferredMicrophoneDirection(direction)); |
Paul McLean | 366b643 | 2019-02-25 10:35:51 -0700 | [diff] [blame] | 1656 | } |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 1657 | } |
| 1658 | |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 1659 | status_t AudioRecord::setPreferredMicrophoneFieldDimension(float zoom) { |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 1660 | AutoMutex lock(mLock); |
Paul McLean | 366b643 | 2019-02-25 10:35:51 -0700 | [diff] [blame] | 1661 | if (mSelectedMicFieldDimension == zoom) { |
| 1662 | // NOP |
| 1663 | return OK; |
| 1664 | } |
| 1665 | |
| 1666 | mSelectedMicFieldDimension = zoom; |
| 1667 | if (mAudioRecord == 0) { |
| 1668 | // the internal AudioRecord hasn't be created yet, so just stash the attribute. |
| 1669 | return OK; |
| 1670 | } else { |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 1671 | return statusTFromBinderStatus(mAudioRecord->setPreferredMicrophoneFieldDimension(zoom)); |
Paul McLean | 366b643 | 2019-02-25 10:35:51 -0700 | [diff] [blame] | 1672 | } |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 1673 | } |
| 1674 | |
Andy Hung | 1a9c21b | 2021-02-25 20:43:18 -0800 | [diff] [blame] | 1675 | void AudioRecord::setLogSessionId(const char *logSessionId) |
| 1676 | { |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 1677 | AutoMutex lock(mLock); |
Andy Hung | 1a9c21b | 2021-02-25 20:43:18 -0800 | [diff] [blame] | 1678 | if (logSessionId == nullptr) logSessionId = ""; // an empty string is an unset session id. |
| 1679 | if (mLogSessionId == logSessionId) return; |
| 1680 | |
| 1681 | mLogSessionId = logSessionId; |
| 1682 | mediametrics::LogItem(mMetricsId) |
| 1683 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_SETLOGSESSIONID) |
| 1684 | .set(AMEDIAMETRICS_PROP_LOGSESSIONID, logSessionId) |
| 1685 | .record(); |
| 1686 | } |
| 1687 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 1688 | status_t AudioRecord::shareAudioHistory(const std::string& sharedPackageName, |
| 1689 | int64_t sharedStartMs) |
| 1690 | { |
| 1691 | AutoMutex lock(mLock); |
| 1692 | if (mAudioRecord == 0) { |
| 1693 | return NO_INIT; |
| 1694 | } |
| 1695 | status_t status = statusTFromBinderStatus( |
| 1696 | mAudioRecord->shareAudioHistory(sharedPackageName, sharedStartMs)); |
| 1697 | if (status == NO_ERROR) { |
| 1698 | mSharedAudioPackageName = sharedPackageName; |
| 1699 | mSharedAudioStartMs = sharedStartMs; |
| 1700 | } |
| 1701 | return status; |
| 1702 | } |
| 1703 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1704 | // ========================================================================= |
| 1705 | |
Glenn Kasten | 7c7be1e | 2013-12-19 16:34:04 -0800 | [diff] [blame] | 1706 | void AudioRecord::DeathNotifier::binderDied(const wp<IBinder>& who __unused) |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1707 | { |
| 1708 | sp<AudioRecord> audioRecord = mAudioRecord.promote(); |
| 1709 | if (audioRecord != 0) { |
| 1710 | AutoMutex lock(audioRecord->mLock); |
| 1711 | audioRecord->mProxy->binderDied(); |
| 1712 | } |
| 1713 | } |
| 1714 | |
| 1715 | // ========================================================================= |
| 1716 | |
Andy Hung | ca35367 | 2019-03-06 11:54:38 -0800 | [diff] [blame] | 1717 | AudioRecord::AudioRecordThread::AudioRecordThread(AudioRecord& receiver) |
Andy Hung | eb46cf9 | 2019-03-06 10:13:38 -0800 | [diff] [blame] | 1718 | : Thread(true /* bCanCallJava */) // binder recursion on restoreRecord_l() may call Java. |
| 1719 | , mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL), |
Glenn Kasten | 41721bb | 2014-01-13 09:59:51 -0800 | [diff] [blame] | 1720 | mIgnoreNextPausedInt(false) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1721 | { |
| 1722 | } |
| 1723 | |
Glenn Kasten | 68337ed | 2012-07-12 09:05:58 -0700 | [diff] [blame] | 1724 | AudioRecord::AudioRecordThread::~AudioRecordThread() |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1725 | { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1726 | } |
| 1727 | |
Glenn Kasten | 68337ed | 2012-07-12 09:05:58 -0700 | [diff] [blame] | 1728 | bool AudioRecord::AudioRecordThread::threadLoop() |
Glenn Kasten | 6dbc135 | 2012-02-02 10:56:47 -0800 | [diff] [blame] | 1729 | { |
Glenn Kasten | 68337ed | 2012-07-12 09:05:58 -0700 | [diff] [blame] | 1730 | { |
| 1731 | AutoMutex _l(mMyLock); |
| 1732 | if (mPaused) { |
Glenn Kasten | 75f7903 | 2017-05-23 11:22:44 -0700 | [diff] [blame] | 1733 | // TODO check return value and handle or log |
Glenn Kasten | 68337ed | 2012-07-12 09:05:58 -0700 | [diff] [blame] | 1734 | mMyCond.wait(mMyLock); |
| 1735 | // caller will check for exitPending() |
| 1736 | return true; |
| 1737 | } |
Glenn Kasten | 41721bb | 2014-01-13 09:59:51 -0800 | [diff] [blame] | 1738 | if (mIgnoreNextPausedInt) { |
| 1739 | mIgnoreNextPausedInt = false; |
| 1740 | mPausedInt = false; |
| 1741 | } |
Glenn Kasten | 5a6cd22 | 2013-09-20 09:20:45 -0700 | [diff] [blame] | 1742 | if (mPausedInt) { |
Glenn Kasten | 5a6cd22 | 2013-09-20 09:20:45 -0700 | [diff] [blame] | 1743 | if (mPausedNs > 0) { |
Glenn Kasten | 75f7903 | 2017-05-23 11:22:44 -0700 | [diff] [blame] | 1744 | // TODO check return value and handle or log |
Glenn Kasten | 5a6cd22 | 2013-09-20 09:20:45 -0700 | [diff] [blame] | 1745 | (void) mMyCond.waitRelative(mMyLock, mPausedNs); |
| 1746 | } else { |
Glenn Kasten | 75f7903 | 2017-05-23 11:22:44 -0700 | [diff] [blame] | 1747 | // TODO check return value and handle or log |
Glenn Kasten | 5a6cd22 | 2013-09-20 09:20:45 -0700 | [diff] [blame] | 1748 | mMyCond.wait(mMyLock); |
| 1749 | } |
Eric Laurent | 9d2c78c | 2013-09-23 12:29:42 -0700 | [diff] [blame] | 1750 | mPausedInt = false; |
Glenn Kasten | 5a6cd22 | 2013-09-20 09:20:45 -0700 | [diff] [blame] | 1751 | return true; |
| 1752 | } |
Glenn Kasten | 6dbc135 | 2012-02-02 10:56:47 -0800 | [diff] [blame] | 1753 | } |
Kiran Kumar Krishna | e813ef9 | 2016-11-01 07:50:22 -0500 | [diff] [blame] | 1754 | if (exitPending()) { |
| 1755 | return false; |
| 1756 | } |
Glenn Kasten | 7c7be1e | 2013-12-19 16:34:04 -0800 | [diff] [blame] | 1757 | nsecs_t ns = mReceiver.processAudioBuffer(); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1758 | switch (ns) { |
| 1759 | case 0: |
| 1760 | return true; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1761 | case NS_INACTIVE: |
Glenn Kasten | 5a6cd22 | 2013-09-20 09:20:45 -0700 | [diff] [blame] | 1762 | pauseInternal(); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1763 | return true; |
| 1764 | case NS_NEVER: |
| 1765 | return false; |
Glenn Kasten | 5a6cd22 | 2013-09-20 09:20:45 -0700 | [diff] [blame] | 1766 | case NS_WHENEVER: |
Andy Hung | 803b3e5 | 2015-03-18 23:26:55 -0700 | [diff] [blame] | 1767 | // Event driven: call wake() when callback notifications conditions change. |
| 1768 | ns = INT64_MAX; |
Chih-Hung Hsieh | ffe3558 | 2018-09-13 13:59:28 -0700 | [diff] [blame] | 1769 | FALLTHROUGH_INTENDED; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1770 | default: |
Andy Hung | 6b1c612 | 2018-09-12 19:09:07 -0700 | [diff] [blame] | 1771 | LOG_ALWAYS_FATAL_IF(ns < 0, "%s() returned %lld", __func__, (long long)ns); |
Glenn Kasten | 5a6cd22 | 2013-09-20 09:20:45 -0700 | [diff] [blame] | 1772 | pauseInternal(ns); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1773 | return true; |
Glenn Kasten | 68337ed | 2012-07-12 09:05:58 -0700 | [diff] [blame] | 1774 | } |
Glenn Kasten | 68337ed | 2012-07-12 09:05:58 -0700 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | void AudioRecord::AudioRecordThread::requestExit() |
| 1778 | { |
| 1779 | // must be in this order to avoid a race condition |
| 1780 | Thread::requestExit(); |
Glenn Kasten | 41721bb | 2014-01-13 09:59:51 -0800 | [diff] [blame] | 1781 | resume(); |
Glenn Kasten | 68337ed | 2012-07-12 09:05:58 -0700 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | void AudioRecord::AudioRecordThread::pause() |
| 1785 | { |
| 1786 | AutoMutex _l(mMyLock); |
| 1787 | mPaused = true; |
| 1788 | } |
| 1789 | |
| 1790 | void AudioRecord::AudioRecordThread::resume() |
| 1791 | { |
| 1792 | AutoMutex _l(mMyLock); |
Glenn Kasten | 41721bb | 2014-01-13 09:59:51 -0800 | [diff] [blame] | 1793 | mIgnoreNextPausedInt = true; |
Eric Laurent | 9d2c78c | 2013-09-23 12:29:42 -0700 | [diff] [blame] | 1794 | if (mPaused || mPausedInt) { |
Glenn Kasten | 68337ed | 2012-07-12 09:05:58 -0700 | [diff] [blame] | 1795 | mPaused = false; |
Eric Laurent | 9d2c78c | 2013-09-23 12:29:42 -0700 | [diff] [blame] | 1796 | mPausedInt = false; |
Glenn Kasten | 68337ed | 2012-07-12 09:05:58 -0700 | [diff] [blame] | 1797 | mMyCond.signal(); |
| 1798 | } |
Glenn Kasten | 6dbc135 | 2012-02-02 10:56:47 -0800 | [diff] [blame] | 1799 | } |
| 1800 | |
Andy Hung | 803b3e5 | 2015-03-18 23:26:55 -0700 | [diff] [blame] | 1801 | void AudioRecord::AudioRecordThread::wake() |
| 1802 | { |
| 1803 | AutoMutex _l(mMyLock); |
Andy Hung | 805a0c7 | 2015-07-22 11:40:26 -0700 | [diff] [blame] | 1804 | if (!mPaused) { |
| 1805 | // wake() might be called while servicing a callback - ignore the next |
| 1806 | // pause time and call processAudioBuffer. |
Andy Hung | 803b3e5 | 2015-03-18 23:26:55 -0700 | [diff] [blame] | 1807 | mIgnoreNextPausedInt = true; |
Andy Hung | 805a0c7 | 2015-07-22 11:40:26 -0700 | [diff] [blame] | 1808 | if (mPausedInt && mPausedNs > 0) { |
| 1809 | // audio record is active and internally paused with timeout. |
| 1810 | mPausedInt = false; |
| 1811 | mMyCond.signal(); |
| 1812 | } |
Andy Hung | 803b3e5 | 2015-03-18 23:26:55 -0700 | [diff] [blame] | 1813 | } |
| 1814 | } |
| 1815 | |
Glenn Kasten | 5a6cd22 | 2013-09-20 09:20:45 -0700 | [diff] [blame] | 1816 | void AudioRecord::AudioRecordThread::pauseInternal(nsecs_t ns) |
| 1817 | { |
| 1818 | AutoMutex _l(mMyLock); |
| 1819 | mPausedInt = true; |
| 1820 | mPausedNs = ns; |
| 1821 | } |
| 1822 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1823 | // ------------------------------------------------------------------------- |
| 1824 | |
Glenn Kasten | 40bc906 | 2015-03-20 09:09:33 -0700 | [diff] [blame] | 1825 | } // namespace android |