Ignore title parameter for navigator.registerProtocolHandler
[chromium-blink-merge.git] / media / cast / test / end2end_unittest.cc
blob6ee722e3f859bbc4c715b4c72d583a2a9671a995
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
12 #include <math.h>
13 #include <stdint.h>
15 #include <functional>
16 #include <list>
17 #include <map>
19 #include "base/bind.h"
20 #include "base/bind_helpers.h"
21 #include "base/stl_util.h"
22 #include "base/strings/string_number_conversions.h"
23 #include "base/sys_byteorder.h"
24 #include "base/test/simple_test_tick_clock.h"
25 #include "base/time/tick_clock.h"
26 #include "media/base/audio_bus.h"
27 #include "media/base/video_frame.h"
28 #include "media/cast/cast_config.h"
29 #include "media/cast/cast_environment.h"
30 #include "media/cast/cast_receiver.h"
31 #include "media/cast/cast_sender.h"
32 #include "media/cast/logging/simple_event_subscriber.h"
33 #include "media/cast/test/fake_single_thread_task_runner.h"
34 #include "media/cast/test/utility/audio_utility.h"
35 #include "media/cast/test/utility/default_config.h"
36 #include "media/cast/test/utility/udp_proxy.h"
37 #include "media/cast/test/utility/video_utility.h"
38 #include "media/cast/transport/cast_transport_config.h"
39 #include "media/cast/transport/cast_transport_defines.h"
40 #include "media/cast/transport/cast_transport_sender.h"
41 #include "media/cast/transport/cast_transport_sender_impl.h"
42 #include "testing/gtest/include/gtest/gtest.h"
44 namespace media {
45 namespace cast {
47 namespace {
49 static const int64 kStartMillisecond = INT64_C(1245);
50 static const int kAudioChannels = 2;
51 static const double kSoundFrequency = 314.15926535897; // Freq of sine wave.
52 static const float kSoundVolume = 0.5f;
53 static const int kVideoHdWidth = 1280;
54 static const int kVideoHdHeight = 720;
55 static const int kVideoQcifWidth = 176;
56 static const int kVideoQcifHeight = 144;
58 // Since the video encoded and decoded an error will be introduced; when
59 // comparing individual pixels the error can be quite large; we allow a PSNR of
60 // at least |kVideoAcceptedPSNR|.
61 static const double kVideoAcceptedPSNR = 38.0;
63 // The tests are commonly implemented with |kFrameTimerMs| RunTask function;
64 // a normal video is 30 fps hence the 33 ms between frames.
65 static const int kFrameTimerMs = 33;
67 // The packets pass through the pacer which can delay the beginning of the
68 // frame by 10 ms if there is packets belonging to the previous frame being
69 // retransmitted.
70 // In addition, audio packets are sent in 10mS intervals in audio_encoder.cc,
71 // although we send an audio frame every 33mS, which adds an extra delay.
72 // A TODO was added in the code to resolve this.
73 static const int kTimerErrorMs = 20;
75 // Start the video synthetic start value to medium range value, to avoid edge
76 // effects cause by encoding and quantization.
77 static const int kVideoStart = 100;
79 // The size of audio frames. The encoder joins/breaks all inserted audio into
80 // chunks of this size.
81 static const int kAudioFrameDurationMs = 10;
83 std::string ConvertFromBase16String(const std::string base_16) {
84 std::string compressed;
85 DCHECK_EQ(base_16.size() % 2, 0u) << "Must be a multiple of 2";
86 compressed.reserve(base_16.size() / 2);
88 std::vector<uint8> v;
89 if (!base::HexStringToBytes(base_16, &v)) {
90 NOTREACHED();
92 compressed.assign(reinterpret_cast<const char*>(&v[0]), v.size());
93 return compressed;
96 void UpdateCastTransportStatus(transport::CastTransportStatus status) {
97 bool result = (status == transport::TRANSPORT_AUDIO_INITIALIZED ||
98 status == transport::TRANSPORT_VIDEO_INITIALIZED);
99 EXPECT_TRUE(result);
102 void AudioInitializationStatus(CastInitializationStatus status) {
103 EXPECT_EQ(STATUS_AUDIO_INITIALIZED, status);
106 void VideoInitializationStatus(CastInitializationStatus status) {
107 EXPECT_EQ(STATUS_VIDEO_INITIALIZED, status);
110 // This is wrapped in a struct because it needs to be put into a std::map.
111 typedef struct {
112 int counter[kNumOfLoggingEvents+1];
113 } LoggingEventCounts;
115 // Constructs a map from each frame (RTP timestamp) to counts of each event
116 // type logged for that frame.
117 std::map<RtpTimestamp, LoggingEventCounts> GetEventCountForFrameEvents(
118 const std::vector<FrameEvent>& frame_events) {
119 std::map<RtpTimestamp, LoggingEventCounts> event_counter_for_frame;
120 for (std::vector<FrameEvent>::const_iterator it = frame_events.begin();
121 it != frame_events.end();
122 ++it) {
123 std::map<RtpTimestamp, LoggingEventCounts>::iterator map_it =
124 event_counter_for_frame.find(it->rtp_timestamp);
125 if (map_it == event_counter_for_frame.end()) {
126 LoggingEventCounts new_counter;
127 memset(&new_counter, 0, sizeof(new_counter));
128 ++(new_counter.counter[it->type]);
129 event_counter_for_frame.insert(
130 std::make_pair(it->rtp_timestamp, new_counter));
131 } else {
132 ++(map_it->second.counter[it->type]);
135 return event_counter_for_frame;
138 // Constructs a map from each packet (Packet ID) to counts of each event
139 // type logged for that packet.
140 std::map<uint16, LoggingEventCounts> GetEventCountForPacketEvents(
141 const std::vector<PacketEvent>& packet_events) {
142 std::map<uint16, LoggingEventCounts> event_counter_for_packet;
143 for (std::vector<PacketEvent>::const_iterator it = packet_events.begin();
144 it != packet_events.end();
145 ++it) {
146 std::map<uint16, LoggingEventCounts>::iterator map_it =
147 event_counter_for_packet.find(it->packet_id);
148 if (map_it == event_counter_for_packet.end()) {
149 LoggingEventCounts new_counter;
150 memset(&new_counter, 0, sizeof(new_counter));
151 ++(new_counter.counter[it->type]);
152 event_counter_for_packet.insert(
153 std::make_pair(it->packet_id, new_counter));
154 } else {
155 ++(map_it->second.counter[it->type]);
158 return event_counter_for_packet;
161 void CountVideoFrame(int* counter,
162 const scoped_refptr<media::VideoFrame>& video_frame,
163 const base::TimeTicks& render_time, bool continuous) {
164 ++*counter;
167 } // namespace
169 class LoopBackPacketPipe : public test::PacketPipe {
170 public:
171 LoopBackPacketPipe(const transport::PacketReceiverCallback& packet_receiver)
172 : packet_receiver_(packet_receiver) {}
174 virtual ~LoopBackPacketPipe() {}
176 // PacketPipe implementations.
177 virtual void Send(scoped_ptr<transport::Packet> packet) OVERRIDE {
178 packet_receiver_.Run(packet.Pass());
181 private:
182 transport::PacketReceiverCallback packet_receiver_;
185 // Class that sends the packet direct from sender into the receiver with the
186 // ability to drop packets between the two.
187 class LoopBackTransport : public transport::PacketSender {
188 public:
189 explicit LoopBackTransport(scoped_refptr<CastEnvironment> cast_environment)
190 : send_packets_(true),
191 drop_packets_belonging_to_odd_frames_(false),
192 cast_environment_(cast_environment) {}
194 void SetPacketReceiver(
195 const transport::PacketReceiverCallback& packet_receiver) {
196 scoped_ptr<test::PacketPipe> loopback_pipe(
197 new LoopBackPacketPipe(packet_receiver));
198 if (packet_pipe_) {
199 packet_pipe_->AppendToPipe(loopback_pipe.Pass());
200 } else {
201 packet_pipe_ = loopback_pipe.Pass();
205 virtual bool SendPacket(transport::PacketRef packet,
206 const base::Closure& cb) OVERRIDE {
207 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
208 if (!send_packets_)
209 return false;
211 if (drop_packets_belonging_to_odd_frames_) {
212 uint32 frame_id = packet->data[13];
213 if (frame_id % 2 == 1)
214 return true;
217 scoped_ptr<Packet> packet_copy(new Packet(packet->data));
218 packet_pipe_->Send(packet_copy.Pass());
219 return true;
222 void SetSendPackets(bool send_packets) { send_packets_ = send_packets; }
224 void DropAllPacketsBelongingToOddFrames() {
225 drop_packets_belonging_to_odd_frames_ = true;
228 void SetPacketPipe(scoped_ptr<test::PacketPipe> pipe) {
229 // Append the loopback pipe to the end.
230 pipe->AppendToPipe(packet_pipe_.Pass());
231 packet_pipe_ = pipe.Pass();
234 private:
235 bool send_packets_;
236 bool drop_packets_belonging_to_odd_frames_;
237 scoped_refptr<CastEnvironment> cast_environment_;
238 scoped_ptr<test::PacketPipe> packet_pipe_;
241 // Class that verifies the audio frames coming out of the receiver.
242 class TestReceiverAudioCallback
243 : public base::RefCountedThreadSafe<TestReceiverAudioCallback> {
244 public:
245 struct ExpectedAudioFrame {
246 scoped_ptr<AudioBus> audio_bus;
247 base::TimeTicks record_time;
250 TestReceiverAudioCallback() : num_called_(0) {}
252 void SetExpectedSamplingFrequency(int expected_sampling_frequency) {
253 expected_sampling_frequency_ = expected_sampling_frequency;
256 void AddExpectedResult(const AudioBus& audio_bus,
257 const base::TimeTicks& record_time) {
258 scoped_ptr<ExpectedAudioFrame> expected_audio_frame(
259 new ExpectedAudioFrame());
260 expected_audio_frame->audio_bus =
261 AudioBus::Create(audio_bus.channels(), audio_bus.frames()).Pass();
262 audio_bus.CopyTo(expected_audio_frame->audio_bus.get());
263 expected_audio_frame->record_time = record_time;
264 expected_frames_.push_back(expected_audio_frame.release());
267 void IgnoreAudioFrame(scoped_ptr<AudioBus> audio_bus,
268 const base::TimeTicks& playout_time,
269 bool is_continuous) {
270 ++num_called_;
273 void CheckAudioFrame(scoped_ptr<AudioBus> audio_bus,
274 const base::TimeTicks& playout_time,
275 bool is_continuous) {
276 ++num_called_;
278 ASSERT_TRUE(!!audio_bus);
279 ASSERT_FALSE(expected_frames_.empty());
280 const scoped_ptr<ExpectedAudioFrame> expected_audio_frame(
281 expected_frames_.front());
282 expected_frames_.pop_front();
284 EXPECT_EQ(audio_bus->channels(), kAudioChannels);
285 EXPECT_EQ(audio_bus->frames(), expected_audio_frame->audio_bus->frames());
286 for (int ch = 0; ch < audio_bus->channels(); ++ch) {
287 EXPECT_NEAR(CountZeroCrossings(
288 expected_audio_frame->audio_bus->channel(ch),
289 expected_audio_frame->audio_bus->frames()),
290 CountZeroCrossings(audio_bus->channel(ch),
291 audio_bus->frames()),
295 // TODO(miu): This is a "fuzzy" way to check the timestamps. We should be
296 // able to compute exact offsets with "omnipotent" knowledge of the system.
297 const base::TimeTicks upper_bound =
298 expected_audio_frame->record_time +
299 base::TimeDelta::FromMilliseconds(kDefaultRtpMaxDelayMs +
300 kTimerErrorMs);
301 EXPECT_GE(upper_bound, playout_time)
302 << "playout_time - upper_bound == "
303 << (playout_time - upper_bound).InMicroseconds() << " usec";
305 EXPECT_TRUE(is_continuous);
308 void CheckCodedAudioFrame(
309 scoped_ptr<transport::EncodedAudioFrame> audio_frame,
310 const base::TimeTicks& playout_time) {
311 ASSERT_TRUE(!!audio_frame);
312 ASSERT_FALSE(expected_frames_.empty());
313 const ExpectedAudioFrame& expected_audio_frame =
314 *(expected_frames_.front());
315 // Note: Just peeking here. Will delegate to CheckAudioFrame() to pop.
317 // We need to "decode" the encoded audio frame. The codec is simply to
318 // swizzle the bytes of each int16 from host-->network-->host order to get
319 // interleaved int16 PCM. Then, make an AudioBus out of that.
320 const int num_elements = audio_frame->data.size() / sizeof(int16);
321 ASSERT_EQ(expected_audio_frame.audio_bus->channels() *
322 expected_audio_frame.audio_bus->frames(),
323 num_elements);
324 int16* const pcm_data =
325 reinterpret_cast<int16*>(string_as_array(&audio_frame->data));
326 for (int i = 0; i < num_elements; ++i)
327 pcm_data[i] = static_cast<int16>(base::NetToHost16(pcm_data[i]));
328 scoped_ptr<AudioBus> audio_bus(
329 AudioBus::Create(expected_audio_frame.audio_bus->channels(),
330 expected_audio_frame.audio_bus->frames()));
331 audio_bus->FromInterleaved(pcm_data, audio_bus->frames(), sizeof(int16));
333 // Delegate the checking from here...
334 CheckAudioFrame(audio_bus.Pass(), playout_time, true);
337 int number_times_called() const { return num_called_; }
339 protected:
340 virtual ~TestReceiverAudioCallback() {
341 STLDeleteElements(&expected_frames_);
344 private:
345 friend class base::RefCountedThreadSafe<TestReceiverAudioCallback>;
347 int num_called_;
348 int expected_sampling_frequency_;
349 std::list<ExpectedAudioFrame*> expected_frames_;
352 // Class that verifies the video frames coming out of the receiver.
353 class TestReceiverVideoCallback
354 : public base::RefCountedThreadSafe<TestReceiverVideoCallback> {
355 public:
356 struct ExpectedVideoFrame {
357 int start_value;
358 int width;
359 int height;
360 base::TimeTicks capture_time;
361 bool should_be_continuous;
364 TestReceiverVideoCallback() : num_called_(0) {}
366 void AddExpectedResult(int start_value,
367 int width,
368 int height,
369 const base::TimeTicks& capture_time,
370 bool should_be_continuous) {
371 ExpectedVideoFrame expected_video_frame;
372 expected_video_frame.start_value = start_value;
373 expected_video_frame.width = width;
374 expected_video_frame.height = height;
375 expected_video_frame.capture_time = capture_time;
376 expected_video_frame.should_be_continuous = should_be_continuous;
377 expected_frame_.push_back(expected_video_frame);
380 void CheckVideoFrame(const scoped_refptr<media::VideoFrame>& video_frame,
381 const base::TimeTicks& render_time,
382 bool is_continuous) {
383 ++num_called_;
385 ASSERT_TRUE(!!video_frame);
386 ASSERT_FALSE(expected_frame_.empty());
387 ExpectedVideoFrame expected_video_frame = expected_frame_.front();
388 expected_frame_.pop_front();
390 base::TimeDelta time_since_capture =
391 render_time - expected_video_frame.capture_time;
392 const base::TimeDelta upper_bound = base::TimeDelta::FromMilliseconds(
393 kDefaultRtpMaxDelayMs + kTimerErrorMs);
395 // TODO(miu): This is a "fuzzy" way to check the timestamps. We should be
396 // able to compute exact offsets with "omnipotent" knowledge of the system.
397 EXPECT_GE(upper_bound, time_since_capture)
398 << "time_since_capture - upper_bound == "
399 << (time_since_capture - upper_bound).InMicroseconds() << " usec";
400 // TODO(miu): I broke the concept of 100 ms target delay timing on the
401 // receiver side, but the logic for computing playout time really isn't any
402 // more broken than it was. This only affects the receiver, and is to be
403 // rectified in an soon-upcoming change. http://crbug.com/356942
404 // EXPECT_LE(expected_video_frame.capture_time, render_time);
405 EXPECT_EQ(expected_video_frame.width, video_frame->visible_rect().width());
406 EXPECT_EQ(expected_video_frame.height,
407 video_frame->visible_rect().height());
409 gfx::Size size(expected_video_frame.width, expected_video_frame.height);
410 scoped_refptr<media::VideoFrame> expected_I420_frame =
411 media::VideoFrame::CreateFrame(
412 VideoFrame::I420, size, gfx::Rect(size), size, base::TimeDelta());
413 PopulateVideoFrame(expected_I420_frame, expected_video_frame.start_value);
415 EXPECT_GE(I420PSNR(expected_I420_frame, video_frame), kVideoAcceptedPSNR);
417 EXPECT_EQ(expected_video_frame.should_be_continuous, is_continuous);
420 int number_times_called() const { return num_called_; }
422 protected:
423 virtual ~TestReceiverVideoCallback() {}
425 private:
426 friend class base::RefCountedThreadSafe<TestReceiverVideoCallback>;
428 int num_called_;
429 std::list<ExpectedVideoFrame> expected_frame_;
432 // The actual test class, generate synthetic data for both audio and video and
433 // send those through the sender and receiver and analyzes the result.
434 class End2EndTest : public ::testing::Test {
435 protected:
436 End2EndTest()
437 : start_time_(),
438 testing_clock_sender_(new base::SimpleTestTickClock()),
439 testing_clock_receiver_(new base::SimpleTestTickClock()),
440 task_runner_(
441 new test::FakeSingleThreadTaskRunner(testing_clock_sender_)),
442 cast_environment_sender_(new CastEnvironment(
443 scoped_ptr<base::TickClock>(testing_clock_sender_).Pass(),
444 task_runner_,
445 task_runner_,
446 task_runner_)),
447 cast_environment_receiver_(new CastEnvironment(
448 scoped_ptr<base::TickClock>(testing_clock_receiver_).Pass(),
449 task_runner_,
450 task_runner_,
451 task_runner_)),
452 receiver_to_sender_(cast_environment_receiver_),
453 sender_to_receiver_(cast_environment_sender_),
454 test_receiver_audio_callback_(new TestReceiverAudioCallback()),
455 test_receiver_video_callback_(new TestReceiverVideoCallback()) {
456 testing_clock_sender_->Advance(
457 base::TimeDelta::FromMilliseconds(kStartMillisecond));
458 testing_clock_receiver_->Advance(
459 base::TimeDelta::FromMilliseconds(kStartMillisecond));
460 cast_environment_sender_->Logging()->AddRawEventSubscriber(
461 &event_subscriber_sender_);
464 void Configure(transport::VideoCodec video_codec,
465 transport::AudioCodec audio_codec,
466 int audio_sampling_frequency,
467 bool external_audio_decoder,
468 int max_number_of_video_buffers_used) {
469 audio_sender_config_.rtp_config.ssrc = 1;
470 audio_sender_config_.incoming_feedback_ssrc = 2;
471 audio_sender_config_.rtp_config.payload_type = 96;
472 audio_sender_config_.use_external_encoder = false;
473 audio_sender_config_.frequency = audio_sampling_frequency;
474 audio_sender_config_.channels = kAudioChannels;
475 audio_sender_config_.bitrate = kDefaultAudioEncoderBitrate;
476 audio_sender_config_.codec = audio_codec;
478 audio_receiver_config_.feedback_ssrc =
479 audio_sender_config_.incoming_feedback_ssrc;
480 audio_receiver_config_.incoming_ssrc = audio_sender_config_.rtp_config.ssrc;
481 audio_receiver_config_.rtp_payload_type =
482 audio_sender_config_.rtp_config.payload_type;
483 audio_receiver_config_.use_external_decoder = external_audio_decoder;
484 audio_receiver_config_.frequency = audio_sender_config_.frequency;
485 audio_receiver_config_.channels = kAudioChannels;
486 audio_receiver_config_.codec = audio_sender_config_.codec;
488 test_receiver_audio_callback_->SetExpectedSamplingFrequency(
489 audio_receiver_config_.frequency);
491 video_sender_config_.rtp_config.ssrc = 3;
492 video_sender_config_.incoming_feedback_ssrc = 4;
493 video_sender_config_.rtp_config.payload_type = 97;
494 video_sender_config_.use_external_encoder = false;
495 video_sender_config_.width = kVideoHdWidth;
496 video_sender_config_.height = kVideoHdHeight;
497 video_sender_config_.max_bitrate = 5000000;
498 video_sender_config_.min_bitrate = 1000000;
499 video_sender_config_.start_bitrate = 5000000;
500 video_sender_config_.max_qp = 30;
501 video_sender_config_.min_qp = 4;
502 video_sender_config_.max_frame_rate = 30;
503 video_sender_config_.max_number_of_video_buffers_used =
504 max_number_of_video_buffers_used;
505 video_sender_config_.codec = video_codec;
507 video_receiver_config_.feedback_ssrc =
508 video_sender_config_.incoming_feedback_ssrc;
509 video_receiver_config_.incoming_ssrc = video_sender_config_.rtp_config.ssrc;
510 video_receiver_config_.rtp_payload_type =
511 video_sender_config_.rtp_config.payload_type;
512 video_receiver_config_.use_external_decoder = false;
513 video_receiver_config_.codec = video_sender_config_.codec;
516 void FeedAudioFrames(int count, bool will_be_checked) {
517 for (int i = 0; i < count; ++i) {
518 scoped_ptr<AudioBus> audio_bus(audio_bus_factory_->NextAudioBus(
519 base::TimeDelta::FromMilliseconds(kAudioFrameDurationMs)));
520 const base::TimeTicks send_time =
521 testing_clock_sender_->NowTicks() +
522 i * base::TimeDelta::FromMilliseconds(kAudioFrameDurationMs);
523 if (will_be_checked)
524 test_receiver_audio_callback_->AddExpectedResult(*audio_bus, send_time);
525 audio_frame_input_->InsertAudio(audio_bus.Pass(), send_time);
529 void FeedAudioFramesWithExpectedDelay(int count,
530 const base::TimeDelta& delay) {
531 for (int i = 0; i < count; ++i) {
532 scoped_ptr<AudioBus> audio_bus(audio_bus_factory_->NextAudioBus(
533 base::TimeDelta::FromMilliseconds(kAudioFrameDurationMs)));
534 const base::TimeTicks send_time =
535 testing_clock_sender_->NowTicks() +
536 i * base::TimeDelta::FromMilliseconds(kAudioFrameDurationMs);
537 test_receiver_audio_callback_->AddExpectedResult(*audio_bus,
538 send_time + delay);
539 audio_frame_input_->InsertAudio(audio_bus.Pass(), send_time);
543 void RequestAudioFrames(int count, bool with_check) {
544 for (int i = 0; i < count; ++i) {
545 frame_receiver_->GetRawAudioFrame(
546 base::Bind(with_check ? &TestReceiverAudioCallback::CheckAudioFrame :
547 &TestReceiverAudioCallback::IgnoreAudioFrame,
548 test_receiver_audio_callback_));
552 void Create() {
553 cast_receiver_ = CastReceiver::Create(cast_environment_receiver_,
554 audio_receiver_config_,
555 video_receiver_config_,
556 &receiver_to_sender_);
557 net::IPEndPoint dummy_endpoint;
558 transport_sender_.reset(new transport::CastTransportSenderImpl(
559 NULL,
560 testing_clock_sender_,
561 dummy_endpoint,
562 base::Bind(&UpdateCastTransportStatus),
563 base::Bind(&End2EndTest::LogRawEvents, base::Unretained(this)),
564 base::TimeDelta::FromSeconds(1),
565 task_runner_,
566 &sender_to_receiver_));
568 cast_sender_ =
569 CastSender::Create(cast_environment_sender_, transport_sender_.get());
571 // Initializing audio and video senders.
572 cast_sender_->InitializeAudio(audio_sender_config_,
573 base::Bind(&AudioInitializationStatus));
574 cast_sender_->InitializeVideo(video_sender_config_,
575 base::Bind(&VideoInitializationStatus),
576 CreateDefaultVideoEncodeAcceleratorCallback(),
577 CreateDefaultVideoEncodeMemoryCallback());
579 receiver_to_sender_.SetPacketReceiver(cast_sender_->packet_receiver());
580 sender_to_receiver_.SetPacketReceiver(cast_receiver_->packet_receiver());
582 audio_frame_input_ = cast_sender_->audio_frame_input();
583 video_frame_input_ = cast_sender_->video_frame_input();
585 frame_receiver_ = cast_receiver_->frame_receiver();
587 audio_bus_factory_.reset(
588 new TestAudioBusFactory(audio_sender_config_.channels,
589 audio_sender_config_.frequency,
590 kSoundFrequency,
591 kSoundVolume));
594 virtual ~End2EndTest() {
595 cast_environment_sender_->Logging()->RemoveRawEventSubscriber(
596 &event_subscriber_sender_);
599 virtual void TearDown() OVERRIDE {
600 cast_sender_.reset();
601 cast_receiver_.reset();
602 task_runner_->RunTasks();
605 void SendVideoFrame(int start_value, const base::TimeTicks& capture_time) {
606 if (start_time_.is_null())
607 start_time_ = capture_time;
608 base::TimeDelta time_diff = capture_time - start_time_;
609 gfx::Size size(video_sender_config_.width, video_sender_config_.height);
610 EXPECT_TRUE(VideoFrame::IsValidConfig(
611 VideoFrame::I420, size, gfx::Rect(size), size));
612 scoped_refptr<media::VideoFrame> video_frame =
613 media::VideoFrame::CreateFrame(
614 VideoFrame::I420, size, gfx::Rect(size), size, time_diff);
615 PopulateVideoFrame(video_frame, start_value);
616 video_frame_input_->InsertRawVideoFrame(video_frame, capture_time);
619 void SendFakeVideoFrame(const base::TimeTicks& capture_time) {
620 video_frame_input_->InsertRawVideoFrame(
621 media::VideoFrame::CreateBlackFrame(gfx::Size(2, 2)), capture_time);
624 void RunTasks(int during_ms) {
625 for (int i = 0; i < during_ms; ++i) {
626 // Call process the timers every 1 ms.
627 testing_clock_sender_->Advance(base::TimeDelta::FromMilliseconds(1));
628 testing_clock_receiver_->Advance(base::TimeDelta::FromMilliseconds(1));
629 task_runner_->RunTasks();
633 void LogRawEvents(const std::vector<PacketEvent>& packet_events) {
634 EXPECT_FALSE(packet_events.empty());
635 for (std::vector<media::cast::PacketEvent>::const_iterator it =
636 packet_events.begin();
637 it != packet_events.end();
638 ++it) {
639 cast_environment_sender_->Logging()->InsertPacketEvent(it->timestamp,
640 it->type,
641 it->media_type,
642 it->rtp_timestamp,
643 it->frame_id,
644 it->packet_id,
645 it->max_packet_id,
646 it->size);
650 AudioReceiverConfig audio_receiver_config_;
651 VideoReceiverConfig video_receiver_config_;
652 AudioSenderConfig audio_sender_config_;
653 VideoSenderConfig video_sender_config_;
655 base::TimeTicks start_time_;
656 base::SimpleTestTickClock* testing_clock_sender_;
657 base::SimpleTestTickClock* testing_clock_receiver_;
658 scoped_refptr<test::FakeSingleThreadTaskRunner> task_runner_;
659 scoped_refptr<CastEnvironment> cast_environment_sender_;
660 scoped_refptr<CastEnvironment> cast_environment_receiver_;
662 LoopBackTransport receiver_to_sender_;
663 LoopBackTransport sender_to_receiver_;
664 scoped_ptr<transport::CastTransportSenderImpl> transport_sender_;
666 scoped_ptr<CastReceiver> cast_receiver_;
667 scoped_ptr<CastSender> cast_sender_;
668 scoped_refptr<AudioFrameInput> audio_frame_input_;
669 scoped_refptr<VideoFrameInput> video_frame_input_;
670 scoped_refptr<FrameReceiver> frame_receiver_;
672 scoped_refptr<TestReceiverAudioCallback> test_receiver_audio_callback_;
673 scoped_refptr<TestReceiverVideoCallback> test_receiver_video_callback_;
675 scoped_ptr<TestAudioBusFactory> audio_bus_factory_;
677 SimpleEventSubscriber event_subscriber_sender_;
678 std::vector<FrameEvent> frame_events_;
679 std::vector<PacketEvent> packet_events_;
680 // |transport_sender_| has a RepeatingTimer which needs a MessageLoop.
681 base::MessageLoop message_loop_;
684 TEST_F(End2EndTest, LoopNoLossPcm16) {
685 Configure(transport::kVp8, transport::kPcm16, 32000, false, 1);
686 // Reduce video resolution to allow processing multiple frames within a
687 // reasonable time frame.
688 video_sender_config_.width = kVideoQcifWidth;
689 video_sender_config_.height = kVideoQcifHeight;
690 Create();
692 const int kNumIterations = 50;
693 int video_start = kVideoStart;
694 int audio_diff = kFrameTimerMs;
695 int num_audio_frames_requested = 0;
696 for (int i = 0; i < kNumIterations; ++i) {
697 const int num_audio_frames = audio_diff / kAudioFrameDurationMs;
698 audio_diff -= num_audio_frames * kAudioFrameDurationMs;
700 if (num_audio_frames > 0)
701 FeedAudioFrames(1, true);
703 test_receiver_video_callback_->AddExpectedResult(
704 video_start,
705 video_sender_config_.width,
706 video_sender_config_.height,
707 testing_clock_sender_->NowTicks(),
708 true);
709 SendVideoFrame(video_start, testing_clock_sender_->NowTicks());
711 if (num_audio_frames > 0)
712 RunTasks(kAudioFrameDurationMs); // Advance clock forward.
713 if (num_audio_frames > 1)
714 FeedAudioFrames(num_audio_frames - 1, true);
716 RequestAudioFrames(num_audio_frames, true);
717 num_audio_frames_requested += num_audio_frames;
719 frame_receiver_->GetRawVideoFrame(
720 base::Bind(&TestReceiverVideoCallback::CheckVideoFrame,
721 test_receiver_video_callback_));
723 RunTasks(kFrameTimerMs - kAudioFrameDurationMs);
724 audio_diff += kFrameTimerMs;
725 video_start++;
728 RunTasks(2 * kFrameTimerMs + 1); // Empty the receiver pipeline.
729 EXPECT_EQ(num_audio_frames_requested,
730 test_receiver_audio_callback_->number_times_called());
731 EXPECT_EQ(kNumIterations,
732 test_receiver_video_callback_->number_times_called());
735 // This tests our external decoder interface for Audio.
736 // Audio test without packet loss using raw PCM 16 audio "codec";
737 TEST_F(End2EndTest, LoopNoLossPcm16ExternalDecoder) {
738 Configure(transport::kVp8, transport::kPcm16, 32000, true, 1);
739 Create();
741 const int kNumIterations = 10;
742 for (int i = 0; i < kNumIterations; ++i) {
743 FeedAudioFrames(1, true);
744 RunTasks(kAudioFrameDurationMs);
745 frame_receiver_->GetCodedAudioFrame(
746 base::Bind(&TestReceiverAudioCallback::CheckCodedAudioFrame,
747 test_receiver_audio_callback_));
749 RunTasks(2 * kFrameTimerMs + 1); // Empty the receiver pipeline.
750 EXPECT_EQ(kNumIterations,
751 test_receiver_audio_callback_->number_times_called());
754 // This tests our Opus audio codec without video.
755 TEST_F(End2EndTest, LoopNoLossOpus) {
756 Configure(transport::kVp8, transport::kOpus, kDefaultAudioSamplingRate,
757 false, 1);
758 Create();
760 const int kNumIterations = 300;
761 for (int i = 0; i < kNumIterations; ++i) {
762 // Opus introduces a tiny delay before the sinewave starts; so don't examine
763 // the first frame.
764 const bool examine_audio_data = i > 0;
765 FeedAudioFrames(1, examine_audio_data);
766 RunTasks(kAudioFrameDurationMs);
767 RequestAudioFrames(1, examine_audio_data);
769 RunTasks(2 * kFrameTimerMs + 1); // Empty the receiver pipeline.
770 EXPECT_EQ(kNumIterations,
771 test_receiver_audio_callback_->number_times_called());
774 // This tests start sending audio and video at start-up time before the receiver
775 // is ready; it sends 2 frames before the receiver comes online.
777 // Test disabled due to flakiness: It appears that the RTCP synchronization
778 // sometimes kicks in, and sometimes doesn't. When it does, there's a sharp
779 // discontinuity in the timeline, throwing off the test expectations. See TODOs
780 // in audio_receiver.cc for likely cause(s) of this bug.
781 // http://crbug.com/356942
782 TEST_F(End2EndTest, DISABLED_StartSenderBeforeReceiver) {
783 Configure(transport::kVp8, transport::kPcm16, kDefaultAudioSamplingRate,
784 false, 1);
785 Create();
787 int video_start = kVideoStart;
788 int audio_diff = kFrameTimerMs;
790 sender_to_receiver_.SetSendPackets(false);
792 const int test_delay_ms = 100;
794 const int kNumVideoFramesBeforeReceiverStarted = 2;
795 const base::TimeTicks initial_send_time = testing_clock_sender_->NowTicks();
796 const base::TimeDelta expected_delay =
797 base::TimeDelta::FromMilliseconds(test_delay_ms + kFrameTimerMs);
798 for (int i = 0; i < kNumVideoFramesBeforeReceiverStarted; ++i) {
799 const int num_audio_frames = audio_diff / kAudioFrameDurationMs;
800 audio_diff -= num_audio_frames * kAudioFrameDurationMs;
802 if (num_audio_frames > 0)
803 FeedAudioFramesWithExpectedDelay(1, expected_delay);
805 // Frame will be rendered with 100mS delay, as the transmission is delayed.
806 // The receiver at this point cannot be synced to the sender's clock, as no
807 // packets, and specifically no RTCP packets were sent.
808 test_receiver_video_callback_->AddExpectedResult(
809 video_start,
810 video_sender_config_.width,
811 video_sender_config_.height,
812 initial_send_time + expected_delay,
813 true);
814 SendVideoFrame(video_start, testing_clock_sender_->NowTicks());
816 if (num_audio_frames > 0)
817 RunTasks(kAudioFrameDurationMs); // Advance clock forward.
818 if (num_audio_frames > 1)
819 FeedAudioFramesWithExpectedDelay(num_audio_frames - 1, expected_delay);
821 RunTasks(kFrameTimerMs - kAudioFrameDurationMs);
822 audio_diff += kFrameTimerMs;
823 video_start++;
826 RunTasks(test_delay_ms);
827 sender_to_receiver_.SetSendPackets(true);
829 int num_audio_frames_requested = 0;
830 for (int j = 0; j < 10; ++j) {
831 const int num_audio_frames = audio_diff / kAudioFrameDurationMs;
832 audio_diff -= num_audio_frames * kAudioFrameDurationMs;
834 if (num_audio_frames > 0)
835 FeedAudioFrames(1, true);
837 test_receiver_video_callback_->AddExpectedResult(
838 video_start,
839 video_sender_config_.width,
840 video_sender_config_.height,
841 testing_clock_sender_->NowTicks(),
842 true);
843 SendVideoFrame(video_start, testing_clock_sender_->NowTicks());
845 if (num_audio_frames > 0)
846 RunTasks(kAudioFrameDurationMs); // Advance clock forward.
847 if (num_audio_frames > 1)
848 FeedAudioFrames(num_audio_frames - 1, true);
850 RequestAudioFrames(num_audio_frames, true);
851 num_audio_frames_requested += num_audio_frames;
853 frame_receiver_->GetRawVideoFrame(
854 base::Bind(&TestReceiverVideoCallback::CheckVideoFrame,
855 test_receiver_video_callback_));
857 RunTasks(kFrameTimerMs - kAudioFrameDurationMs);
858 audio_diff += kFrameTimerMs;
859 video_start++;
861 RunTasks(2 * kFrameTimerMs + 1); // Empty the receiver pipeline.
862 EXPECT_EQ(num_audio_frames_requested,
863 test_receiver_audio_callback_->number_times_called());
864 EXPECT_EQ(10, test_receiver_video_callback_->number_times_called());
867 // This tests a network glitch lasting for 10 video frames.
868 // Flaky. See crbug.com/351596.
869 TEST_F(End2EndTest, DISABLED_GlitchWith3Buffers) {
870 Configure(transport::kVp8, transport::kOpus, kDefaultAudioSamplingRate,
871 false, 3);
872 video_sender_config_.rtp_config.max_delay_ms = 67;
873 video_receiver_config_.rtp_max_delay_ms = 67;
874 Create();
876 int video_start = kVideoStart;
877 base::TimeTicks send_time;
878 // Frames will rendered on completion until the render time stabilizes, i.e.
879 // we got enough data.
880 const int frames_before_glitch = 20;
881 for (int i = 0; i < frames_before_glitch; ++i) {
882 send_time = testing_clock_sender_->NowTicks();
883 SendVideoFrame(video_start, send_time);
884 test_receiver_video_callback_->AddExpectedResult(
885 video_start,
886 video_sender_config_.width,
887 video_sender_config_.height,
888 send_time,
889 true);
890 frame_receiver_->GetRawVideoFrame(
891 base::Bind(&TestReceiverVideoCallback::CheckVideoFrame,
892 test_receiver_video_callback_));
893 RunTasks(kFrameTimerMs);
894 video_start++;
897 // Introduce a glitch lasting for 10 frames.
898 sender_to_receiver_.SetSendPackets(false);
899 for (int i = 0; i < 10; ++i) {
900 send_time = testing_clock_sender_->NowTicks();
901 // First 3 will be sent and lost.
902 SendVideoFrame(video_start, send_time);
903 RunTasks(kFrameTimerMs);
904 video_start++;
906 sender_to_receiver_.SetSendPackets(true);
907 RunTasks(100);
908 send_time = testing_clock_sender_->NowTicks();
910 // Frame 1 should be acked by now and we should have an opening to send 4.
911 SendVideoFrame(video_start, send_time);
912 RunTasks(kFrameTimerMs);
914 // Frames 1-3 are old frames by now, and therefore should be decoded, but
915 // not rendered. The next frame we expect to render is frame #4.
916 test_receiver_video_callback_->AddExpectedResult(video_start,
917 video_sender_config_.width,
918 video_sender_config_.height,
919 send_time,
920 true);
922 frame_receiver_->GetRawVideoFrame(
923 base::Bind(&TestReceiverVideoCallback::CheckVideoFrame,
924 test_receiver_video_callback_));
926 RunTasks(2 * kFrameTimerMs + 1); // Empty the receiver pipeline.
927 EXPECT_EQ(frames_before_glitch + 1,
928 test_receiver_video_callback_->number_times_called());
931 // Disabled due to flakiness and crashiness. http://crbug.com/360951
932 TEST_F(End2EndTest, DISABLED_DropEveryOtherFrame3Buffers) {
933 Configure(transport::kVp8, transport::kOpus, kDefaultAudioSamplingRate, false,
935 video_sender_config_.rtp_config.max_delay_ms = 67;
936 video_receiver_config_.rtp_max_delay_ms = 67;
937 Create();
938 sender_to_receiver_.DropAllPacketsBelongingToOddFrames();
940 int video_start = kVideoStart;
941 base::TimeTicks send_time;
943 int i = 0;
944 for (; i < 20; ++i) {
945 send_time = testing_clock_sender_->NowTicks();
946 SendVideoFrame(video_start, send_time);
948 if (i % 2 == 0) {
949 test_receiver_video_callback_->AddExpectedResult(
950 video_start,
951 video_sender_config_.width,
952 video_sender_config_.height,
953 send_time,
954 i == 0);
956 // GetRawVideoFrame will not return the frame until we are close in
957 // time before we should render the frame.
958 frame_receiver_->GetRawVideoFrame(
959 base::Bind(&TestReceiverVideoCallback::CheckVideoFrame,
960 test_receiver_video_callback_));
962 RunTasks(kFrameTimerMs);
963 video_start++;
966 RunTasks(2 * kFrameTimerMs + 1); // Empty the pipeline.
967 EXPECT_EQ(i / 2, test_receiver_video_callback_->number_times_called());
970 TEST_F(End2EndTest, CryptoVideo) {
971 Configure(transport::kVp8, transport::kPcm16, 32000, false, 1);
973 video_sender_config_.rtp_config.aes_iv_mask =
974 ConvertFromBase16String("1234567890abcdeffedcba0987654321");
975 video_sender_config_.rtp_config.aes_key =
976 ConvertFromBase16String("deadbeefcafeb0b0b0b0cafedeadbeef");
978 video_receiver_config_.aes_iv_mask =
979 video_sender_config_.rtp_config.aes_iv_mask;
980 video_receiver_config_.aes_key =
981 video_sender_config_.rtp_config.aes_key;
983 Create();
985 int frames_counter = 0;
986 for (; frames_counter < 3; ++frames_counter) {
987 const base::TimeTicks send_time = testing_clock_sender_->NowTicks();
988 SendVideoFrame(frames_counter, send_time);
990 test_receiver_video_callback_->AddExpectedResult(
991 frames_counter,
992 video_sender_config_.width,
993 video_sender_config_.height,
994 send_time,
995 true);
997 RunTasks(kFrameTimerMs);
999 frame_receiver_->GetRawVideoFrame(
1000 base::Bind(&TestReceiverVideoCallback::CheckVideoFrame,
1001 test_receiver_video_callback_));
1003 RunTasks(2 * kFrameTimerMs + 1); // Empty the pipeline.
1004 EXPECT_EQ(frames_counter,
1005 test_receiver_video_callback_->number_times_called());
1008 TEST_F(End2EndTest, CryptoAudio) {
1009 Configure(transport::kVp8, transport::kPcm16, 32000, false, 1);
1011 audio_sender_config_.rtp_config.aes_iv_mask =
1012 ConvertFromBase16String("abcdeffedcba12345678900987654321");
1013 audio_sender_config_.rtp_config.aes_key =
1014 ConvertFromBase16String("deadbeefcafecafedeadbeefb0b0b0b0");
1016 audio_receiver_config_.aes_iv_mask =
1017 audio_sender_config_.rtp_config.aes_iv_mask;
1018 audio_receiver_config_.aes_key =
1019 audio_sender_config_.rtp_config.aes_key;
1021 Create();
1023 const int kNumIterations = 3;
1024 const int kNumAudioFramesPerIteration = 2;
1025 for (int i = 0; i < kNumIterations; ++i) {
1026 FeedAudioFrames(kNumAudioFramesPerIteration, true);
1027 RunTasks(kNumAudioFramesPerIteration * kAudioFrameDurationMs);
1028 RequestAudioFrames(kNumAudioFramesPerIteration, true);
1030 RunTasks(2 * kFrameTimerMs + 1); // Empty the pipeline.
1031 EXPECT_EQ(kNumIterations * kNumAudioFramesPerIteration,
1032 test_receiver_audio_callback_->number_times_called());
1035 // Video test without packet loss - tests the logging aspects of the end2end,
1036 // but is basically equivalent to LoopNoLossPcm16.
1037 TEST_F(End2EndTest, VideoLogging) {
1038 Configure(transport::kVp8, transport::kPcm16, 32000, false, 1);
1039 Create();
1041 int video_start = kVideoStart;
1042 const int num_frames = 5;
1043 for (int i = 0; i < num_frames; ++i) {
1044 base::TimeTicks send_time = testing_clock_sender_->NowTicks();
1045 test_receiver_video_callback_->AddExpectedResult(
1046 video_start,
1047 video_sender_config_.width,
1048 video_sender_config_.height,
1049 send_time,
1050 true);
1052 SendVideoFrame(video_start, send_time);
1053 RunTasks(kFrameTimerMs);
1055 frame_receiver_->GetRawVideoFrame(
1056 base::Bind(&TestReceiverVideoCallback::CheckVideoFrame,
1057 test_receiver_video_callback_));
1059 video_start++;
1062 // Basic tests.
1063 RunTasks(2 * kFrameTimerMs + 1); // Empty the receiver pipeline.
1064 int num_callbacks_called =
1065 test_receiver_video_callback_->number_times_called();
1066 EXPECT_EQ(num_frames, num_callbacks_called);
1068 RunTasks(750); // Make sure that we send a RTCP message with the log.
1070 // Logging tests.
1071 // Frame logging.
1072 // Verify that all frames and all required events were logged.
1073 event_subscriber_sender_.GetFrameEventsAndReset(&frame_events_);
1075 // For each frame, count the number of events that occurred for each event
1076 // for that frame.
1077 std::map<RtpTimestamp, LoggingEventCounts> event_counter_for_frame =
1078 GetEventCountForFrameEvents(frame_events_);
1080 // Verify that there are logs for expected number of frames.
1081 EXPECT_EQ(num_frames, static_cast<int>(event_counter_for_frame.size()));
1083 // Verify that each frame have the expected types of events logged.
1084 for (std::map<RtpTimestamp, LoggingEventCounts>::iterator map_it =
1085 event_counter_for_frame.begin();
1086 map_it != event_counter_for_frame.end();
1087 ++map_it) {
1088 int total_event_count_for_frame = 0;
1089 for (int i = 0; i <= kNumOfLoggingEvents; ++i) {
1090 total_event_count_for_frame += map_it->second.counter[i];
1093 int expected_event_count_for_frame = 0;
1095 EXPECT_EQ(1, map_it->second.counter[FRAME_CAPTURE_BEGIN]);
1096 expected_event_count_for_frame +=
1097 map_it->second.counter[FRAME_CAPTURE_BEGIN];
1099 EXPECT_EQ(1, map_it->second.counter[FRAME_CAPTURE_END]);
1100 expected_event_count_for_frame +=
1101 map_it->second.counter[FRAME_CAPTURE_END];
1103 EXPECT_EQ(1, map_it->second.counter[FRAME_ENCODED]);
1104 expected_event_count_for_frame +=
1105 map_it->second.counter[FRAME_ENCODED];
1107 EXPECT_EQ(1, map_it->second.counter[FRAME_DECODED]);
1108 expected_event_count_for_frame +=
1109 map_it->second.counter[FRAME_DECODED];
1111 EXPECT_EQ(1, map_it->second.counter[FRAME_PLAYOUT]);
1112 expected_event_count_for_frame += map_it->second.counter[FRAME_PLAYOUT];
1115 // There is no guarantee that FRAME_ACK_SENT is loggeed exactly once per
1116 // frame.
1117 EXPECT_GT(map_it->second.counter[FRAME_ACK_SENT], 0);
1118 expected_event_count_for_frame += map_it->second.counter[FRAME_ACK_SENT];
1120 // There is no guarantee that FRAME_ACK_RECEIVED is loggeed exactly once per
1121 // frame.
1122 EXPECT_GT(map_it->second.counter[FRAME_ACK_RECEIVED], 0);
1123 expected_event_count_for_frame +=
1124 map_it->second.counter[FRAME_ACK_RECEIVED];
1126 // Verify that there were no other events logged with respect to this
1127 // frame.
1128 // (i.e. Total event count = expected event count)
1129 EXPECT_EQ(total_event_count_for_frame, expected_event_count_for_frame);
1132 // Packet logging.
1133 // Verify that all packet related events were logged.
1134 event_subscriber_sender_.GetPacketEventsAndReset(&packet_events_);
1135 std::map<uint16, LoggingEventCounts> event_count_for_packet =
1136 GetEventCountForPacketEvents(packet_events_);
1138 // Verify that each packet have the expected types of events logged.
1139 for (std::map<uint16, LoggingEventCounts>::iterator map_it =
1140 event_count_for_packet.begin();
1141 map_it != event_count_for_packet.end();
1142 ++map_it) {
1143 int total_event_count_for_packet = 0;
1144 for (int i = 0; i <= kNumOfLoggingEvents; ++i) {
1145 total_event_count_for_packet += map_it->second.counter[i];
1148 int expected_event_count_for_packet = 0;
1149 EXPECT_GT(map_it->second.counter[PACKET_RECEIVED], 0);
1150 expected_event_count_for_packet +=
1151 map_it->second.counter[PACKET_RECEIVED];
1153 // Verify that there were no other events logged with respect to this
1154 // packet. (i.e. Total event count = expected event count)
1155 EXPECT_EQ(total_event_count_for_packet, expected_event_count_for_packet);
1159 // Audio test without packet loss - tests the logging aspects of the end2end,
1160 // but is basically equivalent to LoopNoLossPcm16.
1161 TEST_F(End2EndTest, AudioLogging) {
1162 Configure(transport::kVp8, transport::kPcm16, 32000, false, 1);
1163 Create();
1165 int audio_diff = kFrameTimerMs;
1166 const int kNumVideoFrames = 10;
1167 int num_audio_frames_requested = 0;
1168 for (int i = 0; i < kNumVideoFrames; ++i) {
1169 const int num_audio_frames = audio_diff / kAudioFrameDurationMs;
1170 audio_diff -= num_audio_frames * kAudioFrameDurationMs;
1172 FeedAudioFrames(num_audio_frames, true);
1174 RunTasks(kFrameTimerMs);
1175 audio_diff += kFrameTimerMs;
1177 RequestAudioFrames(num_audio_frames, true);
1178 num_audio_frames_requested += num_audio_frames;
1181 // Basic tests.
1182 RunTasks(2 * kFrameTimerMs + 1); // Empty the receiver pipeline.
1184 EXPECT_EQ(num_audio_frames_requested,
1185 test_receiver_audio_callback_->number_times_called());
1187 // Logging tests.
1188 // Verify that all frames and all required events were logged.
1189 event_subscriber_sender_.GetFrameEventsAndReset(&frame_events_);
1191 // Construct a map from each frame (RTP timestamp) to a count of each event
1192 // type logged for that frame.
1193 std::map<RtpTimestamp, LoggingEventCounts> event_counter_for_frame =
1194 GetEventCountForFrameEvents(frame_events_);
1196 int encoded_count = 0;
1198 // Verify the right number of events were logged for each event type.
1199 for (std::map<RtpTimestamp, LoggingEventCounts>::iterator it =
1200 event_counter_for_frame.begin();
1201 it != event_counter_for_frame.end();
1202 ++it) {
1203 encoded_count += it->second.counter[FRAME_ENCODED];
1206 EXPECT_EQ(num_audio_frames_requested, encoded_count);
1208 // Verify that each frame have the expected types of events logged.
1209 for (std::map<RtpTimestamp, LoggingEventCounts>::const_iterator map_it =
1210 event_counter_for_frame.begin();
1211 map_it != event_counter_for_frame.end(); ++map_it) {
1212 int total_event_count_for_frame = 0;
1213 for (int j = 0; j <= kNumOfLoggingEvents; ++j)
1214 total_event_count_for_frame += map_it->second.counter[j];
1216 int expected_event_count_for_frame = 0;
1218 EXPECT_EQ(1, map_it->second.counter[FRAME_ENCODED]);
1219 expected_event_count_for_frame +=
1220 map_it->second.counter[FRAME_ENCODED];
1222 EXPECT_EQ(1, map_it->second.counter[FRAME_PLAYOUT]);
1223 expected_event_count_for_frame +=
1224 map_it->second.counter[FRAME_PLAYOUT];
1226 EXPECT_EQ(1, map_it->second.counter[FRAME_DECODED]);
1227 expected_event_count_for_frame +=
1228 map_it->second.counter[FRAME_DECODED];
1230 EXPECT_GT(map_it->second.counter[FRAME_ACK_SENT], 0);
1231 expected_event_count_for_frame += map_it->second.counter[FRAME_ACK_SENT];
1233 // Verify that there were no other events logged with respect to this frame.
1234 // (i.e. Total event count = expected event count)
1235 EXPECT_EQ(total_event_count_for_frame, expected_event_count_for_frame);
1239 TEST_F(End2EndTest, BasicFakeSoftwareVideo) {
1240 Configure(transport::kFakeSoftwareVideo, transport::kPcm16, 32000, false, 1);
1241 Create();
1243 int frames_counter = 0;
1244 int received_counter = 0;
1245 for (; frames_counter < 1000; ++frames_counter) {
1246 SendFakeVideoFrame(testing_clock_sender_->NowTicks());
1247 frame_receiver_->GetRawVideoFrame(
1248 base::Bind(&CountVideoFrame, &received_counter));
1249 RunTasks(kFrameTimerMs);
1251 RunTasks(2 * kFrameTimerMs + 1); // Empty the pipeline.
1252 EXPECT_EQ(1000, received_counter);
1255 // TODO(pwestin): Add repeatable packet loss test.
1256 // TODO(pwestin): Add test for misaligned send get calls.
1257 // TODO(pwestin): Add more tests that does not resample.
1258 // TODO(pwestin): Add test when we have starvation for our RunTask.
1260 } // namespace cast
1261 } // namespace media