Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "VideoEditorSRC" |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 19 | |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <utils/Log.h> |
Glenn Kasten | 7bdbbc7 | 2011-12-16 11:07:44 -0800 | [diff] [blame] | 22 | #include <audio_utils/primitives.h> |
James Dong | c4689fa | 2012-02-08 13:51:46 -0800 | [diff] [blame^] | 23 | #include <media/stagefright/foundation/ADebug.h> |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 24 | #include <media/stagefright/MetaData.h> |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 25 | #include <media/stagefright/MediaBuffer.h> |
| 26 | #include <media/stagefright/MediaDefs.h> |
| 27 | #include "AudioMixer.h" |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame] | 28 | #include "VideoEditorSRC.h" |
| 29 | |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 33 | VideoEditorSRC::VideoEditorSRC(const sp<MediaSource> &source) { |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame] | 34 | ALOGV("VideoEditorSRC %p(%p)", this, source.get()); |
| 35 | static const int32_t kDefaultSamplingFreqencyHz = kFreq32000Hz; |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 36 | mSource = source; |
| 37 | mResampler = NULL; |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 38 | mChannelCnt = 0; |
| 39 | mSampleRate = 0; |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame] | 40 | mOutputSampleRate = kDefaultSamplingFreqencyHz; |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 41 | mStarted = false; |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 42 | mInitialTimeStampUs = -1; |
| 43 | mAccuOutBufferSize = 0; |
| 44 | mSeekTimeUs = -1; |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 45 | mBuffer = NULL; |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 46 | mLeftover = 0; |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 47 | mFormatChanged = false; |
Chih-Chung Chang | 2db7671 | 2011-11-15 19:47:25 +0800 | [diff] [blame] | 48 | mStopPending = false; |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 49 | mSeekMode = ReadOptions::SEEK_PREVIOUS_SYNC; |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 50 | |
| 51 | // Input Source validation |
| 52 | sp<MetaData> format = mSource->getFormat(); |
| 53 | const char *mime; |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 54 | CHECK(format->findCString(kKeyMIMEType, &mime)); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 55 | CHECK(!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_RAW)); |
| 56 | |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 57 | // Set the metadata of the output after resampling. |
| 58 | mOutputFormat = new MetaData; |
| 59 | mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW); |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame] | 60 | mOutputFormat->setInt32(kKeySampleRate, kDefaultSamplingFreqencyHz); |
| 61 | mOutputFormat->setInt32(kKeyChannelCount, 2); // always stereo |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 62 | } |
| 63 | |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 64 | VideoEditorSRC::~VideoEditorSRC() { |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame] | 65 | ALOGV("~VideoEditorSRC %p(%p)", this, mSource.get()); |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 66 | stop(); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 67 | } |
| 68 | |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 69 | status_t VideoEditorSRC::start(MetaData *params) { |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame] | 70 | ALOGV("start %p(%p)", this, mSource.get()); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 71 | CHECK(!mStarted); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 72 | |
Rajneesh Chowdury | 1c3c543 | 2011-07-20 13:47:34 -0700 | [diff] [blame] | 73 | // Set resampler if required |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 74 | checkAndSetResampler(); |
Rajneesh Chowdury | 1c3c543 | 2011-07-20 13:47:34 -0700 | [diff] [blame] | 75 | |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 76 | mSeekTimeUs = -1; |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 77 | mSeekMode = ReadOptions::SEEK_PREVIOUS_SYNC; |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 78 | mStarted = true; |
| 79 | mSource->start(); |
| 80 | |
| 81 | return OK; |
| 82 | } |
| 83 | |
| 84 | status_t VideoEditorSRC::stop() { |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame] | 85 | ALOGV("stop %p(%p)", this, mSource.get()); |
| 86 | if (!mStarted) { |
| 87 | return OK; |
| 88 | } |
| 89 | |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 90 | if (mBuffer) { |
| 91 | mBuffer->release(); |
| 92 | mBuffer = NULL; |
| 93 | } |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 94 | mSource->stop(); |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame] | 95 | if (mResampler != NULL) { |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 96 | delete mResampler; |
| 97 | mResampler = NULL; |
| 98 | } |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame] | 99 | |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 100 | mStarted = false; |
| 101 | mInitialTimeStampUs = -1; |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 102 | mAccuOutBufferSize = 0; |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 103 | mLeftover = 0; |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 104 | |
| 105 | return OK; |
| 106 | } |
| 107 | |
| 108 | sp<MetaData> VideoEditorSRC::getFormat() { |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame] | 109 | ALOGV("getFormat"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 110 | return mOutputFormat; |
| 111 | } |
| 112 | |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 113 | status_t VideoEditorSRC::read( |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 114 | MediaBuffer **buffer_out, const ReadOptions *options) { |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame] | 115 | ALOGV("read %p(%p)", this, mSource.get()); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 116 | *buffer_out = NULL; |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 117 | |
| 118 | if (!mStarted) { |
| 119 | return ERROR_END_OF_STREAM; |
| 120 | } |
| 121 | |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 122 | if (mResampler) { |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 123 | // Store the seek parameters |
| 124 | int64_t seekTimeUs; |
| 125 | ReadOptions::SeekMode mode = ReadOptions::SEEK_PREVIOUS_SYNC; |
| 126 | if (options && options->getSeekTo(&seekTimeUs, &mode)) { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 127 | ALOGV("read Seek %lld", seekTimeUs); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 128 | mSeekTimeUs = seekTimeUs; |
| 129 | mSeekMode = mode; |
| 130 | } |
| 131 | |
| 132 | // We ask for 1024 frames in output |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 133 | // resampler output is always 2 channels and 32 bits |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame] | 134 | const size_t kOutputFrameCount = 1024; |
| 135 | const size_t kBytes = kOutputFrameCount * 2 * sizeof(int32_t); |
| 136 | int32_t *pTmpBuffer = (int32_t *)calloc(1, kBytes); |
| 137 | if (!pTmpBuffer) { |
| 138 | ALOGE("calloc failed to allocate memory: %d bytes", kBytes); |
| 139 | return NO_MEMORY; |
| 140 | } |
| 141 | |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 142 | // Resample to target quality |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame] | 143 | mResampler->resample(pTmpBuffer, kOutputFrameCount, this); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 144 | |
Chih-Chung Chang | 2db7671 | 2011-11-15 19:47:25 +0800 | [diff] [blame] | 145 | if (mStopPending) { |
| 146 | stop(); |
| 147 | mStopPending = false; |
| 148 | } |
| 149 | |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 150 | // Change resampler and retry if format change happened |
| 151 | if (mFormatChanged) { |
| 152 | mFormatChanged = false; |
| 153 | checkAndSetResampler(); |
| 154 | free(pTmpBuffer); |
| 155 | return read(buffer_out, NULL); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 156 | } |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 157 | |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 158 | // Create a new MediaBuffer |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame] | 159 | int32_t outBufferSize = kOutputFrameCount * 2 * sizeof(int16_t); |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 160 | MediaBuffer* outBuffer = new MediaBuffer(outBufferSize); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 161 | |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 162 | // Convert back to 2 channels and 16 bits |
Glenn Kasten | 7bdbbc7 | 2011-12-16 11:07:44 -0800 | [diff] [blame] | 163 | ditherAndClamp( |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 164 | (int32_t *)((uint8_t*)outBuffer->data() + outBuffer->range_offset()), |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame] | 165 | pTmpBuffer, kOutputFrameCount); |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 166 | free(pTmpBuffer); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 167 | |
| 168 | // Compute and set the new timestamp |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 169 | sp<MetaData> to = outBuffer->meta_data(); |
| 170 | int64_t totalOutDurationUs = (mAccuOutBufferSize * 1000000) / (mOutputSampleRate * 2 * 2); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 171 | int64_t timeUs = mInitialTimeStampUs + totalOutDurationUs; |
| 172 | to->setInt64(kKeyTime, timeUs); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 173 | |
| 174 | // update the accumulate size |
| 175 | mAccuOutBufferSize += outBufferSize; |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 176 | *buffer_out = outBuffer; |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 177 | } else { |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 178 | // Resampling not required. Read and pass-through. |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 179 | MediaBuffer *aBuffer; |
| 180 | status_t err = mSource->read(&aBuffer, options); |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 181 | if (err != OK) { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 182 | ALOGV("read returns err = %d", err); |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | if (err == INFO_FORMAT_CHANGED) { |
| 186 | checkAndSetResampler(); |
| 187 | return read(buffer_out, NULL); |
| 188 | } |
| 189 | |
| 190 | // EOS or some other error |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 191 | if(err != OK) { |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 192 | stop(); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 193 | *buffer_out = NULL; |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 194 | return err; |
| 195 | } |
| 196 | *buffer_out = aBuffer; |
| 197 | } |
| 198 | |
| 199 | return OK; |
| 200 | } |
| 201 | |
| 202 | status_t VideoEditorSRC::getNextBuffer(AudioBufferProvider::Buffer *pBuffer) { |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame] | 203 | ALOGV("getNextBuffer %d, chan = %d", pBuffer->frameCount, mChannelCnt); |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 204 | uint32_t done = 0; |
| 205 | uint32_t want = pBuffer->frameCount * mChannelCnt * 2; |
| 206 | pBuffer->raw = malloc(want); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 207 | |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 208 | while (mStarted && want > 0) { |
| 209 | // If we don't have any data left, read a new buffer. |
| 210 | if (!mBuffer) { |
| 211 | // if we seek, reset the initial time stamp and accumulated time |
| 212 | ReadOptions options; |
| 213 | if (mSeekTimeUs >= 0) { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 214 | ALOGV("%p cacheMore_l Seek requested = %lld", this, mSeekTimeUs); |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 215 | ReadOptions::SeekMode mode = mSeekMode; |
| 216 | options.setSeekTo(mSeekTimeUs, mode); |
| 217 | mSeekTimeUs = -1; |
| 218 | mInitialTimeStampUs = -1; |
| 219 | mAccuOutBufferSize = 0; |
Rajneesh Chowdury | 1c3c543 | 2011-07-20 13:47:34 -0700 | [diff] [blame] | 220 | } |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 221 | |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 222 | status_t err = mSource->read(&mBuffer, &options); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 223 | |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 224 | if (err != OK) { |
| 225 | free(pBuffer->raw); |
| 226 | pBuffer->raw = NULL; |
| 227 | pBuffer->frameCount = 0; |
| 228 | } |
| 229 | |
| 230 | if (err == INFO_FORMAT_CHANGED) { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 231 | ALOGV("getNextBuffer: source read returned INFO_FORMAT_CHANGED"); |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 232 | // At this point we cannot switch to a new AudioResampler because |
| 233 | // we are in a callback called by the AudioResampler itself. So |
| 234 | // just remember the fact that the format has changed, and let |
| 235 | // read() handles this. |
| 236 | mFormatChanged = true; |
| 237 | return err; |
| 238 | } |
| 239 | |
| 240 | // EOS or some other error |
| 241 | if (err != OK) { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 242 | ALOGV("EOS or some err: %d", err); |
Chih-Chung Chang | 2db7671 | 2011-11-15 19:47:25 +0800 | [diff] [blame] | 243 | // We cannot call stop() here because stop() will release the |
| 244 | // AudioResampler, and we are in a callback of the AudioResampler. |
| 245 | // So just remember the fact and let read() call stop(). |
| 246 | mStopPending = true; |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 247 | return err; |
| 248 | } |
| 249 | |
| 250 | CHECK(mBuffer); |
| 251 | mLeftover = mBuffer->range_length(); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 252 | if (mInitialTimeStampUs == -1) { |
| 253 | int64_t curTS; |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 254 | sp<MetaData> from = mBuffer->meta_data(); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 255 | from->findInt64(kKeyTime, &curTS); |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 256 | ALOGV("setting mInitialTimeStampUs to %lld", mInitialTimeStampUs); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 257 | mInitialTimeStampUs = curTS; |
| 258 | } |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 259 | } |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 260 | |
| 261 | // Now copy data to the destination |
| 262 | uint32_t todo = mLeftover; |
| 263 | if (todo > want) { |
| 264 | todo = want; |
| 265 | } |
| 266 | |
| 267 | uint8_t* end = (uint8_t*)mBuffer->data() + mBuffer->range_offset() |
| 268 | + mBuffer->range_length(); |
| 269 | memcpy((uint8_t*)pBuffer->raw + done, end - mLeftover, todo); |
| 270 | done += todo; |
| 271 | want -= todo; |
| 272 | mLeftover -= todo; |
| 273 | |
| 274 | // Release MediaBuffer as soon as possible. |
| 275 | if (mLeftover == 0) { |
| 276 | mBuffer->release(); |
| 277 | mBuffer = NULL; |
| 278 | } |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 279 | } |
| 280 | |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 281 | pBuffer->frameCount = done / (mChannelCnt * 2); |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 282 | ALOGV("getNextBuffer done %d", pBuffer->frameCount); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 283 | return OK; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | void VideoEditorSRC::releaseBuffer(AudioBufferProvider::Buffer *pBuffer) { |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame] | 288 | ALOGV("releaseBuffer: %p", pBuffers); |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 289 | free(pBuffer->raw); |
| 290 | pBuffer->raw = NULL; |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 291 | pBuffer->frameCount = 0; |
| 292 | } |
| 293 | |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 294 | void VideoEditorSRC::checkAndSetResampler() { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 295 | ALOGV("checkAndSetResampler"); |
Rajneesh Chowdury | 1c3c543 | 2011-07-20 13:47:34 -0700 | [diff] [blame] | 296 | |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame] | 297 | static const uint16_t kUnityGain = 0x1000; |
Rajneesh Chowdury | 1c3c543 | 2011-07-20 13:47:34 -0700 | [diff] [blame] | 298 | sp<MetaData> format = mSource->getFormat(); |
| 299 | const char *mime; |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 300 | CHECK(format->findCString(kKeyMIMEType, &mime)); |
Rajneesh Chowdury | 1c3c543 | 2011-07-20 13:47:34 -0700 | [diff] [blame] | 301 | CHECK(!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_RAW)); |
| 302 | |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 303 | CHECK(format->findInt32(kKeySampleRate, &mSampleRate)); |
| 304 | CHECK(format->findInt32(kKeyChannelCount, &mChannelCnt)); |
Rajneesh Chowdury | 1c3c543 | 2011-07-20 13:47:34 -0700 | [diff] [blame] | 305 | |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 306 | // If a resampler exists, delete it first |
Rajneesh Chowdury | 1c3c543 | 2011-07-20 13:47:34 -0700 | [diff] [blame] | 307 | if (mResampler != NULL) { |
| 308 | delete mResampler; |
| 309 | mResampler = NULL; |
| 310 | } |
| 311 | |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 312 | // Clear previous buffer |
| 313 | if (mBuffer) { |
| 314 | mBuffer->release(); |
| 315 | mBuffer = NULL; |
Rajneesh Chowdury | 1c3c543 | 2011-07-20 13:47:34 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 318 | if (mSampleRate != mOutputSampleRate || mChannelCnt != 2) { |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame] | 319 | ALOGV("Resampling required (%d => %d Hz, # channels = %d)", |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 320 | mSampleRate, mOutputSampleRate, mChannelCnt); |
Rajneesh Chowdury | 1c3c543 | 2011-07-20 13:47:34 -0700 | [diff] [blame] | 321 | |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 322 | mResampler = AudioResampler::create( |
| 323 | 16 /* bit depth */, |
| 324 | mChannelCnt, |
| 325 | mOutputSampleRate, |
| 326 | AudioResampler::DEFAULT); |
| 327 | CHECK(mResampler); |
| 328 | mResampler->setSampleRate(mSampleRate); |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame] | 329 | mResampler->setVolume(kUnityGain, kUnityGain); |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 330 | } else { |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame] | 331 | ALOGV("Resampling not required (%d => %d Hz, # channels = %d)", |
| 332 | mSampleRate, mOutputSampleRate, mChannelCnt); |
Chih-Chung Chang | 3d974e7 | 2011-08-18 19:41:30 +0800 | [diff] [blame] | 333 | } |
Rajneesh Chowdury | 1c3c543 | 2011-07-20 13:47:34 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 336 | } //namespce android |