Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / rtp_rtcp / test / testAPI / test_api_audio.cc
1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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 <algorithm>
12 #include <vector>
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 #include "webrtc/modules/rtp_rtcp/test/testAPI/test_api.h"
16
17 #include "webrtc/common_types.h"
18 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h"
19 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
20 #include "webrtc/modules/rtp_rtcp/source/rtp_receiver_audio.h"
21
22 using namespace webrtc;
23
24 #define test_rate 64000u
25
26 class VerifyingAudioReceiver : public NullRtpData {
27  public:
28   virtual int32_t OnReceivedPayloadData(
29       const uint8_t* payloadData,
30       const uint16_t payloadSize,
31       const webrtc::WebRtcRTPHeader* rtpHeader) OVERRIDE {
32     if (rtpHeader->header.payloadType == 98 ||
33         rtpHeader->header.payloadType == 99) {
34       EXPECT_EQ(4, payloadSize);
35       char str[5];
36       memcpy(str, payloadData, payloadSize);
37       str[4] = 0;
38       // All our test vectors for payload type 96 and 97 even the stereo is on
39       // a per channel base equal to the 4 chars "test".
40       // Note there is no null termination so we add that to use the
41       // test EXPECT_STRCASEEQ.
42       EXPECT_STRCASEEQ("test", str);
43       return 0;
44     }
45     if (rtpHeader->header.payloadType == 100 ||
46         rtpHeader->header.payloadType == 101 ||
47         rtpHeader->header.payloadType == 102) {
48       if (rtpHeader->type.Audio.channel == 1) {
49         if (payloadData[0] == 0xff) {
50           // All our test vectors for payload type 100, 101 and 102 have the
51           // first channel data being equal to 0xff.
52           return 0;
53         }
54       }
55       ADD_FAILURE() << "This code path should never happen.";
56       return -1;
57     }
58     return 0;
59   }
60 };
61
62 class RTPCallback : public NullRtpFeedback {
63  public:
64   virtual int32_t OnInitializeDecoder(
65       const int32_t id,
66       const int8_t payloadType,
67       const char payloadName[RTP_PAYLOAD_NAME_SIZE],
68       const int frequency,
69       const uint8_t channels,
70       const uint32_t rate) OVERRIDE {
71     if (payloadType == 96) {
72       EXPECT_EQ(test_rate, rate) <<
73           "The rate should be 64K for this payloadType";
74     }
75     return 0;
76   }
77 };
78
79 class RtpRtcpAudioTest : public ::testing::Test {
80  protected:
81   RtpRtcpAudioTest() : fake_clock(123456) {
82     test_CSRC[0] = 1234;
83     test_CSRC[2] = 2345;
84     test_id = 123;
85     test_ssrc = 3456;
86     test_timestamp = 4567;
87     test_sequence_number = 2345;
88   }
89   ~RtpRtcpAudioTest() {}
90
91   virtual void SetUp() OVERRIDE {
92     audioFeedback = new NullRtpAudioFeedback();
93     data_receiver1 = new VerifyingAudioReceiver();
94     data_receiver2 = new VerifyingAudioReceiver();
95     rtp_callback = new RTPCallback();
96     transport1 = new LoopBackTransport();
97     transport2 = new LoopBackTransport();
98
99     receive_statistics1_.reset(ReceiveStatistics::Create(&fake_clock));
100     receive_statistics2_.reset(ReceiveStatistics::Create(&fake_clock));
101
102     rtp_payload_registry1_.reset(new RTPPayloadRegistry(
103         RTPPayloadStrategy::CreateStrategy(true)));
104     rtp_payload_registry2_.reset(new RTPPayloadRegistry(
105         RTPPayloadStrategy::CreateStrategy(true)));
106
107     RtpRtcp::Configuration configuration;
108     configuration.id = test_id;
109     configuration.audio = true;
110     configuration.clock = &fake_clock;
111     configuration.receive_statistics = receive_statistics1_.get();
112     configuration.outgoing_transport = transport1;
113     configuration.audio_messages = audioFeedback;
114
115     module1 = RtpRtcp::CreateRtpRtcp(configuration);
116     rtp_receiver1_.reset(RtpReceiver::CreateAudioReceiver(
117         test_id, &fake_clock, audioFeedback, data_receiver1, NULL,
118         rtp_payload_registry1_.get()));
119
120     configuration.id = test_id + 1;
121     configuration.receive_statistics = receive_statistics2_.get();
122     configuration.outgoing_transport = transport2;
123     configuration.audio_messages = audioFeedback;
124
125     module2 = RtpRtcp::CreateRtpRtcp(configuration);
126     rtp_receiver2_.reset(RtpReceiver::CreateAudioReceiver(
127             test_id + 1, &fake_clock, audioFeedback, data_receiver2, NULL,
128             rtp_payload_registry2_.get()));
129
130     transport1->SetSendModule(module2, rtp_payload_registry2_.get(),
131                               rtp_receiver2_.get(), receive_statistics2_.get());
132     transport2->SetSendModule(module1, rtp_payload_registry1_.get(),
133                               rtp_receiver1_.get(), receive_statistics1_.get());
134   }
135
136   virtual void TearDown() OVERRIDE {
137     delete module1;
138     delete module2;
139     delete transport1;
140     delete transport2;
141     delete audioFeedback;
142     delete data_receiver1;
143     delete data_receiver2;
144     delete rtp_callback;
145   }
146
147   int test_id;
148   RtpRtcp* module1;
149   RtpRtcp* module2;
150   scoped_ptr<ReceiveStatistics> receive_statistics1_;
151   scoped_ptr<ReceiveStatistics> receive_statistics2_;
152   scoped_ptr<RtpReceiver> rtp_receiver1_;
153   scoped_ptr<RtpReceiver> rtp_receiver2_;
154   scoped_ptr<RTPPayloadRegistry> rtp_payload_registry1_;
155   scoped_ptr<RTPPayloadRegistry> rtp_payload_registry2_;
156   VerifyingAudioReceiver* data_receiver1;
157   VerifyingAudioReceiver* data_receiver2;
158   LoopBackTransport* transport1;
159   LoopBackTransport* transport2;
160   NullRtpAudioFeedback* audioFeedback;
161   RTPCallback* rtp_callback;
162   uint32_t test_ssrc;
163   uint32_t test_timestamp;
164   uint16_t test_sequence_number;
165   uint32_t test_CSRC[webrtc::kRtpCsrcSize];
166   SimulatedClock fake_clock;
167 };
168
169 TEST_F(RtpRtcpAudioTest, Basic) {
170   module1->SetSSRC(test_ssrc);
171   EXPECT_EQ(0, module1->SetStartTimestamp(test_timestamp));
172
173   // Test detection at the end of a DTMF tone.
174   //EXPECT_EQ(0, module2->SetTelephoneEventForwardToDecoder(true));
175
176   EXPECT_EQ(0, module1->SetSendingStatus(true));
177
178   // Start basic RTP test.
179
180   // Send an empty RTP packet.
181   // Should fail since we have not registered the payload type.
182   EXPECT_EQ(-1, module1->SendOutgoingData(webrtc::kAudioFrameSpeech,
183                                           96, 0, -1, NULL, 0));
184
185   CodecInst voice_codec;
186   memset(&voice_codec, 0, sizeof(voice_codec));
187   voice_codec.pltype = 96;
188   voice_codec.plfreq = 8000;
189   memcpy(voice_codec.plname, "PCMU", 5);
190
191   EXPECT_EQ(0, module1->RegisterSendPayload(voice_codec));
192   EXPECT_EQ(0, rtp_receiver1_->RegisterReceivePayload(
193       voice_codec.plname,
194       voice_codec.pltype,
195       voice_codec.plfreq,
196       voice_codec.channels,
197       (voice_codec.rate < 0) ? 0 : voice_codec.rate));
198   EXPECT_EQ(0, module2->RegisterSendPayload(voice_codec));
199   voice_codec.rate = test_rate;
200   EXPECT_EQ(0, rtp_receiver2_->RegisterReceivePayload(
201       voice_codec.plname,
202       voice_codec.pltype,
203       voice_codec.plfreq,
204       voice_codec.channels,
205       (voice_codec.rate < 0) ? 0 : voice_codec.rate));
206   printf("4\n");
207
208   const uint8_t test[5] = "test";
209   EXPECT_EQ(0, module1->SendOutgoingData(webrtc::kAudioFrameSpeech, 96,
210                                          0, -1, test, 4));
211
212   EXPECT_EQ(test_ssrc, rtp_receiver2_->SSRC());
213   uint32_t timestamp;
214   EXPECT_TRUE(rtp_receiver2_->Timestamp(&timestamp));
215   EXPECT_EQ(test_timestamp, timestamp);
216 }
217
218 TEST_F(RtpRtcpAudioTest, RED) {
219   CodecInst voice_codec;
220   memset(&voice_codec, 0, sizeof(voice_codec));
221   voice_codec.pltype = 96;
222   voice_codec.plfreq = 8000;
223   memcpy(voice_codec.plname, "PCMU", 5);
224
225   EXPECT_EQ(0, module1->RegisterSendPayload(voice_codec));
226   EXPECT_EQ(0, rtp_receiver1_->RegisterReceivePayload(
227       voice_codec.plname,
228       voice_codec.pltype,
229       voice_codec.plfreq,
230       voice_codec.channels,
231       (voice_codec.rate < 0) ? 0 : voice_codec.rate));
232   EXPECT_EQ(0, module2->RegisterSendPayload(voice_codec));
233   voice_codec.rate = test_rate;
234   EXPECT_EQ(0, rtp_receiver2_->RegisterReceivePayload(
235       voice_codec.plname,
236       voice_codec.pltype,
237       voice_codec.plfreq,
238       voice_codec.channels,
239       (voice_codec.rate < 0) ? 0 : voice_codec.rate));
240
241   module1->SetSSRC(test_ssrc);
242   EXPECT_EQ(0, module1->SetStartTimestamp(test_timestamp));
243   EXPECT_EQ(0, module1->SetSendingStatus(true));
244
245   voice_codec.pltype = 127;
246   voice_codec.plfreq = 8000;
247   memcpy(voice_codec.plname, "RED", 4);
248
249   EXPECT_EQ(0, module1->SetSendREDPayloadType(voice_codec.pltype));
250   int8_t red = 0;
251   EXPECT_EQ(0, module1->SendREDPayloadType(red));
252   EXPECT_EQ(voice_codec.pltype, red);
253   EXPECT_EQ(0, rtp_receiver1_->RegisterReceivePayload(
254       voice_codec.plname,
255       voice_codec.pltype,
256       voice_codec.plfreq,
257       voice_codec.channels,
258       (voice_codec.rate < 0) ? 0 : voice_codec.rate));
259   EXPECT_EQ(0, rtp_receiver2_->RegisterReceivePayload(
260       voice_codec.plname,
261       voice_codec.pltype,
262       voice_codec.plfreq,
263       voice_codec.channels,
264       (voice_codec.rate < 0) ? 0 : voice_codec.rate));
265
266   RTPFragmentationHeader fragmentation;
267   fragmentation.fragmentationVectorSize = 2;
268   fragmentation.fragmentationLength = new uint32_t[2];
269   fragmentation.fragmentationLength[0] = 4;
270   fragmentation.fragmentationLength[1] = 4;
271   fragmentation.fragmentationOffset = new uint32_t[2];
272   fragmentation.fragmentationOffset[0] = 0;
273   fragmentation.fragmentationOffset[1] = 4;
274   fragmentation.fragmentationTimeDiff = new uint16_t[2];
275   fragmentation.fragmentationTimeDiff[0] = 0;
276   fragmentation.fragmentationTimeDiff[1] = 0;
277   fragmentation.fragmentationPlType = new uint8_t[2];
278   fragmentation.fragmentationPlType[0] = 96;
279   fragmentation.fragmentationPlType[1] = 96;
280
281   const uint8_t test[5] = "test";
282   // Send a RTP packet.
283   EXPECT_EQ(0, module1->SendOutgoingData(webrtc::kAudioFrameSpeech,
284                                          96, 160, -1, test, 4,
285                                          &fragmentation));
286
287   EXPECT_EQ(0, module1->SetSendREDPayloadType(-1));
288   EXPECT_EQ(-1, module1->SendREDPayloadType(red));
289 }
290
291 TEST_F(RtpRtcpAudioTest, DTMF) {
292   CodecInst voice_codec;
293   memset(&voice_codec, 0, sizeof(voice_codec));
294   voice_codec.pltype = 96;
295   voice_codec.plfreq = 8000;
296   memcpy(voice_codec.plname, "PCMU", 5);
297
298   EXPECT_EQ(0, module1->RegisterSendPayload(voice_codec));
299   EXPECT_EQ(0, rtp_receiver1_->RegisterReceivePayload(
300       voice_codec.plname,
301       voice_codec.pltype,
302       voice_codec.plfreq,
303       voice_codec.channels,
304       (voice_codec.rate < 0) ? 0 : voice_codec.rate));
305   EXPECT_EQ(0, module2->RegisterSendPayload(voice_codec));
306   voice_codec.rate = test_rate;
307   EXPECT_EQ(0, rtp_receiver2_->RegisterReceivePayload(
308       voice_codec.plname,
309       voice_codec.pltype,
310       voice_codec.plfreq,
311       voice_codec.channels,
312       (voice_codec.rate < 0) ? 0 : voice_codec.rate));
313
314   module1->SetSSRC(test_ssrc);
315   EXPECT_EQ(0, module1->SetStartTimestamp(test_timestamp));
316   EXPECT_EQ(0, module1->SetSendingStatus(true));
317
318   // Prepare for DTMF.
319   voice_codec.pltype = 97;
320   voice_codec.plfreq = 8000;
321   memcpy(voice_codec.plname, "telephone-event", 16);
322
323   EXPECT_EQ(0, module1->RegisterSendPayload(voice_codec));
324   EXPECT_EQ(0, rtp_receiver2_->RegisterReceivePayload(
325         voice_codec.plname,
326         voice_codec.pltype,
327         voice_codec.plfreq,
328         voice_codec.channels,
329         (voice_codec.rate < 0) ? 0 : voice_codec.rate));
330
331   // Start DTMF test.
332   uint32_t timeStamp = 160;
333
334   // Send a DTMF tone using RFC 2833 (4733).
335   for (int i = 0; i < 16; i++) {
336     EXPECT_EQ(0, module1->SendTelephoneEventOutband(i, timeStamp, 10));
337   }
338   timeStamp += 160;  // Prepare for next packet.
339
340   const uint8_t test[9] = "test";
341
342   // Send RTP packets for 16 tones a 160 ms  100ms
343   // pause between = 2560ms + 1600ms = 4160ms
344   for (;timeStamp <= 250 * 160; timeStamp += 160) {
345     EXPECT_EQ(0, module1->SendOutgoingData(webrtc::kAudioFrameSpeech, 96,
346                                            timeStamp, -1, test, 4));
347     fake_clock.AdvanceTimeMilliseconds(20);
348     module1->Process();
349   }
350   EXPECT_EQ(0, module1->SendTelephoneEventOutband(32, 9000, 10));
351
352   for (;timeStamp <= 740 * 160; timeStamp += 160) {
353     EXPECT_EQ(0, module1->SendOutgoingData(webrtc::kAudioFrameSpeech, 96,
354                                            timeStamp, -1, test, 4));
355     fake_clock.AdvanceTimeMilliseconds(20);
356     module1->Process();
357   }
358 }