niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
mflodman@webrtc.org | c80d9d9 | 2012-02-06 10:11:25 +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 | |
| 11 | #include <assert.h> |
| 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "modules/media_file/media_file_impl.h" |
| 14 | #include "rtc_base/format_macros.h" |
| 15 | #include "rtc_base/logging.h" |
| 16 | #include "system_wrappers/include/file_wrapper.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 17 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | namespace webrtc { |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 19 | MediaFile* MediaFile::CreateMediaFile(const int32_t id) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | return new MediaFileImpl(id); |
| 22 | } |
| 23 | |
| 24 | void MediaFile::DestroyMediaFile(MediaFile* module) |
| 25 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | delete static_cast<MediaFileImpl*>(module); |
| 27 | } |
| 28 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 29 | MediaFileImpl::MediaFileImpl(const int32_t id) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | : _id(id), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | _ptrFileUtilityObj(NULL), |
| 32 | codec_info_(), |
| 33 | _ptrInStream(NULL), |
| 34 | _ptrOutStream(NULL), |
| 35 | _fileFormat((FileFormats)-1), |
| 36 | _recordDurationMs(0), |
| 37 | _playoutPositionMs(0), |
| 38 | _notificationMs(0), |
| 39 | _playingActive(false), |
| 40 | _recordingActive(false), |
| 41 | _isStereo(false), |
| 42 | _openFile(false), |
| 43 | _fileName(), |
| 44 | _ptrCallback(NULL) |
| 45 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 46 | LOG(LS_INFO) << "MediaFileImpl()"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | |
| 48 | codec_info_.plname[0] = '\0'; |
| 49 | _fileName[0] = '\0'; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | MediaFileImpl::~MediaFileImpl() |
| 54 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 55 | LOG(LS_INFO) << "~MediaFileImpl()"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 56 | { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 57 | rtc::CritScope lock(&_crit); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 58 | |
| 59 | if(_playingActive) |
| 60 | { |
| 61 | StopPlaying(); |
| 62 | } |
| 63 | |
| 64 | if(_recordingActive) |
| 65 | { |
| 66 | StopRecording(); |
| 67 | } |
| 68 | |
| 69 | delete _ptrFileUtilityObj; |
| 70 | |
| 71 | if(_openFile) |
| 72 | { |
| 73 | delete _ptrInStream; |
| 74 | _ptrInStream = NULL; |
| 75 | delete _ptrOutStream; |
| 76 | _ptrOutStream = NULL; |
| 77 | } |
| 78 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 79 | } |
| 80 | |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 81 | int64_t MediaFileImpl::TimeUntilNextProcess() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 83 | LOG(LS_WARNING) |
| 84 | << "TimeUntilNextProcess: This method is not used by MediaFile class."; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 85 | return -1; |
| 86 | } |
| 87 | |
pbos | a26ac92 | 2016-02-25 04:50:01 -0800 | [diff] [blame] | 88 | void MediaFileImpl::Process() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 89 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 90 | LOG(LS_WARNING) << "Process: This method is not used by MediaFile class."; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 91 | } |
| 92 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 93 | int32_t MediaFileImpl::PlayoutAudioData(int8_t* buffer, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 94 | size_t& dataLengthInBytes) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 95 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 96 | LOG(LS_INFO) << "MediaFileImpl::PlayoutData(buffer= " |
| 97 | << static_cast<void*>(buffer) |
| 98 | << ", bufLen= " << dataLengthInBytes << ")"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 99 | |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 100 | const size_t bufferLengthInBytes = dataLengthInBytes; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 101 | dataLengthInBytes = 0; |
| 102 | |
| 103 | if(buffer == NULL || bufferLengthInBytes == 0) |
| 104 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 105 | LOG(LS_ERROR) << "Buffer pointer or length is NULL!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 106 | return -1; |
| 107 | } |
| 108 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 109 | int32_t bytesRead = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 110 | { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 111 | rtc::CritScope lock(&_crit); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 112 | |
| 113 | if(!_playingActive) |
| 114 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 115 | LOG(LS_WARNING) << "Not currently playing!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 116 | return -1; |
| 117 | } |
| 118 | |
| 119 | if(!_ptrFileUtilityObj) |
| 120 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 121 | LOG(LS_ERROR) << "Playing, but no FileUtility object!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 122 | StopPlaying(); |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | switch(_fileFormat) |
| 127 | { |
Minyue Li | 85a3b6b | 2017-09-01 14:36:33 +0200 | [diff] [blame] | 128 | case kFileFormatPcm48kHzFile: |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 129 | case kFileFormatPcm32kHzFile: |
| 130 | case kFileFormatPcm16kHzFile: |
| 131 | case kFileFormatPcm8kHzFile: |
| 132 | bytesRead = _ptrFileUtilityObj->ReadPCMData( |
| 133 | *_ptrInStream, |
| 134 | buffer, |
| 135 | bufferLengthInBytes); |
| 136 | break; |
| 137 | case kFileFormatCompressedFile: |
| 138 | bytesRead = _ptrFileUtilityObj->ReadCompressedData( |
| 139 | *_ptrInStream, |
| 140 | buffer, |
| 141 | bufferLengthInBytes); |
| 142 | break; |
| 143 | case kFileFormatWavFile: |
| 144 | bytesRead = _ptrFileUtilityObj->ReadWavDataAsMono( |
| 145 | *_ptrInStream, |
| 146 | buffer, |
| 147 | bufferLengthInBytes); |
| 148 | break; |
| 149 | case kFileFormatPreencodedFile: |
| 150 | bytesRead = _ptrFileUtilityObj->ReadPreEncodedData( |
| 151 | *_ptrInStream, |
| 152 | buffer, |
| 153 | bufferLengthInBytes); |
| 154 | if(bytesRead > 0) |
| 155 | { |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 156 | dataLengthInBytes = static_cast<size_t>(bytesRead); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 157 | return 0; |
| 158 | } |
| 159 | break; |
andresp@webrtc.org | e8f50df | 2015-03-02 13:07:02 +0000 | [diff] [blame] | 160 | default: |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 161 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 162 | LOG(LS_ERROR) << "Invalid file format: " << _fileFormat; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 163 | assert(false); |
| 164 | break; |
mflodman@webrtc.org | c80d9d9 | 2012-02-06 10:11:25 +0000 | [diff] [blame] | 165 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | if( bytesRead > 0) |
| 169 | { |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 170 | dataLengthInBytes = static_cast<size_t>(bytesRead); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | HandlePlayCallbacks(bytesRead); |
| 174 | return 0; |
| 175 | } |
| 176 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 177 | void MediaFileImpl::HandlePlayCallbacks(int32_t bytesRead) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 178 | { |
| 179 | bool playEnded = false; |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 180 | uint32_t callbackNotifyMs = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 181 | |
| 182 | if(bytesRead > 0) |
| 183 | { |
| 184 | // Check if it's time for PlayNotification(..). |
| 185 | _playoutPositionMs = _ptrFileUtilityObj->PlayoutPositionMs(); |
| 186 | if(_notificationMs) |
| 187 | { |
| 188 | if(_playoutPositionMs >= _notificationMs) |
| 189 | { |
| 190 | _notificationMs = 0; |
| 191 | callbackNotifyMs = _playoutPositionMs; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | else |
| 196 | { |
| 197 | // If no bytes were read assume end of file. |
| 198 | StopPlaying(); |
| 199 | playEnded = true; |
| 200 | } |
| 201 | |
| 202 | // Only _callbackCrit may and should be taken when making callbacks. |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 203 | rtc::CritScope lock(&_callbackCrit); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 204 | if(_ptrCallback) |
| 205 | { |
| 206 | if(callbackNotifyMs) |
| 207 | { |
| 208 | _ptrCallback->PlayNotification(_id, callbackNotifyMs); |
| 209 | } |
| 210 | if(playEnded) |
| 211 | { |
| 212 | _ptrCallback->PlayFileEnded(_id); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 217 | int32_t MediaFileImpl::PlayoutStereoData( |
| 218 | int8_t* bufferLeft, |
| 219 | int8_t* bufferRight, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 220 | size_t& dataLengthInBytes) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 221 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 222 | LOG(LS_INFO) |
| 223 | << "MediaFileImpl::PlayoutStereoData(Left = " |
| 224 | << static_cast<void*>(bufferLeft) << ", Right = " |
| 225 | << static_cast<void*>(bufferRight) << ", Len= " << dataLengthInBytes |
| 226 | << ")"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 227 | |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 228 | const size_t bufferLengthInBytes = dataLengthInBytes; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 229 | dataLengthInBytes = 0; |
| 230 | |
| 231 | if(bufferLeft == NULL || bufferRight == NULL || bufferLengthInBytes == 0) |
| 232 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 233 | LOG(LS_ERROR) << "A buffer pointer or the length is NULL!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 234 | return -1; |
| 235 | } |
| 236 | |
| 237 | bool playEnded = false; |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 238 | uint32_t callbackNotifyMs = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 239 | { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 240 | rtc::CritScope lock(&_crit); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 241 | |
| 242 | if(!_playingActive || !_isStereo) |
| 243 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 244 | LOG(LS_WARNING) << "Not currently playing stereo!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 245 | return -1; |
| 246 | } |
| 247 | |
| 248 | if(!_ptrFileUtilityObj) |
| 249 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 250 | LOG(LS_ERROR) |
| 251 | << "Playing stereo, but the FileUtility objects is NULL!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 252 | StopPlaying(); |
| 253 | return -1; |
| 254 | } |
| 255 | |
| 256 | // Stereo playout only supported for WAV files. |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 257 | int32_t bytesRead = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 258 | switch(_fileFormat) |
| 259 | { |
| 260 | case kFileFormatWavFile: |
| 261 | bytesRead = _ptrFileUtilityObj->ReadWavDataAsStereo( |
| 262 | *_ptrInStream, |
| 263 | bufferLeft, |
| 264 | bufferRight, |
| 265 | bufferLengthInBytes); |
| 266 | break; |
| 267 | default: |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 268 | LOG(LS_ERROR) |
| 269 | << "Trying to read non-WAV as stereo audio (not supported)"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 270 | break; |
| 271 | } |
| 272 | |
| 273 | if(bytesRead > 0) |
| 274 | { |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 275 | dataLengthInBytes = static_cast<size_t>(bytesRead); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 276 | |
| 277 | // Check if it's time for PlayNotification(..). |
| 278 | _playoutPositionMs = _ptrFileUtilityObj->PlayoutPositionMs(); |
| 279 | if(_notificationMs) |
| 280 | { |
| 281 | if(_playoutPositionMs >= _notificationMs) |
| 282 | { |
| 283 | _notificationMs = 0; |
| 284 | callbackNotifyMs = _playoutPositionMs; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | // If no bytes were read assume end of file. |
| 291 | StopPlaying(); |
| 292 | playEnded = true; |
| 293 | } |
| 294 | } |
| 295 | |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 296 | rtc::CritScope lock(&_callbackCrit); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 297 | if(_ptrCallback) |
| 298 | { |
| 299 | if(callbackNotifyMs) |
| 300 | { |
| 301 | _ptrCallback->PlayNotification(_id, callbackNotifyMs); |
| 302 | } |
| 303 | if(playEnded) |
| 304 | { |
| 305 | _ptrCallback->PlayFileEnded(_id); |
| 306 | } |
| 307 | } |
| 308 | return 0; |
| 309 | } |
| 310 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 311 | int32_t MediaFileImpl::StartPlayingAudioFile( |
leozwang@webrtc.org | 09e7719 | 2012-03-01 18:35:54 +0000 | [diff] [blame] | 312 | const char* fileName, |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 313 | const uint32_t notificationTimeMs, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 314 | const bool loop, |
| 315 | const FileFormats format, |
| 316 | const CodecInst* codecInst, |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 317 | const uint32_t startPointMs, |
| 318 | const uint32_t stopPointMs) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 319 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 320 | if(!ValidFileName(fileName)) |
| 321 | { |
| 322 | return -1; |
| 323 | } |
| 324 | if(!ValidFileFormat(format,codecInst)) |
| 325 | { |
| 326 | return -1; |
| 327 | } |
| 328 | if(!ValidFilePositions(startPointMs,stopPointMs)) |
| 329 | { |
| 330 | return -1; |
| 331 | } |
| 332 | |
| 333 | // Check that the file will play longer than notificationTimeMs ms. |
| 334 | if((startPointMs && stopPointMs && !loop) && |
| 335 | (notificationTimeMs > (stopPointMs - startPointMs))) |
| 336 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 337 | LOG(LS_ERROR) << "specified notification time is longer than amount of" |
| 338 | << " ms that will be played"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 339 | return -1; |
| 340 | } |
| 341 | |
| 342 | FileWrapper* inputStream = FileWrapper::Create(); |
| 343 | if(inputStream == NULL) |
| 344 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 345 | LOG(LS_INFO) << "Failed to allocate input stream for file " << fileName; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 346 | return -1; |
| 347 | } |
| 348 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 349 | if (!inputStream->OpenFile(fileName, true)) { |
| 350 | delete inputStream; |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 351 | LOG(LS_ERROR) << "Could not open input file " << fileName; |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 352 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 353 | } |
| 354 | |
andresp@webrtc.org | e8f50df | 2015-03-02 13:07:02 +0000 | [diff] [blame] | 355 | if(StartPlayingStream(*inputStream, loop, notificationTimeMs, |
| 356 | format, codecInst, startPointMs, stopPointMs) == -1) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 357 | { |
andresp@webrtc.org | e8f50df | 2015-03-02 13:07:02 +0000 | [diff] [blame] | 358 | inputStream->CloseFile(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 359 | delete inputStream; |
| 360 | return -1; |
| 361 | } |
| 362 | |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 363 | rtc::CritScope lock(&_crit); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 364 | _openFile = true; |
| 365 | strncpy(_fileName, fileName, sizeof(_fileName)); |
| 366 | _fileName[sizeof(_fileName) - 1] = '\0'; |
| 367 | return 0; |
| 368 | } |
| 369 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 370 | int32_t MediaFileImpl::StartPlayingAudioStream( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 371 | InStream& stream, |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 372 | const uint32_t notificationTimeMs, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 373 | const FileFormats format, |
| 374 | const CodecInst* codecInst, |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 375 | const uint32_t startPointMs, |
| 376 | const uint32_t stopPointMs) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 377 | { |
andresp@webrtc.org | e8f50df | 2015-03-02 13:07:02 +0000 | [diff] [blame] | 378 | return StartPlayingStream(stream, false, notificationTimeMs, format, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 379 | codecInst, startPointMs, stopPointMs); |
| 380 | } |
| 381 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 382 | int32_t MediaFileImpl::StartPlayingStream( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 383 | InStream& stream, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 384 | bool loop, |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 385 | const uint32_t notificationTimeMs, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 386 | const FileFormats format, |
| 387 | const CodecInst* codecInst, |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 388 | const uint32_t startPointMs, |
andresp@webrtc.org | e8f50df | 2015-03-02 13:07:02 +0000 | [diff] [blame] | 389 | const uint32_t stopPointMs) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 390 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 391 | if(!ValidFileFormat(format,codecInst)) |
| 392 | { |
| 393 | return -1; |
| 394 | } |
| 395 | |
| 396 | if(!ValidFilePositions(startPointMs,stopPointMs)) |
| 397 | { |
| 398 | return -1; |
| 399 | } |
| 400 | |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 401 | rtc::CritScope lock(&_crit); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 402 | if(_playingActive || _recordingActive) |
| 403 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 404 | LOG(LS_ERROR) |
| 405 | << "StartPlaying called, but already playing or recording file " |
| 406 | << ((_fileName[0] == '\0') ? "(name not set)" : _fileName); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 407 | return -1; |
| 408 | } |
| 409 | |
| 410 | if(_ptrFileUtilityObj != NULL) |
| 411 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 412 | LOG(LS_ERROR) |
| 413 | << "StartPlaying called, but FileUtilityObj already exists!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 414 | StopPlaying(); |
| 415 | return -1; |
| 416 | } |
| 417 | |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 418 | _ptrFileUtilityObj = new ModuleFileUtility(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 419 | if(_ptrFileUtilityObj == NULL) |
| 420 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 421 | LOG(LS_INFO) << "Failed to create FileUtilityObj!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 422 | return -1; |
| 423 | } |
| 424 | |
| 425 | switch(format) |
| 426 | { |
| 427 | case kFileFormatWavFile: |
| 428 | { |
| 429 | if(_ptrFileUtilityObj->InitWavReading(stream, startPointMs, |
| 430 | stopPointMs) == -1) |
| 431 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 432 | LOG(LS_ERROR) << "Not a valid WAV file!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 433 | StopPlaying(); |
| 434 | return -1; |
| 435 | } |
| 436 | _fileFormat = kFileFormatWavFile; |
| 437 | break; |
| 438 | } |
| 439 | case kFileFormatCompressedFile: |
| 440 | { |
| 441 | if(_ptrFileUtilityObj->InitCompressedReading(stream, startPointMs, |
| 442 | stopPointMs) == -1) |
| 443 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 444 | LOG(LS_ERROR) << "Not a valid Compressed file!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 445 | StopPlaying(); |
| 446 | return -1; |
| 447 | } |
| 448 | _fileFormat = kFileFormatCompressedFile; |
| 449 | break; |
| 450 | } |
| 451 | case kFileFormatPcm8kHzFile: |
| 452 | case kFileFormatPcm16kHzFile: |
| 453 | case kFileFormatPcm32kHzFile: |
Minyue Li | 85a3b6b | 2017-09-01 14:36:33 +0200 | [diff] [blame] | 454 | case kFileFormatPcm48kHzFile: |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 455 | { |
henrike@webrtc.org | 26085e1 | 2012-02-27 21:50:40 +0000 | [diff] [blame] | 456 | // ValidFileFormat() called in the beginneing of this function |
| 457 | // prevents codecInst from being NULL here. |
| 458 | assert(codecInst != NULL); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 459 | if(!ValidFrequency(codecInst->plfreq) || |
| 460 | _ptrFileUtilityObj->InitPCMReading(stream, startPointMs, |
| 461 | stopPointMs, |
| 462 | codecInst->plfreq) == -1) |
| 463 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 464 | LOG(LS_ERROR) << "Not a valid raw 8 or 16 KHz PCM file!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 465 | StopPlaying(); |
| 466 | return -1; |
| 467 | } |
| 468 | |
| 469 | _fileFormat = format; |
| 470 | break; |
| 471 | } |
| 472 | case kFileFormatPreencodedFile: |
| 473 | { |
henrike@webrtc.org | 26085e1 | 2012-02-27 21:50:40 +0000 | [diff] [blame] | 474 | // ValidFileFormat() called in the beginneing of this function |
| 475 | // prevents codecInst from being NULL here. |
| 476 | assert(codecInst != NULL); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 477 | if(_ptrFileUtilityObj->InitPreEncodedReading(stream, *codecInst) == |
| 478 | -1) |
| 479 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 480 | LOG(LS_ERROR) << "Not a valid PreEncoded file!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 481 | StopPlaying(); |
| 482 | return -1; |
| 483 | } |
| 484 | |
| 485 | _fileFormat = kFileFormatPreencodedFile; |
| 486 | break; |
| 487 | } |
andresp@webrtc.org | e8f50df | 2015-03-02 13:07:02 +0000 | [diff] [blame] | 488 | default: |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 489 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 490 | LOG(LS_ERROR) << "Invalid file format: " << format; |
mflodman@webrtc.org | c80d9d9 | 2012-02-06 10:11:25 +0000 | [diff] [blame] | 491 | assert(false); |
| 492 | break; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | if(_ptrFileUtilityObj->codec_info(codec_info_) == -1) |
| 496 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 497 | LOG(LS_ERROR) << "Failed to retrieve codec info!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 498 | StopPlaying(); |
| 499 | return -1; |
| 500 | } |
| 501 | |
| 502 | _isStereo = (codec_info_.channels == 2); |
| 503 | if(_isStereo && (_fileFormat != kFileFormatWavFile)) |
| 504 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 505 | LOG(LS_WARNING) << "Stereo is only allowed for WAV files"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 506 | StopPlaying(); |
| 507 | return -1; |
| 508 | } |
| 509 | _playingActive = true; |
| 510 | _playoutPositionMs = _ptrFileUtilityObj->PlayoutPositionMs(); |
| 511 | _ptrInStream = &stream; |
| 512 | _notificationMs = notificationTimeMs; |
| 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 517 | int32_t MediaFileImpl::StopPlaying() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 518 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 519 | |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 520 | rtc::CritScope lock(&_crit); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 521 | _isStereo = false; |
| 522 | if(_ptrFileUtilityObj) |
| 523 | { |
| 524 | delete _ptrFileUtilityObj; |
| 525 | _ptrFileUtilityObj = NULL; |
| 526 | } |
| 527 | if(_ptrInStream) |
| 528 | { |
| 529 | // If MediaFileImpl opened the InStream it must be reclaimed here. |
| 530 | if(_openFile) |
| 531 | { |
| 532 | delete _ptrInStream; |
| 533 | _openFile = false; |
| 534 | } |
| 535 | _ptrInStream = NULL; |
| 536 | } |
| 537 | |
| 538 | codec_info_.pltype = 0; |
| 539 | codec_info_.plname[0] = '\0'; |
| 540 | |
| 541 | if(!_playingActive) |
| 542 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 543 | LOG(LS_WARNING) << "playing is not active!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 544 | return -1; |
| 545 | } |
| 546 | |
| 547 | _playingActive = false; |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | bool MediaFileImpl::IsPlaying() |
| 552 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 553 | LOG(LS_VERBOSE) << "MediaFileImpl::IsPlaying()"; |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 554 | rtc::CritScope lock(&_crit); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 555 | return _playingActive; |
| 556 | } |
| 557 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 558 | int32_t MediaFileImpl::IncomingAudioData( |
| 559 | const int8_t* buffer, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 560 | const size_t bufferLengthInBytes) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 561 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 562 | LOG(LS_INFO) << "MediaFile::IncomingData(buffer= " |
| 563 | << static_cast<const void*>(buffer) << ", bufLen= " |
| 564 | << bufferLengthInBytes << ")"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 565 | |
| 566 | if(buffer == NULL || bufferLengthInBytes == 0) |
| 567 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 568 | LOG(LS_ERROR) << "Buffer pointer or length is NULL!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 569 | return -1; |
| 570 | } |
| 571 | |
| 572 | bool recordingEnded = false; |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 573 | uint32_t callbackNotifyMs = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 574 | { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 575 | rtc::CritScope lock(&_crit); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 576 | |
| 577 | if(!_recordingActive) |
| 578 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 579 | LOG(LS_WARNING) << "Not currently recording!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 580 | return -1; |
| 581 | } |
| 582 | if(_ptrOutStream == NULL) |
| 583 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 584 | LOG(LS_ERROR) << "Recording is active, but output stream is NULL!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 585 | assert(false); |
| 586 | return -1; |
| 587 | } |
| 588 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 589 | int32_t bytesWritten = 0; |
| 590 | uint32_t samplesWritten = codec_info_.pacsize; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 591 | if(_ptrFileUtilityObj) |
| 592 | { |
| 593 | switch(_fileFormat) |
| 594 | { |
| 595 | case kFileFormatPcm8kHzFile: |
| 596 | case kFileFormatPcm16kHzFile: |
| 597 | case kFileFormatPcm32kHzFile: |
Minyue Li | 85a3b6b | 2017-09-01 14:36:33 +0200 | [diff] [blame] | 598 | case kFileFormatPcm48kHzFile: |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 599 | bytesWritten = _ptrFileUtilityObj->WritePCMData( |
| 600 | *_ptrOutStream, |
| 601 | buffer, |
| 602 | bufferLengthInBytes); |
| 603 | |
| 604 | // Sample size is 2 bytes. |
| 605 | if(bytesWritten > 0) |
| 606 | { |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 607 | samplesWritten = bytesWritten/sizeof(int16_t); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 608 | } |
| 609 | break; |
| 610 | case kFileFormatCompressedFile: |
| 611 | bytesWritten = _ptrFileUtilityObj->WriteCompressedData( |
| 612 | *_ptrOutStream, buffer, bufferLengthInBytes); |
| 613 | break; |
| 614 | case kFileFormatWavFile: |
| 615 | bytesWritten = _ptrFileUtilityObj->WriteWavData( |
| 616 | *_ptrOutStream, |
| 617 | buffer, |
| 618 | bufferLengthInBytes); |
| 619 | if(bytesWritten > 0 && STR_NCASE_CMP(codec_info_.plname, |
| 620 | "L16", 4) == 0) |
| 621 | { |
| 622 | // Sample size is 2 bytes. |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 623 | samplesWritten = bytesWritten/sizeof(int16_t); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 624 | } |
| 625 | break; |
| 626 | case kFileFormatPreencodedFile: |
| 627 | bytesWritten = _ptrFileUtilityObj->WritePreEncodedData( |
| 628 | *_ptrOutStream, buffer, bufferLengthInBytes); |
| 629 | break; |
andresp@webrtc.org | e8f50df | 2015-03-02 13:07:02 +0000 | [diff] [blame] | 630 | default: |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 631 | LOG(LS_ERROR) << "Invalid file format: " << _fileFormat; |
mflodman@webrtc.org | c80d9d9 | 2012-02-06 10:11:25 +0000 | [diff] [blame] | 632 | assert(false); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 633 | break; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 634 | } |
| 635 | } else { |
| 636 | // TODO (hellner): quick look at the code makes me think that this |
| 637 | // code is never executed. Remove? |
| 638 | if(_ptrOutStream) |
| 639 | { |
| 640 | if(_ptrOutStream->Write(buffer, bufferLengthInBytes)) |
| 641 | { |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 642 | bytesWritten = static_cast<int32_t>(bufferLengthInBytes); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
andresp@webrtc.org | e8f50df | 2015-03-02 13:07:02 +0000 | [diff] [blame] | 647 | _recordDurationMs += samplesWritten / (codec_info_.plfreq / 1000); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 648 | |
| 649 | // Check if it's time for RecordNotification(..). |
| 650 | if(_notificationMs) |
| 651 | { |
| 652 | if(_recordDurationMs >= _notificationMs) |
| 653 | { |
| 654 | _notificationMs = 0; |
| 655 | callbackNotifyMs = _recordDurationMs; |
| 656 | } |
| 657 | } |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 658 | if(bytesWritten < (int32_t)bufferLengthInBytes) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 659 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 660 | LOG(LS_WARNING) << "Failed to write all requested bytes!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 661 | StopRecording(); |
| 662 | recordingEnded = true; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | // Only _callbackCrit may and should be taken when making callbacks. |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 667 | rtc::CritScope lock(&_callbackCrit); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 668 | if(_ptrCallback) |
| 669 | { |
| 670 | if(callbackNotifyMs) |
| 671 | { |
| 672 | _ptrCallback->RecordNotification(_id, callbackNotifyMs); |
| 673 | } |
| 674 | if(recordingEnded) |
| 675 | { |
| 676 | _ptrCallback->RecordFileEnded(_id); |
| 677 | return -1; |
| 678 | } |
| 679 | } |
| 680 | return 0; |
| 681 | } |
| 682 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 683 | int32_t MediaFileImpl::StartRecordingAudioFile( |
leozwang@webrtc.org | 09e7719 | 2012-03-01 18:35:54 +0000 | [diff] [blame] | 684 | const char* fileName, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 685 | const FileFormats format, |
| 686 | const CodecInst& codecInst, |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 687 | const uint32_t notificationTimeMs, |
| 688 | const uint32_t maxSizeBytes) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 689 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 690 | if(!ValidFileName(fileName)) |
| 691 | { |
| 692 | return -1; |
| 693 | } |
| 694 | if(!ValidFileFormat(format,&codecInst)) |
| 695 | { |
| 696 | return -1; |
| 697 | } |
| 698 | |
| 699 | FileWrapper* outputStream = FileWrapper::Create(); |
| 700 | if(outputStream == NULL) |
| 701 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 702 | LOG(LS_INFO) << "Failed to allocate memory for output stream"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 703 | return -1; |
| 704 | } |
| 705 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 706 | if (!outputStream->OpenFile(fileName, false)) { |
| 707 | delete outputStream; |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 708 | LOG(LS_ERROR) << "Could not open output file '" << fileName |
| 709 | << "' for writing!"; |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 710 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 711 | } |
andresp@webrtc.org | e8f50df | 2015-03-02 13:07:02 +0000 | [diff] [blame] | 712 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 713 | if(maxSizeBytes) |
| 714 | { |
| 715 | outputStream->SetMaxFileSize(maxSizeBytes); |
| 716 | } |
| 717 | |
andresp@webrtc.org | e8f50df | 2015-03-02 13:07:02 +0000 | [diff] [blame] | 718 | if(StartRecordingAudioStream(*outputStream, format, codecInst, |
| 719 | notificationTimeMs) == -1) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 720 | { |
andresp@webrtc.org | e8f50df | 2015-03-02 13:07:02 +0000 | [diff] [blame] | 721 | outputStream->CloseFile(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 722 | delete outputStream; |
| 723 | return -1; |
| 724 | } |
| 725 | |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 726 | rtc::CritScope lock(&_crit); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 727 | _openFile = true; |
| 728 | strncpy(_fileName, fileName, sizeof(_fileName)); |
| 729 | _fileName[sizeof(_fileName) - 1] = '\0'; |
| 730 | return 0; |
| 731 | } |
| 732 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 733 | int32_t MediaFileImpl::StartRecordingAudioStream( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 734 | OutStream& stream, |
| 735 | const FileFormats format, |
| 736 | const CodecInst& codecInst, |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 737 | const uint32_t notificationTimeMs) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 738 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 739 | // Check codec info |
| 740 | if(!ValidFileFormat(format,&codecInst)) |
| 741 | { |
| 742 | return -1; |
| 743 | } |
| 744 | |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 745 | rtc::CritScope lock(&_crit); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 746 | if(_recordingActive || _playingActive) |
| 747 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 748 | LOG(LS_ERROR) |
| 749 | << "StartRecording called, but already recording or playing file " |
| 750 | << _fileName << "!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 751 | return -1; |
| 752 | } |
| 753 | |
| 754 | if(_ptrFileUtilityObj != NULL) |
| 755 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 756 | LOG(LS_ERROR) |
| 757 | << "StartRecording called, but fileUtilityObj already exists!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 758 | StopRecording(); |
| 759 | return -1; |
| 760 | } |
| 761 | |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 762 | _ptrFileUtilityObj = new ModuleFileUtility(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 763 | if(_ptrFileUtilityObj == NULL) |
| 764 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 765 | LOG(LS_INFO) << "Cannot allocate fileUtilityObj!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 766 | return -1; |
| 767 | } |
| 768 | |
| 769 | CodecInst tmpAudioCodec; |
| 770 | memcpy(&tmpAudioCodec, &codecInst, sizeof(CodecInst)); |
| 771 | switch(format) |
| 772 | { |
| 773 | case kFileFormatWavFile: |
| 774 | { |
| 775 | if(_ptrFileUtilityObj->InitWavWriting(stream, codecInst) == -1) |
| 776 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 777 | LOG(LS_ERROR) << "Failed to initialize WAV file!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 778 | delete _ptrFileUtilityObj; |
| 779 | _ptrFileUtilityObj = NULL; |
| 780 | return -1; |
| 781 | } |
| 782 | _fileFormat = kFileFormatWavFile; |
| 783 | break; |
| 784 | } |
| 785 | case kFileFormatCompressedFile: |
| 786 | { |
| 787 | // Write compression codec name at beginning of file |
| 788 | if(_ptrFileUtilityObj->InitCompressedWriting(stream, codecInst) == |
| 789 | -1) |
| 790 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 791 | LOG(LS_ERROR) << "Failed to initialize Compressed file!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 792 | delete _ptrFileUtilityObj; |
| 793 | _ptrFileUtilityObj = NULL; |
| 794 | return -1; |
| 795 | } |
| 796 | _fileFormat = kFileFormatCompressedFile; |
| 797 | break; |
| 798 | } |
| 799 | case kFileFormatPcm8kHzFile: |
| 800 | case kFileFormatPcm16kHzFile: |
Minyue Li | 85a3b6b | 2017-09-01 14:36:33 +0200 | [diff] [blame] | 801 | case kFileFormatPcm32kHzFile: |
| 802 | case kFileFormatPcm48kHzFile: |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 803 | { |
| 804 | if(!ValidFrequency(codecInst.plfreq) || |
| 805 | _ptrFileUtilityObj->InitPCMWriting(stream, codecInst.plfreq) == |
| 806 | -1) |
| 807 | { |
Minyue Li | 85a3b6b | 2017-09-01 14:36:33 +0200 | [diff] [blame] | 808 | LOG(LS_ERROR) << "Failed to initialize PCM file!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 809 | delete _ptrFileUtilityObj; |
| 810 | _ptrFileUtilityObj = NULL; |
| 811 | return -1; |
| 812 | } |
| 813 | _fileFormat = format; |
| 814 | break; |
| 815 | } |
| 816 | case kFileFormatPreencodedFile: |
| 817 | { |
| 818 | if(_ptrFileUtilityObj->InitPreEncodedWriting(stream, codecInst) == |
| 819 | -1) |
| 820 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 821 | LOG(LS_ERROR) << "Failed to initialize Pre-Encoded file!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 822 | delete _ptrFileUtilityObj; |
| 823 | _ptrFileUtilityObj = NULL; |
| 824 | return -1; |
| 825 | } |
| 826 | |
| 827 | _fileFormat = kFileFormatPreencodedFile; |
| 828 | break; |
| 829 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 830 | default: |
| 831 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 832 | LOG(LS_ERROR) << "Invalid file format " << format << " specified!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 833 | delete _ptrFileUtilityObj; |
| 834 | _ptrFileUtilityObj = NULL; |
| 835 | return -1; |
| 836 | } |
| 837 | } |
| 838 | _isStereo = (tmpAudioCodec.channels == 2); |
| 839 | if(_isStereo) |
| 840 | { |
| 841 | if(_fileFormat != kFileFormatWavFile) |
| 842 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 843 | LOG(LS_WARNING) << "Stereo is only allowed for WAV files"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 844 | StopRecording(); |
| 845 | return -1; |
| 846 | } |
niklas.enbom@webrtc.org | 87885e8 | 2012-02-07 14:48:59 +0000 | [diff] [blame] | 847 | if((STR_NCASE_CMP(tmpAudioCodec.plname, "L16", 4) != 0) && |
| 848 | (STR_NCASE_CMP(tmpAudioCodec.plname, "PCMU", 5) != 0) && |
| 849 | (STR_NCASE_CMP(tmpAudioCodec.plname, "PCMA", 5) != 0)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 850 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 851 | LOG(LS_WARNING) |
| 852 | << "Stereo is only allowed for codec PCMU, PCMA and L16 "; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 853 | StopRecording(); |
| 854 | return -1; |
| 855 | } |
| 856 | } |
| 857 | memcpy(&codec_info_, &tmpAudioCodec, sizeof(CodecInst)); |
| 858 | _recordingActive = true; |
| 859 | _ptrOutStream = &stream; |
| 860 | _notificationMs = notificationTimeMs; |
| 861 | _recordDurationMs = 0; |
| 862 | return 0; |
| 863 | } |
| 864 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 865 | int32_t MediaFileImpl::StopRecording() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 866 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 867 | |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 868 | rtc::CritScope lock(&_crit); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 869 | if(!_recordingActive) |
| 870 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 871 | LOG(LS_WARNING) << "recording is not active!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 872 | return -1; |
| 873 | } |
| 874 | |
| 875 | _isStereo = false; |
| 876 | |
| 877 | if(_ptrFileUtilityObj != NULL) |
| 878 | { |
| 879 | // Both AVI and WAV header has to be updated before closing the stream |
| 880 | // because they contain size information. |
| 881 | if((_fileFormat == kFileFormatWavFile) && |
| 882 | (_ptrOutStream != NULL)) |
| 883 | { |
| 884 | _ptrFileUtilityObj->UpdateWavHeader(*_ptrOutStream); |
| 885 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 886 | delete _ptrFileUtilityObj; |
| 887 | _ptrFileUtilityObj = NULL; |
| 888 | } |
| 889 | |
| 890 | if(_ptrOutStream != NULL) |
| 891 | { |
| 892 | // If MediaFileImpl opened the OutStream it must be reclaimed here. |
| 893 | if(_openFile) |
| 894 | { |
| 895 | delete _ptrOutStream; |
| 896 | _openFile = false; |
| 897 | } |
| 898 | _ptrOutStream = NULL; |
| 899 | } |
| 900 | |
| 901 | _recordingActive = false; |
| 902 | codec_info_.pltype = 0; |
| 903 | codec_info_.plname[0] = '\0'; |
| 904 | |
| 905 | return 0; |
| 906 | } |
| 907 | |
| 908 | bool MediaFileImpl::IsRecording() |
| 909 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 910 | LOG(LS_VERBOSE) << "MediaFileImpl::IsRecording()"; |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 911 | rtc::CritScope lock(&_crit); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 912 | return _recordingActive; |
| 913 | } |
| 914 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 915 | int32_t MediaFileImpl::RecordDurationMs(uint32_t& durationMs) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 916 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 917 | |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 918 | rtc::CritScope lock(&_crit); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 919 | if(!_recordingActive) |
| 920 | { |
| 921 | durationMs = 0; |
| 922 | return -1; |
| 923 | } |
| 924 | durationMs = _recordDurationMs; |
| 925 | return 0; |
| 926 | } |
| 927 | |
| 928 | bool MediaFileImpl::IsStereo() |
| 929 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 930 | LOG(LS_VERBOSE) << "MediaFileImpl::IsStereo()"; |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 931 | rtc::CritScope lock(&_crit); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 932 | return _isStereo; |
| 933 | } |
| 934 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 935 | int32_t MediaFileImpl::SetModuleFileCallback(FileCallback* callback) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 936 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 937 | |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 938 | rtc::CritScope lock(&_callbackCrit); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 939 | |
| 940 | _ptrCallback = callback; |
| 941 | return 0; |
| 942 | } |
| 943 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 944 | int32_t MediaFileImpl::FileDurationMs(const char* fileName, |
| 945 | uint32_t& durationMs, |
| 946 | const FileFormats format, |
| 947 | const uint32_t freqInHz) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 948 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 949 | |
| 950 | if(!ValidFileName(fileName)) |
| 951 | { |
| 952 | return -1; |
| 953 | } |
| 954 | if(!ValidFrequency(freqInHz)) |
| 955 | { |
| 956 | return -1; |
| 957 | } |
| 958 | |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 959 | ModuleFileUtility* utilityObj = new ModuleFileUtility(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 960 | if(utilityObj == NULL) |
| 961 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 962 | LOG(LS_ERROR) << "failed to allocate utility object!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 963 | return -1; |
| 964 | } |
| 965 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 966 | const int32_t duration = utilityObj->FileDurationMs(fileName, format, |
| 967 | freqInHz); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 968 | delete utilityObj; |
| 969 | if(duration == -1) |
| 970 | { |
| 971 | durationMs = 0; |
| 972 | return -1; |
| 973 | } |
| 974 | |
| 975 | durationMs = duration; |
| 976 | return 0; |
| 977 | } |
| 978 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 979 | int32_t MediaFileImpl::PlayoutPositionMs(uint32_t& positionMs) const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 980 | { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 981 | rtc::CritScope lock(&_crit); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 982 | if(!_playingActive) |
| 983 | { |
| 984 | positionMs = 0; |
| 985 | return -1; |
| 986 | } |
| 987 | positionMs = _playoutPositionMs; |
| 988 | return 0; |
| 989 | } |
| 990 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 991 | int32_t MediaFileImpl::codec_info(CodecInst& codecInst) const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 992 | { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 993 | rtc::CritScope lock(&_crit); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 994 | if(!_playingActive && !_recordingActive) |
| 995 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 996 | LOG(LS_ERROR) << "Neither playout nor recording has been initialized!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 997 | return -1; |
| 998 | } |
| 999 | if (codec_info_.pltype == 0 && codec_info_.plname[0] == '\0') |
| 1000 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 1001 | LOG(LS_ERROR) << "The CodecInst for " |
| 1002 | << (_playingActive ? "Playback" : "Recording") |
| 1003 | << " is unknown!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1004 | return -1; |
| 1005 | } |
| 1006 | memcpy(&codecInst,&codec_info_,sizeof(CodecInst)); |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1010 | bool MediaFileImpl::ValidFileFormat(const FileFormats format, |
| 1011 | const CodecInst* codecInst) |
| 1012 | { |
| 1013 | if(codecInst == NULL) |
| 1014 | { |
| 1015 | if(format == kFileFormatPreencodedFile || |
| 1016 | format == kFileFormatPcm8kHzFile || |
| 1017 | format == kFileFormatPcm16kHzFile || |
Minyue Li | 85a3b6b | 2017-09-01 14:36:33 +0200 | [diff] [blame] | 1018 | format == kFileFormatPcm32kHzFile || |
| 1019 | format == kFileFormatPcm48kHzFile) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1020 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 1021 | LOG(LS_ERROR) << "Codec info required for file format specified!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1022 | return false; |
| 1023 | } |
| 1024 | } |
| 1025 | return true; |
| 1026 | } |
| 1027 | |
leozwang@webrtc.org | 09e7719 | 2012-03-01 18:35:54 +0000 | [diff] [blame] | 1028 | bool MediaFileImpl::ValidFileName(const char* fileName) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1029 | { |
| 1030 | if((fileName == NULL) ||(fileName[0] == '\0')) |
| 1031 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 1032 | LOG(LS_ERROR) << "FileName not specified!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1033 | return false; |
| 1034 | } |
| 1035 | return true; |
| 1036 | } |
| 1037 | |
| 1038 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 1039 | bool MediaFileImpl::ValidFilePositions(const uint32_t startPointMs, |
| 1040 | const uint32_t stopPointMs) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1041 | { |
| 1042 | if(startPointMs == 0 && stopPointMs == 0) // Default values |
| 1043 | { |
| 1044 | return true; |
| 1045 | } |
| 1046 | if(stopPointMs &&(startPointMs >= stopPointMs)) |
| 1047 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 1048 | LOG(LS_ERROR) << "startPointMs must be less than stopPointMs!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1049 | return false; |
| 1050 | } |
| 1051 | if(stopPointMs &&((stopPointMs - startPointMs) < 20)) |
| 1052 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame] | 1053 | LOG(LS_ERROR) << "minimum play duration for files is 20 ms!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1054 | return false; |
| 1055 | } |
| 1056 | return true; |
| 1057 | } |
| 1058 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 1059 | bool MediaFileImpl::ValidFrequency(const uint32_t frequency) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1060 | { |
Minyue Li | 85a3b6b | 2017-09-01 14:36:33 +0200 | [diff] [blame] | 1061 | if((frequency == 8000) || (frequency == 16000)|| (frequency == 32000) || |
| 1062 | (frequency == 48000)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1063 | { |
| 1064 | return true; |
| 1065 | } |
Minyue Li | 85a3b6b | 2017-09-01 14:36:33 +0200 | [diff] [blame] | 1066 | LOG(LS_ERROR) << "Frequency should be 8000, 16000, 32000, or 48000 (Hz)"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1067 | return false; |
| 1068 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 1069 | } // namespace webrtc |