Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "AudioSource" |
| 19 | #include <utils/Log.h> |
| 20 | |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 21 | #include <media/AudioRecord.h> |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 22 | #include <media/stagefright/AudioSource.h> |
| 23 | #include <media/stagefright/MediaBuffer.h> |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 24 | #include <media/stagefright/MediaDefs.h> |
| 25 | #include <media/stagefright/MetaData.h> |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 26 | #include <media/stagefright/foundation/ADebug.h> |
James Dong | dae9fd3 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 27 | #include <cutils/properties.h> |
James Dong | c3ae937 | 2010-07-29 18:06:08 -0700 | [diff] [blame] | 28 | #include <stdlib.h> |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 32 | static void AudioRecordCallbackFunction(int event, void *user, void *info) { |
| 33 | AudioSource *source = (AudioSource *) user; |
| 34 | switch (event) { |
| 35 | case AudioRecord::EVENT_MORE_DATA: { |
| 36 | source->dataCallbackTimestamp(*((AudioRecord::Buffer *) info), systemTime() / 1000); |
| 37 | break; |
| 38 | } |
| 39 | case AudioRecord::EVENT_OVERRUN: { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 40 | ALOGW("AudioRecord reported overrun!"); |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 41 | break; |
| 42 | } |
| 43 | default: |
| 44 | // does nothing |
| 45 | break; |
| 46 | } |
| 47 | } |
| 48 | |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 49 | AudioSource::AudioSource( |
Glenn Kasten | 0f0fbd9 | 2012-01-23 13:58:49 -0800 | [diff] [blame] | 50 | audio_source_t inputSource, uint32_t sampleRate, uint32_t channels) |
James Dong | d77d2a9 | 2010-06-14 17:45:35 -0700 | [diff] [blame] | 51 | : mStarted(false), |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 52 | mSampleRate(sampleRate), |
James Dong | c3ae937 | 2010-07-29 18:06:08 -0700 | [diff] [blame] | 53 | mPrevSampleTimeUs(0), |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 54 | mNumFramesReceived(0), |
| 55 | mNumClientOwnedBuffers(0) { |
James Dong | d77d2a9 | 2010-06-14 17:45:35 -0700 | [diff] [blame] | 56 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 57 | ALOGV("sampleRate: %d, channels: %d", sampleRate, channels); |
James Dong | d7f1c3d | 2010-08-26 16:28:17 -0700 | [diff] [blame] | 58 | CHECK(channels == 1 || channels == 2); |
Glenn Kasten | f60a5d7 | 2012-03-07 09:04:02 -0800 | [diff] [blame^] | 59 | AudioRecord::record_flags flags = (AudioRecord::record_flags) |
| 60 | (AudioRecord::RECORD_AGC_ENABLE | |
James Dong | d77d2a9 | 2010-06-14 17:45:35 -0700 | [diff] [blame] | 61 | AudioRecord::RECORD_NS_ENABLE | |
Glenn Kasten | f60a5d7 | 2012-03-07 09:04:02 -0800 | [diff] [blame^] | 62 | AudioRecord::RECORD_IIR_ENABLE); |
James Dong | d77d2a9 | 2010-06-14 17:45:35 -0700 | [diff] [blame] | 63 | mRecord = new AudioRecord( |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 64 | inputSource, sampleRate, AUDIO_FORMAT_PCM_16_BIT, |
| 65 | channels > 1? AUDIO_CHANNEL_IN_STEREO: AUDIO_CHANNEL_IN_MONO, |
James Dong | d77d2a9 | 2010-06-14 17:45:35 -0700 | [diff] [blame] | 66 | 4 * kMaxBufferSize / sizeof(int16_t), /* Enable ping-pong buffers */ |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 67 | flags, |
| 68 | AudioRecordCallbackFunction, |
| 69 | this); |
James Dong | d77d2a9 | 2010-06-14 17:45:35 -0700 | [diff] [blame] | 70 | |
| 71 | mInitCheck = mRecord->initCheck(); |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | AudioSource::~AudioSource() { |
| 75 | if (mStarted) { |
James Dong | 946ab0f | 2012-02-02 18:04:02 -0800 | [diff] [blame] | 76 | reset(); |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | delete mRecord; |
| 80 | mRecord = NULL; |
| 81 | } |
| 82 | |
| 83 | status_t AudioSource::initCheck() const { |
| 84 | return mInitCheck; |
| 85 | } |
| 86 | |
| 87 | status_t AudioSource::start(MetaData *params) { |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 88 | Mutex::Autolock autoLock(mLock); |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 89 | if (mStarted) { |
| 90 | return UNKNOWN_ERROR; |
| 91 | } |
| 92 | |
James Dong | a87544b | 2010-09-01 14:02:51 -0700 | [diff] [blame] | 93 | if (mInitCheck != OK) { |
| 94 | return NO_INIT; |
| 95 | } |
| 96 | |
James Dong | 57e7f83 | 2010-06-24 19:55:31 -0700 | [diff] [blame] | 97 | mTrackMaxAmplitude = false; |
| 98 | mMaxAmplitude = 0; |
James Dong | 4c23815 | 2010-09-01 18:48:35 -0700 | [diff] [blame] | 99 | mInitialReadTimeUs = 0; |
James Dong | 36e573b | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 100 | mStartTimeUs = 0; |
| 101 | int64_t startTimeUs; |
| 102 | if (params && params->findInt64(kKeyTime, &startTimeUs)) { |
| 103 | mStartTimeUs = startTimeUs; |
| 104 | } |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 105 | status_t err = mRecord->start(); |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 106 | if (err == OK) { |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 107 | mStarted = true; |
James Dong | d7ef5b6 | 2011-01-25 12:37:43 -0800 | [diff] [blame] | 108 | } else { |
| 109 | delete mRecord; |
| 110 | mRecord = NULL; |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 111 | } |
| 112 | |
James Dong | d7ef5b6 | 2011-01-25 12:37:43 -0800 | [diff] [blame] | 113 | |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 114 | return err; |
| 115 | } |
| 116 | |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 117 | void AudioSource::releaseQueuedFrames_l() { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 118 | ALOGV("releaseQueuedFrames_l"); |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 119 | List<MediaBuffer *>::iterator it; |
| 120 | while (!mBuffersReceived.empty()) { |
| 121 | it = mBuffersReceived.begin(); |
| 122 | (*it)->release(); |
| 123 | mBuffersReceived.erase(it); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | void AudioSource::waitOutstandingEncodingFrames_l() { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 128 | ALOGV("waitOutstandingEncodingFrames_l: %lld", mNumClientOwnedBuffers); |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 129 | while (mNumClientOwnedBuffers > 0) { |
| 130 | mFrameEncodingCompletionCondition.wait(mLock); |
| 131 | } |
| 132 | } |
| 133 | |
James Dong | 946ab0f | 2012-02-02 18:04:02 -0800 | [diff] [blame] | 134 | status_t AudioSource::reset() { |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 135 | Mutex::Autolock autoLock(mLock); |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 136 | if (!mStarted) { |
| 137 | return UNKNOWN_ERROR; |
| 138 | } |
| 139 | |
James Dong | a87544b | 2010-09-01 14:02:51 -0700 | [diff] [blame] | 140 | if (mInitCheck != OK) { |
| 141 | return NO_INIT; |
| 142 | } |
| 143 | |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 144 | mStarted = false; |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 145 | mRecord->stop(); |
| 146 | waitOutstandingEncodingFrames_l(); |
| 147 | releaseQueuedFrames_l(); |
James Dong | dae9fd3 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 148 | |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 149 | return OK; |
| 150 | } |
| 151 | |
| 152 | sp<MetaData> AudioSource::getFormat() { |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 153 | Mutex::Autolock autoLock(mLock); |
James Dong | a87544b | 2010-09-01 14:02:51 -0700 | [diff] [blame] | 154 | if (mInitCheck != OK) { |
| 155 | return 0; |
| 156 | } |
| 157 | |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 158 | sp<MetaData> meta = new MetaData; |
| 159 | meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW); |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 160 | meta->setInt32(kKeySampleRate, mSampleRate); |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 161 | meta->setInt32(kKeyChannelCount, mRecord->channelCount()); |
| 162 | meta->setInt32(kKeyMaxInputSize, kMaxBufferSize); |
| 163 | |
| 164 | return meta; |
| 165 | } |
| 166 | |
James Dong | d7f1c3d | 2010-08-26 16:28:17 -0700 | [diff] [blame] | 167 | void AudioSource::rampVolume( |
| 168 | int32_t startFrame, int32_t rampDurationFrames, |
| 169 | uint8_t *data, size_t bytes) { |
| 170 | |
| 171 | const int32_t kShift = 14; |
| 172 | int32_t fixedMultiplier = (startFrame << kShift) / rampDurationFrames; |
| 173 | const int32_t nChannels = mRecord->channelCount(); |
| 174 | int32_t stopFrame = startFrame + bytes / sizeof(int16_t); |
| 175 | int16_t *frame = (int16_t *) data; |
| 176 | if (stopFrame > rampDurationFrames) { |
| 177 | stopFrame = rampDurationFrames; |
| 178 | } |
| 179 | |
| 180 | while (startFrame < stopFrame) { |
| 181 | if (nChannels == 1) { // mono |
| 182 | frame[0] = (frame[0] * fixedMultiplier) >> kShift; |
| 183 | ++frame; |
| 184 | ++startFrame; |
| 185 | } else { // stereo |
| 186 | frame[0] = (frame[0] * fixedMultiplier) >> kShift; |
| 187 | frame[1] = (frame[1] * fixedMultiplier) >> kShift; |
| 188 | frame += 2; |
| 189 | startFrame += 2; |
| 190 | } |
| 191 | |
| 192 | // Update the multiplier every 4 frames |
| 193 | if ((startFrame & 3) == 0) { |
| 194 | fixedMultiplier = (startFrame << kShift) / rampDurationFrames; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 199 | status_t AudioSource::read( |
| 200 | MediaBuffer **out, const ReadOptions *options) { |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 201 | Mutex::Autolock autoLock(mLock); |
| 202 | *out = NULL; |
James Dong | a87544b | 2010-09-01 14:02:51 -0700 | [diff] [blame] | 203 | |
| 204 | if (mInitCheck != OK) { |
| 205 | return NO_INIT; |
| 206 | } |
| 207 | |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 208 | while (mStarted && mBuffersReceived.empty()) { |
| 209 | mFrameAvailableCondition.wait(mLock); |
| 210 | } |
| 211 | if (!mStarted) { |
James Dong | 53d4e0d | 2010-07-21 14:51:35 -0700 | [diff] [blame] | 212 | return OK; |
James Dong | dae9fd3 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 213 | } |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 214 | MediaBuffer *buffer = *mBuffersReceived.begin(); |
| 215 | mBuffersReceived.erase(mBuffersReceived.begin()); |
| 216 | ++mNumClientOwnedBuffers; |
| 217 | buffer->setObserver(this); |
| 218 | buffer->add_ref(); |
| 219 | |
| 220 | // Mute/suppress the recording sound |
| 221 | int64_t timeUs; |
| 222 | CHECK(buffer->meta_data()->findInt64(kKeyTime, &timeUs)); |
| 223 | int64_t elapsedTimeUs = timeUs - mStartTimeUs; |
| 224 | if (elapsedTimeUs < kAutoRampStartUs) { |
| 225 | memset((uint8_t *) buffer->data(), 0, buffer->range_length()); |
| 226 | } else if (elapsedTimeUs < kAutoRampStartUs + kAutoRampDurationUs) { |
| 227 | int32_t autoRampDurationFrames = |
| 228 | (kAutoRampDurationUs * mSampleRate + 500000LL) / 1000000LL; |
| 229 | |
| 230 | int32_t autoRampStartFrames = |
| 231 | (kAutoRampStartUs * mSampleRate + 500000LL) / 1000000LL; |
| 232 | |
| 233 | int32_t nFrames = mNumFramesReceived - autoRampStartFrames; |
| 234 | rampVolume(nFrames, autoRampDurationFrames, |
| 235 | (uint8_t *) buffer->data(), buffer->range_length()); |
| 236 | } |
| 237 | |
| 238 | // Track the max recording signal amplitude. |
| 239 | if (mTrackMaxAmplitude) { |
| 240 | trackMaxAmplitude( |
| 241 | (int16_t *) buffer->data(), buffer->range_length() >> 1); |
| 242 | } |
| 243 | |
| 244 | *out = buffer; |
| 245 | return OK; |
| 246 | } |
| 247 | |
| 248 | void AudioSource::signalBufferReturned(MediaBuffer *buffer) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 249 | ALOGV("signalBufferReturned: %p", buffer->data()); |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 250 | Mutex::Autolock autoLock(mLock); |
| 251 | --mNumClientOwnedBuffers; |
| 252 | buffer->setObserver(0); |
| 253 | buffer->release(); |
| 254 | mFrameEncodingCompletionCondition.signal(); |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | status_t AudioSource::dataCallbackTimestamp( |
| 259 | const AudioRecord::Buffer& audioBuffer, int64_t timeUs) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 260 | ALOGV("dataCallbackTimestamp: %lld us", timeUs); |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 261 | Mutex::Autolock autoLock(mLock); |
| 262 | if (!mStarted) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 263 | ALOGW("Spurious callback from AudioRecord. Drop the audio data."); |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 264 | return OK; |
| 265 | } |
| 266 | |
James Dong | cbeebb1 | 2011-02-16 12:28:26 -0800 | [diff] [blame] | 267 | // Drop retrieved and previously lost audio data. |
| 268 | if (mNumFramesReceived == 0 && timeUs < mStartTimeUs) { |
| 269 | mRecord->getInputFramesLost(); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 270 | ALOGV("Drop audio data at %lld/%lld us", timeUs, mStartTimeUs); |
James Dong | cbeebb1 | 2011-02-16 12:28:26 -0800 | [diff] [blame] | 271 | return OK; |
| 272 | } |
| 273 | |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 274 | if (mNumFramesReceived == 0 && mPrevSampleTimeUs == 0) { |
| 275 | mInitialReadTimeUs = timeUs; |
| 276 | // Initial delay |
| 277 | if (mStartTimeUs > 0) { |
| 278 | mStartTimeUs = timeUs - mStartTimeUs; |
| 279 | } else { |
| 280 | // Assume latency is constant. |
| 281 | mStartTimeUs += mRecord->latency() * 1000; |
| 282 | } |
| 283 | mPrevSampleTimeUs = mStartTimeUs; |
| 284 | } |
| 285 | |
James Dong | cbeebb1 | 2011-02-16 12:28:26 -0800 | [diff] [blame] | 286 | size_t numLostBytes = 0; |
| 287 | if (mNumFramesReceived > 0) { // Ignore earlier frame lost |
| 288 | // getInputFramesLost() returns the number of lost frames. |
| 289 | // Convert number of frames lost to number of bytes lost. |
| 290 | numLostBytes = mRecord->getInputFramesLost() * mRecord->frameSize(); |
| 291 | } |
| 292 | |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 293 | CHECK_EQ(numLostBytes & 1, 0u); |
| 294 | CHECK_EQ(audioBuffer.size & 1, 0u); |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 295 | if (numLostBytes > 0) { |
James Dong | 745fcc0 | 2012-02-14 14:58:20 -0800 | [diff] [blame] | 296 | // Loss of audio frames should happen rarely; thus the LOGW should |
| 297 | // not cause a logging spam |
| 298 | ALOGW("Lost audio record data: %d bytes", numLostBytes); |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 299 | } |
| 300 | |
James Dong | 745fcc0 | 2012-02-14 14:58:20 -0800 | [diff] [blame] | 301 | while (numLostBytes > 0) { |
| 302 | size_t bufferSize = numLostBytes; |
| 303 | if (numLostBytes > kMaxBufferSize) { |
| 304 | numLostBytes -= kMaxBufferSize; |
| 305 | bufferSize = kMaxBufferSize; |
| 306 | } else { |
| 307 | numLostBytes = 0; |
| 308 | } |
| 309 | MediaBuffer *lostAudioBuffer = new MediaBuffer(bufferSize); |
| 310 | memset(lostAudioBuffer->data(), 0, bufferSize); |
| 311 | lostAudioBuffer->set_range(0, bufferSize); |
| 312 | queueInputBuffer_l(lostAudioBuffer, timeUs); |
| 313 | } |
| 314 | |
| 315 | if (audioBuffer.size == 0) { |
| 316 | ALOGW("Nothing is available from AudioRecord callback buffer"); |
| 317 | return OK; |
| 318 | } |
| 319 | |
| 320 | const size_t bufferSize = audioBuffer.size; |
| 321 | MediaBuffer *buffer = new MediaBuffer(bufferSize); |
| 322 | memcpy((uint8_t *) buffer->data(), |
| 323 | audioBuffer.i16, audioBuffer.size); |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 324 | buffer->set_range(0, bufferSize); |
James Dong | 745fcc0 | 2012-02-14 14:58:20 -0800 | [diff] [blame] | 325 | queueInputBuffer_l(buffer, timeUs); |
| 326 | return OK; |
| 327 | } |
| 328 | |
| 329 | void AudioSource::queueInputBuffer_l(MediaBuffer *buffer, int64_t timeUs) { |
| 330 | const size_t bufferSize = buffer->range_length(); |
| 331 | const size_t frameSize = mRecord->frameSize(); |
| 332 | const int64_t timestampUs = |
| 333 | mPrevSampleTimeUs + |
| 334 | ((1000000LL * (bufferSize / frameSize)) + |
| 335 | (mSampleRate >> 1)) / mSampleRate; |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 336 | |
| 337 | if (mNumFramesReceived == 0) { |
| 338 | buffer->meta_data()->setInt64(kKeyAnchorTime, mStartTimeUs); |
| 339 | } |
James Dong | 745fcc0 | 2012-02-14 14:58:20 -0800 | [diff] [blame] | 340 | |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 341 | buffer->meta_data()->setInt64(kKeyTime, mPrevSampleTimeUs); |
| 342 | buffer->meta_data()->setInt64(kKeyDriftTime, timeUs - mInitialReadTimeUs); |
| 343 | mPrevSampleTimeUs = timestampUs; |
James Dong | 745fcc0 | 2012-02-14 14:58:20 -0800 | [diff] [blame] | 344 | mNumFramesReceived += bufferSize / frameSize; |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 345 | mBuffersReceived.push_back(buffer); |
| 346 | mFrameAvailableCondition.signal(); |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 347 | } |
| 348 | |
James Dong | 57e7f83 | 2010-06-24 19:55:31 -0700 | [diff] [blame] | 349 | void AudioSource::trackMaxAmplitude(int16_t *data, int nSamples) { |
| 350 | for (int i = nSamples; i > 0; --i) { |
| 351 | int16_t value = *data++; |
| 352 | if (value < 0) { |
| 353 | value = -value; |
| 354 | } |
| 355 | if (mMaxAmplitude < value) { |
| 356 | mMaxAmplitude = value; |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | int16_t AudioSource::getMaxAmplitude() { |
| 362 | // First call activates the tracking. |
| 363 | if (!mTrackMaxAmplitude) { |
| 364 | mTrackMaxAmplitude = true; |
| 365 | } |
| 366 | int16_t value = mMaxAmplitude; |
| 367 | mMaxAmplitude = 0; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 368 | ALOGV("max amplitude since last call: %d", value); |
James Dong | 57e7f83 | 2010-06-24 19:55:31 -0700 | [diff] [blame] | 369 | return value; |
| 370 | } |
| 371 | |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 372 | } // namespace android |