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