Dharmaray Kundargi | 643290d | 2011-01-16 16:02:42 -0800 | [diff] [blame] | 1 | /*
|
| 2 | * Copyright (C) 2011 NXP Software
|
| 3 | * Copyright (C) 2011 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 1
|
| 19 | #define LOG_TAG "VEAudioSource"
|
| 20 | #include <utils/Log.h>
|
| 21 |
|
| 22 |
|
| 23 | #include "VideoEditorSRC.h"
|
| 24 | #include <media/stagefright/MetaData.h>
|
| 25 | #include <media/stagefright/MediaDebug.h>
|
| 26 | #include "AudioMixer.h"
|
| 27 |
|
| 28 |
|
| 29 | namespace android {
|
| 30 |
|
| 31 | VideoEditorSRC::VideoEditorSRC(
|
| 32 | const sp<MediaSource> &source) {
|
| 33 |
|
| 34 | LOGV("VideoEditorSRC::Create");
|
| 35 | mSource = source;
|
| 36 | mResampler = NULL;
|
| 37 | mBitDepth = 16;
|
| 38 | mChannelCnt = 0;
|
| 39 | mSampleRate = 0;
|
| 40 | mOutputSampleRate = DEFAULT_SAMPLING_FREQ;
|
| 41 | mStarted = false;
|
| 42 | mIsResamplingRequired = false;
|
| 43 | mIsChannelConvertionRequired = false;
|
| 44 | mInitialTimeStampUs = -1;
|
| 45 | mAccuOutBufferSize = 0;
|
| 46 | mSeekTimeUs = -1;
|
| 47 | mLeftover = 0;
|
| 48 | mLastReadSize = 0;
|
| 49 | #ifndef FROYO
|
| 50 | mSeekMode = ReadOptions::SEEK_PREVIOUS_SYNC;
|
| 51 | #endif
|
| 52 |
|
| 53 | mOutputFormat = new MetaData;
|
| 54 |
|
| 55 | // Input Source validation
|
| 56 | sp<MetaData> format = mSource->getFormat();
|
| 57 | const char *mime;
|
| 58 | bool success = format->findCString(kKeyMIMEType, &mime);
|
| 59 | CHECK(success);
|
| 60 | CHECK(!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_RAW));
|
| 61 |
|
| 62 | //set the meta data of the output after convertion.
|
| 63 | if(mOutputFormat != NULL) {
|
| 64 | mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
|
| 65 | mOutputFormat->setInt32(kKeySampleRate, DEFAULT_SAMPLING_FREQ);
|
| 66 |
|
| 67 | //by default we convert all data to stereo
|
| 68 | mOutputFormat->setInt32(kKeyChannelCount, 2);
|
| 69 | } else {
|
| 70 | LOGE("Meta data was not allocated.");
|
| 71 | }
|
| 72 |
|
| 73 | // Allocate a 1 sec buffer (test only, to be refined)
|
| 74 | mInterframeBufferPosition = 0;
|
| 75 | pInterframeBuffer = new uint8_t[DEFAULT_SAMPLING_FREQ * 2 * 2]; //stereo=2 * bytespersample=2
|
| 76 |
|
| 77 |
|
| 78 | }
|
| 79 |
|
| 80 | VideoEditorSRC::~VideoEditorSRC(){
|
| 81 | if (mStarted == true)
|
| 82 | stop();
|
| 83 |
|
| 84 | if(mOutputFormat != NULL) {
|
| 85 | mOutputFormat.clear();
|
| 86 | mOutputFormat = NULL;
|
| 87 | }
|
| 88 |
|
| 89 | if (pInterframeBuffer != NULL){
|
| 90 | delete pInterframeBuffer;
|
| 91 | pInterframeBuffer = NULL;
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | void VideoEditorSRC::setResampling(int32_t sampleRate) {
|
| 96 | Mutex::Autolock autoLock(mLock);
|
| 97 | LOGV("VideoEditorSRC::setResampling called with samplreRate = %d", sampleRate);
|
| 98 | if(sampleRate != DEFAULT_SAMPLING_FREQ) { //default case
|
| 99 | LOGV("VideoEditor Audio resampler, freq set is other than default");
|
| 100 | CHECK(mOutputFormat->setInt32(kKeySampleRate, DEFAULT_SAMPLING_FREQ));
|
| 101 | }
|
| 102 | mOutputSampleRate = sampleRate;
|
| 103 | return;
|
| 104 | }
|
| 105 |
|
Dharmaray Kundargi | 643290d | 2011-01-16 16:02:42 -0800 | [diff] [blame] | 106 | status_t VideoEditorSRC::start (MetaData *params) {
|
| 107 | Mutex::Autolock autoLock(mLock);
|
| 108 |
|
| 109 | CHECK(!mStarted);
|
| 110 | LOGV(" VideoEditorSRC:start() called");
|
| 111 |
|
| 112 | sp<MetaData> format = mSource->getFormat();
|
| 113 | const char *mime;
|
| 114 | bool success = format->findCString(kKeyMIMEType, &mime);
|
| 115 | CHECK(success);
|
| 116 | CHECK(!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_RAW));
|
| 117 |
|
| 118 | success = format->findInt32(kKeySampleRate, &mSampleRate);
|
| 119 | CHECK(success);
|
| 120 |
|
| 121 | int32_t numChannels;
|
| 122 | success = format->findInt32(kKeyChannelCount, &mChannelCnt);
|
| 123 | CHECK(success);
|
| 124 |
|
| 125 | mInputFrameSize = mChannelCnt * 2; //2 is the byte depth
|
| 126 | if(mSampleRate != mOutputSampleRate) {
|
| 127 | LOGV("Resampling required (%d != %d)", mSampleRate, mOutputSampleRate);
|
| 128 | mIsResamplingRequired = true;
|
| 129 | LOGV("Create resampler %d %d %d", mBitDepth, mChannelCnt, mOutputSampleRate);
|
| 130 |
|
| 131 | mResampler = AudioResampler::create(
|
| 132 | mBitDepth, mChannelCnt, mOutputSampleRate, AudioResampler::DEFAULT);
|
| 133 |
|
| 134 | if(mResampler == NULL) {
|
| 135 | return NO_MEMORY;
|
| 136 | }
|
| 137 | LOGV("Set input rate %d", mSampleRate);
|
| 138 | mResampler->setSampleRate(mSampleRate);
|
| 139 | mResampler->setVolume(UNITY_GAIN, UNITY_GAIN);
|
| 140 |
|
| 141 | } else {
|
| 142 | if(mChannelCnt != 2) { //we always make sure to provide stereo
|
| 143 | LOGV("Only Channel convertion required");
|
| 144 | mIsChannelConvertionRequired = true;
|
| 145 | }
|
| 146 | }
|
| 147 | mSeekTimeUs = -1;
|
| 148 | #ifndef FROYO
|
| 149 | mSeekMode = ReadOptions::SEEK_PREVIOUS_SYNC;
|
| 150 | #endif
|
| 151 | mStarted = true;
|
| 152 | mSource->start();
|
| 153 |
|
Dharmaray Kundargi | 643290d | 2011-01-16 16:02:42 -0800 | [diff] [blame] | 154 | return OK;
|
| 155 | }
|
| 156 |
|
| 157 | status_t VideoEditorSRC::stop() {
|
| 158 |
|
| 159 | Mutex::Autolock autoLock(mLock);
|
| 160 | LOGV("VideoEditorSRC::stop()");
|
| 161 | mSource->stop();
|
| 162 | if(mResampler != NULL) {
|
| 163 | delete mResampler;
|
| 164 | mResampler = NULL;
|
| 165 | }
|
| 166 | mStarted = false;
|
| 167 | mInitialTimeStampUs = -1;
|
| 168 | mAccuOutBufferSize = 0;
|
| 169 | mLeftover = 0;
|
| 170 | mLastReadSize = 0;
|
| 171 |
|
Dharmaray Kundargi | 643290d | 2011-01-16 16:02:42 -0800 | [diff] [blame] | 172 | return OK;
|
| 173 | }
|
| 174 |
|
| 175 | sp<MetaData> VideoEditorSRC::getFormat() {
|
| 176 | LOGV("AudioSRC getFormat");
|
| 177 | //Mutex::Autolock autoLock(mLock);
|
| 178 | return mOutputFormat;
|
| 179 | }
|
| 180 |
|
| 181 | status_t VideoEditorSRC::read (
|
| 182 | MediaBuffer **buffer_out, const ReadOptions *options) {
|
| 183 | Mutex::Autolock autoLock(mLock);
|
| 184 | *buffer_out = NULL;
|
| 185 | int32_t leftover = 0;
|
| 186 |
|
| 187 | LOGV("VideoEditorSRC::read");
|
| 188 |
|
| 189 | if (!mStarted) {
|
| 190 | return ERROR_END_OF_STREAM;
|
| 191 | }
|
| 192 |
|
| 193 | if(mIsResamplingRequired == true) {
|
| 194 |
|
| 195 | LOGV("mIsResamplingRequired = true");
|
| 196 |
|
| 197 | // Store the seek parameters
|
| 198 | int64_t seekTimeUs;
|
| 199 | #ifndef FROYO
|
| 200 | ReadOptions::SeekMode mode = ReadOptions::SEEK_PREVIOUS_SYNC;
|
| 201 | if (options && options->getSeekTo(&seekTimeUs, &mode)) {
|
| 202 | #else
|
| 203 | if (options && options->getSeekTo(&seekTimeUs)) {
|
| 204 | #endif
|
| 205 | LOGV("read Seek %lld", seekTimeUs);
|
| 206 | mInitialTimeStampUs = -1;
|
| 207 | mSeekTimeUs = seekTimeUs;
|
| 208 | #ifndef FROYO
|
| 209 | mSeekMode = mode;
|
| 210 | #else
|
| 211 | mReadOptions = *options;
|
| 212 | #endif
|
| 213 | }
|
| 214 |
|
| 215 | // We ask for 1024 frames in output
|
| 216 | size_t outFrameCnt = 1024;
|
| 217 | int32_t outBufferSize = (outFrameCnt) * 2 * sizeof(int16_t); //out is always 2 channels & 16 bits
|
| 218 | int64_t outDurationUs = (outBufferSize * 1000000) /(mOutputSampleRate * 2 * sizeof(int16_t)); //2 channels out * 2 bytes per sample
|
| 219 | LOGV("outBufferSize %d", outBufferSize);
|
| 220 | LOGV("outFrameCnt %d", outFrameCnt);
|
| 221 |
|
| 222 | pTmpBuffer = (int32_t*)malloc(outFrameCnt * 2 * sizeof(int32_t)); //out is always 2 channels and resampler out is 32 bits
|
| 223 | memset(pTmpBuffer, 0x00, outFrameCnt * 2 * sizeof(int32_t));
|
| 224 | // Resample to target quality
|
| 225 | mResampler->resample(pTmpBuffer, outFrameCnt, this);
|
| 226 | int16_t *reSampledBuffer = (int16_t*)malloc(outBufferSize);
|
| 227 | memset(reSampledBuffer, 0x00, outBufferSize);
|
| 228 |
|
| 229 | // Convert back to 16 bits
|
| 230 | AudioMixer::ditherAndClamp((int32_t*)reSampledBuffer, pTmpBuffer, outFrameCnt);
|
| 231 | LOGV("Resampled buffer size %d", outFrameCnt* 2 * sizeof(int16_t));
|
| 232 |
|
| 233 | // Create new MediaBuffer
|
| 234 | mCopyBuffer = new MediaBuffer((void*)reSampledBuffer, outBufferSize);
|
| 235 |
|
| 236 | // Compute and set the new timestamp
|
| 237 | sp<MetaData> to = mCopyBuffer->meta_data();
|
| 238 | int64_t totalOutDurationUs = (mAccuOutBufferSize * 1000000) /(mOutputSampleRate * 2 * 2); //2 channels out * 2 bytes per sample
|
| 239 | int64_t timeUs = mInitialTimeStampUs + totalOutDurationUs;
|
| 240 | to->setInt64(kKeyTime, timeUs);
|
| 241 | LOGV("buffer duration %lld timestamp %lld init %lld", outDurationUs, timeUs, mInitialTimeStampUs);
|
| 242 |
|
| 243 | // update the accumulate size
|
| 244 | mAccuOutBufferSize += outBufferSize;
|
| 245 |
|
| 246 | mCopyBuffer->set_range(0, outBufferSize);
|
| 247 | *buffer_out = mCopyBuffer;
|
| 248 |
|
| 249 | free(pTmpBuffer);
|
| 250 |
|
| 251 | } else if(mIsChannelConvertionRequired == true) {
|
| 252 | //TODO convert to stereo here.
|
| 253 | } else {
|
| 254 | //LOGI("Resampling not required");
|
| 255 | MediaBuffer *aBuffer;
|
| 256 | status_t err = mSource->read(&aBuffer, options);
|
| 257 | LOGV("mSource->read returned %d", err);
|
| 258 | if(err != OK) {
|
| 259 | *buffer_out = NULL;
|
| 260 | mStarted = false;
|
| 261 | return err;
|
| 262 | }
|
| 263 | *buffer_out = aBuffer;
|
| 264 | }
|
| 265 |
|
| 266 | return OK;
|
| 267 | }
|
| 268 |
|
| 269 | status_t VideoEditorSRC::getNextBuffer(AudioBufferProvider::Buffer *pBuffer) {
|
| 270 | LOGV("Requesting %d", pBuffer->frameCount);
|
| 271 | uint32_t availableFrames;
|
| 272 | bool lastBuffer = false;
|
| 273 | MediaBuffer *aBuffer;
|
| 274 |
|
| 275 |
|
| 276 | //update the internal buffer
|
| 277 | // Store the leftover at the beginning of the local buffer
|
| 278 | if (mLeftover > 0) {
|
| 279 | LOGV("Moving mLeftover =%d from %d", mLeftover, mLastReadSize);
|
| 280 | if (mLastReadSize > 0) {
|
| 281 | memcpy(pInterframeBuffer, (uint8_t*) (pInterframeBuffer + mLastReadSize), mLeftover);
|
| 282 | }
|
| 283 | mInterframeBufferPosition = mLeftover;
|
| 284 | }
|
| 285 | else {
|
| 286 | mInterframeBufferPosition = 0;
|
| 287 | }
|
| 288 |
|
| 289 | availableFrames = mInterframeBufferPosition / (mChannelCnt*2);
|
| 290 |
|
| 291 | while ((availableFrames < pBuffer->frameCount)&&(mStarted)) {
|
| 292 | // if we seek, reset the initial time stamp and accumulated time
|
| 293 | #ifndef FROYO
|
| 294 | ReadOptions options;
|
| 295 | if (mSeekTimeUs >= 0) {
|
| 296 | LOGV("%p cacheMore_l Seek requested = %lld", this, mSeekTimeUs);
|
| 297 | ReadOptions::SeekMode mode = mSeekMode;
|
| 298 | options.setSeekTo(mSeekTimeUs, mode);
|
| 299 | mSeekTimeUs = -1;
|
| 300 | }
|
| 301 | #else
|
| 302 | ReadOptions options;
|
| 303 | if (mSeekTimeUs >= 0) {
|
| 304 | LOGV("%p cacheMore_l Seek requested = %lld", this, mSeekTimeUs);
|
| 305 | options = mReadOptions;
|
| 306 | mSeekTimeUs = -1;
|
| 307 | }
|
| 308 | #endif
|
| 309 | /* The first call to read() will require to buffer twice as much data */
|
| 310 | /* This will be needed by the resampler */
|
| 311 | status_t err = mSource->read(&aBuffer, &options);
|
| 312 | LOGV("mSource->read returned %d", err);
|
| 313 | if(err != OK) {
|
| 314 | if (mInterframeBufferPosition == 0) {
|
| 315 | mStarted = false;
|
| 316 | }
|
| 317 | //Empty the internal buffer if there is no more data left in the source
|
| 318 | else {
|
| 319 | lastBuffer = true;
|
| 320 | mInputByteBuffer = pInterframeBuffer;
|
| 321 | //clear the end of the buffer, just in case
|
| 322 | memset(pInterframeBuffer+mInterframeBufferPosition, 0x00, DEFAULT_SAMPLING_FREQ * 2 * 2 - mInterframeBufferPosition);
|
| 323 | mStarted = false;
|
| 324 | }
|
| 325 | }
|
| 326 | else {
|
| 327 | //copy the buffer
|
Kenny Root | eb5b265 | 2011-02-08 11:13:19 -0800 | [diff] [blame^] | 328 | memcpy(((uint8_t*) pInterframeBuffer) + mInterframeBufferPosition,
|
| 329 | ((uint8_t*) aBuffer->data()) + aBuffer->range_offset(),
|
| 330 | aBuffer->range_length());
|
Dharmaray Kundargi | 643290d | 2011-01-16 16:02:42 -0800 | [diff] [blame] | 331 | LOGV("Read from buffer %d", aBuffer->range_length());
|
| 332 |
|
| 333 | mInterframeBufferPosition += aBuffer->range_length();
|
| 334 | LOGV("Stored %d", mInterframeBufferPosition);
|
| 335 |
|
| 336 | // Get the time stamp of the first buffer
|
| 337 | if (mInitialTimeStampUs == -1) {
|
| 338 | int64_t curTS;
|
| 339 | sp<MetaData> from = aBuffer->meta_data();
|
| 340 | from->findInt64(kKeyTime, &curTS);
|
| 341 | LOGV("setting mInitialTimeStampUs to %lld", mInitialTimeStampUs);
|
| 342 | mInitialTimeStampUs = curTS;
|
| 343 | }
|
| 344 |
|
| 345 | // release the buffer
|
| 346 | aBuffer->release();
|
| 347 | }
|
| 348 | availableFrames = mInterframeBufferPosition / (mChannelCnt*2);
|
| 349 | LOGV("availableFrames %d", availableFrames);
|
| 350 | }
|
| 351 |
|
| 352 | if (lastBuffer) {
|
| 353 | pBuffer->frameCount = availableFrames;
|
| 354 | }
|
| 355 |
|
| 356 | //update the input buffer
|
| 357 | pBuffer->raw = (void*)(pInterframeBuffer);
|
| 358 |
|
| 359 | // Update how many bytes are left
|
| 360 | // (actualReadSize is updated in getNextBuffer() called from resample())
|
| 361 | int32_t actualReadSize = pBuffer->frameCount * mChannelCnt * 2;
|
| 362 | mLeftover = mInterframeBufferPosition - actualReadSize;
|
| 363 | LOGV("mLeftover %d", mLeftover);
|
| 364 |
|
| 365 | mLastReadSize = actualReadSize;
|
| 366 |
|
Dharmaray Kundargi | 643290d | 2011-01-16 16:02:42 -0800 | [diff] [blame] | 367 | LOGV("inFrameCount %d", pBuffer->frameCount);
|
| 368 |
|
| 369 | return OK;
|
| 370 | }
|
| 371 |
|
| 372 |
|
| 373 | void VideoEditorSRC::releaseBuffer(AudioBufferProvider::Buffer *pBuffer) {
|
| 374 | if(pBuffer->raw != NULL) {
|
| 375 | pBuffer->raw = NULL;
|
| 376 | }
|
| 377 | pBuffer->frameCount = 0;
|
| 378 | }
|
| 379 |
|
| 380 | } //namespce android
|