Andreas Huber | e7c9cb4 | 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 | 050b28a | 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 | e7c9cb4 | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 21 | #include <media/AudioRecord.h> |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 22 | #include <media/stagefright/AudioSource.h> |
| 23 | #include <media/stagefright/MediaBuffer.h> |
Andreas Huber | e7c9cb4 | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 24 | #include <media/stagefright/MediaDefs.h> |
| 25 | #include <media/stagefright/MetaData.h> |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 26 | #include <media/stagefright/foundation/ADebug.h> |
Andreas Huber | bd381af | 2012-08-30 10:56:14 -0700 | [diff] [blame] | 27 | #include <media/stagefright/foundation/ALooper.h> |
James Dong | 365a963 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 28 | #include <cutils/properties.h> |
James Dong | 46292fb | 2010-07-29 18:06:08 -0700 | [diff] [blame] | 29 | #include <stdlib.h> |
Andreas Huber | e7c9cb4 | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 33 | static void AudioRecordCallbackFunction(int event, void *user, void *info) { |
| 34 | AudioSource *source = (AudioSource *) user; |
| 35 | switch (event) { |
| 36 | case AudioRecord::EVENT_MORE_DATA: { |
Andreas Huber | bd381af | 2012-08-30 10:56:14 -0700 | [diff] [blame] | 37 | source->dataCallback(*((AudioRecord::Buffer *) info)); |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 38 | break; |
| 39 | } |
| 40 | case AudioRecord::EVENT_OVERRUN: { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 41 | ALOGW("AudioRecord reported overrun!"); |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 42 | break; |
| 43 | } |
| 44 | default: |
| 45 | // does nothing |
| 46 | break; |
| 47 | } |
| 48 | } |
| 49 | |
Andreas Huber | e7c9cb4 | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 50 | AudioSource::AudioSource( |
Glenn Kasten | ab334fd | 2012-03-14 12:56:06 -0700 | [diff] [blame] | 51 | audio_source_t inputSource, uint32_t sampleRate, uint32_t channelCount) |
James Dong | a010869 | 2012-06-29 15:14:20 -0700 | [diff] [blame] | 52 | : mRecord(NULL), |
| 53 | mStarted(false), |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 54 | mSampleRate(sampleRate), |
James Dong | 46292fb | 2010-07-29 18:06:08 -0700 | [diff] [blame] | 55 | mPrevSampleTimeUs(0), |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 56 | mNumFramesReceived(0), |
Andreas Huber | fa0e033 | 2012-10-01 16:03:44 -0700 | [diff] [blame] | 57 | mNumClientOwnedBuffers(0) { |
Glenn Kasten | ab334fd | 2012-03-14 12:56:06 -0700 | [diff] [blame] | 58 | ALOGV("sampleRate: %d, channelCount: %d", sampleRate, channelCount); |
| 59 | CHECK(channelCount == 1 || channelCount == 2); |
James Dong | be6ec71 | 2010-06-14 17:45:35 -0700 | [diff] [blame] | 60 | |
Glenn Kasten | 7da35f2 | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 61 | size_t minFrameCount; |
Eric Laurent | e49f2b4 | 2012-06-12 17:09:30 -0700 | [diff] [blame] | 62 | status_t status = AudioRecord::getMinFrameCount(&minFrameCount, |
| 63 | sampleRate, |
| 64 | AUDIO_FORMAT_PCM_16_BIT, |
Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 65 | audio_channel_in_mask_from_count(channelCount)); |
Eric Laurent | e49f2b4 | 2012-06-12 17:09:30 -0700 | [diff] [blame] | 66 | if (status == OK) { |
| 67 | // make sure that the AudioRecord callback never returns more than the maximum |
| 68 | // buffer size |
| 69 | int frameCount = kMaxBufferSize / sizeof(int16_t) / channelCount; |
| 70 | |
| 71 | // make sure that the AudioRecord total buffer size is large enough |
| 72 | int bufCount = 2; |
| 73 | while ((bufCount * frameCount) < minFrameCount) { |
| 74 | bufCount++; |
| 75 | } |
| 76 | |
Eric Laurent | e49f2b4 | 2012-06-12 17:09:30 -0700 | [diff] [blame] | 77 | mRecord = new AudioRecord( |
| 78 | inputSource, sampleRate, AUDIO_FORMAT_PCM_16_BIT, |
| 79 | audio_channel_in_mask_from_count(channelCount), |
| 80 | bufCount * frameCount, |
Eric Laurent | e49f2b4 | 2012-06-12 17:09:30 -0700 | [diff] [blame] | 81 | AudioRecordCallbackFunction, |
| 82 | this, |
| 83 | frameCount); |
| 84 | mInitCheck = mRecord->initCheck(); |
| 85 | } else { |
| 86 | mInitCheck = status; |
| 87 | } |
Andreas Huber | e7c9cb4 | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | AudioSource::~AudioSource() { |
| 91 | if (mStarted) { |
James Dong | b44c9d2 | 2012-02-02 18:04:02 -0800 | [diff] [blame] | 92 | reset(); |
Andreas Huber | e7c9cb4 | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | delete mRecord; |
| 96 | mRecord = NULL; |
| 97 | } |
| 98 | |
| 99 | status_t AudioSource::initCheck() const { |
| 100 | return mInitCheck; |
| 101 | } |
| 102 | |
| 103 | status_t AudioSource::start(MetaData *params) { |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 104 | Mutex::Autolock autoLock(mLock); |
Andreas Huber | e7c9cb4 | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 105 | if (mStarted) { |
| 106 | return UNKNOWN_ERROR; |
| 107 | } |
| 108 | |
James Dong | 6e20bdf | 2010-09-01 14:02:51 -0700 | [diff] [blame] | 109 | if (mInitCheck != OK) { |
| 110 | return NO_INIT; |
| 111 | } |
| 112 | |
James Dong | d3d4e50 | 2010-06-24 19:55:31 -0700 | [diff] [blame] | 113 | mTrackMaxAmplitude = false; |
| 114 | mMaxAmplitude = 0; |
James Dong | d707fcb | 2010-09-01 18:48:35 -0700 | [diff] [blame] | 115 | mInitialReadTimeUs = 0; |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 116 | mStartTimeUs = 0; |
| 117 | int64_t startTimeUs; |
| 118 | if (params && params->findInt64(kKeyTime, &startTimeUs)) { |
| 119 | mStartTimeUs = startTimeUs; |
| 120 | } |
Andreas Huber | e7c9cb4 | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 121 | status_t err = mRecord->start(); |
Andreas Huber | e7c9cb4 | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 122 | if (err == OK) { |
Andreas Huber | e7c9cb4 | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 123 | mStarted = true; |
James Dong | eaae384 | 2011-01-25 12:37:43 -0800 | [diff] [blame] | 124 | } else { |
| 125 | delete mRecord; |
| 126 | mRecord = NULL; |
Andreas Huber | e7c9cb4 | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 127 | } |
| 128 | |
James Dong | eaae384 | 2011-01-25 12:37:43 -0800 | [diff] [blame] | 129 | |
Andreas Huber | e7c9cb4 | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 130 | return err; |
| 131 | } |
| 132 | |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 133 | void AudioSource::releaseQueuedFrames_l() { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 134 | ALOGV("releaseQueuedFrames_l"); |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 135 | List<MediaBuffer *>::iterator it; |
| 136 | while (!mBuffersReceived.empty()) { |
| 137 | it = mBuffersReceived.begin(); |
| 138 | (*it)->release(); |
| 139 | mBuffersReceived.erase(it); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void AudioSource::waitOutstandingEncodingFrames_l() { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 144 | ALOGV("waitOutstandingEncodingFrames_l: %lld", mNumClientOwnedBuffers); |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 145 | while (mNumClientOwnedBuffers > 0) { |
| 146 | mFrameEncodingCompletionCondition.wait(mLock); |
| 147 | } |
| 148 | } |
| 149 | |
James Dong | b44c9d2 | 2012-02-02 18:04:02 -0800 | [diff] [blame] | 150 | status_t AudioSource::reset() { |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 151 | Mutex::Autolock autoLock(mLock); |
Andreas Huber | e7c9cb4 | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 152 | if (!mStarted) { |
| 153 | return UNKNOWN_ERROR; |
| 154 | } |
| 155 | |
James Dong | 6e20bdf | 2010-09-01 14:02:51 -0700 | [diff] [blame] | 156 | if (mInitCheck != OK) { |
| 157 | return NO_INIT; |
| 158 | } |
| 159 | |
Andreas Huber | e7c9cb4 | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 160 | mStarted = false; |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 161 | mRecord->stop(); |
| 162 | waitOutstandingEncodingFrames_l(); |
| 163 | releaseQueuedFrames_l(); |
James Dong | 365a963 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 164 | |
Andreas Huber | e7c9cb4 | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 165 | return OK; |
| 166 | } |
| 167 | |
| 168 | sp<MetaData> AudioSource::getFormat() { |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 169 | Mutex::Autolock autoLock(mLock); |
James Dong | 6e20bdf | 2010-09-01 14:02:51 -0700 | [diff] [blame] | 170 | if (mInitCheck != OK) { |
| 171 | return 0; |
| 172 | } |
| 173 | |
Andreas Huber | e7c9cb4 | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 174 | sp<MetaData> meta = new MetaData; |
| 175 | meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW); |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 176 | meta->setInt32(kKeySampleRate, mSampleRate); |
Andreas Huber | e7c9cb4 | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 177 | meta->setInt32(kKeyChannelCount, mRecord->channelCount()); |
| 178 | meta->setInt32(kKeyMaxInputSize, kMaxBufferSize); |
| 179 | |
| 180 | return meta; |
| 181 | } |
| 182 | |
James Dong | f1ae196 | 2010-08-26 16:28:17 -0700 | [diff] [blame] | 183 | void AudioSource::rampVolume( |
| 184 | int32_t startFrame, int32_t rampDurationFrames, |
| 185 | uint8_t *data, size_t bytes) { |
| 186 | |
| 187 | const int32_t kShift = 14; |
| 188 | int32_t fixedMultiplier = (startFrame << kShift) / rampDurationFrames; |
| 189 | const int32_t nChannels = mRecord->channelCount(); |
| 190 | int32_t stopFrame = startFrame + bytes / sizeof(int16_t); |
| 191 | int16_t *frame = (int16_t *) data; |
| 192 | if (stopFrame > rampDurationFrames) { |
| 193 | stopFrame = rampDurationFrames; |
| 194 | } |
| 195 | |
| 196 | while (startFrame < stopFrame) { |
| 197 | if (nChannels == 1) { // mono |
| 198 | frame[0] = (frame[0] * fixedMultiplier) >> kShift; |
| 199 | ++frame; |
| 200 | ++startFrame; |
| 201 | } else { // stereo |
| 202 | frame[0] = (frame[0] * fixedMultiplier) >> kShift; |
| 203 | frame[1] = (frame[1] * fixedMultiplier) >> kShift; |
| 204 | frame += 2; |
| 205 | startFrame += 2; |
| 206 | } |
| 207 | |
| 208 | // Update the multiplier every 4 frames |
| 209 | if ((startFrame & 3) == 0) { |
| 210 | fixedMultiplier = (startFrame << kShift) / rampDurationFrames; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
Andreas Huber | e7c9cb4 | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 215 | status_t AudioSource::read( |
| 216 | MediaBuffer **out, const ReadOptions *options) { |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 217 | Mutex::Autolock autoLock(mLock); |
| 218 | *out = NULL; |
James Dong | 6e20bdf | 2010-09-01 14:02:51 -0700 | [diff] [blame] | 219 | |
| 220 | if (mInitCheck != OK) { |
| 221 | return NO_INIT; |
| 222 | } |
| 223 | |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 224 | while (mStarted && mBuffersReceived.empty()) { |
| 225 | mFrameAvailableCondition.wait(mLock); |
| 226 | } |
| 227 | if (!mStarted) { |
James Dong | 542db5d | 2010-07-21 14:51:35 -0700 | [diff] [blame] | 228 | return OK; |
James Dong | 365a963 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 229 | } |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 230 | MediaBuffer *buffer = *mBuffersReceived.begin(); |
| 231 | mBuffersReceived.erase(mBuffersReceived.begin()); |
| 232 | ++mNumClientOwnedBuffers; |
| 233 | buffer->setObserver(this); |
| 234 | buffer->add_ref(); |
| 235 | |
| 236 | // Mute/suppress the recording sound |
| 237 | int64_t timeUs; |
| 238 | CHECK(buffer->meta_data()->findInt64(kKeyTime, &timeUs)); |
| 239 | int64_t elapsedTimeUs = timeUs - mStartTimeUs; |
| 240 | if (elapsedTimeUs < kAutoRampStartUs) { |
| 241 | memset((uint8_t *) buffer->data(), 0, buffer->range_length()); |
| 242 | } else if (elapsedTimeUs < kAutoRampStartUs + kAutoRampDurationUs) { |
| 243 | int32_t autoRampDurationFrames = |
| 244 | (kAutoRampDurationUs * mSampleRate + 500000LL) / 1000000LL; |
| 245 | |
| 246 | int32_t autoRampStartFrames = |
| 247 | (kAutoRampStartUs * mSampleRate + 500000LL) / 1000000LL; |
| 248 | |
| 249 | int32_t nFrames = mNumFramesReceived - autoRampStartFrames; |
| 250 | rampVolume(nFrames, autoRampDurationFrames, |
| 251 | (uint8_t *) buffer->data(), buffer->range_length()); |
| 252 | } |
| 253 | |
| 254 | // Track the max recording signal amplitude. |
| 255 | if (mTrackMaxAmplitude) { |
| 256 | trackMaxAmplitude( |
| 257 | (int16_t *) buffer->data(), buffer->range_length() >> 1); |
| 258 | } |
| 259 | |
| 260 | *out = buffer; |
| 261 | return OK; |
| 262 | } |
| 263 | |
| 264 | void AudioSource::signalBufferReturned(MediaBuffer *buffer) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 265 | ALOGV("signalBufferReturned: %p", buffer->data()); |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 266 | Mutex::Autolock autoLock(mLock); |
| 267 | --mNumClientOwnedBuffers; |
| 268 | buffer->setObserver(0); |
| 269 | buffer->release(); |
| 270 | mFrameEncodingCompletionCondition.signal(); |
| 271 | return; |
| 272 | } |
| 273 | |
Andreas Huber | bd381af | 2012-08-30 10:56:14 -0700 | [diff] [blame] | 274 | status_t AudioSource::dataCallback(const AudioRecord::Buffer& audioBuffer) { |
Andreas Huber | fa0e033 | 2012-10-01 16:03:44 -0700 | [diff] [blame] | 275 | int64_t timeUs = systemTime() / 1000ll; |
Andreas Huber | bd381af | 2012-08-30 10:56:14 -0700 | [diff] [blame] | 276 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 277 | ALOGV("dataCallbackTimestamp: %lld us", timeUs); |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 278 | Mutex::Autolock autoLock(mLock); |
| 279 | if (!mStarted) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 280 | ALOGW("Spurious callback from AudioRecord. Drop the audio data."); |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 281 | return OK; |
| 282 | } |
| 283 | |
James Dong | a472613 | 2011-02-16 12:28:26 -0800 | [diff] [blame] | 284 | // Drop retrieved and previously lost audio data. |
| 285 | if (mNumFramesReceived == 0 && timeUs < mStartTimeUs) { |
| 286 | mRecord->getInputFramesLost(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 287 | ALOGV("Drop audio data at %lld/%lld us", timeUs, mStartTimeUs); |
James Dong | a472613 | 2011-02-16 12:28:26 -0800 | [diff] [blame] | 288 | return OK; |
| 289 | } |
| 290 | |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 291 | if (mNumFramesReceived == 0 && mPrevSampleTimeUs == 0) { |
| 292 | mInitialReadTimeUs = timeUs; |
| 293 | // Initial delay |
Andreas Huber | fa0e033 | 2012-10-01 16:03:44 -0700 | [diff] [blame] | 294 | if (mStartTimeUs > 0) { |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 295 | mStartTimeUs = timeUs - mStartTimeUs; |
| 296 | } else { |
| 297 | // Assume latency is constant. |
| 298 | mStartTimeUs += mRecord->latency() * 1000; |
| 299 | } |
Andreas Huber | bd381af | 2012-08-30 10:56:14 -0700 | [diff] [blame] | 300 | |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 301 | mPrevSampleTimeUs = mStartTimeUs; |
| 302 | } |
| 303 | |
James Dong | a472613 | 2011-02-16 12:28:26 -0800 | [diff] [blame] | 304 | size_t numLostBytes = 0; |
| 305 | if (mNumFramesReceived > 0) { // Ignore earlier frame lost |
| 306 | // getInputFramesLost() returns the number of lost frames. |
| 307 | // Convert number of frames lost to number of bytes lost. |
| 308 | numLostBytes = mRecord->getInputFramesLost() * mRecord->frameSize(); |
| 309 | } |
| 310 | |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 311 | CHECK_EQ(numLostBytes & 1, 0u); |
| 312 | CHECK_EQ(audioBuffer.size & 1, 0u); |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 313 | if (numLostBytes > 0) { |
James Dong | b575ddc | 2012-02-14 14:58:20 -0800 | [diff] [blame] | 314 | // Loss of audio frames should happen rarely; thus the LOGW should |
| 315 | // not cause a logging spam |
| 316 | ALOGW("Lost audio record data: %d bytes", numLostBytes); |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 317 | } |
| 318 | |
James Dong | b575ddc | 2012-02-14 14:58:20 -0800 | [diff] [blame] | 319 | while (numLostBytes > 0) { |
| 320 | size_t bufferSize = numLostBytes; |
| 321 | if (numLostBytes > kMaxBufferSize) { |
| 322 | numLostBytes -= kMaxBufferSize; |
| 323 | bufferSize = kMaxBufferSize; |
| 324 | } else { |
| 325 | numLostBytes = 0; |
| 326 | } |
| 327 | MediaBuffer *lostAudioBuffer = new MediaBuffer(bufferSize); |
| 328 | memset(lostAudioBuffer->data(), 0, bufferSize); |
| 329 | lostAudioBuffer->set_range(0, bufferSize); |
| 330 | queueInputBuffer_l(lostAudioBuffer, timeUs); |
| 331 | } |
| 332 | |
| 333 | if (audioBuffer.size == 0) { |
| 334 | ALOGW("Nothing is available from AudioRecord callback buffer"); |
| 335 | return OK; |
| 336 | } |
| 337 | |
| 338 | const size_t bufferSize = audioBuffer.size; |
| 339 | MediaBuffer *buffer = new MediaBuffer(bufferSize); |
| 340 | memcpy((uint8_t *) buffer->data(), |
| 341 | audioBuffer.i16, audioBuffer.size); |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 342 | buffer->set_range(0, bufferSize); |
James Dong | b575ddc | 2012-02-14 14:58:20 -0800 | [diff] [blame] | 343 | queueInputBuffer_l(buffer, timeUs); |
| 344 | return OK; |
| 345 | } |
| 346 | |
| 347 | void AudioSource::queueInputBuffer_l(MediaBuffer *buffer, int64_t timeUs) { |
| 348 | const size_t bufferSize = buffer->range_length(); |
| 349 | const size_t frameSize = mRecord->frameSize(); |
| 350 | const int64_t timestampUs = |
| 351 | mPrevSampleTimeUs + |
| 352 | ((1000000LL * (bufferSize / frameSize)) + |
| 353 | (mSampleRate >> 1)) / mSampleRate; |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 354 | |
| 355 | if (mNumFramesReceived == 0) { |
| 356 | buffer->meta_data()->setInt64(kKeyAnchorTime, mStartTimeUs); |
| 357 | } |
James Dong | b575ddc | 2012-02-14 14:58:20 -0800 | [diff] [blame] | 358 | |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 359 | buffer->meta_data()->setInt64(kKeyTime, mPrevSampleTimeUs); |
| 360 | buffer->meta_data()->setInt64(kKeyDriftTime, timeUs - mInitialReadTimeUs); |
| 361 | mPrevSampleTimeUs = timestampUs; |
James Dong | b575ddc | 2012-02-14 14:58:20 -0800 | [diff] [blame] | 362 | mNumFramesReceived += bufferSize / frameSize; |
James Dong | 6b61f43 | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 363 | mBuffersReceived.push_back(buffer); |
| 364 | mFrameAvailableCondition.signal(); |
Andreas Huber | e7c9cb4 | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 365 | } |
| 366 | |
James Dong | d3d4e50 | 2010-06-24 19:55:31 -0700 | [diff] [blame] | 367 | void AudioSource::trackMaxAmplitude(int16_t *data, int nSamples) { |
| 368 | for (int i = nSamples; i > 0; --i) { |
| 369 | int16_t value = *data++; |
| 370 | if (value < 0) { |
| 371 | value = -value; |
| 372 | } |
| 373 | if (mMaxAmplitude < value) { |
| 374 | mMaxAmplitude = value; |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | int16_t AudioSource::getMaxAmplitude() { |
| 380 | // First call activates the tracking. |
| 381 | if (!mTrackMaxAmplitude) { |
| 382 | mTrackMaxAmplitude = true; |
| 383 | } |
| 384 | int16_t value = mMaxAmplitude; |
| 385 | mMaxAmplitude = 0; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 386 | ALOGV("max amplitude since last call: %d", value); |
James Dong | d3d4e50 | 2010-06-24 19:55:31 -0700 | [diff] [blame] | 387 | return value; |
| 388 | } |
| 389 | |
Andreas Huber | e7c9cb4 | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 390 | } // namespace android |