niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
mflodman@webrtc.org | 1f99280 | 2012-01-27 13:42:53 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
pbos@webrtc.org | 8b06200 | 2013-07-12 08:28:10 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/utility/source/file_player_impl.h" |
| 12 | #include "webrtc/system_wrappers/interface/trace.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
| 14 | #ifdef WEBRTC_MODULE_UTILITY_VIDEO |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | #include "frame_scaler.h" |
| 16 | #include "tick_util.h" |
| 17 | #include "video_coder.h" |
| 18 | #endif |
| 19 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | namespace webrtc { |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 21 | FilePlayer* FilePlayer::CreateFilePlayer(uint32_t instanceID, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | FileFormats fileFormat) |
| 23 | { |
| 24 | switch(fileFormat) |
| 25 | { |
| 26 | case kFileFormatWavFile: |
| 27 | case kFileFormatCompressedFile: |
| 28 | case kFileFormatPreencodedFile: |
| 29 | case kFileFormatPcm16kHzFile: |
| 30 | case kFileFormatPcm8kHzFile: |
| 31 | case kFileFormatPcm32kHzFile: |
| 32 | // audio formats |
| 33 | return new FilePlayerImpl(instanceID, fileFormat); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | case kFileFormatAviFile: |
mflodman@webrtc.org | c80d9d9 | 2012-02-06 10:11:25 +0000 | [diff] [blame] | 35 | #ifdef WEBRTC_MODULE_UTILITY_VIDEO |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 36 | return new VideoFilePlayerImpl(instanceID, fileFormat); |
mflodman@webrtc.org | c80d9d9 | 2012-02-06 10:11:25 +0000 | [diff] [blame] | 37 | #else |
| 38 | WEBRTC_TRACE(kTraceError, kTraceFile, -1, |
| 39 | "Invalid file format: %d", kFileFormatAviFile); |
| 40 | assert(false); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | return NULL; |
mflodman@webrtc.org | c80d9d9 | 2012-02-06 10:11:25 +0000 | [diff] [blame] | 42 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | } |
mflodman@webrtc.org | 657b2a4 | 2012-02-06 11:06:01 +0000 | [diff] [blame] | 44 | assert(false); |
| 45 | return NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void FilePlayer::DestroyFilePlayer(FilePlayer* player) |
| 49 | { |
| 50 | delete player; |
| 51 | } |
| 52 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 53 | FilePlayerImpl::FilePlayerImpl(const uint32_t instanceID, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | const FileFormats fileFormat) |
| 55 | : _instanceID(instanceID), |
| 56 | _fileFormat(fileFormat), |
| 57 | _fileModule(*MediaFile::CreateMediaFile(instanceID)), |
| 58 | _decodedLengthInMS(0), |
| 59 | _audioDecoder(instanceID), |
| 60 | _codec(), |
| 61 | _numberOf10MsPerFrame(0), |
| 62 | _numberOf10MsInDecoder(0), |
henrike@webrtc.org | 6b9253e | 2012-02-15 18:48:16 +0000 | [diff] [blame] | 63 | _resampler(), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 64 | _scaling(1.0) |
| 65 | { |
| 66 | _codec.plfreq = 0; |
| 67 | } |
| 68 | |
| 69 | FilePlayerImpl::~FilePlayerImpl() |
| 70 | { |
| 71 | MediaFile::DestroyMediaFile(&_fileModule); |
| 72 | } |
| 73 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 74 | int32_t FilePlayerImpl::Frequency() const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 75 | { |
| 76 | if(_codec.plfreq == 0) |
| 77 | { |
| 78 | return -1; |
| 79 | } |
| 80 | // Make sure that sample rate is 8,16 or 32 kHz. E.g. WAVE files may have |
| 81 | // other sampling rates. |
| 82 | if(_codec.plfreq == 11000) |
| 83 | { |
| 84 | return 16000; |
| 85 | } |
| 86 | else if(_codec.plfreq == 22000) |
| 87 | { |
| 88 | return 32000; |
| 89 | } |
| 90 | else if(_codec.plfreq == 44000) |
| 91 | { |
| 92 | return 32000; |
| 93 | } |
| 94 | else if(_codec.plfreq == 48000) |
| 95 | { |
| 96 | return 32000; |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | return _codec.plfreq; |
| 101 | } |
| 102 | } |
| 103 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 104 | int32_t FilePlayerImpl::AudioCodec(CodecInst& audioCodec) const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 105 | { |
| 106 | audioCodec = _codec; |
| 107 | return 0; |
| 108 | } |
| 109 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 110 | int32_t FilePlayerImpl::Get10msAudioFromFile( |
andrew@webrtc.org | e59a0ac | 2012-05-08 17:12:40 +0000 | [diff] [blame] | 111 | int16_t* outBuffer, |
| 112 | int& lengthInSamples, |
| 113 | int frequencyInHz) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | { |
| 115 | if(_codec.plfreq == 0) |
| 116 | { |
| 117 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, _instanceID, |
| 118 | "FilePlayerImpl::Get10msAudioFromFile() playing not started!\ |
| 119 | codecFreq = %d, wantedFreq = %d", |
| 120 | _codec.plfreq, frequencyInHz); |
| 121 | return -1; |
| 122 | } |
| 123 | |
| 124 | AudioFrame unresampledAudioFrame; |
| 125 | if(STR_CASE_CMP(_codec.plname, "L16") == 0) |
| 126 | { |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 127 | unresampledAudioFrame.sample_rate_hz_ = _codec.plfreq; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 128 | |
| 129 | // L16 is un-encoded data. Just pull 10 ms. |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 130 | uint32_t lengthInBytes = |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 131 | sizeof(unresampledAudioFrame.data_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 132 | if (_fileModule.PlayoutAudioData( |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 133 | (int8_t*)unresampledAudioFrame.data_, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 134 | lengthInBytes) == -1) |
| 135 | { |
| 136 | // End of file reached. |
| 137 | return -1; |
| 138 | } |
| 139 | if(lengthInBytes == 0) |
| 140 | { |
| 141 | lengthInSamples = 0; |
| 142 | return 0; |
| 143 | } |
| 144 | // One sample is two bytes. |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 145 | unresampledAudioFrame.samples_per_channel_ = |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 146 | (uint16_t)lengthInBytes >> 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 147 | |
| 148 | }else { |
| 149 | // Decode will generate 10 ms of audio data. PlayoutAudioData(..) |
| 150 | // expects a full frame. If the frame size is larger than 10 ms, |
| 151 | // PlayoutAudioData(..) data should be called proportionally less often. |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 152 | int16_t encodedBuffer[MAX_AUDIO_BUFFER_IN_SAMPLES]; |
| 153 | uint32_t encodedLengthInBytes = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 154 | if(++_numberOf10MsInDecoder >= _numberOf10MsPerFrame) |
| 155 | { |
| 156 | _numberOf10MsInDecoder = 0; |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 157 | uint32_t bytesFromFile = sizeof(encodedBuffer); |
| 158 | if (_fileModule.PlayoutAudioData((int8_t*)encodedBuffer, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 159 | bytesFromFile) == -1) |
| 160 | { |
| 161 | // End of file reached. |
| 162 | return -1; |
| 163 | } |
| 164 | encodedLengthInBytes = bytesFromFile; |
| 165 | } |
| 166 | if(_audioDecoder.Decode(unresampledAudioFrame,frequencyInHz, |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 167 | (int8_t*)encodedBuffer, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 168 | encodedLengthInBytes) == -1) |
| 169 | { |
| 170 | return -1; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | int outLen = 0; |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 175 | if(_resampler.ResetIfNeeded(unresampledAudioFrame.sample_rate_hz_, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 176 | frequencyInHz, kResamplerSynchronous)) |
| 177 | { |
| 178 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, _instanceID, |
| 179 | "FilePlayerImpl::Get10msAudioFromFile() unexpected codec"); |
| 180 | |
| 181 | // New sampling frequency. Update state. |
| 182 | outLen = frequencyInHz / 100; |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 183 | memset(outBuffer, 0, outLen * sizeof(int16_t)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 184 | return 0; |
| 185 | } |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 186 | _resampler.Push(unresampledAudioFrame.data_, |
| 187 | unresampledAudioFrame.samples_per_channel_, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 188 | outBuffer, |
| 189 | MAX_AUDIO_BUFFER_IN_SAMPLES, |
| 190 | outLen); |
| 191 | |
| 192 | lengthInSamples = outLen; |
| 193 | |
| 194 | if(_scaling != 1.0) |
| 195 | { |
| 196 | for (int i = 0;i < outLen; i++) |
| 197 | { |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 198 | outBuffer[i] = (int16_t)(outBuffer[i] * _scaling); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | _decodedLengthInMS += 10; |
| 202 | return 0; |
| 203 | } |
| 204 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 205 | int32_t FilePlayerImpl::RegisterModuleFileCallback(FileCallback* callback) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 206 | { |
| 207 | return _fileModule.SetModuleFileCallback(callback); |
| 208 | } |
| 209 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 210 | int32_t FilePlayerImpl::SetAudioScaling(float scaleFactor) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 211 | { |
| 212 | if((scaleFactor >= 0)&&(scaleFactor <= 2.0)) |
| 213 | { |
| 214 | _scaling = scaleFactor; |
| 215 | return 0; |
| 216 | } |
| 217 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, _instanceID, |
| 218 | "FilePlayerImpl::SetAudioScaling() not allowed scale factor"); |
| 219 | return -1; |
| 220 | } |
| 221 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 222 | int32_t FilePlayerImpl::StartPlayingFile(const char* fileName, |
| 223 | bool loop, |
| 224 | uint32_t startPosition, |
| 225 | float volumeScaling, |
| 226 | uint32_t notification, |
| 227 | uint32_t stopPosition, |
| 228 | const CodecInst* codecInst) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 229 | { |
| 230 | if (_fileFormat == kFileFormatPcm16kHzFile || |
| 231 | _fileFormat == kFileFormatPcm8kHzFile|| |
| 232 | _fileFormat == kFileFormatPcm32kHzFile ) |
| 233 | { |
| 234 | CodecInst codecInstL16; |
| 235 | strncpy(codecInstL16.plname,"L16",32); |
| 236 | codecInstL16.pltype = 93; |
| 237 | codecInstL16.channels = 1; |
| 238 | |
| 239 | if (_fileFormat == kFileFormatPcm8kHzFile) |
| 240 | { |
| 241 | codecInstL16.rate = 128000; |
| 242 | codecInstL16.plfreq = 8000; |
| 243 | codecInstL16.pacsize = 80; |
| 244 | |
| 245 | } else if(_fileFormat == kFileFormatPcm16kHzFile) |
| 246 | { |
| 247 | codecInstL16.rate = 256000; |
| 248 | codecInstL16.plfreq = 16000; |
| 249 | codecInstL16.pacsize = 160; |
| 250 | |
| 251 | }else if(_fileFormat == kFileFormatPcm32kHzFile) |
| 252 | { |
| 253 | codecInstL16.rate = 512000; |
| 254 | codecInstL16.plfreq = 32000; |
| 255 | codecInstL16.pacsize = 160; |
| 256 | } else |
| 257 | { |
| 258 | WEBRTC_TRACE(kTraceError, kTraceVoice, _instanceID, |
| 259 | "FilePlayerImpl::StartPlayingFile() sample frequency\ |
| 260 | specifed not supported for PCM format."); |
| 261 | return -1; |
| 262 | } |
| 263 | |
| 264 | if (_fileModule.StartPlayingAudioFile(fileName, notification, loop, |
| 265 | _fileFormat, &codecInstL16, |
| 266 | startPosition, |
| 267 | stopPosition) == -1) |
| 268 | { |
| 269 | WEBRTC_TRACE( |
| 270 | kTraceWarning, |
| 271 | kTraceVoice, |
| 272 | _instanceID, |
| 273 | "FilePlayerImpl::StartPlayingFile() failed to initialize file\ |
| 274 | %s playout.", fileName); |
| 275 | return -1; |
| 276 | } |
| 277 | SetAudioScaling(volumeScaling); |
| 278 | }else if(_fileFormat == kFileFormatPreencodedFile) |
| 279 | { |
| 280 | if (_fileModule.StartPlayingAudioFile(fileName, notification, loop, |
| 281 | _fileFormat, codecInst) == -1) |
| 282 | { |
| 283 | WEBRTC_TRACE( |
| 284 | kTraceWarning, |
| 285 | kTraceVoice, |
| 286 | _instanceID, |
| 287 | "FilePlayerImpl::StartPlayingPreEncodedFile() failed to\ |
| 288 | initialize pre-encoded file %s playout.", |
| 289 | fileName); |
| 290 | return -1; |
| 291 | } |
| 292 | } else |
| 293 | { |
| 294 | CodecInst* no_inst = NULL; |
| 295 | if (_fileModule.StartPlayingAudioFile(fileName, notification, loop, |
| 296 | _fileFormat, no_inst, |
| 297 | startPosition, |
| 298 | stopPosition) == -1) |
| 299 | { |
| 300 | WEBRTC_TRACE( |
| 301 | kTraceWarning, |
| 302 | kTraceVoice, |
| 303 | _instanceID, |
| 304 | "FilePlayerImpl::StartPlayingFile() failed to initialize file\ |
| 305 | %s playout.", fileName); |
| 306 | return -1; |
| 307 | } |
| 308 | SetAudioScaling(volumeScaling); |
| 309 | } |
| 310 | if (SetUpAudioDecoder() == -1) |
| 311 | { |
| 312 | StopPlayingFile(); |
| 313 | return -1; |
| 314 | } |
| 315 | return 0; |
| 316 | } |
| 317 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 318 | int32_t FilePlayerImpl::StartPlayingFile(InStream& sourceStream, |
| 319 | uint32_t startPosition, |
| 320 | float volumeScaling, |
| 321 | uint32_t notification, |
| 322 | uint32_t stopPosition, |
| 323 | const CodecInst* codecInst) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 324 | { |
| 325 | if (_fileFormat == kFileFormatPcm16kHzFile || |
| 326 | _fileFormat == kFileFormatPcm32kHzFile || |
| 327 | _fileFormat == kFileFormatPcm8kHzFile) |
| 328 | { |
| 329 | CodecInst codecInstL16; |
| 330 | strncpy(codecInstL16.plname,"L16",32); |
| 331 | codecInstL16.pltype = 93; |
| 332 | codecInstL16.channels = 1; |
| 333 | |
| 334 | if (_fileFormat == kFileFormatPcm8kHzFile) |
| 335 | { |
| 336 | codecInstL16.rate = 128000; |
| 337 | codecInstL16.plfreq = 8000; |
| 338 | codecInstL16.pacsize = 80; |
| 339 | |
| 340 | }else if (_fileFormat == kFileFormatPcm16kHzFile) |
| 341 | { |
| 342 | codecInstL16.rate = 256000; |
| 343 | codecInstL16.plfreq = 16000; |
| 344 | codecInstL16.pacsize = 160; |
| 345 | |
| 346 | }else if (_fileFormat == kFileFormatPcm32kHzFile) |
| 347 | { |
| 348 | codecInstL16.rate = 512000; |
| 349 | codecInstL16.plfreq = 32000; |
| 350 | codecInstL16.pacsize = 160; |
| 351 | }else |
| 352 | { |
| 353 | WEBRTC_TRACE( |
| 354 | kTraceError, |
| 355 | kTraceVoice, |
| 356 | _instanceID, |
| 357 | "FilePlayerImpl::StartPlayingFile() sample frequency specifed\ |
| 358 | not supported for PCM format."); |
| 359 | return -1; |
| 360 | } |
| 361 | if (_fileModule.StartPlayingAudioStream(sourceStream, notification, |
| 362 | _fileFormat, &codecInstL16, |
| 363 | startPosition, |
| 364 | stopPosition) == -1) |
| 365 | { |
| 366 | WEBRTC_TRACE( |
| 367 | kTraceError, |
| 368 | kTraceVoice, |
| 369 | _instanceID, |
| 370 | "FilePlayerImpl::StartPlayingFile() failed to initialize stream\ |
| 371 | playout."); |
| 372 | return -1; |
| 373 | } |
| 374 | |
| 375 | }else if(_fileFormat == kFileFormatPreencodedFile) |
| 376 | { |
| 377 | if (_fileModule.StartPlayingAudioStream(sourceStream, notification, |
| 378 | _fileFormat, codecInst) == -1) |
| 379 | { |
| 380 | WEBRTC_TRACE( |
| 381 | kTraceWarning, |
| 382 | kTraceVoice, |
| 383 | _instanceID, |
| 384 | "FilePlayerImpl::StartPlayingFile() failed to initialize stream\ |
| 385 | playout."); |
| 386 | return -1; |
| 387 | } |
| 388 | } else { |
| 389 | CodecInst* no_inst = NULL; |
| 390 | if (_fileModule.StartPlayingAudioStream(sourceStream, notification, |
| 391 | _fileFormat, no_inst, |
| 392 | startPosition, |
| 393 | stopPosition) == -1) |
| 394 | { |
| 395 | WEBRTC_TRACE(kTraceError, kTraceVoice, _instanceID, |
| 396 | "FilePlayerImpl::StartPlayingFile() failed to initialize\ |
| 397 | stream playout."); |
| 398 | return -1; |
| 399 | } |
| 400 | } |
| 401 | SetAudioScaling(volumeScaling); |
| 402 | |
| 403 | if (SetUpAudioDecoder() == -1) |
| 404 | { |
| 405 | StopPlayingFile(); |
| 406 | return -1; |
| 407 | } |
| 408 | return 0; |
| 409 | } |
| 410 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 411 | int32_t FilePlayerImpl::StopPlayingFile() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 412 | { |
| 413 | memset(&_codec, 0, sizeof(CodecInst)); |
| 414 | _numberOf10MsPerFrame = 0; |
| 415 | _numberOf10MsInDecoder = 0; |
| 416 | return _fileModule.StopPlaying(); |
| 417 | } |
| 418 | |
| 419 | bool FilePlayerImpl::IsPlayingFile() const |
| 420 | { |
| 421 | return _fileModule.IsPlaying(); |
| 422 | } |
| 423 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 424 | int32_t FilePlayerImpl::GetPlayoutPosition(uint32_t& durationMs) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 425 | { |
| 426 | return _fileModule.PlayoutPositionMs(durationMs); |
| 427 | } |
| 428 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 429 | int32_t FilePlayerImpl::SetUpAudioDecoder() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 430 | { |
| 431 | if ((_fileModule.codec_info(_codec) == -1)) |
| 432 | { |
| 433 | WEBRTC_TRACE( |
| 434 | kTraceWarning, |
| 435 | kTraceVoice, |
| 436 | _instanceID, |
| 437 | "FilePlayerImpl::StartPlayingFile() failed to retrieve Codec info\ |
| 438 | of file data."); |
| 439 | return -1; |
| 440 | } |
| 441 | if( STR_CASE_CMP(_codec.plname, "L16") != 0 && |
| 442 | _audioDecoder.SetDecodeCodec(_codec,AMRFileStorage) == -1) |
| 443 | { |
| 444 | WEBRTC_TRACE( |
| 445 | kTraceWarning, |
| 446 | kTraceVoice, |
| 447 | _instanceID, |
| 448 | "FilePlayerImpl::StartPlayingFile() codec %s not supported", |
| 449 | _codec.plname); |
| 450 | return -1; |
| 451 | } |
| 452 | _numberOf10MsPerFrame = _codec.pacsize / (_codec.plfreq / 100); |
| 453 | _numberOf10MsInDecoder = 0; |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | #ifdef WEBRTC_MODULE_UTILITY_VIDEO |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 458 | VideoFilePlayerImpl::VideoFilePlayerImpl(uint32_t instanceID, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 459 | FileFormats fileFormat) |
andresp@webrtc.org | 8fa436b | 2013-09-16 11:26:35 +0000 | [diff] [blame] | 460 | : FilePlayerImpl(instanceID, fileFormat), |
| 461 | video_decoder_(new VideoCoder(instanceID)), |
henrike@webrtc.org | 6b9253e | 2012-02-15 18:48:16 +0000 | [diff] [blame] | 462 | video_codec_info_(), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 463 | _decodedVideoFrames(0), |
| 464 | _encodedData(*new EncodedVideoData()), |
| 465 | _frameScaler(*new FrameScaler()), |
henrike@webrtc.org | 105e071 | 2011-12-16 19:53:46 +0000 | [diff] [blame] | 466 | _critSec(CriticalSectionWrapper::CreateCriticalSection()), |
henrike@webrtc.org | 6b9253e | 2012-02-15 18:48:16 +0000 | [diff] [blame] | 467 | _startTime(), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 468 | _accumulatedRenderTimeMs(0), |
henrike@webrtc.org | 6b9253e | 2012-02-15 18:48:16 +0000 | [diff] [blame] | 469 | _frameLengthMS(0), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 470 | _numberOfFramesRead(0), |
andresp@webrtc.org | 8fa436b | 2013-09-16 11:26:35 +0000 | [diff] [blame] | 471 | _videoOnly(false) { |
| 472 | memset(&video_codec_info_, 0, sizeof(video_codec_info_)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | VideoFilePlayerImpl::~VideoFilePlayerImpl() |
| 476 | { |
henrike@webrtc.org | 105e071 | 2011-12-16 19:53:46 +0000 | [diff] [blame] | 477 | delete _critSec; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 478 | delete &_frameScaler; |
andresp@webrtc.org | 8fa436b | 2013-09-16 11:26:35 +0000 | [diff] [blame] | 479 | video_decoder_.reset(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 480 | delete &_encodedData; |
| 481 | } |
| 482 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 483 | int32_t VideoFilePlayerImpl::StartPlayingVideoFile( |
leozwang@webrtc.org | 2559cbf | 2012-02-27 19:18:25 +0000 | [diff] [blame] | 484 | const char* fileName, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 485 | bool loop, |
| 486 | bool videoOnly) |
| 487 | { |
| 488 | CriticalSectionScoped lock( _critSec); |
| 489 | |
| 490 | if(_fileModule.StartPlayingVideoFile(fileName, loop, videoOnly, |
| 491 | _fileFormat) != 0) |
| 492 | { |
| 493 | return -1; |
| 494 | } |
| 495 | |
| 496 | _decodedVideoFrames = 0; |
| 497 | _accumulatedRenderTimeMs = 0; |
| 498 | _frameLengthMS = 0; |
| 499 | _numberOfFramesRead = 0; |
| 500 | _videoOnly = videoOnly; |
| 501 | |
| 502 | // Set up video_codec_info_ according to file, |
| 503 | if(SetUpVideoDecoder() != 0) |
| 504 | { |
| 505 | StopPlayingFile(); |
| 506 | return -1; |
| 507 | } |
| 508 | if(!videoOnly) |
| 509 | { |
| 510 | // Set up _codec according to file, |
| 511 | if(SetUpAudioDecoder() != 0) |
| 512 | { |
| 513 | StopPlayingFile(); |
| 514 | return -1; |
| 515 | } |
| 516 | } |
| 517 | return 0; |
| 518 | } |
| 519 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 520 | int32_t VideoFilePlayerImpl::StopPlayingFile() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 521 | { |
| 522 | CriticalSectionScoped lock( _critSec); |
| 523 | |
| 524 | _decodedVideoFrames = 0; |
andresp@webrtc.org | 8fa436b | 2013-09-16 11:26:35 +0000 | [diff] [blame] | 525 | video_decoder_.reset(new VideoCoder(_instanceID)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 526 | |
| 527 | return FilePlayerImpl::StopPlayingFile(); |
| 528 | } |
| 529 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 530 | int32_t VideoFilePlayerImpl::GetVideoFromFile(I420VideoFrame& videoFrame, |
| 531 | uint32_t outWidth, |
| 532 | uint32_t outHeight) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 533 | { |
| 534 | CriticalSectionScoped lock( _critSec); |
| 535 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 536 | int32_t retVal = GetVideoFromFile(videoFrame); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 537 | if(retVal != 0) |
| 538 | { |
| 539 | return retVal; |
| 540 | } |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 541 | if (!videoFrame.IsZeroSize()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 542 | { |
mflodman@webrtc.org | 1f99280 | 2012-01-27 13:42:53 +0000 | [diff] [blame] | 543 | retVal = _frameScaler.ResizeFrameIfNeeded(&videoFrame, outWidth, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 544 | outHeight); |
| 545 | } |
| 546 | return retVal; |
| 547 | } |
| 548 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 549 | int32_t VideoFilePlayerImpl::GetVideoFromFile(I420VideoFrame& videoFrame) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 550 | { |
| 551 | CriticalSectionScoped lock( _critSec); |
| 552 | // No new video data read from file. |
| 553 | if(_encodedData.payloadSize == 0) |
| 554 | { |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 555 | videoFrame.ResetSize(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 556 | return -1; |
| 557 | } |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 558 | int32_t retVal = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 559 | if(strncmp(video_codec_info_.plName, "I420", 5) == 0) |
| 560 | { |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 561 | int size_y = video_codec_info_.width * video_codec_info_.height; |
| 562 | int half_width = (video_codec_info_.width + 1) / 2; |
| 563 | int half_height = (video_codec_info_.height + 1) / 2; |
| 564 | int size_uv = half_width * half_height; |
| 565 | |
| 566 | // TODO(mikhal): Do we need to align the stride here? |
| 567 | const uint8_t* buffer_y = _encodedData.payloadData; |
| 568 | const uint8_t* buffer_u = buffer_y + size_y; |
| 569 | const uint8_t* buffer_v = buffer_u + size_uv; |
| 570 | videoFrame.CreateFrame(size_y, buffer_y, |
| 571 | size_uv, buffer_u, |
| 572 | size_uv, buffer_v, |
| 573 | video_codec_info_.width, video_codec_info_.height, |
| 574 | video_codec_info_.height, half_width, half_width); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 575 | }else |
| 576 | { |
| 577 | // Set the timestamp manually since there is no timestamp in the file. |
| 578 | // Update timestam according to 90 kHz stream. |
| 579 | _encodedData.timeStamp += (90000 / video_codec_info_.maxFramerate); |
andresp@webrtc.org | 8fa436b | 2013-09-16 11:26:35 +0000 | [diff] [blame] | 580 | retVal = video_decoder_->Decode(videoFrame, _encodedData); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 581 | } |
| 582 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 583 | int64_t renderTimeMs = TickTime::MillisecondTimestamp(); |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 584 | videoFrame.set_render_time_ms(renderTimeMs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 585 | |
| 586 | // Indicate that the current frame in the encoded buffer is old/has |
| 587 | // already been read. |
| 588 | _encodedData.payloadSize = 0; |
| 589 | if( retVal == 0) |
| 590 | { |
| 591 | _decodedVideoFrames++; |
| 592 | } |
| 593 | return retVal; |
| 594 | } |
| 595 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 596 | int32_t VideoFilePlayerImpl::video_codec_info( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 597 | VideoCodec& videoCodec) const |
| 598 | { |
| 599 | if(video_codec_info_.plName[0] == 0) |
| 600 | { |
| 601 | return -1; |
| 602 | } |
| 603 | memcpy(&videoCodec, &video_codec_info_, sizeof(VideoCodec)); |
| 604 | return 0; |
| 605 | } |
| 606 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 607 | int32_t VideoFilePlayerImpl::TimeUntilNextVideoFrame() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 608 | { |
| 609 | if(_fileFormat != kFileFormatAviFile) |
| 610 | { |
| 611 | return -1; |
| 612 | } |
| 613 | if(!_fileModule.IsPlaying()) |
| 614 | { |
| 615 | return -1; |
| 616 | } |
| 617 | if(_encodedData.payloadSize <= 0) |
| 618 | { |
| 619 | // Read next frame from file. |
| 620 | CriticalSectionScoped lock( _critSec); |
| 621 | |
| 622 | if(_fileFormat == kFileFormatAviFile) |
| 623 | { |
| 624 | // Get next video frame |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 625 | uint32_t encodedBufferLengthInBytes = _encodedData.bufferSize; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 626 | if(_fileModule.PlayoutAVIVideoData( |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 627 | reinterpret_cast< int8_t*>(_encodedData.payloadData), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 628 | encodedBufferLengthInBytes) != 0) |
| 629 | { |
| 630 | WEBRTC_TRACE( |
| 631 | kTraceWarning, |
| 632 | kTraceVideo, |
| 633 | _instanceID, |
| 634 | "FilePlayerImpl::TimeUntilNextVideoFrame() error reading\ |
| 635 | video data"); |
| 636 | return -1; |
| 637 | } |
| 638 | _encodedData.payloadSize = encodedBufferLengthInBytes; |
| 639 | _encodedData.codec = video_codec_info_.codecType; |
| 640 | _numberOfFramesRead++; |
| 641 | |
| 642 | if(_accumulatedRenderTimeMs == 0) |
| 643 | { |
| 644 | _startTime = TickTime::Now(); |
| 645 | // This if-statement should only trigger once. |
| 646 | _accumulatedRenderTimeMs = 1; |
| 647 | } else { |
| 648 | // A full seconds worth of frames have been read. |
| 649 | if(_numberOfFramesRead % video_codec_info_.maxFramerate == 0) |
| 650 | { |
| 651 | // Frame rate is in frames per seconds. Frame length is |
| 652 | // calculated as an integer division which means it may |
| 653 | // be rounded down. Compensate for this every second. |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 654 | uint32_t rest = 1000%_frameLengthMS; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 655 | _accumulatedRenderTimeMs += rest; |
| 656 | } |
| 657 | _accumulatedRenderTimeMs += _frameLengthMS; |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 662 | int64_t timeToNextFrame; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 663 | if(_videoOnly) |
| 664 | { |
| 665 | timeToNextFrame = _accumulatedRenderTimeMs - |
| 666 | (TickTime::Now() - _startTime).Milliseconds(); |
| 667 | |
| 668 | } else { |
| 669 | // Synchronize with the audio stream instead of system clock. |
| 670 | timeToNextFrame = _accumulatedRenderTimeMs - _decodedLengthInMS; |
| 671 | } |
| 672 | if(timeToNextFrame < 0) |
| 673 | { |
| 674 | return 0; |
| 675 | |
| 676 | } else if(timeToNextFrame > 0x0fffffff) |
| 677 | { |
| 678 | // Wraparound or audio stream has gone to far ahead of the video stream. |
| 679 | return -1; |
| 680 | } |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 681 | return static_cast<int32_t>(timeToNextFrame); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 682 | } |
| 683 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 684 | int32_t VideoFilePlayerImpl::SetUpVideoDecoder() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 685 | { |
| 686 | if (_fileModule.VideoCodecInst(video_codec_info_) != 0) |
| 687 | { |
| 688 | WEBRTC_TRACE( |
| 689 | kTraceWarning, |
| 690 | kTraceVideo, |
| 691 | _instanceID, |
| 692 | "FilePlayerImpl::SetVideoDecoder() failed to retrieve Codec info of\ |
| 693 | file data."); |
| 694 | return -1; |
| 695 | } |
| 696 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 697 | int32_t useNumberOfCores = 1; |
andresp@webrtc.org | 8fa436b | 2013-09-16 11:26:35 +0000 | [diff] [blame] | 698 | if (video_decoder_->SetDecodeCodec(video_codec_info_, useNumberOfCores) != |
| 699 | 0) { |
| 700 | WEBRTC_TRACE(kTraceWarning, |
| 701 | kTraceVideo, |
| 702 | _instanceID, |
| 703 | "FilePlayerImpl::SetUpVideoDecoder() codec %s not supported", |
| 704 | video_codec_info_.plName); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 705 | return -1; |
| 706 | } |
| 707 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 708 | _frameLengthMS = 1000/video_codec_info_.maxFramerate; |
| 709 | |
| 710 | // Size of unencoded data (I420) should be the largest possible frame size |
| 711 | // in a file. |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 712 | const uint32_t KReadBufferSize = 3 * video_codec_info_.width * |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 713 | video_codec_info_.height / 2; |
| 714 | _encodedData.VerifyAndAllocate(KReadBufferSize); |
| 715 | _encodedData.encodedHeight = video_codec_info_.height; |
| 716 | _encodedData.encodedWidth = video_codec_info_.width; |
| 717 | _encodedData.payloadType = video_codec_info_.plType; |
| 718 | _encodedData.timeStamp = 0; |
| 719 | return 0; |
| 720 | } |
| 721 | #endif // WEBRTC_MODULE_UTILITY_VIDEO |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 722 | } // namespace webrtc |