• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * libjingle
3  * Copyright 2012 Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *  1. Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *  2. Redistributions in binary form must reproduce the above copyright notice,
11  *     this list of conditions and the following disclaimer in the documentation
12  *     and/or other materials provided with the distribution.
13  *  3. The name of the author may not be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
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
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "talk/app/webrtc/test/fakeaudiocapturemodule.h"
29 
30 #include <algorithm>
31 
32 #include "webrtc/base/criticalsection.h"
33 #include "webrtc/base/gunit.h"
34 #include "webrtc/base/scoped_ref_ptr.h"
35 #include "webrtc/base/thread.h"
36 
37 using std::min;
38 
39 class FakeAdmTest : public testing::Test,
40                     public webrtc::AudioTransport {
41  protected:
42   static const int kMsInSecond = 1000;
43 
FakeAdmTest()44   FakeAdmTest()
45       : push_iterations_(0),
46         pull_iterations_(0),
47         rec_buffer_bytes_(0) {
48     memset(rec_buffer_, 0, sizeof(rec_buffer_));
49   }
50 
SetUp()51   virtual void SetUp() {
52     fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
53     EXPECT_TRUE(fake_audio_capture_module_.get() != NULL);
54   }
55 
56   // Callbacks inherited from webrtc::AudioTransport.
57   // ADM is pushing data.
RecordedDataIsAvailable(const void * audioSamples,const size_t nSamples,const size_t nBytesPerSample,const size_t nChannels,const uint32_t samplesPerSec,const uint32_t totalDelayMS,const int32_t clockDrift,const uint32_t currentMicLevel,const bool keyPressed,uint32_t & newMicLevel)58   int32_t RecordedDataIsAvailable(const void* audioSamples,
59                                   const size_t nSamples,
60                                   const size_t nBytesPerSample,
61                                   const size_t nChannels,
62                                   const uint32_t samplesPerSec,
63                                   const uint32_t totalDelayMS,
64                                   const int32_t clockDrift,
65                                   const uint32_t currentMicLevel,
66                                   const bool keyPressed,
67                                   uint32_t& newMicLevel) override {
68     rtc::CritScope cs(&crit_);
69     rec_buffer_bytes_ = nSamples * nBytesPerSample;
70     if ((rec_buffer_bytes_ == 0) ||
71         (rec_buffer_bytes_ > FakeAudioCaptureModule::kNumberSamples *
72          FakeAudioCaptureModule::kNumberBytesPerSample)) {
73       ADD_FAILURE();
74       return -1;
75     }
76     memcpy(rec_buffer_, audioSamples, rec_buffer_bytes_);
77     ++push_iterations_;
78     newMicLevel = currentMicLevel;
79     return 0;
80   }
81 
82   // ADM is pulling data.
NeedMorePlayData(const size_t nSamples,const size_t nBytesPerSample,const size_t nChannels,const uint32_t samplesPerSec,void * audioSamples,size_t & nSamplesOut,int64_t * elapsed_time_ms,int64_t * ntp_time_ms)83   int32_t NeedMorePlayData(const size_t nSamples,
84                            const size_t nBytesPerSample,
85                            const size_t nChannels,
86                            const uint32_t samplesPerSec,
87                            void* audioSamples,
88                            size_t& nSamplesOut,
89                            int64_t* elapsed_time_ms,
90                            int64_t* ntp_time_ms) override {
91     rtc::CritScope cs(&crit_);
92     ++pull_iterations_;
93     const size_t audio_buffer_size = nSamples * nBytesPerSample;
94     const size_t bytes_out = RecordedDataReceived() ?
95         CopyFromRecBuffer(audioSamples, audio_buffer_size):
96         GenerateZeroBuffer(audioSamples, audio_buffer_size);
97     nSamplesOut = bytes_out / nBytesPerSample;
98     *elapsed_time_ms = 0;
99     *ntp_time_ms = 0;
100     return 0;
101   }
102 
push_iterations() const103   int push_iterations() const {
104     rtc::CritScope cs(&crit_);
105     return push_iterations_;
106   }
pull_iterations() const107   int pull_iterations() const {
108     rtc::CritScope cs(&crit_);
109     return pull_iterations_;
110   }
111 
112   rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
113 
114  private:
RecordedDataReceived() const115   bool RecordedDataReceived() const {
116     return rec_buffer_bytes_ != 0;
117   }
GenerateZeroBuffer(void * audio_buffer,size_t audio_buffer_size)118   size_t GenerateZeroBuffer(void* audio_buffer, size_t audio_buffer_size) {
119     memset(audio_buffer, 0, audio_buffer_size);
120     return audio_buffer_size;
121   }
CopyFromRecBuffer(void * audio_buffer,size_t audio_buffer_size)122   size_t CopyFromRecBuffer(void* audio_buffer, size_t audio_buffer_size) {
123     EXPECT_EQ(audio_buffer_size, rec_buffer_bytes_);
124     const size_t min_buffer_size = min(audio_buffer_size, rec_buffer_bytes_);
125     memcpy(audio_buffer, rec_buffer_, min_buffer_size);
126     return min_buffer_size;
127   }
128 
129   mutable rtc::CriticalSection crit_;
130 
131   int push_iterations_;
132   int pull_iterations_;
133 
134   char rec_buffer_[FakeAudioCaptureModule::kNumberSamples *
135                    FakeAudioCaptureModule::kNumberBytesPerSample];
136   size_t rec_buffer_bytes_;
137 };
138 
TEST_F(FakeAdmTest,TestProccess)139 TEST_F(FakeAdmTest, TestProccess) {
140   // Next process call must be some time in the future (or now).
141   EXPECT_LE(0, fake_audio_capture_module_->TimeUntilNextProcess());
142   // Process call updates TimeUntilNextProcess() but there are no guarantees on
143   // timing so just check that Process can ba called successfully.
144   EXPECT_LE(0, fake_audio_capture_module_->Process());
145 }
146 
TEST_F(FakeAdmTest,PlayoutTest)147 TEST_F(FakeAdmTest, PlayoutTest) {
148   EXPECT_EQ(0, fake_audio_capture_module_->RegisterAudioCallback(this));
149 
150   bool stereo_available = false;
151   EXPECT_EQ(0,
152             fake_audio_capture_module_->StereoPlayoutIsAvailable(
153                 &stereo_available));
154   EXPECT_TRUE(stereo_available);
155 
156   EXPECT_NE(0, fake_audio_capture_module_->StartPlayout());
157   EXPECT_FALSE(fake_audio_capture_module_->PlayoutIsInitialized());
158   EXPECT_FALSE(fake_audio_capture_module_->Playing());
159   EXPECT_EQ(0, fake_audio_capture_module_->StopPlayout());
160 
161   EXPECT_EQ(0, fake_audio_capture_module_->InitPlayout());
162   EXPECT_TRUE(fake_audio_capture_module_->PlayoutIsInitialized());
163   EXPECT_FALSE(fake_audio_capture_module_->Playing());
164 
165   EXPECT_EQ(0, fake_audio_capture_module_->StartPlayout());
166   EXPECT_TRUE(fake_audio_capture_module_->Playing());
167 
168   uint16_t delay_ms = 10;
169   EXPECT_EQ(0, fake_audio_capture_module_->PlayoutDelay(&delay_ms));
170   EXPECT_EQ(0, delay_ms);
171 
172   EXPECT_TRUE_WAIT(pull_iterations() > 0, kMsInSecond);
173   EXPECT_GE(0, push_iterations());
174 
175   EXPECT_EQ(0, fake_audio_capture_module_->StopPlayout());
176   EXPECT_FALSE(fake_audio_capture_module_->Playing());
177 }
178 
TEST_F(FakeAdmTest,RecordTest)179 TEST_F(FakeAdmTest, RecordTest) {
180   EXPECT_EQ(0, fake_audio_capture_module_->RegisterAudioCallback(this));
181 
182   bool stereo_available = false;
183   EXPECT_EQ(0, fake_audio_capture_module_->StereoRecordingIsAvailable(
184       &stereo_available));
185   EXPECT_FALSE(stereo_available);
186 
187   EXPECT_NE(0, fake_audio_capture_module_->StartRecording());
188   EXPECT_FALSE(fake_audio_capture_module_->Recording());
189   EXPECT_EQ(0, fake_audio_capture_module_->StopRecording());
190 
191   EXPECT_EQ(0, fake_audio_capture_module_->InitRecording());
192   EXPECT_EQ(0, fake_audio_capture_module_->StartRecording());
193   EXPECT_TRUE(fake_audio_capture_module_->Recording());
194 
195   EXPECT_TRUE_WAIT(push_iterations() > 0, kMsInSecond);
196   EXPECT_GE(0, pull_iterations());
197 
198   EXPECT_EQ(0, fake_audio_capture_module_->StopRecording());
199   EXPECT_FALSE(fake_audio_capture_module_->Recording());
200 }
201 
TEST_F(FakeAdmTest,DuplexTest)202 TEST_F(FakeAdmTest, DuplexTest) {
203   EXPECT_EQ(0, fake_audio_capture_module_->RegisterAudioCallback(this));
204 
205   EXPECT_EQ(0, fake_audio_capture_module_->InitPlayout());
206   EXPECT_EQ(0, fake_audio_capture_module_->StartPlayout());
207 
208   EXPECT_EQ(0, fake_audio_capture_module_->InitRecording());
209   EXPECT_EQ(0, fake_audio_capture_module_->StartRecording());
210 
211   EXPECT_TRUE_WAIT(push_iterations() > 0, kMsInSecond);
212   EXPECT_TRUE_WAIT(pull_iterations() > 0, kMsInSecond);
213 
214   EXPECT_EQ(0, fake_audio_capture_module_->StopPlayout());
215   EXPECT_EQ(0, fake_audio_capture_module_->StopRecording());
216 }
217