OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 #include "webrtc/base/logging.h" | 10 #include "webrtc/base/logging.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 _recordingFramesLeft(0), | 32 _recordingFramesLeft(0), |
33 _playoutFramesLeft(0), | 33 _playoutFramesLeft(0), |
34 _critSect(*CriticalSectionWrapper::CreateCriticalSection()), | 34 _critSect(*CriticalSectionWrapper::CreateCriticalSection()), |
35 _recordingBufferSizeIn10MS(0), | 35 _recordingBufferSizeIn10MS(0), |
36 _recordingFramesIn10MS(0), | 36 _recordingFramesIn10MS(0), |
37 _playoutFramesIn10MS(0), | 37 _playoutFramesIn10MS(0), |
38 _playing(false), | 38 _playing(false), |
39 _recording(false), | 39 _recording(false), |
40 _lastCallPlayoutMillis(0), | 40 _lastCallPlayoutMillis(0), |
41 _lastCallRecordMillis(0), | 41 _lastCallRecordMillis(0), |
| 42 _outputFile(*FileWrapper::Create()), |
| 43 _inputFile(*FileWrapper::Create()), |
42 _outputFilename(outputFilename), | 44 _outputFilename(outputFilename), |
43 _inputFilename(inputFilename), | 45 _inputFilename(inputFilename), |
44 _clock(Clock::GetRealTimeClock()) { | 46 _clock(Clock::GetRealTimeClock()) { |
45 } | 47 } |
46 | 48 |
| 49 FileAudioDevice::~FileAudioDevice() { |
| 50 delete &_outputFile; |
| 51 delete &_inputFile; |
| 52 } |
| 53 |
47 int32_t FileAudioDevice::ActiveAudioLayer( | 54 int32_t FileAudioDevice::ActiveAudioLayer( |
48 AudioDeviceModule::AudioLayer& audioLayer) const { | 55 AudioDeviceModule::AudioLayer& audioLayer) const { |
49 return -1; | 56 return -1; |
50 } | 57 } |
51 | 58 |
52 AudioDeviceGeneric::InitStatus FileAudioDevice::Init() { | 59 AudioDeviceGeneric::InitStatus FileAudioDevice::Init() { |
53 return InitStatus::OK; | 60 return InitStatus::OK; |
54 } | 61 } |
55 | 62 |
56 int32_t FileAudioDevice::Terminate() { return 0; } | 63 int32_t FileAudioDevice::Terminate() { return 0; } |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 | 190 |
184 if (!_playoutBuffer) { | 191 if (!_playoutBuffer) { |
185 _playoutBuffer = new int8_t[kPlayoutBufferSize]; | 192 _playoutBuffer = new int8_t[kPlayoutBufferSize]; |
186 } | 193 } |
187 if (!_playoutBuffer) { | 194 if (!_playoutBuffer) { |
188 _playing = false; | 195 _playing = false; |
189 return -1; | 196 return -1; |
190 } | 197 } |
191 | 198 |
192 // PLAYOUT | 199 // PLAYOUT |
193 if (!_outputFilename.empty()) { | 200 if (!_outputFilename.empty() && |
194 _outputFile = rtc::File::Open(_outputFilename); | 201 !_outputFile.OpenFile(_outputFilename.c_str(), false)) { |
195 if (!_outputFile.IsOpen()) { | 202 LOG(LS_ERROR) << "Failed to open playout file: " << _outputFilename; |
196 LOG(LS_ERROR) << "Failed to open playout file: " << _outputFilename; | 203 _playing = false; |
197 _playing = false; | 204 delete [] _playoutBuffer; |
198 delete[] _playoutBuffer; | 205 _playoutBuffer = NULL; |
199 _playoutBuffer = NULL; | 206 return -1; |
200 return -1; | |
201 } | |
202 } | 207 } |
203 | 208 |
204 _ptrThreadPlay.reset(new rtc::PlatformThread( | 209 _ptrThreadPlay.reset(new rtc::PlatformThread( |
205 PlayThreadFunc, this, "webrtc_audio_module_play_thread")); | 210 PlayThreadFunc, this, "webrtc_audio_module_play_thread")); |
206 _ptrThreadPlay->Start(); | 211 _ptrThreadPlay->Start(); |
207 _ptrThreadPlay->SetPriority(rtc::kRealtimePriority); | 212 _ptrThreadPlay->SetPriority(rtc::kRealtimePriority); |
208 | 213 |
209 LOG(LS_INFO) << "Started playout capture to output file: " | 214 LOG(LS_INFO) << "Started playout capture to output file: " |
210 << _outputFilename; | 215 << _outputFilename; |
211 return 0; | 216 return 0; |
212 } | 217 } |
213 | 218 |
214 int32_t FileAudioDevice::StopPlayout() { | 219 int32_t FileAudioDevice::StopPlayout() { |
215 { | 220 { |
216 CriticalSectionScoped lock(&_critSect); | 221 CriticalSectionScoped lock(&_critSect); |
217 _playing = false; | 222 _playing = false; |
218 } | 223 } |
219 | 224 |
220 // stop playout thread first | 225 // stop playout thread first |
221 if (_ptrThreadPlay) { | 226 if (_ptrThreadPlay) { |
222 _ptrThreadPlay->Stop(); | 227 _ptrThreadPlay->Stop(); |
223 _ptrThreadPlay.reset(); | 228 _ptrThreadPlay.reset(); |
224 } | 229 } |
225 | 230 |
226 CriticalSectionScoped lock(&_critSect); | 231 CriticalSectionScoped lock(&_critSect); |
227 | 232 |
228 _playoutFramesLeft = 0; | 233 _playoutFramesLeft = 0; |
229 delete [] _playoutBuffer; | 234 delete [] _playoutBuffer; |
230 _playoutBuffer = NULL; | 235 _playoutBuffer = NULL; |
231 _outputFile.Close(); | 236 _outputFile.CloseFile(); |
232 | 237 |
233 LOG(LS_INFO) << "Stopped playout capture to output file: " | 238 LOG(LS_INFO) << "Stopped playout capture to output file: " |
234 << _outputFilename; | 239 << _outputFilename; |
235 return 0; | 240 return 0; |
236 } | 241 } |
237 | 242 |
238 bool FileAudioDevice::Playing() const { | 243 bool FileAudioDevice::Playing() const { |
239 return true; | 244 return true; |
240 } | 245 } |
241 | 246 |
242 int32_t FileAudioDevice::StartRecording() { | 247 int32_t FileAudioDevice::StartRecording() { |
243 _recording = true; | 248 _recording = true; |
244 | 249 |
245 // Make sure we only create the buffer once. | 250 // Make sure we only create the buffer once. |
246 _recordingBufferSizeIn10MS = _recordingFramesIn10MS * | 251 _recordingBufferSizeIn10MS = _recordingFramesIn10MS * |
247 kRecordingNumChannels * | 252 kRecordingNumChannels * |
248 2; | 253 2; |
249 if (!_recordingBuffer) { | 254 if (!_recordingBuffer) { |
250 _recordingBuffer = new int8_t[_recordingBufferSizeIn10MS]; | 255 _recordingBuffer = new int8_t[_recordingBufferSizeIn10MS]; |
251 } | 256 } |
252 | 257 |
253 if (!_inputFilename.empty()) { | 258 if (!_inputFilename.empty() && |
254 _inputFile = rtc::File::Open(_inputFilename); | 259 !_inputFile.OpenFile(_inputFilename.c_str(), true)) { |
255 if (!_inputFile.IsOpen()) { | 260 LOG(LS_ERROR) << "Failed to open audio input file: " << _inputFilename; |
256 LOG(LS_ERROR) << "Failed to open audio input file: " << _inputFilename; | 261 _recording = false; |
257 _recording = false; | 262 delete[] _recordingBuffer; |
258 delete[] _recordingBuffer; | 263 _recordingBuffer = NULL; |
259 _recordingBuffer = NULL; | 264 return -1; |
260 return -1; | |
261 } | |
262 } | 265 } |
263 | 266 |
264 _ptrThreadRec.reset(new rtc::PlatformThread( | 267 _ptrThreadRec.reset(new rtc::PlatformThread( |
265 RecThreadFunc, this, "webrtc_audio_module_capture_thread")); | 268 RecThreadFunc, this, "webrtc_audio_module_capture_thread")); |
266 | 269 |
267 _ptrThreadRec->Start(); | 270 _ptrThreadRec->Start(); |
268 _ptrThreadRec->SetPriority(rtc::kRealtimePriority); | 271 _ptrThreadRec->SetPriority(rtc::kRealtimePriority); |
269 | 272 |
270 LOG(LS_INFO) << "Started recording from input file: " | 273 LOG(LS_INFO) << "Started recording from input file: " |
271 << _inputFilename; | 274 << _inputFilename; |
(...skipping 12 matching lines...) Expand all Loading... |
284 _ptrThreadRec->Stop(); | 287 _ptrThreadRec->Stop(); |
285 _ptrThreadRec.reset(); | 288 _ptrThreadRec.reset(); |
286 } | 289 } |
287 | 290 |
288 CriticalSectionScoped lock(&_critSect); | 291 CriticalSectionScoped lock(&_critSect); |
289 _recordingFramesLeft = 0; | 292 _recordingFramesLeft = 0; |
290 if (_recordingBuffer) { | 293 if (_recordingBuffer) { |
291 delete [] _recordingBuffer; | 294 delete [] _recordingBuffer; |
292 _recordingBuffer = NULL; | 295 _recordingBuffer = NULL; |
293 } | 296 } |
294 _inputFile.Close(); | 297 _inputFile.CloseFile(); |
295 | 298 |
296 LOG(LS_INFO) << "Stopped recording from input file: " | 299 LOG(LS_INFO) << "Stopped recording from input file: " |
297 << _inputFilename; | 300 << _inputFilename; |
298 return 0; | 301 return 0; |
299 } | 302 } |
300 | 303 |
301 bool FileAudioDevice::Recording() const { | 304 bool FileAudioDevice::Recording() const { |
302 return _recording; | 305 return _recording; |
303 } | 306 } |
304 | 307 |
305 int32_t FileAudioDevice::SetAGC(bool enable) { return -1; } | 308 int32_t FileAudioDevice::SetAGC(bool enable) { return -1; } |
306 | 309 |
307 bool FileAudioDevice::AGC() const { return false; } | 310 bool FileAudioDevice::AGC() const { return false; } |
308 | 311 |
309 int32_t FileAudioDevice::SetWaveOutVolume(uint16_t volumeLeft, | 312 int32_t FileAudioDevice::SetWaveOutVolume(uint16_t volumeLeft, |
310 uint16_t volumeRight) { | 313 uint16_t volumeRight) { |
311 return -1; | 314 return -1; |
312 } | 315 } |
313 | 316 |
314 int32_t FileAudioDevice::WaveOutVolume(uint16_t& volumeLeft, | 317 int32_t FileAudioDevice::WaveOutVolume(uint16_t& volumeLeft, |
315 uint16_t& volumeRight) const { | 318 uint16_t& volumeRight) const { |
316 return -1; | 319 return -1; |
317 } | 320 } |
318 | 321 |
319 int32_t FileAudioDevice::InitSpeaker() { return -1; } | 322 int32_t FileAudioDevice::InitSpeaker() { return -1; } |
320 | 323 |
321 bool FileAudioDevice::SpeakerIsInitialized() const { return false; } | 324 bool FileAudioDevice::SpeakerIsInitialized() const { return false; } |
322 | 325 |
323 int32_t FileAudioDevice::InitMicrophone() { return 0; } | 326 int32_t FileAudioDevice::InitMicrophone() { return 0; } |
324 | 327 |
325 bool FileAudioDevice::MicrophoneIsInitialized() const { return true; } | 328 bool FileAudioDevice::MicrophoneIsInitialized() const { return true; } |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 | 461 |
459 // Inform the AudioBuffer about default settings for this implementation. | 462 // Inform the AudioBuffer about default settings for this implementation. |
460 // Set all values to zero here since the actual settings will be done by | 463 // Set all values to zero here since the actual settings will be done by |
461 // InitPlayout and InitRecording later. | 464 // InitPlayout and InitRecording later. |
462 _ptrAudioBuffer->SetRecordingSampleRate(0); | 465 _ptrAudioBuffer->SetRecordingSampleRate(0); |
463 _ptrAudioBuffer->SetPlayoutSampleRate(0); | 466 _ptrAudioBuffer->SetPlayoutSampleRate(0); |
464 _ptrAudioBuffer->SetRecordingChannels(0); | 467 _ptrAudioBuffer->SetRecordingChannels(0); |
465 _ptrAudioBuffer->SetPlayoutChannels(0); | 468 _ptrAudioBuffer->SetPlayoutChannels(0); |
466 } | 469 } |
467 | 470 |
468 bool FileAudioDevice::PlayThreadFunc(void* pThis) { | 471 bool FileAudioDevice::PlayThreadFunc(void* pThis) |
469 return (static_cast<FileAudioDevice*>(pThis)->PlayThreadProcess()); | 472 { |
| 473 return (static_cast<FileAudioDevice*>(pThis)->PlayThreadProcess()); |
470 } | 474 } |
471 | 475 |
472 bool FileAudioDevice::RecThreadFunc(void* pThis) { | 476 bool FileAudioDevice::RecThreadFunc(void* pThis) |
473 return (static_cast<FileAudioDevice*>(pThis)->RecThreadProcess()); | 477 { |
| 478 return (static_cast<FileAudioDevice*>(pThis)->RecThreadProcess()); |
474 } | 479 } |
475 | 480 |
476 bool FileAudioDevice::PlayThreadProcess() { | 481 bool FileAudioDevice::PlayThreadProcess() |
477 if (!_playing) { | 482 { |
478 return false; | 483 if(!_playing) { |
479 } | 484 return false; |
480 uint64_t currentTime = _clock->CurrentNtpInMilliseconds(); | 485 } |
481 _critSect.Enter(); | 486 uint64_t currentTime = _clock->CurrentNtpInMilliseconds(); |
482 | |
483 if (_lastCallPlayoutMillis == 0 || | |
484 currentTime - _lastCallPlayoutMillis >= 10) { | |
485 _critSect.Leave(); | |
486 _ptrAudioBuffer->RequestPlayoutData(_playoutFramesIn10MS); | |
487 _critSect.Enter(); | 487 _critSect.Enter(); |
488 | 488 |
489 _playoutFramesLeft = _ptrAudioBuffer->GetPlayoutData(_playoutBuffer); | 489 if (_lastCallPlayoutMillis == 0 || |
490 assert(_playoutFramesLeft == _playoutFramesIn10MS); | 490 currentTime - _lastCallPlayoutMillis >= 10) { |
491 if (_outputFile.IsOpen()) { | 491 _critSect.Leave(); |
492 _outputFile.Write(reinterpret_cast<uint8_t*>(_playoutBuffer), | 492 _ptrAudioBuffer->RequestPlayoutData(_playoutFramesIn10MS); |
493 kPlayoutBufferSize); | 493 _critSect.Enter(); |
| 494 |
| 495 _playoutFramesLeft = _ptrAudioBuffer->GetPlayoutData(_playoutBuffer); |
| 496 assert(_playoutFramesLeft == _playoutFramesIn10MS); |
| 497 if (_outputFile.is_open()) { |
| 498 _outputFile.Write(_playoutBuffer, kPlayoutBufferSize); |
| 499 } |
| 500 _lastCallPlayoutMillis = currentTime; |
494 } | 501 } |
495 _lastCallPlayoutMillis = currentTime; | 502 _playoutFramesLeft = 0; |
496 } | 503 _critSect.Leave(); |
497 _playoutFramesLeft = 0; | |
498 _critSect.Leave(); | |
499 | 504 |
500 uint64_t deltaTimeMillis = _clock->CurrentNtpInMilliseconds() - currentTime; | 505 uint64_t deltaTimeMillis = _clock->CurrentNtpInMilliseconds() - currentTime; |
501 if (deltaTimeMillis < 10) { | 506 if(deltaTimeMillis < 10) { |
502 SleepMs(10 - deltaTimeMillis); | 507 SleepMs(10 - deltaTimeMillis); |
503 } | 508 } |
504 | 509 |
505 return true; | 510 return true; |
506 } | 511 } |
507 | 512 |
508 bool FileAudioDevice::RecThreadProcess() { | 513 bool FileAudioDevice::RecThreadProcess() |
509 if (!_recording) { | 514 { |
510 return false; | 515 if (!_recording) { |
511 } | 516 return false; |
| 517 } |
512 | 518 |
513 uint64_t currentTime = _clock->CurrentNtpInMilliseconds(); | 519 uint64_t currentTime = _clock->CurrentNtpInMilliseconds(); |
514 _critSect.Enter(); | 520 _critSect.Enter(); |
515 | 521 |
516 if (_lastCallRecordMillis == 0 || currentTime - _lastCallRecordMillis >= 10) { | 522 if (_lastCallRecordMillis == 0 || |
517 if (_inputFile.IsOpen()) { | 523 currentTime - _lastCallRecordMillis >= 10) { |
518 if (_inputFile.Read(reinterpret_cast<uint8_t*>(_recordingBuffer), | 524 if (_inputFile.is_open()) { |
519 kRecordingBufferSize) > 0) { | 525 if (_inputFile.Read(_recordingBuffer, kRecordingBufferSize) > 0) { |
520 _ptrAudioBuffer->SetRecordedBuffer(_recordingBuffer, | 526 _ptrAudioBuffer->SetRecordedBuffer(_recordingBuffer, |
521 _recordingFramesIn10MS); | 527 _recordingFramesIn10MS); |
522 } else { | 528 } else { |
523 _inputFile.Seek(0); | 529 _inputFile.Rewind(); |
| 530 } |
| 531 _lastCallRecordMillis = currentTime; |
| 532 _critSect.Leave(); |
| 533 _ptrAudioBuffer->DeliverRecordedData(); |
| 534 _critSect.Enter(); |
524 } | 535 } |
525 _lastCallRecordMillis = currentTime; | |
526 _critSect.Leave(); | |
527 _ptrAudioBuffer->DeliverRecordedData(); | |
528 _critSect.Enter(); | |
529 } | 536 } |
530 } | |
531 | 537 |
532 _critSect.Leave(); | 538 _critSect.Leave(); |
533 | 539 |
534 uint64_t deltaTimeMillis = _clock->CurrentNtpInMilliseconds() - currentTime; | 540 uint64_t deltaTimeMillis = _clock->CurrentNtpInMilliseconds() - currentTime; |
535 if (deltaTimeMillis < 10) { | 541 if(deltaTimeMillis < 10) { |
536 SleepMs(10 - deltaTimeMillis); | 542 SleepMs(10 - deltaTimeMillis); |
537 } | 543 } |
538 | 544 |
539 return true; | 545 return true; |
540 } | 546 } |
541 | 547 |
542 } // namespace webrtc | 548 } // namespace webrtc |
OLD | NEW |