1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 // 5 // This test generate synthetic data. For audio it's a sinusoid waveform with 6 // frequency kSoundFrequency and different amplitudes. For video it's a pattern 7 // that is shifting by one pixel per frame, each pixels neighbors right and down 8 // is this pixels value +1, since the pixel value is 8 bit it will wrap 9 // frequently within the image. Visually this will create diagonally color bands 10 // that moves across the screen 11 12 #include <math.h> 13 14 #include <list> 15 16 #include "base/bind.h" 17 #include "base/bind_helpers.h" 18 #include "base/test/simple_test_tick_clock.h" 19 #include "base/time/tick_clock.h" 20 #include "media/base/video_frame.h" 21 #include "media/cast/cast_config.h" 22 #include "media/cast/cast_environment.h" 23 #include "media/cast/cast_receiver.h" 24 #include "media/cast/cast_sender.h" 25 #include "media/cast/test/audio_utility.h" 26 #include "media/cast/test/crypto_utility.h" 27 #include "media/cast/test/fake_task_runner.h" 28 #include "media/cast/test/video_utility.h" 29 #include "testing/gtest/include/gtest/gtest.h" 30 31 namespace media { 32 namespace cast { 33 34 static const int64 kStartMillisecond = GG_INT64_C(1245); 35 static const int kAudioChannels = 2; 36 static const double kSoundFrequency = 314.15926535897; // Freq of sine wave. 37 static const float kSoundVolume = 0.5f; 38 static const int kVideoWidth = 1280; 39 static const int kVideoHeight = 720; 40 static const int kCommonRtpHeaderLength = 12; 41 static const uint8 kCastReferenceFrameIdBitReset = 0xDF; // Mask is 0x40. 42 43 // Since the video encoded and decoded an error will be introduced; when 44 // comparing individual pixels the error can be quite large; we allow a PSNR of 45 // at least |kVideoAcceptedPSNR|. 46 static const double kVideoAcceptedPSNR = 38.0; 47 48 // The tests are commonly implemented with |kFrameTimerMs| RunTask function; 49 // a normal video is 30 fps hence the 33 ms between frames. 50 static const int kFrameTimerMs = 33; 51 52 // The packets pass through the pacer which can delay the beginning of the 53 // frame by 10 ms if there is packets belonging to the previous frame being 54 // retransmitted. 55 static const int kTimerErrorMs = 15; 56 57 namespace { 58 // Dummy callback function that does nothing except to accept ownership of 59 // |audio_bus| for destruction. 60 void OwnThatAudioBus(scoped_ptr<AudioBus> audio_bus) { 61 } 62 } // namespace 63 64 // Class that sends the packet direct from sender into the receiver with the 65 // ability to drop packets between the two. 66 class LoopBackTransport : public PacketSender { 67 public: 68 explicit LoopBackTransport(scoped_refptr<CastEnvironment> cast_environment) 69 : packet_receiver_(NULL), 70 send_packets_(true), 71 drop_packets_belonging_to_odd_frames_(false), 72 reset_reference_frame_id_(false), 73 cast_environment_(cast_environment) { 74 } 75 76 void RegisterPacketReceiver(PacketReceiver* packet_receiver) { 77 DCHECK(packet_receiver); 78 packet_receiver_ = packet_receiver; 79 } 80 81 virtual bool SendPacket(const Packet& packet) OVERRIDE { 82 DCHECK(packet_receiver_); 83 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); 84 if (!send_packets_) return false; 85 86 uint8* packet_copy = new uint8[packet.size()]; 87 memcpy(packet_copy, packet.data(), packet.size()); 88 packet_receiver_->ReceivedPacket(packet_copy, packet.size(), 89 base::Bind(PacketReceiver::DeletePacket, packet_copy)); 90 return true; 91 } 92 93 virtual bool SendPackets(const PacketList& packets) OVERRIDE { 94 DCHECK(packet_receiver_); 95 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); 96 if (!send_packets_) return false; 97 98 for (size_t i = 0; i < packets.size(); ++i) { 99 const Packet& packet = packets[i]; 100 if (drop_packets_belonging_to_odd_frames_) { 101 uint32 frame_id = packet[13]; 102 if (frame_id % 2 == 1) continue; 103 } 104 uint8* packet_copy = new uint8[packet.size()]; 105 memcpy(packet_copy, packet.data(), packet.size()); 106 if (reset_reference_frame_id_) { 107 // Reset the is_reference bit in the cast header. 108 packet_copy[kCommonRtpHeaderLength] &= kCastReferenceFrameIdBitReset; 109 } 110 packet_receiver_->ReceivedPacket(packet_copy, packet.size(), 111 base::Bind(PacketReceiver::DeletePacket, packet_copy)); 112 } 113 return true; 114 } 115 116 void SetSendPackets(bool send_packets) { 117 send_packets_ = send_packets; 118 } 119 120 void DropAllPacketsBelongingToOddFrames() { 121 drop_packets_belonging_to_odd_frames_ = true; 122 } 123 124 void AlwaysResetReferenceFrameId() { 125 reset_reference_frame_id_ = true; 126 } 127 128 private: 129 PacketReceiver* packet_receiver_; 130 bool send_packets_; 131 bool drop_packets_belonging_to_odd_frames_; 132 bool reset_reference_frame_id_; 133 scoped_refptr<CastEnvironment> cast_environment_; 134 }; 135 136 // Class that verifies the audio frames coming out of the receiver. 137 class TestReceiverAudioCallback : 138 public base::RefCountedThreadSafe<TestReceiverAudioCallback> { 139 public: 140 struct ExpectedAudioFrame { 141 PcmAudioFrame audio_frame; 142 int num_10ms_blocks; 143 base::TimeTicks record_time; 144 }; 145 146 TestReceiverAudioCallback() 147 : num_called_(0) {} 148 149 void SetExpectedSamplingFrequency(int expected_sampling_frequency) { 150 expected_sampling_frequency_ = expected_sampling_frequency; 151 } 152 153 void AddExpectedResult(scoped_ptr<PcmAudioFrame> audio_frame, 154 int expected_num_10ms_blocks, 155 const base::TimeTicks& record_time) { 156 ExpectedAudioFrame expected_audio_frame; 157 expected_audio_frame.audio_frame = *audio_frame; 158 expected_audio_frame.num_10ms_blocks = expected_num_10ms_blocks; 159 expected_audio_frame.record_time = record_time; 160 expected_frame_.push_back(expected_audio_frame); 161 } 162 163 void IgnoreAudioFrame(scoped_ptr<PcmAudioFrame> audio_frame, 164 const base::TimeTicks& playout_time) {} 165 166 // Check the audio frame parameters but not the audio samples. 167 void CheckBasicAudioFrame(const scoped_ptr<PcmAudioFrame>& audio_frame, 168 const base::TimeTicks& playout_time) { 169 EXPECT_FALSE(expected_frame_.empty()); // Test for bug in test code. 170 ExpectedAudioFrame expected_audio_frame = expected_frame_.front(); 171 EXPECT_EQ(audio_frame->channels, kAudioChannels); 172 EXPECT_EQ(audio_frame->frequency, expected_sampling_frequency_); 173 EXPECT_EQ(static_cast<int>(audio_frame->samples.size()), 174 expected_audio_frame.num_10ms_blocks * kAudioChannels * 175 expected_sampling_frequency_ / 100); 176 177 const base::TimeTicks upper_bound = expected_audio_frame.record_time + 178 base::TimeDelta::FromMilliseconds(kDefaultRtpMaxDelayMs + 179 kTimerErrorMs); 180 EXPECT_GE(upper_bound, playout_time) 181 << "playout_time - upper_bound == " 182 << (playout_time - upper_bound).InMicroseconds() << " usec"; 183 EXPECT_LT(expected_audio_frame.record_time, playout_time) 184 << "playout_time - expected == " 185 << (playout_time - expected_audio_frame.record_time).InMilliseconds() 186 << " mS"; 187 188 EXPECT_EQ(audio_frame->samples.size(), 189 expected_audio_frame.audio_frame.samples.size()); 190 } 191 192 void CheckPcmAudioFrame(scoped_ptr<PcmAudioFrame> audio_frame, 193 const base::TimeTicks& playout_time) { 194 ++num_called_; 195 196 CheckBasicAudioFrame(audio_frame, playout_time); 197 ExpectedAudioFrame expected_audio_frame = expected_frame_.front(); 198 expected_frame_.pop_front(); 199 if (audio_frame->samples.size() == 0) return; // No more checks needed. 200 201 EXPECT_NEAR(CountZeroCrossings(expected_audio_frame.audio_frame.samples), 202 CountZeroCrossings(audio_frame->samples), 203 1); 204 } 205 206 void CheckCodedPcmAudioFrame(scoped_ptr<EncodedAudioFrame> audio_frame, 207 const base::TimeTicks& playout_time) { 208 ++num_called_; 209 210 EXPECT_FALSE(expected_frame_.empty()); // Test for bug in test code. 211 ExpectedAudioFrame expected_audio_frame = expected_frame_.front(); 212 expected_frame_.pop_front(); 213 214 EXPECT_EQ(static_cast<int>(audio_frame->data.size()), 215 2 * kAudioChannels * expected_sampling_frequency_ / 100); 216 217 base::TimeDelta time_since_recording = 218 playout_time - expected_audio_frame.record_time; 219 220 EXPECT_LE(time_since_recording, base::TimeDelta::FromMilliseconds( 221 kDefaultRtpMaxDelayMs + kTimerErrorMs)); 222 223 EXPECT_LT(expected_audio_frame.record_time, playout_time); 224 if (audio_frame->data.size() == 0) return; // No more checks needed. 225 226 // We need to convert our "coded" audio frame to our raw format. 227 std::vector<int16> output_audio_samples; 228 size_t number_of_samples = audio_frame->data.size() / 2; 229 230 for (size_t i = 0; i < number_of_samples; ++i) { 231 uint16 sample = 232 static_cast<uint8>(audio_frame->data[1 + i * sizeof(uint16)]) + 233 (static_cast<uint16>(audio_frame->data[i * sizeof(uint16)]) << 8); 234 output_audio_samples.push_back(static_cast<int16>(sample)); 235 } 236 237 EXPECT_NEAR(CountZeroCrossings(expected_audio_frame.audio_frame.samples), 238 CountZeroCrossings(output_audio_samples), 239 1); 240 } 241 242 int number_times_called() const { 243 return num_called_; 244 } 245 246 protected: 247 virtual ~TestReceiverAudioCallback() {} 248 249 private: 250 friend class base::RefCountedThreadSafe<TestReceiverAudioCallback>; 251 252 int num_called_; 253 int expected_sampling_frequency_; 254 std::list<ExpectedAudioFrame> expected_frame_; 255 }; 256 257 // Class that verifies the video frames coming out of the receiver. 258 class TestReceiverVideoCallback : 259 public base::RefCountedThreadSafe<TestReceiverVideoCallback> { 260 public: 261 struct ExpectedVideoFrame { 262 int start_value; 263 int width; 264 int height; 265 base::TimeTicks capture_time; 266 }; 267 268 TestReceiverVideoCallback() 269 : num_called_(0) {} 270 271 void AddExpectedResult(int start_value, 272 int width, 273 int height, 274 const base::TimeTicks& capture_time) { 275 ExpectedVideoFrame expected_video_frame; 276 expected_video_frame.start_value = start_value; 277 expected_video_frame.capture_time = capture_time; 278 expected_video_frame.width = width; 279 expected_video_frame.height = height; 280 expected_frame_.push_back(expected_video_frame); 281 } 282 283 void CheckVideoFrame(const scoped_refptr<media::VideoFrame>& video_frame, 284 const base::TimeTicks& render_time) { 285 ++num_called_; 286 287 EXPECT_FALSE(expected_frame_.empty()); // Test for bug in test code. 288 ExpectedVideoFrame expected_video_frame = expected_frame_.front(); 289 expected_frame_.pop_front(); 290 291 base::TimeDelta time_since_capture = 292 render_time - expected_video_frame.capture_time; 293 const base::TimeDelta upper_bound = base::TimeDelta::FromMilliseconds( 294 kDefaultRtpMaxDelayMs + kTimerErrorMs); 295 296 EXPECT_GE(upper_bound, time_since_capture) 297 << "time_since_capture - upper_bound == " 298 << (time_since_capture - upper_bound).InMilliseconds() << " mS"; 299 EXPECT_LE(expected_video_frame.capture_time, render_time); 300 EXPECT_EQ(expected_video_frame.width, video_frame->coded_size().width()); 301 EXPECT_EQ(expected_video_frame.height, video_frame->coded_size().height()); 302 303 gfx::Size size(expected_video_frame.width, expected_video_frame.height); 304 scoped_refptr<media::VideoFrame> expected_I420_frame = 305 media::VideoFrame::CreateFrame( 306 VideoFrame::I420, size, gfx::Rect(size), size, base::TimeDelta()); 307 PopulateVideoFrame(expected_I420_frame, expected_video_frame.start_value); 308 309 EXPECT_GE(I420PSNR(expected_I420_frame, video_frame), kVideoAcceptedPSNR); 310 } 311 312 int number_times_called() { return num_called_;} 313 314 protected: 315 virtual ~TestReceiverVideoCallback() {} 316 317 private: 318 friend class base::RefCountedThreadSafe<TestReceiverVideoCallback>; 319 320 int num_called_; 321 std::list<ExpectedVideoFrame> expected_frame_; 322 }; 323 324 CastLoggingConfig EnableCastLoggingConfig() { 325 CastLoggingConfig config; 326 config.enable_data_collection = true; 327 return config; 328 } 329 // The actual test class, generate synthetic data for both audio and video and 330 // send those through the sender and receiver and analyzes the result. 331 class End2EndTest : public ::testing::Test { 332 protected: 333 End2EndTest() 334 : task_runner_(new test::FakeTaskRunner(&testing_clock_)), 335 cast_environment_(new CastEnvironment(&testing_clock_, task_runner_, 336 task_runner_, task_runner_, task_runner_, task_runner_, 337 EnableCastLoggingConfig())), 338 start_time_(), 339 sender_to_receiver_(cast_environment_), 340 receiver_to_sender_(cast_environment_), 341 test_receiver_audio_callback_(new TestReceiverAudioCallback()), 342 test_receiver_video_callback_(new TestReceiverVideoCallback()) { 343 testing_clock_.Advance( 344 base::TimeDelta::FromMilliseconds(kStartMillisecond)); 345 } 346 347 void SetupConfig(AudioCodec audio_codec, 348 int audio_sampling_frequency, 349 // TODO(miu): 3rd arg is meaningless?!? 350 bool external_audio_decoder, 351 int max_number_of_video_buffers_used) { 352 audio_sender_config_.sender_ssrc = 1; 353 audio_sender_config_.incoming_feedback_ssrc = 2; 354 audio_sender_config_.rtp_payload_type = 96; 355 audio_sender_config_.use_external_encoder = false; 356 audio_sender_config_.frequency = audio_sampling_frequency; 357 audio_sender_config_.channels = kAudioChannels; 358 audio_sender_config_.bitrate = kDefaultAudioEncoderBitrate; 359 audio_sender_config_.codec = audio_codec; 360 361 audio_receiver_config_.feedback_ssrc = 362 audio_sender_config_.incoming_feedback_ssrc; 363 audio_receiver_config_.incoming_ssrc = 364 audio_sender_config_.sender_ssrc; 365 audio_receiver_config_.rtp_payload_type = 366 audio_sender_config_.rtp_payload_type; 367 audio_receiver_config_.use_external_decoder = external_audio_decoder; 368 audio_receiver_config_.frequency = audio_sender_config_.frequency; 369 audio_receiver_config_.channels = kAudioChannels; 370 audio_receiver_config_.codec = audio_sender_config_.codec; 371 372 test_receiver_audio_callback_->SetExpectedSamplingFrequency( 373 audio_receiver_config_.frequency); 374 375 video_sender_config_.sender_ssrc = 3; 376 video_sender_config_.incoming_feedback_ssrc = 4; 377 video_sender_config_.rtp_payload_type = 97; 378 video_sender_config_.use_external_encoder = false; 379 video_sender_config_.width = kVideoWidth; 380 video_sender_config_.height = kVideoHeight; 381 video_sender_config_.max_bitrate = 5000000; 382 video_sender_config_.min_bitrate = 1000000; 383 video_sender_config_.start_bitrate = 5000000; 384 video_sender_config_.max_qp = 30; 385 video_sender_config_.min_qp = 4; 386 video_sender_config_.max_frame_rate = 30; 387 video_sender_config_.max_number_of_video_buffers_used = 388 max_number_of_video_buffers_used; 389 video_sender_config_.codec = kVp8; 390 video_sender_config_.number_of_cores = 1; 391 392 video_receiver_config_.feedback_ssrc = 393 video_sender_config_.incoming_feedback_ssrc; 394 video_receiver_config_.incoming_ssrc = 395 video_sender_config_.sender_ssrc; 396 video_receiver_config_.rtp_payload_type = 397 video_sender_config_.rtp_payload_type; 398 video_receiver_config_.use_external_decoder = false; 399 video_receiver_config_.codec = video_sender_config_.codec; 400 } 401 402 void Create() { 403 cast_receiver_.reset(CastReceiver::CreateCastReceiver(cast_environment_, 404 audio_receiver_config_, video_receiver_config_, &receiver_to_sender_)); 405 406 cast_sender_.reset(CastSender::CreateCastSender(cast_environment_, 407 audio_sender_config_, 408 video_sender_config_, 409 NULL, 410 &sender_to_receiver_)); 411 412 receiver_to_sender_.RegisterPacketReceiver(cast_sender_->packet_receiver()); 413 sender_to_receiver_.RegisterPacketReceiver( 414 cast_receiver_->packet_receiver()); 415 416 frame_input_ = cast_sender_->frame_input(); 417 frame_receiver_ = cast_receiver_->frame_receiver(); 418 419 audio_bus_factory_.reset(new TestAudioBusFactory( 420 audio_sender_config_.channels, audio_sender_config_.frequency, 421 kSoundFrequency, kSoundVolume)); 422 } 423 424 virtual ~End2EndTest() {} 425 426 void SendVideoFrame(int start_value, const base::TimeTicks& capture_time) { 427 if (start_time_.is_null()) 428 start_time_ = testing_clock_.NowTicks(); 429 start_time_ = testing_clock_.NowTicks(); 430 base::TimeDelta time_diff = testing_clock_.NowTicks() - start_time_; 431 gfx::Size size(kVideoWidth, kVideoHeight); 432 EXPECT_TRUE(VideoFrame::IsValidConfig(VideoFrame::I420, 433 size, gfx::Rect(size), size)); 434 scoped_refptr<media::VideoFrame> video_frame = 435 media::VideoFrame::CreateFrame( 436 VideoFrame::I420, size, gfx::Rect(size), size, time_diff); 437 PopulateVideoFrame(video_frame, start_value); 438 frame_input_->InsertRawVideoFrame(video_frame, capture_time); 439 } 440 441 void RunTasks(int during_ms) { 442 for (int i = 0; i < during_ms; ++i) { 443 // Call process the timers every 1 ms. 444 testing_clock_.Advance(base::TimeDelta::FromMilliseconds(1)); 445 task_runner_->RunTasks(); 446 } 447 } 448 449 AudioReceiverConfig audio_receiver_config_; 450 VideoReceiverConfig video_receiver_config_; 451 AudioSenderConfig audio_sender_config_; 452 VideoSenderConfig video_sender_config_; 453 454 base::SimpleTestTickClock testing_clock_; 455 scoped_refptr<test::FakeTaskRunner> task_runner_; 456 scoped_refptr<CastEnvironment> cast_environment_; 457 base::TimeTicks start_time_; 458 459 LoopBackTransport sender_to_receiver_; 460 LoopBackTransport receiver_to_sender_; 461 462 scoped_ptr<CastReceiver> cast_receiver_; 463 scoped_ptr<CastSender> cast_sender_; 464 scoped_refptr<FrameInput> frame_input_; 465 scoped_refptr<FrameReceiver> frame_receiver_; 466 467 scoped_refptr<TestReceiverAudioCallback> test_receiver_audio_callback_; 468 scoped_refptr<TestReceiverVideoCallback> test_receiver_video_callback_; 469 470 scoped_ptr<TestAudioBusFactory> audio_bus_factory_; 471 }; 472 473 // Audio and video test without packet loss using raw PCM 16 audio "codec"; 474 TEST_F(End2EndTest, LoopNoLossPcm16) { 475 SetupConfig(kPcm16, 32000, false, 1); 476 Create(); 477 478 int video_start = 1; 479 int audio_diff = kFrameTimerMs; 480 int i = 0; 481 482 std::cout << "Progress "; 483 for (; i < 300; ++i) { 484 int num_10ms_blocks = audio_diff / 10; 485 audio_diff -= num_10ms_blocks * 10; 486 base::TimeTicks send_time = testing_clock_.NowTicks(); 487 488 test_receiver_video_callback_->AddExpectedResult(video_start, 489 video_sender_config_.width, video_sender_config_.height, send_time); 490 491 scoped_ptr<AudioBus> audio_bus(audio_bus_factory_->NextAudioBus( 492 base::TimeDelta::FromMilliseconds(10) * num_10ms_blocks)); 493 494 if (i != 0) { 495 // Due to the re-sampler and NetEq in the webrtc AudioCodingModule the 496 // first samples will be 0 and then slowly ramp up to its real amplitude; 497 // ignore the first frame. 498 test_receiver_audio_callback_->AddExpectedResult( 499 ToPcmAudioFrame(*audio_bus, audio_sender_config_.frequency), 500 num_10ms_blocks, send_time); 501 } 502 503 AudioBus* const audio_bus_ptr = audio_bus.get(); 504 frame_input_->InsertAudio(audio_bus_ptr, send_time, 505 base::Bind(&OwnThatAudioBus, base::Passed(&audio_bus))); 506 507 SendVideoFrame(video_start, send_time); 508 509 RunTasks(kFrameTimerMs); 510 audio_diff += kFrameTimerMs; 511 512 if (i == 0) { 513 frame_receiver_->GetRawAudioFrame(num_10ms_blocks, 514 audio_sender_config_.frequency, 515 base::Bind(&TestReceiverAudioCallback::IgnoreAudioFrame, 516 test_receiver_audio_callback_)); 517 } else { 518 frame_receiver_->GetRawAudioFrame(num_10ms_blocks, 519 audio_sender_config_.frequency, 520 base::Bind(&TestReceiverAudioCallback::CheckPcmAudioFrame, 521 test_receiver_audio_callback_)); 522 } 523 524 frame_receiver_->GetRawVideoFrame( 525 base::Bind(&TestReceiverVideoCallback::CheckVideoFrame, 526 test_receiver_video_callback_)); 527 528 std::cout << " " << i << std::flush; 529 video_start++; 530 } 531 std::cout << std::endl; 532 533 RunTasks(2 * kFrameTimerMs + 1); // Empty the receiver pipeline. 534 EXPECT_EQ(i - 1, test_receiver_audio_callback_->number_times_called()); 535 EXPECT_EQ(i, test_receiver_video_callback_->number_times_called()); 536 } 537 538 // This tests our external decoder interface for Audio. 539 // Audio test without packet loss using raw PCM 16 audio "codec"; 540 TEST_F(End2EndTest, LoopNoLossPcm16ExternalDecoder) { 541 SetupConfig(kPcm16, 32000, true, 1); 542 Create(); 543 544 int i = 0; 545 for (; i < 100; ++i) { 546 base::TimeTicks send_time = testing_clock_.NowTicks(); 547 scoped_ptr<AudioBus> audio_bus(audio_bus_factory_->NextAudioBus( 548 base::TimeDelta::FromMilliseconds(10))); 549 test_receiver_audio_callback_->AddExpectedResult( 550 ToPcmAudioFrame(*audio_bus, audio_sender_config_.frequency), 551 1, send_time); 552 553 AudioBus* const audio_bus_ptr = audio_bus.get(); 554 frame_input_->InsertAudio(audio_bus_ptr, send_time, 555 base::Bind(&OwnThatAudioBus, base::Passed(&audio_bus))); 556 557 RunTasks(10); 558 frame_receiver_->GetCodedAudioFrame( 559 base::Bind(&TestReceiverAudioCallback::CheckCodedPcmAudioFrame, 560 test_receiver_audio_callback_)); 561 } 562 RunTasks(2 * kFrameTimerMs + 1); // Empty the receiver pipeline. 563 EXPECT_EQ(100, test_receiver_audio_callback_->number_times_called()); 564 } 565 566 // This tests our Opus audio codec without video. 567 TEST_F(End2EndTest, LoopNoLossOpus) { 568 SetupConfig(kOpus, kDefaultAudioSamplingRate, false, 1); 569 Create(); 570 571 int i = 0; 572 for (; i < 100; ++i) { 573 int num_10ms_blocks = 3; 574 base::TimeTicks send_time = testing_clock_.NowTicks(); 575 576 scoped_ptr<AudioBus> audio_bus(audio_bus_factory_->NextAudioBus( 577 base::TimeDelta::FromMilliseconds(10) * num_10ms_blocks)); 578 579 if (i != 0) { 580 test_receiver_audio_callback_->AddExpectedResult( 581 ToPcmAudioFrame(*audio_bus, audio_sender_config_.frequency), 582 num_10ms_blocks, send_time); 583 } 584 585 AudioBus* const audio_bus_ptr = audio_bus.get(); 586 frame_input_->InsertAudio(audio_bus_ptr, send_time, 587 base::Bind(&OwnThatAudioBus, base::Passed(&audio_bus))); 588 589 RunTasks(30); 590 591 if (i == 0) { 592 frame_receiver_->GetRawAudioFrame(num_10ms_blocks, 593 audio_sender_config_.frequency, 594 base::Bind(&TestReceiverAudioCallback::IgnoreAudioFrame, 595 test_receiver_audio_callback_)); 596 } else { 597 frame_receiver_->GetRawAudioFrame(num_10ms_blocks, 598 audio_sender_config_.frequency, 599 base::Bind(&TestReceiverAudioCallback::CheckPcmAudioFrame, 600 test_receiver_audio_callback_)); 601 } 602 } 603 RunTasks(2 * kFrameTimerMs + 1); // Empty the receiver pipeline. 604 EXPECT_EQ(i - 1, test_receiver_audio_callback_->number_times_called()); 605 } 606 607 // This tests start sending audio and video before the receiver is ready. 608 // 609 // TODO(miu): Test disabled because of non-determinism. 610 // http://crbug.com/314233 611 TEST_F(End2EndTest, DISABLED_StartSenderBeforeReceiver) { 612 SetupConfig(kOpus, kDefaultAudioSamplingRate, false, 1); 613 Create(); 614 615 int video_start = 1; 616 int audio_diff = kFrameTimerMs; 617 618 sender_to_receiver_.SetSendPackets(false); 619 620 for (int i = 0; i < 3; ++i) { 621 int num_10ms_blocks = audio_diff / 10; 622 audio_diff -= num_10ms_blocks * 10; 623 624 base::TimeTicks send_time = testing_clock_.NowTicks(); 625 scoped_ptr<AudioBus> audio_bus(audio_bus_factory_->NextAudioBus( 626 base::TimeDelta::FromMilliseconds(10) * num_10ms_blocks)); 627 628 AudioBus* const audio_bus_ptr = audio_bus.get(); 629 frame_input_->InsertAudio(audio_bus_ptr, send_time, 630 base::Bind(&OwnThatAudioBus, base::Passed(&audio_bus))); 631 632 SendVideoFrame(video_start, send_time); 633 RunTasks(kFrameTimerMs); 634 audio_diff += kFrameTimerMs; 635 video_start++; 636 } 637 RunTasks(100); 638 sender_to_receiver_.SetSendPackets(true); 639 640 int j = 0; 641 const int number_of_audio_frames_to_ignore = 3; 642 for (; j < 10; ++j) { 643 int num_10ms_blocks = audio_diff / 10; 644 audio_diff -= num_10ms_blocks * 10; 645 base::TimeTicks send_time = testing_clock_.NowTicks(); 646 647 scoped_ptr<AudioBus> audio_bus(audio_bus_factory_->NextAudioBus( 648 base::TimeDelta::FromMilliseconds(10) * num_10ms_blocks)); 649 650 if (j >= number_of_audio_frames_to_ignore) { 651 test_receiver_audio_callback_->AddExpectedResult( 652 ToPcmAudioFrame(*audio_bus, audio_sender_config_.frequency), 653 num_10ms_blocks, send_time); 654 } 655 656 AudioBus* const audio_bus_ptr = audio_bus.get(); 657 frame_input_->InsertAudio(audio_bus_ptr, send_time, 658 base::Bind(&OwnThatAudioBus, base::Passed(&audio_bus))); 659 660 test_receiver_video_callback_->AddExpectedResult(video_start, 661 video_sender_config_.width, video_sender_config_.height, send_time); 662 663 SendVideoFrame(video_start, send_time); 664 RunTasks(kFrameTimerMs); 665 audio_diff += kFrameTimerMs; 666 667 if (j < number_of_audio_frames_to_ignore) { 668 frame_receiver_->GetRawAudioFrame(num_10ms_blocks, 669 audio_sender_config_.frequency, 670 base::Bind(&TestReceiverAudioCallback::IgnoreAudioFrame, 671 test_receiver_audio_callback_)); 672 } else { 673 frame_receiver_->GetRawAudioFrame(num_10ms_blocks, 674 audio_sender_config_.frequency, 675 base::Bind(&TestReceiverAudioCallback::CheckPcmAudioFrame, 676 test_receiver_audio_callback_)); 677 } 678 frame_receiver_->GetRawVideoFrame( 679 base::Bind(&TestReceiverVideoCallback::CheckVideoFrame, 680 test_receiver_video_callback_)); 681 video_start++; 682 } 683 RunTasks(2 * kFrameTimerMs + 1); // Empty the receiver pipeline. 684 EXPECT_EQ(j - number_of_audio_frames_to_ignore, 685 test_receiver_audio_callback_->number_times_called()); 686 EXPECT_EQ(j, test_receiver_video_callback_->number_times_called()); 687 } 688 689 // This tests a network glitch lasting for 10 video frames. 690 TEST_F(End2EndTest, GlitchWith3Buffers) { 691 SetupConfig(kOpus, kDefaultAudioSamplingRate, false, 3); 692 video_sender_config_.rtp_max_delay_ms = 67; 693 video_receiver_config_.rtp_max_delay_ms = 67; 694 Create(); 695 696 int video_start = 50; 697 base::TimeTicks send_time = testing_clock_.NowTicks(); 698 SendVideoFrame(video_start, send_time); 699 RunTasks(kFrameTimerMs); 700 701 test_receiver_video_callback_->AddExpectedResult(video_start, 702 video_sender_config_.width, video_sender_config_.height, send_time); 703 frame_receiver_->GetRawVideoFrame( 704 base::Bind(&TestReceiverVideoCallback::CheckVideoFrame, 705 test_receiver_video_callback_)); 706 707 RunTasks(750); // Make sure that we send a RTCP packet. 708 709 video_start++; 710 711 // Introduce a glitch lasting for 10 frames. 712 sender_to_receiver_.SetSendPackets(false); 713 for (int i = 0; i < 10; ++i) { 714 send_time = testing_clock_.NowTicks(); 715 // First 3 will be sent and lost. 716 SendVideoFrame(video_start, send_time); 717 RunTasks(kFrameTimerMs); 718 video_start++; 719 } 720 sender_to_receiver_.SetSendPackets(true); 721 RunTasks(100); 722 send_time = testing_clock_.NowTicks(); 723 724 // Frame 1 should be acked by now and we should have an opening to send 4. 725 SendVideoFrame(video_start, send_time); 726 RunTasks(kFrameTimerMs); 727 728 test_receiver_video_callback_->AddExpectedResult(video_start, 729 video_sender_config_.width, video_sender_config_.height, send_time); 730 731 frame_receiver_->GetRawVideoFrame( 732 base::Bind(&TestReceiverVideoCallback::CheckVideoFrame, 733 test_receiver_video_callback_)); 734 735 RunTasks(2 * kFrameTimerMs + 1); // Empty the receiver pipeline. 736 EXPECT_EQ(2, test_receiver_video_callback_->number_times_called()); 737 } 738 739 TEST_F(End2EndTest, DropEveryOtherFrame3Buffers) { 740 SetupConfig(kOpus, kDefaultAudioSamplingRate, false, 3); 741 video_sender_config_.rtp_max_delay_ms = 67; 742 video_receiver_config_.rtp_max_delay_ms = 67; 743 Create(); 744 sender_to_receiver_.DropAllPacketsBelongingToOddFrames(); 745 746 int video_start = 50; 747 base::TimeTicks send_time; 748 749 std::cout << "Progress "; 750 int i = 0; 751 for (; i < 20; ++i) { 752 send_time = testing_clock_.NowTicks(); 753 SendVideoFrame(video_start, send_time); 754 755 if (i % 2 == 0) { 756 test_receiver_video_callback_->AddExpectedResult(video_start, 757 video_sender_config_.width, video_sender_config_.height, send_time); 758 759 // GetRawVideoFrame will not return the frame until we are close in 760 // time before we should render the frame. 761 frame_receiver_->GetRawVideoFrame( 762 base::Bind(&TestReceiverVideoCallback::CheckVideoFrame, 763 test_receiver_video_callback_)); 764 } 765 RunTasks(kFrameTimerMs); 766 std::cout << " " << i << std::flush; 767 video_start++; 768 } 769 std::cout << std::endl; 770 RunTasks(2 * kFrameTimerMs + 1); // Empty the pipeline. 771 EXPECT_EQ(i / 2, test_receiver_video_callback_->number_times_called()); 772 } 773 774 TEST_F(End2EndTest, ResetReferenceFrameId) { 775 SetupConfig(kOpus, kDefaultAudioSamplingRate, false, 3); 776 video_sender_config_.rtp_max_delay_ms = 67; 777 video_receiver_config_.rtp_max_delay_ms = 67; 778 Create(); 779 sender_to_receiver_.AlwaysResetReferenceFrameId(); 780 781 int frames_counter = 0; 782 for (; frames_counter < 20; ++frames_counter) { 783 const base::TimeTicks send_time = testing_clock_.NowTicks(); 784 SendVideoFrame(frames_counter, send_time); 785 786 test_receiver_video_callback_->AddExpectedResult(frames_counter, 787 video_sender_config_.width, video_sender_config_.height, send_time); 788 789 // GetRawVideoFrame will not return the frame until we are close to the 790 // time in which we should render the frame. 791 frame_receiver_->GetRawVideoFrame( 792 base::Bind(&TestReceiverVideoCallback::CheckVideoFrame, 793 test_receiver_video_callback_)); 794 RunTasks(kFrameTimerMs); 795 } 796 RunTasks(2 * kFrameTimerMs + 1); // Empty the pipeline. 797 EXPECT_EQ(frames_counter, 798 test_receiver_video_callback_->number_times_called()); 799 } 800 801 TEST_F(End2EndTest, CryptoVideo) { 802 SetupConfig(kPcm16, 32000, false, 1); 803 804 video_sender_config_.aes_iv_mask = 805 ConvertFromBase16String("1234567890abcdeffedcba0987654321"); 806 video_sender_config_.aes_key = 807 ConvertFromBase16String("deadbeefcafeb0b0b0b0cafedeadbeef"); 808 809 video_receiver_config_.aes_iv_mask = video_sender_config_.aes_iv_mask; 810 video_receiver_config_.aes_key = video_sender_config_.aes_key; 811 812 Create(); 813 814 int frames_counter = 0; 815 for (; frames_counter < 20; ++frames_counter) { 816 const base::TimeTicks send_time = testing_clock_.NowTicks(); 817 818 SendVideoFrame(frames_counter, send_time); 819 820 test_receiver_video_callback_->AddExpectedResult(frames_counter, 821 video_sender_config_.width, video_sender_config_.height, send_time); 822 823 // GetRawVideoFrame will not return the frame until we are close to the 824 // time in which we should render the frame. 825 frame_receiver_->GetRawVideoFrame( 826 base::Bind(&TestReceiverVideoCallback::CheckVideoFrame, 827 test_receiver_video_callback_)); 828 RunTasks(kFrameTimerMs); 829 } 830 RunTasks(2 * kFrameTimerMs + 1); // Empty the pipeline. 831 EXPECT_EQ(frames_counter, 832 test_receiver_video_callback_->number_times_called()); 833 } 834 835 TEST_F(End2EndTest, CryptoAudio) { 836 SetupConfig(kPcm16, 32000, false, 1); 837 838 audio_sender_config_.aes_iv_mask = 839 ConvertFromBase16String("abcdeffedcba12345678900987654321"); 840 audio_sender_config_.aes_key = 841 ConvertFromBase16String("deadbeefcafecafedeadbeefb0b0b0b0"); 842 843 audio_receiver_config_.aes_iv_mask = audio_sender_config_.aes_iv_mask; 844 audio_receiver_config_.aes_key = audio_sender_config_.aes_key; 845 846 Create(); 847 848 int frames_counter = 0; 849 for (; frames_counter < 20; ++frames_counter) { 850 int num_10ms_blocks = 2; 851 852 const base::TimeTicks send_time = testing_clock_.NowTicks(); 853 854 scoped_ptr<AudioBus> audio_bus(audio_bus_factory_->NextAudioBus( 855 base::TimeDelta::FromMilliseconds(10) * num_10ms_blocks)); 856 857 if (frames_counter != 0) { 858 // Due to the re-sampler and NetEq in the webrtc AudioCodingModule the 859 // first samples will be 0 and then slowly ramp up to its real amplitude; 860 // ignore the first frame. 861 test_receiver_audio_callback_->AddExpectedResult( 862 ToPcmAudioFrame(*audio_bus, audio_sender_config_.frequency), 863 num_10ms_blocks, send_time); 864 } 865 AudioBus* const audio_bus_ptr = audio_bus.get(); 866 frame_input_->InsertAudio(audio_bus_ptr, send_time, 867 base::Bind(&OwnThatAudioBus, base::Passed(&audio_bus))); 868 869 RunTasks(num_10ms_blocks * 10); 870 871 if (frames_counter == 0) { 872 frame_receiver_->GetRawAudioFrame(num_10ms_blocks, 873 32000, 874 base::Bind(&TestReceiverAudioCallback::IgnoreAudioFrame, 875 test_receiver_audio_callback_)); 876 } else { 877 frame_receiver_->GetRawAudioFrame(num_10ms_blocks, 878 32000, 879 base::Bind(&TestReceiverAudioCallback::CheckPcmAudioFrame, 880 test_receiver_audio_callback_)); 881 } 882 } 883 RunTasks(2 * kFrameTimerMs + 1); // Empty the pipeline. 884 EXPECT_EQ(frames_counter - 1, 885 test_receiver_audio_callback_->number_times_called()); 886 } 887 888 // Video test without packet loss; This test is targeted at testing the logging 889 // aspects of the end2end, but is basically equivalent to LoopNoLossPcm16. 890 TEST_F(End2EndTest, VideoLogging) { 891 SetupConfig(kPcm16, 32000, false, 1); 892 Create(); 893 894 int video_start = 1; 895 int i = 0; 896 for (; i < 1; ++i) { 897 base::TimeTicks send_time = testing_clock_.NowTicks(); 898 test_receiver_video_callback_->AddExpectedResult(video_start, 899 video_sender_config_.width, video_sender_config_.height, send_time); 900 901 SendVideoFrame(video_start, send_time); 902 RunTasks(kFrameTimerMs); 903 904 frame_receiver_->GetRawVideoFrame( 905 base::Bind(&TestReceiverVideoCallback::CheckVideoFrame, 906 test_receiver_video_callback_)); 907 908 video_start++; 909 } 910 911 // Basic tests. 912 RunTasks(2 * kFrameTimerMs + 1); // Empty the receiver pipeline. 913 EXPECT_EQ(i, test_receiver_video_callback_->number_times_called()); 914 // Logging tests. 915 LoggingImpl* logging = cast_environment_->Logging(); 916 917 // Frame logging. 918 919 // Verify that all frames and all required events were logged. 920 FrameRawMap frame_raw_log = logging->GetFrameRawData(); 921 // Every frame should have only one entry. 922 EXPECT_EQ(static_cast<unsigned int>(i), frame_raw_log.size()); 923 FrameRawMap::const_iterator frame_it = frame_raw_log.begin(); 924 // Choose a video frame, and verify that all events were logged. 925 std::vector<CastLoggingEvent> event_log = frame_it->second.type; 926 EXPECT_TRUE((std::find(event_log.begin(), event_log.end(), 927 kVideoFrameReceived)) != event_log.end()); 928 EXPECT_TRUE((std::find(event_log.begin(), event_log.end(), 929 kVideoFrameSentToEncoder)) != event_log.end()); 930 EXPECT_TRUE((std::find(event_log.begin(), event_log.end(), 931 kVideoFrameEncoded)) != event_log.end()); 932 EXPECT_TRUE((std::find(event_log.begin(), event_log.end(), 933 kVideoRenderDelay)) != event_log.end()); 934 // TODO(mikhal): Plumb this one through. 935 EXPECT_TRUE((std::find(event_log.begin(), event_log.end(), 936 kVideoFrameDecoded)) == event_log.end()); 937 // Verify that there were no other events logged with respect to this frame. 938 EXPECT_EQ(4u, event_log.size()); 939 940 // Packet logging. 941 // Verify that all packet related events were logged. 942 PacketRawMap packet_raw_log = logging->GetPacketRawData(); 943 // Every rtp_timestamp should have only one entry. 944 EXPECT_EQ(static_cast<unsigned int>(i), packet_raw_log.size()); 945 PacketRawMap::const_iterator packet_it = packet_raw_log.begin(); 946 // Choose a packet, and verify that all events were logged. 947 event_log = (++(packet_it->second.packet_map.begin()))->second.type; 948 EXPECT_TRUE((std::find(event_log.begin(), event_log.end(), 949 kPacketSentToPacer)) != event_log.end()); 950 EXPECT_TRUE((std::find(event_log.begin(), event_log.end(), 951 kPacketSentToNetwork)) != event_log.end()); 952 EXPECT_TRUE((std::find(event_log.begin(), event_log.end(), 953 kPacketReceived)) != event_log.end()); 954 // Verify that there were no other events logged with respect to this frame. 955 EXPECT_EQ(3u, event_log.size()); 956 } 957 958 // Audio test without packet loss; This test is targeted at testing the logging 959 // aspects of the end2end, but is basically equivalent to LoopNoLossPcm16. 960 TEST_F(End2EndTest, AudioLogging) { 961 SetupConfig(kPcm16, 32000, false, 1); 962 Create(); 963 964 int audio_diff = kFrameTimerMs; 965 int i = 0; 966 967 for (; i < 10; ++i) { 968 int num_10ms_blocks = audio_diff / 10; 969 audio_diff -= num_10ms_blocks * 10; 970 base::TimeTicks send_time = testing_clock_.NowTicks(); 971 972 scoped_ptr<AudioBus> audio_bus(audio_bus_factory_->NextAudioBus( 973 base::TimeDelta::FromMilliseconds(10) * num_10ms_blocks)); 974 975 if (i != 0) { 976 // Due to the re-sampler and NetEq in the webrtc AudioCodingModule the 977 // first samples will be 0 and then slowly ramp up to its real amplitude; 978 // ignore the first frame. 979 test_receiver_audio_callback_->AddExpectedResult( 980 ToPcmAudioFrame(*audio_bus, audio_sender_config_.frequency), 981 num_10ms_blocks, send_time); 982 } 983 984 AudioBus* const audio_bus_ptr = audio_bus.get(); 985 frame_input_->InsertAudio(audio_bus_ptr, send_time, 986 base::Bind(&OwnThatAudioBus, base::Passed(&audio_bus))); 987 988 RunTasks(kFrameTimerMs); 989 audio_diff += kFrameTimerMs; 990 991 if (i == 0) { 992 frame_receiver_->GetRawAudioFrame(num_10ms_blocks, 993 audio_sender_config_.frequency, 994 base::Bind(&TestReceiverAudioCallback::IgnoreAudioFrame, 995 test_receiver_audio_callback_)); 996 } else { 997 frame_receiver_->GetRawAudioFrame(num_10ms_blocks, 998 audio_sender_config_.frequency, 999 base::Bind(&TestReceiverAudioCallback::CheckPcmAudioFrame, 1000 test_receiver_audio_callback_)); 1001 } 1002 } 1003 1004 // Basic tests. 1005 RunTasks(2 * kFrameTimerMs + 1); // Empty the receiver pipeline. 1006 //EXPECT_EQ(i - 1, test_receiver_audio_callback_->number_times_called()); 1007 EXPECT_EQ(i - 1, test_receiver_audio_callback_->number_times_called()); 1008 // Logging tests. 1009 LoggingImpl* logging = cast_environment_->Logging(); 1010 // Verify that all frames and all required events were logged. 1011 FrameRawMap frame_raw_log = logging->GetFrameRawData(); 1012 // TODO(mikhal): Results are wrong. Need to resolve passing/calculation of 1013 // rtp_timestamp for audio for this to work. 1014 // Should have logged both audio and video. Every frame should have only one 1015 // entry. 1016 //EXPECT_EQ(static_cast<unsigned int>(i - 1), frame_raw_log.size()); 1017 FrameRawMap::const_iterator frame_it = frame_raw_log.begin(); 1018 // Choose a video frame, and verify that all events were logged. 1019 std::vector<CastLoggingEvent> event_log = frame_it->second.type; 1020 EXPECT_TRUE((std::find(event_log.begin(), event_log.end(), 1021 kAudioFrameReceived)) != event_log.end()); 1022 EXPECT_TRUE((std::find(event_log.begin(), event_log.end(), 1023 kAudioFrameEncoded)) != event_log.end()); 1024 // EXPECT_TRUE((std::find(event_log.begin(), event_log.end(), 1025 // kAudioPlayoutDelay)) != event_log.end()); 1026 // TODO(mikhal): Plumb this one through. 1027 EXPECT_TRUE((std::find(event_log.begin(), event_log.end(), 1028 kAudioFrameDecoded)) == event_log.end()); 1029 // Verify that there were no other events logged with respect to this frame. 1030 EXPECT_EQ(2u, event_log.size()); 1031 } 1032 1033 1034 // TODO(pwestin): Add repeatable packet loss test. 1035 // TODO(pwestin): Add test for misaligned send get calls. 1036 // TODO(pwestin): Add more tests that does not resample. 1037 // TODO(pwestin): Add test when we have starvation for our RunTask. 1038 1039 } // namespace cast 1040 } // namespace media 1041