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/platform_thread.h" | 10 #include "webrtc/base/platform_thread.h" |
(...skipping 28 matching lines...) Expand all Loading... |
39 _lastCallPlayoutMillis(0), | 39 _lastCallPlayoutMillis(0), |
40 _lastCallRecordMillis(0), | 40 _lastCallRecordMillis(0), |
41 _outputFile(*FileWrapper::Create()), | 41 _outputFile(*FileWrapper::Create()), |
42 _inputFile(*FileWrapper::Create()), | 42 _inputFile(*FileWrapper::Create()), |
43 _outputFilename(outputFilename), | 43 _outputFilename(outputFilename), |
44 _inputFilename(inputFilename), | 44 _inputFilename(inputFilename), |
45 _clock(Clock::GetRealTimeClock()) { | 45 _clock(Clock::GetRealTimeClock()) { |
46 } | 46 } |
47 | 47 |
48 FileAudioDevice::~FileAudioDevice() { | 48 FileAudioDevice::~FileAudioDevice() { |
49 if (_outputFile.Open()) { | |
50 _outputFile.Flush(); | |
51 _outputFile.CloseFile(); | |
52 } | |
53 delete &_outputFile; | 49 delete &_outputFile; |
54 if (_inputFile.Open()) { | |
55 _inputFile.Flush(); | |
56 _inputFile.CloseFile(); | |
57 } | |
58 delete &_inputFile; | 50 delete &_inputFile; |
59 } | 51 } |
60 | 52 |
61 int32_t FileAudioDevice::ActiveAudioLayer( | 53 int32_t FileAudioDevice::ActiveAudioLayer( |
62 AudioDeviceModule::AudioLayer& audioLayer) const { | 54 AudioDeviceModule::AudioLayer& audioLayer) const { |
63 return -1; | 55 return -1; |
64 } | 56 } |
65 | 57 |
66 int32_t FileAudioDevice::Init() { return 0; } | 58 int32_t FileAudioDevice::Init() { return 0; } |
67 | 59 |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 | 187 |
196 if (!_playoutBuffer) { | 188 if (!_playoutBuffer) { |
197 _playoutBuffer = new int8_t[kPlayoutBufferSize]; | 189 _playoutBuffer = new int8_t[kPlayoutBufferSize]; |
198 } | 190 } |
199 if (!_playoutBuffer) { | 191 if (!_playoutBuffer) { |
200 _playing = false; | 192 _playing = false; |
201 return -1; | 193 return -1; |
202 } | 194 } |
203 | 195 |
204 // PLAYOUT | 196 // PLAYOUT |
205 if (!_outputFilename.empty() && _outputFile.OpenFile( | 197 if (!_outputFilename.empty() && |
206 _outputFilename.c_str(), false, false, false) == -1) { | 198 _outputFile.OpenFile(_outputFilename.c_str(), false) == -1) { |
207 printf("Failed to open playout file %s!\n", _outputFilename.c_str()); | 199 printf("Failed to open playout file %s!\n", _outputFilename.c_str()); |
208 _playing = false; | 200 _playing = false; |
209 delete [] _playoutBuffer; | 201 delete [] _playoutBuffer; |
210 _playoutBuffer = NULL; | 202 _playoutBuffer = NULL; |
211 return -1; | 203 return -1; |
212 } | 204 } |
213 | 205 |
214 _ptrThreadPlay.reset(new rtc::PlatformThread( | 206 _ptrThreadPlay.reset(new rtc::PlatformThread( |
215 PlayThreadFunc, this, "webrtc_audio_module_play_thread")); | 207 PlayThreadFunc, this, "webrtc_audio_module_play_thread")); |
216 _ptrThreadPlay->Start(); | 208 _ptrThreadPlay->Start(); |
(...skipping 11 matching lines...) Expand all Loading... |
228 if (_ptrThreadPlay) { | 220 if (_ptrThreadPlay) { |
229 _ptrThreadPlay->Stop(); | 221 _ptrThreadPlay->Stop(); |
230 _ptrThreadPlay.reset(); | 222 _ptrThreadPlay.reset(); |
231 } | 223 } |
232 | 224 |
233 CriticalSectionScoped lock(&_critSect); | 225 CriticalSectionScoped lock(&_critSect); |
234 | 226 |
235 _playoutFramesLeft = 0; | 227 _playoutFramesLeft = 0; |
236 delete [] _playoutBuffer; | 228 delete [] _playoutBuffer; |
237 _playoutBuffer = NULL; | 229 _playoutBuffer = NULL; |
238 if (_outputFile.Open()) { | 230 _outputFile.CloseFile(); |
239 _outputFile.Flush(); | 231 return 0; |
240 _outputFile.CloseFile(); | |
241 } | |
242 return 0; | |
243 } | 232 } |
244 | 233 |
245 bool FileAudioDevice::Playing() const { | 234 bool FileAudioDevice::Playing() const { |
246 return true; | 235 return true; |
247 } | 236 } |
248 | 237 |
249 int32_t FileAudioDevice::StartRecording() { | 238 int32_t FileAudioDevice::StartRecording() { |
250 _recording = true; | 239 _recording = true; |
251 | 240 |
252 // Make sure we only create the buffer once. | 241 // Make sure we only create the buffer once. |
253 _recordingBufferSizeIn10MS = _recordingFramesIn10MS * | 242 _recordingBufferSizeIn10MS = _recordingFramesIn10MS * |
254 kRecordingNumChannels * | 243 kRecordingNumChannels * |
255 2; | 244 2; |
256 if (!_recordingBuffer) { | 245 if (!_recordingBuffer) { |
257 _recordingBuffer = new int8_t[_recordingBufferSizeIn10MS]; | 246 _recordingBuffer = new int8_t[_recordingBufferSizeIn10MS]; |
258 } | 247 } |
259 | 248 |
260 if (!_inputFilename.empty() && _inputFile.OpenFile( | 249 if (!_inputFilename.empty() && |
261 _inputFilename.c_str(), true, true, false) == -1) { | 250 _inputFile.OpenFile(_inputFilename.c_str(), true) == -1) { |
262 printf("Failed to open audio input file %s!\n", | 251 printf("Failed to open audio input file %s!\n", |
263 _inputFilename.c_str()); | 252 _inputFilename.c_str()); |
264 _recording = false; | 253 _recording = false; |
265 delete[] _recordingBuffer; | 254 delete[] _recordingBuffer; |
266 _recordingBuffer = NULL; | 255 _recordingBuffer = NULL; |
267 return -1; | 256 return -1; |
268 } | 257 } |
269 | 258 |
270 _ptrThreadRec.reset(new rtc::PlatformThread( | 259 _ptrThreadRec.reset(new rtc::PlatformThread( |
271 RecThreadFunc, this, "webrtc_audio_module_capture_thread")); | 260 RecThreadFunc, this, "webrtc_audio_module_capture_thread")); |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
483 _critSect.Enter(); | 472 _critSect.Enter(); |
484 | 473 |
485 if (_lastCallPlayoutMillis == 0 || | 474 if (_lastCallPlayoutMillis == 0 || |
486 currentTime - _lastCallPlayoutMillis >= 10) { | 475 currentTime - _lastCallPlayoutMillis >= 10) { |
487 _critSect.Leave(); | 476 _critSect.Leave(); |
488 _ptrAudioBuffer->RequestPlayoutData(_playoutFramesIn10MS); | 477 _ptrAudioBuffer->RequestPlayoutData(_playoutFramesIn10MS); |
489 _critSect.Enter(); | 478 _critSect.Enter(); |
490 | 479 |
491 _playoutFramesLeft = _ptrAudioBuffer->GetPlayoutData(_playoutBuffer); | 480 _playoutFramesLeft = _ptrAudioBuffer->GetPlayoutData(_playoutBuffer); |
492 assert(_playoutFramesLeft == _playoutFramesIn10MS); | 481 assert(_playoutFramesLeft == _playoutFramesIn10MS); |
493 if (_outputFile.Open()) { | 482 if (_outputFile.is_open()) { |
494 _outputFile.Write(_playoutBuffer, kPlayoutBufferSize); | 483 _outputFile.Write(_playoutBuffer, kPlayoutBufferSize); |
495 _outputFile.Flush(); | |
496 } | 484 } |
497 _lastCallPlayoutMillis = currentTime; | 485 _lastCallPlayoutMillis = currentTime; |
498 } | 486 } |
499 _playoutFramesLeft = 0; | 487 _playoutFramesLeft = 0; |
500 _critSect.Leave(); | 488 _critSect.Leave(); |
501 | 489 |
502 uint64_t deltaTimeMillis = _clock->CurrentNtpInMilliseconds() - currentTime; | 490 uint64_t deltaTimeMillis = _clock->CurrentNtpInMilliseconds() - currentTime; |
503 if(deltaTimeMillis < 10) { | 491 if(deltaTimeMillis < 10) { |
504 SleepMs(10 - deltaTimeMillis); | 492 SleepMs(10 - deltaTimeMillis); |
505 } | 493 } |
506 | 494 |
507 return true; | 495 return true; |
508 } | 496 } |
509 | 497 |
510 bool FileAudioDevice::RecThreadProcess() | 498 bool FileAudioDevice::RecThreadProcess() |
511 { | 499 { |
512 if (!_recording) { | 500 if (!_recording) { |
513 return false; | 501 return false; |
514 } | 502 } |
515 | 503 |
516 uint64_t currentTime = _clock->CurrentNtpInMilliseconds(); | 504 uint64_t currentTime = _clock->CurrentNtpInMilliseconds(); |
517 _critSect.Enter(); | 505 _critSect.Enter(); |
518 | 506 |
519 if (_lastCallRecordMillis == 0 || | 507 if (_lastCallRecordMillis == 0 || |
520 currentTime - _lastCallRecordMillis >= 10) { | 508 currentTime - _lastCallRecordMillis >= 10) { |
521 if (_inputFile.Open()) { | 509 if (_inputFile.is_open()) { |
522 if (_inputFile.Read(_recordingBuffer, kRecordingBufferSize) > 0) { | 510 if (_inputFile.Read(_recordingBuffer, kRecordingBufferSize) > 0) { |
523 _ptrAudioBuffer->SetRecordedBuffer(_recordingBuffer, | 511 _ptrAudioBuffer->SetRecordedBuffer(_recordingBuffer, |
524 _recordingFramesIn10MS); | 512 _recordingFramesIn10MS); |
525 } else { | 513 } else { |
526 _inputFile.Rewind(); | 514 _inputFile.Rewind(); |
527 } | 515 } |
528 _lastCallRecordMillis = currentTime; | 516 _lastCallRecordMillis = currentTime; |
529 _critSect.Leave(); | 517 _critSect.Leave(); |
530 _ptrAudioBuffer->DeliverRecordedData(); | 518 _ptrAudioBuffer->DeliverRecordedData(); |
531 _critSect.Enter(); | 519 _critSect.Enter(); |
532 } | 520 } |
533 } | 521 } |
534 | 522 |
535 _critSect.Leave(); | 523 _critSect.Leave(); |
536 | 524 |
537 uint64_t deltaTimeMillis = _clock->CurrentNtpInMilliseconds() - currentTime; | 525 uint64_t deltaTimeMillis = _clock->CurrentNtpInMilliseconds() - currentTime; |
538 if(deltaTimeMillis < 10) { | 526 if(deltaTimeMillis < 10) { |
539 SleepMs(10 - deltaTimeMillis); | 527 SleepMs(10 - deltaTimeMillis); |
540 } | 528 } |
541 | 529 |
542 return true; | 530 return true; |
543 } | 531 } |
544 | 532 |
545 } // namespace webrtc | 533 } // namespace webrtc |
OLD | NEW |