2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
13 #include "testing/gtest/include/gtest/gtest.h"
15 #include "webrtc/modules/rtp_rtcp/test/testAPI/test_api.h"
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"
22 using namespace webrtc;
24 #define test_rate 64000u
26 class VerifyingAudioReceiver : public NullRtpData {
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);
36 memcpy(str, payloadData, payloadSize);
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);
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.
55 ADD_FAILURE() << "This code path should never happen.";
62 class RTPCallback : public NullRtpFeedback {
64 virtual int32_t OnInitializeDecoder(
66 const int8_t payloadType,
67 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
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";
79 class RtpRtcpAudioTest : public ::testing::Test {
81 RtpRtcpAudioTest() : fake_clock(123456) {
86 test_timestamp = 4567;
87 test_sequence_number = 2345;
89 ~RtpRtcpAudioTest() {}
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();
99 receive_statistics1_.reset(ReceiveStatistics::Create(&fake_clock));
100 receive_statistics2_.reset(ReceiveStatistics::Create(&fake_clock));
102 rtp_payload_registry1_.reset(new RTPPayloadRegistry(
103 RTPPayloadStrategy::CreateStrategy(true)));
104 rtp_payload_registry2_.reset(new RTPPayloadRegistry(
105 RTPPayloadStrategy::CreateStrategy(true)));
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;
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()));
120 configuration.id = test_id + 1;
121 configuration.receive_statistics = receive_statistics2_.get();
122 configuration.outgoing_transport = transport2;
123 configuration.audio_messages = audioFeedback;
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()));
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());
136 virtual void TearDown() OVERRIDE {
141 delete audioFeedback;
142 delete data_receiver1;
143 delete data_receiver2;
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;
163 uint32_t test_timestamp;
164 uint16_t test_sequence_number;
165 uint32_t test_CSRC[webrtc::kRtpCsrcSize];
166 SimulatedClock fake_clock;
169 TEST_F(RtpRtcpAudioTest, Basic) {
170 module1->SetSSRC(test_ssrc);
171 EXPECT_EQ(0, module1->SetStartTimestamp(test_timestamp));
173 // Test detection at the end of a DTMF tone.
174 //EXPECT_EQ(0, module2->SetTelephoneEventForwardToDecoder(true));
176 EXPECT_EQ(0, module1->SetSendingStatus(true));
178 // Start basic RTP test.
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));
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);
191 EXPECT_EQ(0, module1->RegisterSendPayload(voice_codec));
192 EXPECT_EQ(0, rtp_receiver1_->RegisterReceivePayload(
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(
204 voice_codec.channels,
205 (voice_codec.rate < 0) ? 0 : voice_codec.rate));
208 const uint8_t test[5] = "test";
209 EXPECT_EQ(0, module1->SendOutgoingData(webrtc::kAudioFrameSpeech, 96,
212 EXPECT_EQ(test_ssrc, rtp_receiver2_->SSRC());
214 EXPECT_TRUE(rtp_receiver2_->Timestamp(×tamp));
215 EXPECT_EQ(test_timestamp, timestamp);
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);
225 EXPECT_EQ(0, module1->RegisterSendPayload(voice_codec));
226 EXPECT_EQ(0, rtp_receiver1_->RegisterReceivePayload(
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(
238 voice_codec.channels,
239 (voice_codec.rate < 0) ? 0 : voice_codec.rate));
241 module1->SetSSRC(test_ssrc);
242 EXPECT_EQ(0, module1->SetStartTimestamp(test_timestamp));
243 EXPECT_EQ(0, module1->SetSendingStatus(true));
245 voice_codec.pltype = 127;
246 voice_codec.plfreq = 8000;
247 memcpy(voice_codec.plname, "RED", 4);
249 EXPECT_EQ(0, module1->SetSendREDPayloadType(voice_codec.pltype));
251 EXPECT_EQ(0, module1->SendREDPayloadType(red));
252 EXPECT_EQ(voice_codec.pltype, red);
253 EXPECT_EQ(0, rtp_receiver1_->RegisterReceivePayload(
257 voice_codec.channels,
258 (voice_codec.rate < 0) ? 0 : voice_codec.rate));
259 EXPECT_EQ(0, rtp_receiver2_->RegisterReceivePayload(
263 voice_codec.channels,
264 (voice_codec.rate < 0) ? 0 : voice_codec.rate));
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;
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,
287 EXPECT_EQ(0, module1->SetSendREDPayloadType(-1));
288 EXPECT_EQ(-1, module1->SendREDPayloadType(red));
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);
298 EXPECT_EQ(0, module1->RegisterSendPayload(voice_codec));
299 EXPECT_EQ(0, rtp_receiver1_->RegisterReceivePayload(
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(
311 voice_codec.channels,
312 (voice_codec.rate < 0) ? 0 : voice_codec.rate));
314 module1->SetSSRC(test_ssrc);
315 EXPECT_EQ(0, module1->SetStartTimestamp(test_timestamp));
316 EXPECT_EQ(0, module1->SetSendingStatus(true));
319 voice_codec.pltype = 97;
320 voice_codec.plfreq = 8000;
321 memcpy(voice_codec.plname, "telephone-event", 16);
323 EXPECT_EQ(0, module1->RegisterSendPayload(voice_codec));
324 EXPECT_EQ(0, rtp_receiver2_->RegisterReceivePayload(
328 voice_codec.channels,
329 (voice_codec.rate < 0) ? 0 : voice_codec.rate));
332 uint32_t timeStamp = 160;
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));
338 timeStamp += 160; // Prepare for next packet.
340 const uint8_t test[9] = "test";
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);
350 EXPECT_EQ(0, module1->SendTelephoneEventOutband(32, 9000, 10));
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);