The Android Open Source Project | 9066cfe | 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 | |
| 21 | #include <stdint.h> |
| 22 | #include <sys/types.h> |
| 23 | |
| 24 | #include <sched.h> |
| 25 | #include <sys/resource.h> |
| 26 | |
| 27 | #include <private/media/AudioTrackShared.h> |
| 28 | |
| 29 | #include <media/AudioSystem.h> |
| 30 | #include <media/AudioRecord.h> |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 31 | #include <media/mediarecorder.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 33 | #include <binder/IServiceManager.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | #include <utils/Log.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 35 | #include <binder/Parcel.h> |
| 36 | #include <binder/IPCThreadState.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | #include <utils/Timers.h> |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 38 | #include <utils/Atomic.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
Dima Zavin | 34bb419 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 40 | #include <system/audio.h> |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 41 | #include <cutils/bitops.h> |
Glenn Kasten | e80a4cc | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 42 | #include <cutils/compiler.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | |
| 44 | namespace android { |
Chia-chi Yeh | 97d61f7 | 2010-06-22 08:01:13 +0800 | [diff] [blame] | 45 | // --------------------------------------------------------------------------- |
| 46 | |
| 47 | // static |
| 48 | status_t AudioRecord::getMinFrameCount( |
| 49 | int* frameCount, |
| 50 | uint32_t sampleRate, |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 51 | audio_format_t format, |
Chia-chi Yeh | 97d61f7 | 2010-06-22 08:01:13 +0800 | [diff] [blame] | 52 | int channelCount) |
| 53 | { |
| 54 | size_t size = 0; |
| 55 | if (AudioSystem::getInputBufferSize(sampleRate, format, channelCount, &size) |
| 56 | != NO_ERROR) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 57 | ALOGE("AudioSystem could not query the input buffer size."); |
Chia-chi Yeh | 97d61f7 | 2010-06-22 08:01:13 +0800 | [diff] [blame] | 58 | return NO_INIT; |
| 59 | } |
| 60 | |
| 61 | if (size == 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 62 | ALOGE("Unsupported configuration: sampleRate %d, format %d, channelCount %d", |
Chia-chi Yeh | 97d61f7 | 2010-06-22 08:01:13 +0800 | [diff] [blame] | 63 | sampleRate, format, channelCount); |
| 64 | return BAD_VALUE; |
| 65 | } |
| 66 | |
| 67 | // We double the size of input buffer for ping pong use of record buffer. |
| 68 | size <<= 1; |
| 69 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 70 | if (audio_is_linear_pcm(format)) { |
Eric Laurent | c310dcb | 2011-06-16 21:30:45 -0700 | [diff] [blame] | 71 | size /= channelCount * audio_bytes_per_sample(format); |
Chia-chi Yeh | 97d61f7 | 2010-06-22 08:01:13 +0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | *frameCount = size; |
| 75 | return NO_ERROR; |
| 76 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | |
| 78 | // --------------------------------------------------------------------------- |
| 79 | |
| 80 | AudioRecord::AudioRecord() |
Glenn Kasten | 99d5443 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 81 | : mStatus(NO_INIT), mSessionId(0), |
| 82 | mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | { |
| 84 | } |
| 85 | |
| 86 | AudioRecord::AudioRecord( |
Glenn Kasten | 0f0fbd9 | 2012-01-23 13:58:49 -0800 | [diff] [blame] | 87 | audio_source_t inputSource, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | uint32_t sampleRate, |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 89 | audio_format_t format, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 90 | uint32_t channelMask, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | int frameCount, |
Glenn Kasten | f60a5d7 | 2012-03-07 09:04:02 -0800 | [diff] [blame] | 92 | record_flags flags, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | callback_t cbf, |
| 94 | void* user, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 95 | int notificationFrames, |
| 96 | int sessionId) |
Glenn Kasten | 99d5443 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 97 | : mStatus(NO_INIT), mSessionId(0), |
| 98 | mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | { |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 100 | mStatus = set(inputSource, sampleRate, format, channelMask, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 101 | frameCount, flags, cbf, user, notificationFrames, sessionId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | AudioRecord::~AudioRecord() |
| 105 | { |
| 106 | if (mStatus == NO_ERROR) { |
| 107 | // Make sure that callback function exits in the case where |
| 108 | // it is looping on buffer empty condition in obtainBuffer(). |
| 109 | // Otherwise the callback thread will never exit. |
| 110 | stop(); |
| 111 | if (mClientRecordThread != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | mClientRecordThread->requestExitAndWait(); |
| 113 | mClientRecordThread.clear(); |
| 114 | } |
| 115 | mAudioRecord.clear(); |
| 116 | IPCThreadState::self()->flushCommands(); |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 117 | AudioSystem::releaseAudioSessionId(mSessionId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | status_t AudioRecord::set( |
Glenn Kasten | 0f0fbd9 | 2012-01-23 13:58:49 -0800 | [diff] [blame] | 122 | audio_source_t inputSource, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | uint32_t sampleRate, |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 124 | audio_format_t format, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 125 | uint32_t channelMask, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | int frameCount, |
Glenn Kasten | f60a5d7 | 2012-03-07 09:04:02 -0800 | [diff] [blame] | 127 | record_flags flags, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | callback_t cbf, |
| 129 | void* user, |
| 130 | int notificationFrames, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 131 | bool threadCanCallJava, |
| 132 | int sessionId) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | { |
| 134 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 135 | ALOGV("set(): sampleRate %d, channelMask %d, frameCount %d",sampleRate, channelMask, frameCount); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 136 | |
| 137 | AutoMutex lock(mLock); |
| 138 | |
Eric Laurent | ef02827 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 139 | if (mAudioRecord != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | return INVALID_OPERATION; |
| 141 | } |
| 142 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 143 | if (inputSource == AUDIO_SOURCE_DEFAULT) { |
| 144 | inputSource = AUDIO_SOURCE_MIC; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | if (sampleRate == 0) { |
| 148 | sampleRate = DEFAULT_SAMPLE_RATE; |
| 149 | } |
| 150 | // these below should probably come from the audioFlinger too... |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 151 | if (format == AUDIO_FORMAT_DEFAULT) { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 152 | format = AUDIO_FORMAT_PCM_16_BIT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | // validate parameters |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 155 | if (!audio_is_valid_format(format)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 156 | ALOGE("Invalid format"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | return BAD_VALUE; |
| 158 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 159 | |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 160 | if (!audio_is_input_channel(channelMask)) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 161 | return BAD_VALUE; |
| 162 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 163 | |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 164 | int channelCount = popcount(channelMask); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 165 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 166 | if (sessionId == 0 ) { |
| 167 | mSessionId = AudioSystem::newAudioSessionId(); |
| 168 | } else { |
| 169 | mSessionId = sessionId; |
| 170 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 171 | ALOGV("set(): mSessionId %d", mSessionId); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 172 | |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 173 | audio_io_handle_t input = AudioSystem::getInput(inputSource, |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 174 | sampleRate, |
| 175 | format, |
| 176 | channelMask, |
| 177 | (audio_in_acoustics_t)flags, |
| 178 | mSessionId); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 179 | if (input == 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 180 | ALOGE("Could not get audio input for record source %d", inputSource); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | return BAD_VALUE; |
| 182 | } |
| 183 | |
| 184 | // validate framecount |
Chia-chi Yeh | 97d61f7 | 2010-06-22 08:01:13 +0800 | [diff] [blame] | 185 | int minFrameCount = 0; |
| 186 | status_t status = getMinFrameCount(&minFrameCount, sampleRate, format, channelCount); |
| 187 | if (status != NO_ERROR) { |
| 188 | return status; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 190 | ALOGV("AudioRecord::set() minFrameCount = %d", minFrameCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | |
| 192 | if (frameCount == 0) { |
| 193 | frameCount = minFrameCount; |
| 194 | } else if (frameCount < minFrameCount) { |
| 195 | return BAD_VALUE; |
| 196 | } |
| 197 | |
| 198 | if (notificationFrames == 0) { |
| 199 | notificationFrames = frameCount/2; |
| 200 | } |
| 201 | |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 202 | // create the IAudioRecord |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 203 | status = openRecord_l(sampleRate, format, channelMask, |
Chia-chi Yeh | 97d61f7 | 2010-06-22 08:01:13 +0800 | [diff] [blame] | 204 | frameCount, flags, input); |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 205 | if (status != NO_ERROR) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | return status; |
| 207 | } |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 208 | |
Glenn Kasten | 3694ec1 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 209 | if (cbf != NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | mClientRecordThread = new ClientRecordThread(*this, threadCanCallJava); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | mStatus = NO_ERROR; |
| 214 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | mFormat = format; |
| 216 | // Update buffer size in case it has been limited by AudioFlinger during track creation |
| 217 | mFrameCount = mCblk->frameCount; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 218 | mChannelCount = (uint8_t)channelCount; |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 219 | mChannelMask = channelMask; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | mActive = 0; |
| 221 | mCbf = cbf; |
| 222 | mNotificationFrames = notificationFrames; |
| 223 | mRemainingFrames = notificationFrames; |
| 224 | mUserData = user; |
| 225 | // TODO: add audio hardware input latency here |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 226 | mLatency = (1000*mFrameCount) / sampleRate; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | mMarkerPosition = 0; |
Jean-Michel Trivi | 78c1314 | 2009-03-24 19:48:58 -0700 | [diff] [blame] | 228 | mMarkerReached = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | mNewPosition = 0; |
| 230 | mUpdatePeriod = 0; |
Glenn Kasten | 0f0fbd9 | 2012-01-23 13:58:49 -0800 | [diff] [blame] | 231 | mInputSource = inputSource; |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 232 | mFlags = flags; |
Eric Laurent | 47d0a92 | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 233 | mInput = input; |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 234 | AudioSystem::acquireAudioSessionId(mSessionId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | |
| 236 | return NO_ERROR; |
| 237 | } |
| 238 | |
| 239 | status_t AudioRecord::initCheck() const |
| 240 | { |
| 241 | return mStatus; |
| 242 | } |
| 243 | |
| 244 | // ------------------------------------------------------------------------- |
| 245 | |
| 246 | uint32_t AudioRecord::latency() const |
| 247 | { |
| 248 | return mLatency; |
| 249 | } |
| 250 | |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 251 | audio_format_t AudioRecord::format() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | { |
| 253 | return mFormat; |
| 254 | } |
| 255 | |
| 256 | int AudioRecord::channelCount() const |
| 257 | { |
| 258 | return mChannelCount; |
| 259 | } |
| 260 | |
| 261 | uint32_t AudioRecord::frameCount() const |
| 262 | { |
| 263 | return mFrameCount; |
| 264 | } |
| 265 | |
Glenn Kasten | faf354d | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 266 | size_t AudioRecord::frameSize() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 268 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | c310dcb | 2011-06-16 21:30:45 -0700 | [diff] [blame] | 269 | return channelCount()*audio_bytes_per_sample(mFormat); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 270 | } else { |
| 271 | return sizeof(uint8_t); |
| 272 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Glenn Kasten | 0f0fbd9 | 2012-01-23 13:58:49 -0800 | [diff] [blame] | 275 | audio_source_t AudioRecord::inputSource() const |
Eric Laurent | 4bc035a | 2009-05-22 09:18:15 -0700 | [diff] [blame] | 276 | { |
Glenn Kasten | 0f0fbd9 | 2012-01-23 13:58:49 -0800 | [diff] [blame] | 277 | return mInputSource; |
Eric Laurent | 4bc035a | 2009-05-22 09:18:15 -0700 | [diff] [blame] | 278 | } |
| 279 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | // ------------------------------------------------------------------------- |
| 281 | |
| 282 | status_t AudioRecord::start() |
| 283 | { |
| 284 | status_t ret = NO_ERROR; |
| 285 | sp<ClientRecordThread> t = mClientRecordThread; |
| 286 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 287 | ALOGV("start"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | |
| 289 | if (t != 0) { |
| 290 | if (t->exitPending()) { |
| 291 | if (t->requestExitAndWait() == WOULD_BLOCK) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 292 | ALOGE("AudioRecord::start called from thread"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 293 | return WOULD_BLOCK; |
| 294 | } |
| 295 | } |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame^] | 296 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | |
Eric Laurent | f3d6dd0 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 298 | AutoMutex lock(mLock); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 299 | // acquire a strong reference on the IAudioRecord and IMemory so that they cannot be destroyed |
| 300 | // while we are accessing the cblk |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame^] | 301 | sp<IAudioRecord> audioRecord = mAudioRecord; |
| 302 | sp<IMemory> iMem = mCblkMemory; |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 303 | audio_track_cblk_t* cblk = mCblk; |
Eric Laurent | f3d6dd0 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 304 | if (mActive == 0) { |
| 305 | mActive = 1; |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 306 | |
Glenn Kasten | 6a20b26 | 2012-02-02 10:56:47 -0800 | [diff] [blame] | 307 | pid_t tid; |
| 308 | if (t != 0) { |
| 309 | mReadyToRun = WOULD_BLOCK; |
Glenn Kasten | 86e3362 | 2012-02-28 12:30:08 -0800 | [diff] [blame] | 310 | t->run("AudioRecord", ANDROID_PRIORITY_AUDIO); |
Glenn Kasten | 6a20b26 | 2012-02-02 10:56:47 -0800 | [diff] [blame] | 311 | tid = t->getTid(); // pid_t is unknown until run() |
| 312 | ALOGV("getTid=%d", tid); |
| 313 | if (tid == -1) { |
| 314 | tid = 0; |
| 315 | } |
| 316 | // thread blocks in readyToRun() |
| 317 | } else { |
| 318 | tid = 0; // not gettid() |
| 319 | } |
| 320 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 321 | cblk->lock.lock(); |
| 322 | if (!(cblk->flags & CBLK_INVALID_MSK)) { |
| 323 | cblk->lock.unlock(); |
Glenn Kasten | 6a20b26 | 2012-02-02 10:56:47 -0800 | [diff] [blame] | 324 | ALOGV("mAudioRecord->start(tid=%d)", tid); |
| 325 | ret = mAudioRecord->start(tid); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 326 | cblk->lock.lock(); |
| 327 | if (ret == DEAD_OBJECT) { |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 328 | android_atomic_or(CBLK_INVALID_ON, &cblk->flags); |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 329 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | } |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 331 | if (cblk->flags & CBLK_INVALID_MSK) { |
| 332 | ret = restoreRecord_l(cblk); |
| 333 | } |
| 334 | cblk->lock.unlock(); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 335 | if (ret == NO_ERROR) { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 336 | mNewPosition = cblk->user + mUpdatePeriod; |
| 337 | cblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS; |
| 338 | cblk->waitTimeMs = 0; |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 339 | if (t != 0) { |
Glenn Kasten | 6a20b26 | 2012-02-02 10:56:47 -0800 | [diff] [blame] | 340 | // thread unblocks in readyToRun() and returns NO_ERROR |
| 341 | mReadyToRun = NO_ERROR; |
| 342 | mCondition.signal(); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 343 | } else { |
Glenn Kasten | 99d5443 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 344 | mPreviousPriority = getpriority(PRIO_PROCESS, 0); |
| 345 | mPreviousSchedulingGroup = androidGetThreadSchedulingGroup(0); |
| 346 | androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 347 | } |
| 348 | } else { |
Eric Laurent | f3d6dd0 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 349 | mActive = 0; |
Glenn Kasten | 6a20b26 | 2012-02-02 10:56:47 -0800 | [diff] [blame] | 350 | // thread unblocks in readyToRun() and returns NO_INIT |
| 351 | mReadyToRun = NO_INIT; |
| 352 | mCondition.signal(); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 353 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | } |
| 355 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | return ret; |
| 357 | } |
| 358 | |
| 359 | status_t AudioRecord::stop() |
| 360 | { |
| 361 | sp<ClientRecordThread> t = mClientRecordThread; |
| 362 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 363 | ALOGV("stop"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | |
Eric Laurent | f3d6dd0 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 365 | AutoMutex lock(mLock); |
| 366 | if (mActive == 1) { |
| 367 | mActive = 0; |
Eric Laurent | ef02827 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 368 | mCblk->cv.signal(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | mAudioRecord->stop(); |
Jean-Michel Trivi | 78c1314 | 2009-03-24 19:48:58 -0700 | [diff] [blame] | 370 | // the record head position will reset to 0, so if a marker is set, we need |
| 371 | // to activate it again |
| 372 | mMarkerReached = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | if (t != 0) { |
| 374 | t->requestExit(); |
| 375 | } else { |
Glenn Kasten | 99d5443 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 376 | setpriority(PRIO_PROCESS, 0, mPreviousPriority); |
| 377 | androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 381 | return NO_ERROR; |
| 382 | } |
| 383 | |
| 384 | bool AudioRecord::stopped() const |
| 385 | { |
| 386 | return !mActive; |
| 387 | } |
| 388 | |
Glenn Kasten | bd714b6 | 2012-02-24 16:33:14 -0800 | [diff] [blame] | 389 | uint32_t AudioRecord::getSampleRate() const |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 390 | { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 391 | AutoMutex lock(mLock); |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 392 | return mCblk->sampleRate; |
| 393 | } |
| 394 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | status_t AudioRecord::setMarkerPosition(uint32_t marker) |
| 396 | { |
Glenn Kasten | 3694ec1 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 397 | if (mCbf == NULL) return INVALID_OPERATION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | |
| 399 | mMarkerPosition = marker; |
Jean-Michel Trivi | 78c1314 | 2009-03-24 19:48:58 -0700 | [diff] [blame] | 400 | mMarkerReached = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | |
| 402 | return NO_ERROR; |
| 403 | } |
| 404 | |
Glenn Kasten | bd714b6 | 2012-02-24 16:33:14 -0800 | [diff] [blame] | 405 | status_t AudioRecord::getMarkerPosition(uint32_t *marker) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | { |
Glenn Kasten | 3694ec1 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 407 | if (marker == NULL) return BAD_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | |
| 409 | *marker = mMarkerPosition; |
| 410 | |
| 411 | return NO_ERROR; |
| 412 | } |
| 413 | |
| 414 | status_t AudioRecord::setPositionUpdatePeriod(uint32_t updatePeriod) |
| 415 | { |
Glenn Kasten | 3694ec1 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 416 | if (mCbf == NULL) return INVALID_OPERATION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | |
| 418 | uint32_t curPosition; |
| 419 | getPosition(&curPosition); |
| 420 | mNewPosition = curPosition + updatePeriod; |
| 421 | mUpdatePeriod = updatePeriod; |
| 422 | |
| 423 | return NO_ERROR; |
| 424 | } |
| 425 | |
Glenn Kasten | bd714b6 | 2012-02-24 16:33:14 -0800 | [diff] [blame] | 426 | status_t AudioRecord::getPositionUpdatePeriod(uint32_t *updatePeriod) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | { |
Glenn Kasten | 3694ec1 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 428 | if (updatePeriod == NULL) return BAD_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | |
| 430 | *updatePeriod = mUpdatePeriod; |
| 431 | |
| 432 | return NO_ERROR; |
| 433 | } |
| 434 | |
Glenn Kasten | bd714b6 | 2012-02-24 16:33:14 -0800 | [diff] [blame] | 435 | status_t AudioRecord::getPosition(uint32_t *position) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | { |
Glenn Kasten | 3694ec1 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 437 | if (position == NULL) return BAD_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 439 | AutoMutex lock(mLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | *position = mCblk->user; |
| 441 | |
| 442 | return NO_ERROR; |
| 443 | } |
| 444 | |
Glenn Kasten | bd714b6 | 2012-02-24 16:33:14 -0800 | [diff] [blame] | 445 | unsigned int AudioRecord::getInputFramesLost() const |
Eric Laurent | 47d0a92 | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 446 | { |
| 447 | if (mActive) |
| 448 | return AudioSystem::getInputFramesLost(mInput); |
| 449 | else |
| 450 | return 0; |
| 451 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | |
| 453 | // ------------------------------------------------------------------------- |
| 454 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 455 | // must be called with mLock held |
| 456 | status_t AudioRecord::openRecord_l( |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 457 | uint32_t sampleRate, |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 458 | audio_format_t format, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 459 | uint32_t channelMask, |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 460 | int frameCount, |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 461 | uint32_t flags, |
| 462 | audio_io_handle_t input) |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 463 | { |
| 464 | status_t status; |
| 465 | const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger(); |
| 466 | if (audioFlinger == 0) { |
| 467 | return NO_INIT; |
| 468 | } |
| 469 | |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 470 | sp<IAudioRecord> record = audioFlinger->openRecord(getpid(), input, |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 471 | sampleRate, format, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 472 | channelMask, |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 473 | frameCount, |
| 474 | ((uint16_t)flags) << 16, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 475 | &mSessionId, |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 476 | &status); |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 477 | |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 478 | if (record == 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 479 | ALOGE("AudioFlinger could not create record track, status: %d", status); |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 480 | return status; |
| 481 | } |
| 482 | sp<IMemory> cblk = record->getCblk(); |
| 483 | if (cblk == 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 484 | ALOGE("Could not get control block"); |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 485 | return NO_INIT; |
| 486 | } |
| 487 | mAudioRecord.clear(); |
| 488 | mAudioRecord = record; |
| 489 | mCblkMemory.clear(); |
| 490 | mCblkMemory = cblk; |
| 491 | mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer()); |
| 492 | mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t); |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 493 | android_atomic_and(~CBLK_DIRECTION_MSK, &mCblk->flags); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 494 | mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS; |
| 495 | mCblk->waitTimeMs = 0; |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 496 | return NO_ERROR; |
| 497 | } |
| 498 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | status_t AudioRecord::obtainBuffer(Buffer* audioBuffer, int32_t waitCount) |
| 500 | { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 501 | AutoMutex lock(mLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | int active; |
Glenn Kasten | 028ab99 | 2011-06-22 16:18:04 -0700 | [diff] [blame] | 503 | status_t result = NO_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | audio_track_cblk_t* cblk = mCblk; |
| 505 | uint32_t framesReq = audioBuffer->frameCount; |
Eric Laurent | ef02827 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 506 | uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | |
| 508 | audioBuffer->frameCount = 0; |
| 509 | audioBuffer->size = 0; |
| 510 | |
| 511 | uint32_t framesReady = cblk->framesReady(); |
| 512 | |
| 513 | if (framesReady == 0) { |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 514 | cblk->lock.lock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 515 | goto start_loop_here; |
| 516 | while (framesReady == 0) { |
| 517 | active = mActive; |
Glenn Kasten | e80a4cc | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 518 | if (CC_UNLIKELY(!active)) { |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 519 | cblk->lock.unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 520 | return NO_MORE_BUFFERS; |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 521 | } |
Glenn Kasten | e80a4cc | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 522 | if (CC_UNLIKELY(!waitCount)) { |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 523 | cblk->lock.unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 524 | return WOULD_BLOCK; |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 525 | } |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 526 | if (!(cblk->flags & CBLK_INVALID_MSK)) { |
| 527 | mLock.unlock(); |
| 528 | result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs)); |
| 529 | cblk->lock.unlock(); |
| 530 | mLock.lock(); |
| 531 | if (mActive == 0) { |
| 532 | return status_t(STOPPED); |
| 533 | } |
| 534 | cblk->lock.lock(); |
| 535 | } |
| 536 | if (cblk->flags & CBLK_INVALID_MSK) { |
| 537 | goto create_new_record; |
| 538 | } |
Glenn Kasten | e80a4cc | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 539 | if (CC_UNLIKELY(result != NO_ERROR)) { |
Eric Laurent | ef02827 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 540 | cblk->waitTimeMs += waitTimeMs; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 541 | if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 542 | ALOGW( "obtainBuffer timed out (is the CPU pegged?) " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 543 | "user=%08x, server=%08x", cblk->user, cblk->server); |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 544 | cblk->lock.unlock(); |
Glenn Kasten | 6a20b26 | 2012-02-02 10:56:47 -0800 | [diff] [blame] | 545 | result = mAudioRecord->start(0); // callback thread hasn't changed |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 546 | cblk->lock.lock(); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 547 | if (result == DEAD_OBJECT) { |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 548 | android_atomic_or(CBLK_INVALID_ON, &cblk->flags); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 549 | create_new_record: |
| 550 | result = AudioRecord::restoreRecord_l(cblk); |
| 551 | } |
| 552 | if (result != NO_ERROR) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 553 | ALOGW("obtainBuffer create Track error %d", result); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 554 | cblk->lock.unlock(); |
| 555 | return result; |
| 556 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 557 | cblk->waitTimeMs = 0; |
| 558 | } |
| 559 | if (--waitCount == 0) { |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 560 | cblk->lock.unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | return TIMED_OUT; |
| 562 | } |
| 563 | } |
| 564 | // read the server count again |
| 565 | start_loop_here: |
| 566 | framesReady = cblk->framesReady(); |
| 567 | } |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 568 | cblk->lock.unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | } |
| 570 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | cblk->waitTimeMs = 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 572 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 573 | if (framesReq > framesReady) { |
| 574 | framesReq = framesReady; |
| 575 | } |
| 576 | |
| 577 | uint32_t u = cblk->user; |
| 578 | uint32_t bufferEnd = cblk->userBase + cblk->frameCount; |
| 579 | |
| 580 | if (u + framesReq > bufferEnd) { |
| 581 | framesReq = bufferEnd - u; |
| 582 | } |
| 583 | |
| 584 | audioBuffer->flags = 0; |
| 585 | audioBuffer->channelCount= mChannelCount; |
| 586 | audioBuffer->format = mFormat; |
| 587 | audioBuffer->frameCount = framesReq; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 588 | audioBuffer->size = framesReq*cblk->frameSize; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 589 | audioBuffer->raw = (int8_t*)cblk->buffer(u); |
| 590 | active = mActive; |
| 591 | return active ? status_t(NO_ERROR) : status_t(STOPPED); |
| 592 | } |
| 593 | |
| 594 | void AudioRecord::releaseBuffer(Buffer* audioBuffer) |
| 595 | { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 596 | AutoMutex lock(mLock); |
| 597 | mCblk->stepUser(audioBuffer->frameCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | } |
| 599 | |
Glenn Kasten | bd714b6 | 2012-02-24 16:33:14 -0800 | [diff] [blame] | 600 | audio_io_handle_t AudioRecord::getInput() const |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 601 | { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 602 | AutoMutex lock(mLock); |
Eric Laurent | 678cc95 | 2011-07-26 20:32:28 -0700 | [diff] [blame] | 603 | return mInput; |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | // must be called with mLock held |
| 607 | audio_io_handle_t AudioRecord::getInput_l() |
| 608 | { |
Eric Laurent | 47d0a92 | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 609 | mInput = AudioSystem::getInput(mInputSource, |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 610 | mCblk->sampleRate, |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 611 | mFormat, |
| 612 | mChannelMask, |
| 613 | (audio_in_acoustics_t)mFlags, |
| 614 | mSessionId); |
Eric Laurent | 47d0a92 | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 615 | return mInput; |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 616 | } |
| 617 | |
Glenn Kasten | bd714b6 | 2012-02-24 16:33:14 -0800 | [diff] [blame] | 618 | int AudioRecord::getSessionId() const |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 619 | { |
| 620 | return mSessionId; |
| 621 | } |
| 622 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 623 | // ------------------------------------------------------------------------- |
| 624 | |
| 625 | ssize_t AudioRecord::read(void* buffer, size_t userSize) |
| 626 | { |
| 627 | ssize_t read = 0; |
| 628 | Buffer audioBuffer; |
| 629 | int8_t *dst = static_cast<int8_t*>(buffer); |
| 630 | |
| 631 | if (ssize_t(userSize) < 0) { |
| 632 | // sanity-check. user is most-likely passing an error code. |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 633 | ALOGE("AudioRecord::read(buffer=%p, size=%u (%d)", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 634 | buffer, userSize, userSize); |
| 635 | return BAD_VALUE; |
| 636 | } |
| 637 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 638 | mLock.lock(); |
| 639 | // acquire a strong reference on the IAudioRecord and IMemory so that they cannot be destroyed |
| 640 | // while we are accessing the cblk |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame^] | 641 | sp<IAudioRecord> audioRecord = mAudioRecord; |
| 642 | sp<IMemory> iMem = mCblkMemory; |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 643 | mLock.unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 644 | |
| 645 | do { |
| 646 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 647 | audioBuffer.frameCount = userSize/frameSize(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 648 | |
Eric Laurent | ba8811f | 2010-03-02 18:38:06 -0800 | [diff] [blame] | 649 | // By using a wait count corresponding to twice the timeout period in |
| 650 | // obtainBuffer() we give a chance to recover once for a read timeout |
| 651 | // (if media_server crashed for instance) before returning a length of |
| 652 | // 0 bytes read to the client |
| 653 | status_t err = obtainBuffer(&audioBuffer, ((2 * MAX_RUN_TIMEOUT_MS) / WAIT_PERIOD_MS)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | if (err < 0) { |
| 655 | // out of buffers, return #bytes written |
| 656 | if (err == status_t(NO_MORE_BUFFERS)) |
| 657 | break; |
Eric Laurent | ba8811f | 2010-03-02 18:38:06 -0800 | [diff] [blame] | 658 | if (err == status_t(TIMED_OUT)) |
| 659 | err = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 660 | return ssize_t(err); |
| 661 | } |
| 662 | |
| 663 | size_t bytesRead = audioBuffer.size; |
| 664 | memcpy(dst, audioBuffer.i8, bytesRead); |
| 665 | |
| 666 | dst += bytesRead; |
| 667 | userSize -= bytesRead; |
| 668 | read += bytesRead; |
| 669 | |
| 670 | releaseBuffer(&audioBuffer); |
| 671 | } while (userSize); |
| 672 | |
| 673 | return read; |
| 674 | } |
| 675 | |
| 676 | // ------------------------------------------------------------------------- |
| 677 | |
| 678 | bool AudioRecord::processAudioBuffer(const sp<ClientRecordThread>& thread) |
| 679 | { |
| 680 | Buffer audioBuffer; |
| 681 | uint32_t frames = mRemainingFrames; |
| 682 | size_t readSize; |
| 683 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 684 | mLock.lock(); |
| 685 | // acquire a strong reference on the IAudioRecord and IMemory so that they cannot be destroyed |
| 686 | // while we are accessing the cblk |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame^] | 687 | sp<IAudioRecord> audioRecord = mAudioRecord; |
| 688 | sp<IMemory> iMem = mCblkMemory; |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 689 | audio_track_cblk_t* cblk = mCblk; |
| 690 | mLock.unlock(); |
| 691 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | // Manage marker callback |
Jean-Michel Trivi | 78c1314 | 2009-03-24 19:48:58 -0700 | [diff] [blame] | 693 | if (!mMarkerReached && (mMarkerPosition > 0)) { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 694 | if (cblk->user >= mMarkerPosition) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 695 | mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition); |
Jean-Michel Trivi | 78c1314 | 2009-03-24 19:48:58 -0700 | [diff] [blame] | 696 | mMarkerReached = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | |
| 700 | // Manage new position callback |
| 701 | if (mUpdatePeriod > 0) { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 702 | while (cblk->user >= mNewPosition) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 703 | mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition); |
| 704 | mNewPosition += mUpdatePeriod; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | do { |
| 709 | audioBuffer.frameCount = frames; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 710 | // Calling obtainBuffer() with a wait count of 1 |
| 711 | // limits wait time to WAIT_PERIOD_MS. This prevents from being |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 712 | // stuck here not being able to handle timed events (position, markers). |
| 713 | status_t err = obtainBuffer(&audioBuffer, 1); |
| 714 | if (err < NO_ERROR) { |
| 715 | if (err != TIMED_OUT) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 716 | ALOGE_IF(err != status_t(NO_MORE_BUFFERS), "Error obtaining an audio buffer, giving up."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 717 | return false; |
| 718 | } |
| 719 | break; |
| 720 | } |
| 721 | if (err == status_t(STOPPED)) return false; |
| 722 | |
| 723 | size_t reqSize = audioBuffer.size; |
| 724 | mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer); |
| 725 | readSize = audioBuffer.size; |
| 726 | |
| 727 | // Sanity check on returned size |
Eric Laurent | 272beb6 | 2009-03-24 21:23:54 -0700 | [diff] [blame] | 728 | if (ssize_t(readSize) <= 0) { |
| 729 | // The callback is done filling buffers |
| 730 | // Keep this thread going to handle timed events and |
| 731 | // still try to get more data in intervals of WAIT_PERIOD_MS |
| 732 | // but don't just loop and block the CPU, so wait |
| 733 | usleep(WAIT_PERIOD_MS*1000); |
| 734 | break; |
| 735 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 736 | if (readSize > reqSize) readSize = reqSize; |
| 737 | |
| 738 | audioBuffer.size = readSize; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 739 | audioBuffer.frameCount = readSize/frameSize(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 740 | frames -= audioBuffer.frameCount; |
| 741 | |
| 742 | releaseBuffer(&audioBuffer); |
| 743 | |
| 744 | } while (frames); |
| 745 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 746 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 747 | // Manage overrun callback |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 748 | if (mActive && (cblk->framesAvailable() == 0)) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 749 | ALOGV("Overrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags); |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 750 | if (!(android_atomic_or(CBLK_UNDERRUN_ON, &cblk->flags) & CBLK_UNDERRUN_MSK)) { |
Eric Laurent | 913af0b | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 751 | mCbf(EVENT_OVERRUN, mUserData, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 752 | } |
| 753 | } |
| 754 | |
| 755 | if (frames == 0) { |
| 756 | mRemainingFrames = mNotificationFrames; |
| 757 | } else { |
| 758 | mRemainingFrames = frames; |
| 759 | } |
| 760 | return true; |
| 761 | } |
| 762 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 763 | // must be called with mLock and cblk.lock held. Callers must also hold strong references on |
| 764 | // the IAudioRecord and IMemory in case they are recreated here. |
| 765 | // If the IAudioRecord is successfully restored, the cblk pointer is updated |
| 766 | status_t AudioRecord::restoreRecord_l(audio_track_cblk_t*& cblk) |
| 767 | { |
| 768 | status_t result; |
| 769 | |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 770 | if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 771 | ALOGW("dead IAudioRecord, creating a new one"); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 772 | // signal old cblk condition so that other threads waiting for available buffers stop |
| 773 | // waiting now |
| 774 | cblk->cv.broadcast(); |
| 775 | cblk->lock.unlock(); |
| 776 | |
| 777 | // if the new IAudioRecord is created, openRecord_l() will modify the |
| 778 | // following member variables: mAudioRecord, mCblkMemory and mCblk. |
| 779 | // It will also delete the strong references on previous IAudioRecord and IMemory |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 780 | result = openRecord_l(cblk->sampleRate, mFormat, mChannelMask, |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 781 | mFrameCount, mFlags, getInput_l()); |
| 782 | if (result == NO_ERROR) { |
Glenn Kasten | 6a20b26 | 2012-02-02 10:56:47 -0800 | [diff] [blame] | 783 | result = mAudioRecord->start(0); // callback thread hasn't changed |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 784 | } |
| 785 | if (result != NO_ERROR) { |
| 786 | mActive = false; |
| 787 | } |
| 788 | |
| 789 | // signal old cblk condition for other threads waiting for restore completion |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 790 | android_atomic_or(CBLK_RESTORED_ON, &cblk->flags); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 791 | cblk->cv.broadcast(); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 792 | } else { |
| 793 | if (!(cblk->flags & CBLK_RESTORED_MSK)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 794 | ALOGW("dead IAudioRecord, waiting for a new one to be created"); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 795 | mLock.unlock(); |
| 796 | result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS)); |
| 797 | cblk->lock.unlock(); |
| 798 | mLock.lock(); |
| 799 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 800 | ALOGW("dead IAudioRecord, already restored"); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 801 | result = NO_ERROR; |
| 802 | cblk->lock.unlock(); |
| 803 | } |
| 804 | if (result != NO_ERROR || mActive == 0) { |
| 805 | result = status_t(STOPPED); |
| 806 | } |
| 807 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 808 | ALOGV("restoreRecord_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x", |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame^] | 809 | result, mActive, mCblk, cblk, mCblk->flags, cblk->flags); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 810 | |
| 811 | if (result == NO_ERROR) { |
| 812 | // from now on we switch to the newly created cblk |
| 813 | cblk = mCblk; |
| 814 | } |
| 815 | cblk->lock.lock(); |
| 816 | |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 817 | ALOGW_IF(result != NO_ERROR, "restoreRecord_l() error %d", result); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 818 | |
| 819 | return result; |
| 820 | } |
| 821 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 822 | // ========================================================================= |
| 823 | |
| 824 | AudioRecord::ClientRecordThread::ClientRecordThread(AudioRecord& receiver, bool bCanCallJava) |
| 825 | : Thread(bCanCallJava), mReceiver(receiver) |
| 826 | { |
| 827 | } |
| 828 | |
| 829 | bool AudioRecord::ClientRecordThread::threadLoop() |
| 830 | { |
| 831 | return mReceiver.processAudioBuffer(this); |
| 832 | } |
| 833 | |
Glenn Kasten | 6a20b26 | 2012-02-02 10:56:47 -0800 | [diff] [blame] | 834 | status_t AudioRecord::ClientRecordThread::readyToRun() |
| 835 | { |
| 836 | AutoMutex(mReceiver.mLock); |
| 837 | while (mReceiver.mReadyToRun == WOULD_BLOCK) { |
| 838 | mReceiver.mCondition.wait(mReceiver.mLock); |
| 839 | } |
| 840 | return mReceiver.mReadyToRun; |
| 841 | } |
| 842 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 843 | // ------------------------------------------------------------------------- |
| 844 | |
| 845 | }; // namespace android |