Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(214)

Side by Side Diff: webrtc/media/base/rtpdataengine_unittest.cc

Issue 1823503002: Reland Use CopyOnWriteBuffer instead of Buffer to avoid unnecessary copies. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 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 10
11 #include <memory> 11 #include <memory>
12 #include <string> 12 #include <string>
13 13
14 #include "webrtc/base/buffer.h" 14 #include "webrtc/base/copyonwritebuffer.h"
15 #include "webrtc/base/gunit.h" 15 #include "webrtc/base/gunit.h"
16 #include "webrtc/base/helpers.h" 16 #include "webrtc/base/helpers.h"
17 #include "webrtc/base/ssladapter.h" 17 #include "webrtc/base/ssladapter.h"
18 #include "webrtc/base/timing.h" 18 #include "webrtc/base/timing.h"
19 #include "webrtc/media/base/fakenetworkinterface.h" 19 #include "webrtc/media/base/fakenetworkinterface.h"
20 #include "webrtc/media/base/mediaconstants.h" 20 #include "webrtc/media/base/mediaconstants.h"
21 #include "webrtc/media/base/rtpdataengine.h" 21 #include "webrtc/media/base/rtpdataengine.h"
22 #include "webrtc/media/base/rtputils.h" 22 #include "webrtc/media/base/rtputils.h"
23 23
24 class FakeTiming : public rtc::Timing { 24 class FakeTiming : public rtc::Timing {
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 cricket::ReceiveDataParams GetReceivedDataParams() { 117 cricket::ReceiveDataParams GetReceivedDataParams() {
118 return receiver_->last_received_data_params(); 118 return receiver_->last_received_data_params();
119 } 119 }
120 120
121 bool HasSentData(int count) { 121 bool HasSentData(int count) {
122 return (iface_->NumRtpPackets() > count); 122 return (iface_->NumRtpPackets() > count);
123 } 123 }
124 124
125 std::string GetSentData(int index) { 125 std::string GetSentData(int index) {
126 // Assume RTP header of length 12 126 // Assume RTP header of length 12
127 std::unique_ptr<const rtc::Buffer> packet( 127 std::unique_ptr<const rtc::CopyOnWriteBuffer> packet(
128 iface_->GetRtpPacket(index)); 128 iface_->GetRtpPacket(index));
129 if (packet->size() > 12) { 129 if (packet->size() > 12) {
130 return std::string(packet->data<char>() + 12, packet->size() - 12); 130 return std::string(packet->data<char>() + 12, packet->size() - 12);
131 } else { 131 } else {
132 return ""; 132 return "";
133 } 133 }
134 } 134 }
135 135
136 cricket::RtpHeader GetSentDataHeader(int index) { 136 cricket::RtpHeader GetSentDataHeader(int index) {
137 std::unique_ptr<const rtc::Buffer> packet( 137 std::unique_ptr<const rtc::CopyOnWriteBuffer> packet(
138 iface_->GetRtpPacket(index)); 138 iface_->GetRtpPacket(index));
139 cricket::RtpHeader header; 139 cricket::RtpHeader header;
140 GetRtpHeader(packet->data(), packet->size(), &header); 140 GetRtpHeader(packet->data(), packet->size(), &header);
141 return header; 141 return header;
142 } 142 }
143 143
144 private: 144 private:
145 std::unique_ptr<cricket::RtpDataEngine> dme_; 145 std::unique_ptr<cricket::RtpDataEngine> dme_;
146 // Timing passed into dme_. Owned by dme_; 146 // Timing passed into dme_. Owned by dme_;
147 FakeTiming* timing_; 147 FakeTiming* timing_;
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 EXPECT_TRUE(dmc->RemoveRecvStream(41)); 213 EXPECT_TRUE(dmc->RemoveRecvStream(41));
214 EXPECT_TRUE(dmc->RemoveRecvStream(42)); 214 EXPECT_TRUE(dmc->RemoveRecvStream(42));
215 } 215 }
216 216
217 TEST_F(RtpDataMediaChannelTest, SendData) { 217 TEST_F(RtpDataMediaChannelTest, SendData) {
218 std::unique_ptr<cricket::RtpDataMediaChannel> dmc(CreateChannel()); 218 std::unique_ptr<cricket::RtpDataMediaChannel> dmc(CreateChannel());
219 219
220 cricket::SendDataParams params; 220 cricket::SendDataParams params;
221 params.ssrc = 42; 221 params.ssrc = 42;
222 unsigned char data[] = "food"; 222 unsigned char data[] = "food";
223 rtc::Buffer payload(data, 4); 223 rtc::CopyOnWriteBuffer payload(data, 4);
224 unsigned char padded_data[] = { 224 unsigned char padded_data[] = {
225 0x00, 0x00, 0x00, 0x00, 225 0x00, 0x00, 0x00, 0x00,
226 'f', 'o', 'o', 'd', 226 'f', 'o', 'o', 'd',
227 }; 227 };
228 cricket::SendDataResult result; 228 cricket::SendDataResult result;
229 229
230 // Not sending 230 // Not sending
231 EXPECT_FALSE(dmc->SendData(params, payload, &result)); 231 EXPECT_FALSE(dmc->SendData(params, payload, &result));
232 EXPECT_EQ(cricket::SDR_ERROR, result); 232 EXPECT_EQ(cricket::SDR_ERROR, result);
233 EXPECT_FALSE(HasSentData(0)); 233 EXPECT_FALSE(HasSentData(0));
(...skipping 16 matching lines...) Expand all
250 cricket::DataCodec codec; 250 cricket::DataCodec codec;
251 codec.id = 103; 251 codec.id = 103;
252 codec.name = cricket::kGoogleRtpDataCodecName; 252 codec.name = cricket::kGoogleRtpDataCodecName;
253 cricket::DataSendParameters parameters; 253 cricket::DataSendParameters parameters;
254 parameters.codecs.push_back(codec); 254 parameters.codecs.push_back(codec);
255 ASSERT_TRUE(dmc->SetSendParameters(parameters)); 255 ASSERT_TRUE(dmc->SetSendParameters(parameters));
256 256
257 // Length too large; 257 // Length too large;
258 std::string x10000(10000, 'x'); 258 std::string x10000(10000, 'x');
259 EXPECT_FALSE(dmc->SendData( 259 EXPECT_FALSE(dmc->SendData(
260 params, rtc::Buffer(x10000.data(), x10000.length()), &result)); 260 params, rtc::CopyOnWriteBuffer(x10000.data(), x10000.length()), &result));
261 EXPECT_EQ(cricket::SDR_ERROR, result); 261 EXPECT_EQ(cricket::SDR_ERROR, result);
262 EXPECT_FALSE(HasSentData(0)); 262 EXPECT_FALSE(HasSentData(0));
263 263
264 // Finally works! 264 // Finally works!
265 EXPECT_TRUE(dmc->SendData(params, payload, &result)); 265 EXPECT_TRUE(dmc->SendData(params, payload, &result));
266 EXPECT_EQ(cricket::SDR_SUCCESS, result); 266 EXPECT_EQ(cricket::SDR_SUCCESS, result);
267 ASSERT_TRUE(HasSentData(0)); 267 ASSERT_TRUE(HasSentData(0));
268 EXPECT_EQ(sizeof(padded_data), GetSentData(0).length()); 268 EXPECT_EQ(sizeof(padded_data), GetSentData(0).length());
269 EXPECT_EQ(0, memcmp( 269 EXPECT_EQ(0, memcmp(
270 padded_data, GetSentData(0).data(), sizeof(padded_data))); 270 padded_data, GetSentData(0).data(), sizeof(padded_data)));
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 parameters.codecs.push_back(codec); 318 parameters.codecs.push_back(codec);
319 ASSERT_TRUE(dmc1->SetSendParameters(parameters)); 319 ASSERT_TRUE(dmc1->SetSendParameters(parameters));
320 ASSERT_TRUE(dmc2->SetSendParameters(parameters)); 320 ASSERT_TRUE(dmc2->SetSendParameters(parameters));
321 321
322 cricket::SendDataParams params1; 322 cricket::SendDataParams params1;
323 params1.ssrc = 41; 323 params1.ssrc = 41;
324 cricket::SendDataParams params2; 324 cricket::SendDataParams params2;
325 params2.ssrc = 42; 325 params2.ssrc = 42;
326 326
327 unsigned char data[] = "foo"; 327 unsigned char data[] = "foo";
328 rtc::Buffer payload(data, 3); 328 rtc::CopyOnWriteBuffer payload(data, 3);
329 cricket::SendDataResult result; 329 cricket::SendDataResult result;
330 330
331 EXPECT_TRUE(dmc1->SendData(params1, payload, &result)); 331 EXPECT_TRUE(dmc1->SendData(params1, payload, &result));
332 EXPECT_TRUE(dmc2->SendData(params2, payload, &result)); 332 EXPECT_TRUE(dmc2->SendData(params2, payload, &result));
333 333
334 // Should bump timestamp by 90000 because the clock rate is 90khz. 334 // Should bump timestamp by 90000 because the clock rate is 90khz.
335 timing1->set_now(1); 335 timing1->set_now(1);
336 // Should bump timestamp by 180000 because the clock rate is 90khz. 336 // Should bump timestamp by 180000 because the clock rate is 90khz.
337 timing2->set_now(2); 337 timing2->set_now(2);
338 338
(...skipping 26 matching lines...) Expand all
365 parameters.codecs.push_back(codec); 365 parameters.codecs.push_back(codec);
366 ASSERT_TRUE(dmc->SetSendParameters(parameters)); 366 ASSERT_TRUE(dmc->SetSendParameters(parameters));
367 367
368 cricket::StreamParams stream; 368 cricket::StreamParams stream;
369 stream.add_ssrc(42); 369 stream.add_ssrc(42);
370 ASSERT_TRUE(dmc->AddSendStream(stream)); 370 ASSERT_TRUE(dmc->AddSendStream(stream));
371 371
372 cricket::SendDataParams params; 372 cricket::SendDataParams params;
373 params.ssrc = 42; 373 params.ssrc = 42;
374 unsigned char data[] = "food"; 374 unsigned char data[] = "food";
375 rtc::Buffer payload(data, 4); 375 rtc::CopyOnWriteBuffer payload(data, 4);
376 cricket::SendDataResult result; 376 cricket::SendDataResult result;
377 377
378 // With rtp overhead of 32 bytes, each one of our packets is 36 378 // With rtp overhead of 32 bytes, each one of our packets is 36
379 // bytes, or 288 bits. So, a limit of 872bps will allow 3 packets, 379 // bytes, or 288 bits. So, a limit of 872bps will allow 3 packets,
380 // but not four. 380 // but not four.
381 parameters.max_bandwidth_bps = 872; 381 parameters.max_bandwidth_bps = 872;
382 ASSERT_TRUE(dmc->SetSendParameters(parameters)); 382 ASSERT_TRUE(dmc->SetSendParameters(parameters));
383 383
384 EXPECT_TRUE(dmc->SendData(params, payload, &result)); 384 EXPECT_TRUE(dmc->SendData(params, payload, &result));
385 EXPECT_TRUE(dmc->SendData(params, payload, &result)); 385 EXPECT_TRUE(dmc->SendData(params, payload, &result));
(...skipping 17 matching lines...) Expand all
403 EXPECT_FALSE(dmc->SendData(params, payload, &result)); 403 EXPECT_FALSE(dmc->SendData(params, payload, &result));
404 } 404 }
405 405
406 TEST_F(RtpDataMediaChannelTest, ReceiveData) { 406 TEST_F(RtpDataMediaChannelTest, ReceiveData) {
407 // PT= 103, SN=2, TS=3, SSRC = 4, data = "abcde" 407 // PT= 103, SN=2, TS=3, SSRC = 4, data = "abcde"
408 unsigned char data[] = { 408 unsigned char data[] = {
409 0x80, 0x67, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x2A, 409 0x80, 0x67, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x2A,
410 0x00, 0x00, 0x00, 0x00, 410 0x00, 0x00, 0x00, 0x00,
411 'a', 'b', 'c', 'd', 'e' 411 'a', 'b', 'c', 'd', 'e'
412 }; 412 };
413 rtc::Buffer packet(data, sizeof(data)); 413 rtc::CopyOnWriteBuffer packet(data, sizeof(data));
414 414
415 std::unique_ptr<cricket::RtpDataMediaChannel> dmc(CreateChannel()); 415 std::unique_ptr<cricket::RtpDataMediaChannel> dmc(CreateChannel());
416 416
417 // SetReceived not called. 417 // SetReceived not called.
418 dmc->OnPacketReceived(&packet, rtc::PacketTime()); 418 dmc->OnPacketReceived(&packet, rtc::PacketTime());
419 EXPECT_FALSE(HasReceivedData()); 419 EXPECT_FALSE(HasReceivedData());
420 420
421 dmc->SetReceive(true); 421 dmc->SetReceive(true);
422 422
423 // Unknown payload id 423 // Unknown payload id
(...skipping 19 matching lines...) Expand all
443 dmc->OnPacketReceived(&packet, rtc::PacketTime()); 443 dmc->OnPacketReceived(&packet, rtc::PacketTime());
444 EXPECT_TRUE(HasReceivedData()); 444 EXPECT_TRUE(HasReceivedData());
445 EXPECT_EQ("abcde", GetReceivedData()); 445 EXPECT_EQ("abcde", GetReceivedData());
446 EXPECT_EQ(5U, GetReceivedDataLen()); 446 EXPECT_EQ(5U, GetReceivedDataLen());
447 } 447 }
448 448
449 TEST_F(RtpDataMediaChannelTest, InvalidRtpPackets) { 449 TEST_F(RtpDataMediaChannelTest, InvalidRtpPackets) {
450 unsigned char data[] = { 450 unsigned char data[] = {
451 0x80, 0x65, 0x00, 0x02 451 0x80, 0x65, 0x00, 0x02
452 }; 452 };
453 rtc::Buffer packet(data, sizeof(data)); 453 rtc::CopyOnWriteBuffer packet(data, sizeof(data));
454 454
455 std::unique_ptr<cricket::RtpDataMediaChannel> dmc(CreateChannel()); 455 std::unique_ptr<cricket::RtpDataMediaChannel> dmc(CreateChannel());
456 456
457 // Too short 457 // Too short
458 dmc->OnPacketReceived(&packet, rtc::PacketTime()); 458 dmc->OnPacketReceived(&packet, rtc::PacketTime());
459 EXPECT_FALSE(HasReceivedData()); 459 EXPECT_FALSE(HasReceivedData());
460 } 460 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698