| OLD | NEW |
| 1 /* | 1 /* |
| 2 * libjingle | 2 * libjingle |
| 3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 Google Inc. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
| 7 * | 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
| 9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 */ | 26 */ |
| 27 | 27 |
| 28 #include "talk/app/webrtc/test/fakeaudiocapturemodule.h" | 28 #include "talk/app/webrtc/test/fakeaudiocapturemodule.h" |
| 29 | 29 |
| 30 #include <algorithm> | 30 #include <algorithm> |
| 31 | 31 |
| 32 #include "webrtc/base/criticalsection.h" |
| 32 #include "webrtc/base/gunit.h" | 33 #include "webrtc/base/gunit.h" |
| 33 #include "webrtc/base/scoped_ref_ptr.h" | 34 #include "webrtc/base/scoped_ref_ptr.h" |
| 34 #include "webrtc/base/thread.h" | 35 #include "webrtc/base/thread.h" |
| 35 | 36 |
| 36 using std::min; | 37 using std::min; |
| 37 | 38 |
| 38 class FakeAdmTest : public testing::Test, | 39 class FakeAdmTest : public testing::Test, |
| 39 public webrtc::AudioTransport { | 40 public webrtc::AudioTransport { |
| 40 protected: | 41 protected: |
| 41 static const int kMsInSecond = 1000; | 42 static const int kMsInSecond = 1000; |
| 42 | 43 |
| 43 FakeAdmTest() | 44 FakeAdmTest() |
| 44 : push_iterations_(0), | 45 : push_iterations_(0), |
| 45 pull_iterations_(0), | 46 pull_iterations_(0), |
| 46 rec_buffer_bytes_(0) { | 47 rec_buffer_bytes_(0) { |
| 47 memset(rec_buffer_, 0, sizeof(rec_buffer_)); | 48 memset(rec_buffer_, 0, sizeof(rec_buffer_)); |
| 48 } | 49 } |
| 49 | 50 |
| 50 virtual void SetUp() { | 51 virtual void SetUp() { |
| 51 fake_audio_capture_module_ = FakeAudioCaptureModule::Create( | 52 fake_audio_capture_module_ = FakeAudioCaptureModule::Create(); |
| 52 rtc::Thread::Current()); | |
| 53 EXPECT_TRUE(fake_audio_capture_module_.get() != NULL); | 53 EXPECT_TRUE(fake_audio_capture_module_.get() != NULL); |
| 54 } | 54 } |
| 55 | 55 |
| 56 // Callbacks inherited from webrtc::AudioTransport. | 56 // Callbacks inherited from webrtc::AudioTransport. |
| 57 // ADM is pushing data. | 57 // ADM is pushing data. |
| 58 int32_t RecordedDataIsAvailable(const void* audioSamples, | 58 int32_t RecordedDataIsAvailable(const void* audioSamples, |
| 59 const uint32_t nSamples, | 59 const uint32_t nSamples, |
| 60 const uint8_t nBytesPerSample, | 60 const uint8_t nBytesPerSample, |
| 61 const uint8_t nChannels, | 61 const uint8_t nChannels, |
| 62 const uint32_t samplesPerSec, | 62 const uint32_t samplesPerSec, |
| 63 const uint32_t totalDelayMS, | 63 const uint32_t totalDelayMS, |
| 64 const int32_t clockDrift, | 64 const int32_t clockDrift, |
| 65 const uint32_t currentMicLevel, | 65 const uint32_t currentMicLevel, |
| 66 const bool keyPressed, | 66 const bool keyPressed, |
| 67 uint32_t& newMicLevel) override { | 67 uint32_t& newMicLevel) override { |
| 68 rtc::CritScope cs(&crit_); |
| 68 rec_buffer_bytes_ = nSamples * nBytesPerSample; | 69 rec_buffer_bytes_ = nSamples * nBytesPerSample; |
| 69 if ((rec_buffer_bytes_ == 0) || | 70 if ((rec_buffer_bytes_ == 0) || |
| 70 (rec_buffer_bytes_ > FakeAudioCaptureModule::kNumberSamples * | 71 (rec_buffer_bytes_ > FakeAudioCaptureModule::kNumberSamples * |
| 71 FakeAudioCaptureModule::kNumberBytesPerSample)) { | 72 FakeAudioCaptureModule::kNumberBytesPerSample)) { |
| 72 ADD_FAILURE(); | 73 ADD_FAILURE(); |
| 73 return -1; | 74 return -1; |
| 74 } | 75 } |
| 75 memcpy(rec_buffer_, audioSamples, rec_buffer_bytes_); | 76 memcpy(rec_buffer_, audioSamples, rec_buffer_bytes_); |
| 76 ++push_iterations_; | 77 ++push_iterations_; |
| 77 newMicLevel = currentMicLevel; | 78 newMicLevel = currentMicLevel; |
| 78 return 0; | 79 return 0; |
| 79 } | 80 } |
| 80 | 81 |
| 81 // ADM is pulling data. | 82 // ADM is pulling data. |
| 82 int32_t NeedMorePlayData(const uint32_t nSamples, | 83 int32_t NeedMorePlayData(const uint32_t nSamples, |
| 83 const uint8_t nBytesPerSample, | 84 const uint8_t nBytesPerSample, |
| 84 const uint8_t nChannels, | 85 const uint8_t nChannels, |
| 85 const uint32_t samplesPerSec, | 86 const uint32_t samplesPerSec, |
| 86 void* audioSamples, | 87 void* audioSamples, |
| 87 uint32_t& nSamplesOut, | 88 uint32_t& nSamplesOut, |
| 88 int64_t* elapsed_time_ms, | 89 int64_t* elapsed_time_ms, |
| 89 int64_t* ntp_time_ms) override { | 90 int64_t* ntp_time_ms) override { |
| 91 rtc::CritScope cs(&crit_); |
| 90 ++pull_iterations_; | 92 ++pull_iterations_; |
| 91 const uint32_t audio_buffer_size = nSamples * nBytesPerSample; | 93 const uint32_t audio_buffer_size = nSamples * nBytesPerSample; |
| 92 const uint32_t bytes_out = RecordedDataReceived() ? | 94 const uint32_t bytes_out = RecordedDataReceived() ? |
| 93 CopyFromRecBuffer(audioSamples, audio_buffer_size): | 95 CopyFromRecBuffer(audioSamples, audio_buffer_size): |
| 94 GenerateZeroBuffer(audioSamples, audio_buffer_size); | 96 GenerateZeroBuffer(audioSamples, audio_buffer_size); |
| 95 nSamplesOut = bytes_out / nBytesPerSample; | 97 nSamplesOut = bytes_out / nBytesPerSample; |
| 96 *elapsed_time_ms = 0; | 98 *elapsed_time_ms = 0; |
| 97 *ntp_time_ms = 0; | 99 *ntp_time_ms = 0; |
| 98 return 0; | 100 return 0; |
| 99 } | 101 } |
| 100 | 102 |
| 101 int push_iterations() const { return push_iterations_; } | 103 int push_iterations() const { |
| 102 int pull_iterations() const { return pull_iterations_; } | 104 rtc::CritScope cs(&crit_); |
| 105 return push_iterations_; |
| 106 } |
| 107 int pull_iterations() const { |
| 108 rtc::CritScope cs(&crit_); |
| 109 return pull_iterations_; |
| 110 } |
| 103 | 111 |
| 104 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_; | 112 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_; |
| 105 | 113 |
| 106 private: | 114 private: |
| 107 bool RecordedDataReceived() const { | 115 bool RecordedDataReceived() const { |
| 108 return rec_buffer_bytes_ != 0; | 116 return rec_buffer_bytes_ != 0; |
| 109 } | 117 } |
| 110 int32_t GenerateZeroBuffer(void* audio_buffer, uint32_t audio_buffer_size) { | 118 int32_t GenerateZeroBuffer(void* audio_buffer, uint32_t audio_buffer_size) { |
| 111 memset(audio_buffer, 0, audio_buffer_size); | 119 memset(audio_buffer, 0, audio_buffer_size); |
| 112 return audio_buffer_size; | 120 return audio_buffer_size; |
| 113 } | 121 } |
| 114 int32_t CopyFromRecBuffer(void* audio_buffer, uint32_t audio_buffer_size) { | 122 int32_t CopyFromRecBuffer(void* audio_buffer, uint32_t audio_buffer_size) { |
| 115 EXPECT_EQ(audio_buffer_size, rec_buffer_bytes_); | 123 EXPECT_EQ(audio_buffer_size, rec_buffer_bytes_); |
| 116 const uint32_t min_buffer_size = min(audio_buffer_size, rec_buffer_bytes_); | 124 const uint32_t min_buffer_size = min(audio_buffer_size, rec_buffer_bytes_); |
| 117 memcpy(audio_buffer, rec_buffer_, min_buffer_size); | 125 memcpy(audio_buffer, rec_buffer_, min_buffer_size); |
| 118 return min_buffer_size; | 126 return min_buffer_size; |
| 119 } | 127 } |
| 120 | 128 |
| 129 mutable rtc::CriticalSection crit_; |
| 130 |
| 121 int push_iterations_; | 131 int push_iterations_; |
| 122 int pull_iterations_; | 132 int pull_iterations_; |
| 123 | 133 |
| 124 char rec_buffer_[FakeAudioCaptureModule::kNumberSamples * | 134 char rec_buffer_[FakeAudioCaptureModule::kNumberSamples * |
| 125 FakeAudioCaptureModule::kNumberBytesPerSample]; | 135 FakeAudioCaptureModule::kNumberBytesPerSample]; |
| 126 uint32_t rec_buffer_bytes_; | 136 uint32_t rec_buffer_bytes_; |
| 127 }; | 137 }; |
| 128 | 138 |
| 129 TEST_F(FakeAdmTest, TestProccess) { | 139 TEST_F(FakeAdmTest, TestProccess) { |
| 130 // Next process call must be some time in the future (or now). | 140 // Next process call must be some time in the future (or now). |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 | 207 |
| 198 EXPECT_EQ(0, fake_audio_capture_module_->InitRecording()); | 208 EXPECT_EQ(0, fake_audio_capture_module_->InitRecording()); |
| 199 EXPECT_EQ(0, fake_audio_capture_module_->StartRecording()); | 209 EXPECT_EQ(0, fake_audio_capture_module_->StartRecording()); |
| 200 | 210 |
| 201 EXPECT_TRUE_WAIT(push_iterations() > 0, kMsInSecond); | 211 EXPECT_TRUE_WAIT(push_iterations() > 0, kMsInSecond); |
| 202 EXPECT_TRUE_WAIT(pull_iterations() > 0, kMsInSecond); | 212 EXPECT_TRUE_WAIT(pull_iterations() > 0, kMsInSecond); |
| 203 | 213 |
| 204 EXPECT_EQ(0, fake_audio_capture_module_->StopPlayout()); | 214 EXPECT_EQ(0, fake_audio_capture_module_->StopPlayout()); |
| 205 EXPECT_EQ(0, fake_audio_capture_module_->StopRecording()); | 215 EXPECT_EQ(0, fake_audio_capture_module_->StopRecording()); |
| 206 } | 216 } |
| OLD | NEW |