Remove Unused AsTextButtonBorder RTTI helper.
[chromium-blink-merge.git] / media / cast / test / end2end_unittest.cc
blob82fc568d2d2b959c96cedc276c9e8f0008cd87a9
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>
14 #include <functional>
15 #include <list>
16 #include <map>
18 #include "base/bind.h"
19 #include "base/bind_helpers.h"
20 #include "base/strings/string_number_conversions.h"
21 #include "base/test/simple_test_tick_clock.h"
22 #include "base/time/tick_clock.h"
23 #include "media/base/video_frame.h"
24 #include "media/cast/cast_config.h"
25 #include "media/cast/cast_environment.h"
26 #include "media/cast/cast_receiver.h"
27 #include "media/cast/cast_sender.h"
28 #include "media/cast/logging/simple_event_subscriber.h"
29 #include "media/cast/test/fake_single_thread_task_runner.h"
30 #include "media/cast/test/utility/audio_utility.h"
31 #include "media/cast/test/utility/video_utility.h"
32 #include "media/cast/transport/cast_transport_config.h"
33 #include "media/cast/transport/cast_transport_defines.h"
34 #include "media/cast/transport/cast_transport_sender.h"
35 #include "media/cast/transport/cast_transport_sender_impl.h"
36 #include "testing/gtest/include/gtest/gtest.h"
38 namespace media {
39 namespace cast {
41 namespace {
43 static const int64 kStartMillisecond = GG_INT64_C(1245);
44 static const int kAudioChannels = 2;
45 static const double kSoundFrequency = 314.15926535897; // Freq of sine wave.
46 static const float kSoundVolume = 0.5f;
47 static const int kVideoHdWidth = 1280;
48 static const int kVideoHdHeight = 720;
49 static const int kVideoQcifWidth = 176;
50 static const int kVideoQcifHeight = 144;
51 static const int kCommonRtpHeaderLength = 12;
52 static const uint8 kCastReferenceFrameIdBitReset = 0xDF; // Mask is 0x40.
54 // Since the video encoded and decoded an error will be introduced; when
55 // comparing individual pixels the error can be quite large; we allow a PSNR of
56 // at least |kVideoAcceptedPSNR|.
57 static const double kVideoAcceptedPSNR = 38.0;
59 // The tests are commonly implemented with |kFrameTimerMs| RunTask function;
60 // a normal video is 30 fps hence the 33 ms between frames.
61 static const int kFrameTimerMs = 33;
63 // The packets pass through the pacer which can delay the beginning of the
64 // frame by 10 ms if there is packets belonging to the previous frame being
65 // retransmitted.
66 // In addition, audio packets are sent in 10mS intervals in audio_encoder.cc,
67 // although we send an audio frame every 33mS, which adds an extra delay.
68 // A TODO was added in the code to resolve this.
69 static const int kTimerErrorMs = 20;
71 // Start the video synthetic start value to medium range value, to avoid edge
72 // effects cause by encoding and quantization.
73 static const int kVideoStart = 100;
75 std::string ConvertFromBase16String(const std::string base_16) {
76 std::string compressed;
77 DCHECK_EQ(base_16.size() % 2, 0u) << "Must be a multiple of 2";
78 compressed.reserve(base_16.size() / 2);
80 std::vector<uint8> v;
81 if (!base::HexStringToBytes(base_16, &v)) {
82 NOTREACHED();
84 compressed.assign(reinterpret_cast<const char*>(&v[0]), v.size());
85 return compressed;
88 // Dummy callback function that does nothing except to accept ownership of
89 // |audio_bus| for destruction.
90 void OwnThatAudioBus(scoped_ptr<AudioBus> audio_bus) {}
92 void UpdateCastTransportStatus(transport::CastTransportStatus status) {
93 EXPECT_EQ(status, transport::TRANSPORT_INITIALIZED);
96 // This is wrapped in a struct because it needs to be put into a std::map.
97 typedef struct {
98 int counter[kNumOfLoggingEvents];
99 } LoggingEventCounts;
101 // Constructs a map from each frame (RTP timestamp) to counts of each event
102 // type logged for that frame.
103 std::map<RtpTimestamp, LoggingEventCounts> GetEventCountForFrameEvents(
104 const std::vector<FrameEvent>& frame_events) {
105 std::map<RtpTimestamp, LoggingEventCounts> event_counter_for_frame;
106 for (std::vector<FrameEvent>::const_iterator it = frame_events.begin();
107 it != frame_events.end();
108 ++it) {
109 std::map<RtpTimestamp, LoggingEventCounts>::iterator map_it =
110 event_counter_for_frame.find(it->rtp_timestamp);
111 if (map_it == event_counter_for_frame.end()) {
112 LoggingEventCounts new_counter;
113 memset(&new_counter, 0, sizeof(new_counter));
114 ++(new_counter.counter[it->type]);
115 event_counter_for_frame.insert(
116 std::make_pair(it->rtp_timestamp, new_counter));
117 } else {
118 ++(map_it->second.counter[it->type]);
121 return event_counter_for_frame;
124 // Constructs a map from each packet (Packet ID) to counts of each event
125 // type logged for that packet.
126 std::map<uint16, LoggingEventCounts> GetEventCountForPacketEvents(
127 const std::vector<PacketEvent>& packet_events) {
128 std::map<uint16, LoggingEventCounts> event_counter_for_packet;
129 for (std::vector<PacketEvent>::const_iterator it = packet_events.begin();
130 it != packet_events.end();
131 ++it) {
132 std::map<uint16, LoggingEventCounts>::iterator map_it =
133 event_counter_for_packet.find(it->packet_id);
134 if (map_it == event_counter_for_packet.end()) {
135 LoggingEventCounts new_counter;
136 memset(&new_counter, 0, sizeof(new_counter));
137 ++(new_counter.counter[it->type]);
138 event_counter_for_packet.insert(
139 std::make_pair(it->packet_id, new_counter));
140 } else {
141 ++(map_it->second.counter[it->type]);
144 return event_counter_for_packet;
147 } // namespace
149 // Class that sends the packet direct from sender into the receiver with the
150 // ability to drop packets between the two.
151 class LoopBackTransport : public transport::PacketSender {
152 public:
153 explicit LoopBackTransport(scoped_refptr<CastEnvironment> cast_environment)
154 : send_packets_(true),
155 drop_packets_belonging_to_odd_frames_(false),
156 reset_reference_frame_id_(false),
157 cast_environment_(cast_environment) {}
159 void SetPacketReceiver(
160 const transport::PacketReceiverCallback& packet_receiver) {
161 packet_receiver_ = packet_receiver;
164 virtual bool SendPacket(const Packet& packet) OVERRIDE {
165 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
166 if (!send_packets_)
167 return false;
169 if (drop_packets_belonging_to_odd_frames_) {
170 uint32 frame_id = packet[13];
171 if (frame_id % 2 == 1)
172 return true;
175 scoped_ptr<Packet> packet_copy(new Packet(packet));
176 if (reset_reference_frame_id_) {
177 // Reset the is_reference bit in the cast header.
178 (*packet_copy)[kCommonRtpHeaderLength] &= kCastReferenceFrameIdBitReset;
180 packet_receiver_.Run(packet_copy.Pass());
181 return true;
184 void SetSendPackets(bool send_packets) { send_packets_ = send_packets; }
186 void DropAllPacketsBelongingToOddFrames() {
187 drop_packets_belonging_to_odd_frames_ = true;
190 void AlwaysResetReferenceFrameId() { reset_reference_frame_id_ = true; }
192 private:
193 transport::PacketReceiverCallback packet_receiver_;
194 bool send_packets_;
195 bool drop_packets_belonging_to_odd_frames_;
196 bool reset_reference_frame_id_;
197 scoped_refptr<CastEnvironment> cast_environment_;
200 // Class that verifies the audio frames coming out of the receiver.
201 class TestReceiverAudioCallback
202 : public base::RefCountedThreadSafe<TestReceiverAudioCallback> {
203 public:
204 struct ExpectedAudioFrame {
205 PcmAudioFrame audio_frame;
206 int num_10ms_blocks;
207 base::TimeTicks record_time;
210 TestReceiverAudioCallback() : num_called_(0) {}
212 void SetExpectedSamplingFrequency(int expected_sampling_frequency) {
213 expected_sampling_frequency_ = expected_sampling_frequency;
216 void AddExpectedResult(scoped_ptr<PcmAudioFrame> audio_frame,
217 int expected_num_10ms_blocks,
218 const base::TimeTicks& record_time) {
219 ExpectedAudioFrame expected_audio_frame;
220 expected_audio_frame.audio_frame = *audio_frame;
221 expected_audio_frame.num_10ms_blocks = expected_num_10ms_blocks;
222 expected_audio_frame.record_time = record_time;
223 expected_frame_.push_back(expected_audio_frame);
226 void IgnoreAudioFrame(scoped_ptr<PcmAudioFrame> audio_frame,
227 const base::TimeTicks& playout_time) {}
229 // Check the audio frame parameters but not the audio samples.
230 void CheckBasicAudioFrame(const scoped_ptr<PcmAudioFrame>& audio_frame,
231 const base::TimeTicks& playout_time) {
232 EXPECT_FALSE(expected_frame_.empty()); // Test for bug in test code.
233 ExpectedAudioFrame expected_audio_frame = expected_frame_.front();
234 EXPECT_EQ(audio_frame->channels, kAudioChannels);
235 EXPECT_EQ(audio_frame->frequency, expected_sampling_frequency_);
236 EXPECT_EQ(static_cast<int>(audio_frame->samples.size()),
237 expected_audio_frame.num_10ms_blocks * kAudioChannels *
238 expected_sampling_frequency_ / 100);
240 const base::TimeTicks upper_bound =
241 expected_audio_frame.record_time +
242 base::TimeDelta::FromMilliseconds(kDefaultRtpMaxDelayMs +
243 kTimerErrorMs);
244 EXPECT_GE(upper_bound, playout_time)
245 << "playout_time - upper_bound == "
246 << (playout_time - upper_bound).InMicroseconds() << " usec";
247 EXPECT_LT(expected_audio_frame.record_time, playout_time)
248 << "playout_time - expected == "
249 << (playout_time - expected_audio_frame.record_time).InMilliseconds()
250 << " mS";
252 EXPECT_EQ(audio_frame->samples.size(),
253 expected_audio_frame.audio_frame.samples.size());
256 void CheckPcmAudioFrame(scoped_ptr<PcmAudioFrame> audio_frame,
257 const base::TimeTicks& playout_time) {
258 ++num_called_;
260 CheckBasicAudioFrame(audio_frame, playout_time);
261 ExpectedAudioFrame expected_audio_frame = expected_frame_.front();
262 expected_frame_.pop_front();
263 if (audio_frame->samples.size() == 0)
264 return; // No more checks needed.
266 EXPECT_NEAR(CountZeroCrossings(expected_audio_frame.audio_frame.samples),
267 CountZeroCrossings(audio_frame->samples),
271 void CheckCodedPcmAudioFrame(
272 scoped_ptr<transport::EncodedAudioFrame> audio_frame,
273 const base::TimeTicks& playout_time) {
274 ++num_called_;
276 EXPECT_FALSE(expected_frame_.empty()); // Test for bug in test code.
277 ExpectedAudioFrame expected_audio_frame = expected_frame_.front();
278 expected_frame_.pop_front();
280 EXPECT_EQ(static_cast<int>(audio_frame->data.size()),
281 2 * kAudioChannels * expected_sampling_frequency_ / 100);
283 base::TimeDelta time_since_recording =
284 playout_time - expected_audio_frame.record_time;
286 EXPECT_LE(time_since_recording,
287 base::TimeDelta::FromMilliseconds(kDefaultRtpMaxDelayMs +
288 kTimerErrorMs));
290 EXPECT_LT(expected_audio_frame.record_time, playout_time);
291 if (audio_frame->data.size() == 0)
292 return; // No more checks needed.
294 // We need to convert our "coded" audio frame to our raw format.
295 std::vector<int16> output_audio_samples;
296 size_t number_of_samples = audio_frame->data.size() / 2;
298 for (size_t i = 0; i < number_of_samples; ++i) {
299 uint16 sample =
300 static_cast<uint8>(audio_frame->data[1 + i * sizeof(uint16)]) +
301 (static_cast<uint16>(audio_frame->data[i * sizeof(uint16)]) << 8);
302 output_audio_samples.push_back(static_cast<int16>(sample));
305 EXPECT_NEAR(CountZeroCrossings(expected_audio_frame.audio_frame.samples),
306 CountZeroCrossings(output_audio_samples),
310 int number_times_called() const { return num_called_; }
312 protected:
313 virtual ~TestReceiverAudioCallback() {}
315 private:
316 friend class base::RefCountedThreadSafe<TestReceiverAudioCallback>;
318 int num_called_;
319 int expected_sampling_frequency_;
320 std::list<ExpectedAudioFrame> expected_frame_;
323 // Class that verifies the video frames coming out of the receiver.
324 class TestReceiverVideoCallback
325 : public base::RefCountedThreadSafe<TestReceiverVideoCallback> {
326 public:
327 struct ExpectedVideoFrame {
328 int start_value;
329 int width;
330 int height;
331 base::TimeTicks capture_time;
334 TestReceiverVideoCallback() : num_called_(0) {}
336 void AddExpectedResult(int start_value,
337 int width,
338 int height,
339 const base::TimeTicks& capture_time) {
340 ExpectedVideoFrame expected_video_frame;
341 expected_video_frame.start_value = start_value;
342 expected_video_frame.capture_time = capture_time;
343 expected_video_frame.width = width;
344 expected_video_frame.height = height;
345 expected_frame_.push_back(expected_video_frame);
348 void CheckVideoFrame(const scoped_refptr<media::VideoFrame>& video_frame,
349 const base::TimeTicks& render_time) {
350 ++num_called_;
352 EXPECT_FALSE(expected_frame_.empty()); // Test for bug in test code.
353 ExpectedVideoFrame expected_video_frame = expected_frame_.front();
354 expected_frame_.pop_front();
356 base::TimeDelta time_since_capture =
357 render_time - expected_video_frame.capture_time;
358 const base::TimeDelta upper_bound = base::TimeDelta::FromMilliseconds(
359 kDefaultRtpMaxDelayMs + kTimerErrorMs);
361 EXPECT_GE(upper_bound, time_since_capture)
362 << "time_since_capture - upper_bound == "
363 << (time_since_capture - upper_bound).InMilliseconds() << " mS";
364 EXPECT_LE(expected_video_frame.capture_time, render_time);
365 EXPECT_EQ(expected_video_frame.width, video_frame->coded_size().width());
366 EXPECT_EQ(expected_video_frame.height, video_frame->coded_size().height());
368 gfx::Size size(expected_video_frame.width, expected_video_frame.height);
369 scoped_refptr<media::VideoFrame> expected_I420_frame =
370 media::VideoFrame::CreateFrame(
371 VideoFrame::I420, size, gfx::Rect(size), size, base::TimeDelta());
372 PopulateVideoFrame(expected_I420_frame, expected_video_frame.start_value);
374 EXPECT_GE(I420PSNR(expected_I420_frame, video_frame), kVideoAcceptedPSNR);
377 int number_times_called() const { return num_called_; }
379 protected:
380 virtual ~TestReceiverVideoCallback() {}
382 private:
383 friend class base::RefCountedThreadSafe<TestReceiverVideoCallback>;
385 int num_called_;
386 std::list<ExpectedVideoFrame> expected_frame_;
389 // The actual test class, generate synthetic data for both audio and video and
390 // send those through the sender and receiver and analyzes the result.
391 class End2EndTest : public ::testing::Test {
392 protected:
393 End2EndTest()
394 : start_time_(),
395 testing_clock_(new base::SimpleTestTickClock()),
396 task_runner_(new test::FakeSingleThreadTaskRunner(testing_clock_)),
397 cast_environment_(new CastEnvironment(
398 scoped_ptr<base::TickClock>(testing_clock_).Pass(),
399 task_runner_,
400 task_runner_,
401 task_runner_,
402 task_runner_,
403 task_runner_,
404 task_runner_,
405 GetLoggingConfigWithRawEventsAndStatsEnabled())),
406 receiver_to_sender_(cast_environment_),
407 sender_to_receiver_(cast_environment_),
408 test_receiver_audio_callback_(new TestReceiverAudioCallback()),
409 test_receiver_video_callback_(new TestReceiverVideoCallback()) {
410 testing_clock_->Advance(
411 base::TimeDelta::FromMilliseconds(kStartMillisecond));
412 cast_environment_->Logging()->AddRawEventSubscriber(&event_subscriber_);
415 void SetupConfig(transport::AudioCodec audio_codec,
416 int audio_sampling_frequency,
417 // TODO(miu): 3rd arg is meaningless?!?
418 bool external_audio_decoder,
419 int max_number_of_video_buffers_used) {
420 audio_sender_config_.sender_ssrc = 1;
421 audio_sender_config_.incoming_feedback_ssrc = 2;
422 audio_sender_config_.rtp_config.payload_type = 96;
423 audio_sender_config_.use_external_encoder = false;
424 audio_sender_config_.frequency = audio_sampling_frequency;
425 audio_sender_config_.channels = kAudioChannels;
426 audio_sender_config_.bitrate = kDefaultAudioEncoderBitrate;
427 audio_sender_config_.codec = audio_codec;
429 audio_receiver_config_.feedback_ssrc =
430 audio_sender_config_.incoming_feedback_ssrc;
431 audio_receiver_config_.incoming_ssrc = audio_sender_config_.sender_ssrc;
432 audio_receiver_config_.rtp_payload_type =
433 audio_sender_config_.rtp_config.payload_type;
434 audio_receiver_config_.use_external_decoder = external_audio_decoder;
435 audio_receiver_config_.frequency = audio_sender_config_.frequency;
436 audio_receiver_config_.channels = kAudioChannels;
437 audio_receiver_config_.codec = audio_sender_config_.codec;
439 test_receiver_audio_callback_->SetExpectedSamplingFrequency(
440 audio_receiver_config_.frequency);
442 video_sender_config_.sender_ssrc = 3;
443 video_sender_config_.incoming_feedback_ssrc = 4;
444 video_sender_config_.rtp_config.payload_type = 97;
445 video_sender_config_.use_external_encoder = false;
446 video_sender_config_.width = kVideoHdWidth;
447 video_sender_config_.height = kVideoHdHeight;
448 video_sender_config_.max_bitrate = 5000000;
449 video_sender_config_.min_bitrate = 1000000;
450 video_sender_config_.start_bitrate = 5000000;
451 video_sender_config_.max_qp = 30;
452 video_sender_config_.min_qp = 4;
453 video_sender_config_.max_frame_rate = 30;
454 video_sender_config_.max_number_of_video_buffers_used =
455 max_number_of_video_buffers_used;
456 video_sender_config_.codec = transport::kVp8;
457 video_sender_config_.number_of_cores = 1;
459 video_receiver_config_.feedback_ssrc =
460 video_sender_config_.incoming_feedback_ssrc;
461 video_receiver_config_.incoming_ssrc = video_sender_config_.sender_ssrc;
462 video_receiver_config_.rtp_payload_type =
463 video_sender_config_.rtp_config.payload_type;
464 video_receiver_config_.use_external_decoder = false;
465 video_receiver_config_.codec = video_sender_config_.codec;
467 transport_config_.audio_ssrc = audio_sender_config_.sender_ssrc;
468 transport_config_.video_ssrc = video_sender_config_.sender_ssrc;
469 transport_config_.video_codec = video_sender_config_.codec;
470 transport_config_.audio_codec = audio_sender_config_.codec;
471 transport_config_.video_rtp_config = video_sender_config_.rtp_config;
472 transport_config_.audio_rtp_config = audio_sender_config_.rtp_config;
473 transport_config_.audio_frequency = audio_sender_config_.frequency;
474 transport_config_.audio_channels = audio_sender_config_.channels;
477 void Create() {
478 cast_receiver_.reset(
479 CastReceiver::CreateCastReceiver(cast_environment_,
480 audio_receiver_config_,
481 video_receiver_config_,
482 &receiver_to_sender_));
483 transport_sender_.reset(new transport::CastTransportSenderImpl(
484 testing_clock_,
485 transport_config_,
486 base::Bind(&UpdateCastTransportStatus),
487 task_runner_,
488 &sender_to_receiver_));
490 cast_sender_.reset(CastSender::CreateCastSender(
491 cast_environment_,
492 &audio_sender_config_,
493 &video_sender_config_,
494 NULL,
495 base::Bind(&End2EndTest::InitializationResult, base::Unretained(this)),
496 transport_sender_.get()));
498 receiver_to_sender_.SetPacketReceiver(cast_sender_->packet_receiver());
499 sender_to_receiver_.SetPacketReceiver(cast_receiver_->packet_receiver());
501 frame_input_ = cast_sender_->frame_input();
502 frame_receiver_ = cast_receiver_->frame_receiver();
504 audio_bus_factory_.reset(
505 new TestAudioBusFactory(audio_sender_config_.channels,
506 audio_sender_config_.frequency,
507 kSoundFrequency,
508 kSoundVolume));
511 virtual ~End2EndTest() {
512 cast_environment_->Logging()->RemoveRawEventSubscriber(&event_subscriber_);
515 virtual void TearDown() OVERRIDE {
517 cast_sender_.reset();
518 cast_receiver_.reset();
519 task_runner_->RunTasks();
522 void SendVideoFrame(int start_value, const base::TimeTicks& capture_time) {
523 if (start_time_.is_null())
524 start_time_ = capture_time;
525 base::TimeDelta time_diff = capture_time - start_time_;
526 gfx::Size size(video_sender_config_.width, video_sender_config_.height);
527 EXPECT_TRUE(VideoFrame::IsValidConfig(
528 VideoFrame::I420, size, gfx::Rect(size), size));
529 scoped_refptr<media::VideoFrame> video_frame =
530 media::VideoFrame::CreateFrame(
531 VideoFrame::I420, size, gfx::Rect(size), size, time_diff);
532 PopulateVideoFrame(video_frame, start_value);
533 frame_input_->InsertRawVideoFrame(video_frame, capture_time);
536 void RunTasks(int during_ms) {
537 for (int i = 0; i < during_ms; ++i) {
538 // Call process the timers every 1 ms.
539 testing_clock_->Advance(base::TimeDelta::FromMilliseconds(1));
540 task_runner_->RunTasks();
544 void InitializationResult(CastInitializationStatus result) {
545 EXPECT_EQ(result, STATUS_INITIALIZED);
548 AudioReceiverConfig audio_receiver_config_;
549 VideoReceiverConfig video_receiver_config_;
550 AudioSenderConfig audio_sender_config_;
551 VideoSenderConfig video_sender_config_;
552 transport::CastTransportConfig transport_config_;
554 base::TimeTicks start_time_;
555 base::SimpleTestTickClock* testing_clock_;
556 scoped_refptr<test::FakeSingleThreadTaskRunner> task_runner_;
557 scoped_refptr<CastEnvironment> cast_environment_;
559 LoopBackTransport receiver_to_sender_;
560 LoopBackTransport sender_to_receiver_;
561 scoped_ptr<transport::CastTransportSenderImpl> transport_sender_;
563 scoped_ptr<CastReceiver> cast_receiver_;
564 scoped_ptr<CastSender> cast_sender_;
565 scoped_refptr<FrameInput> frame_input_;
566 scoped_refptr<FrameReceiver> frame_receiver_;
568 scoped_refptr<TestReceiverAudioCallback> test_receiver_audio_callback_;
569 scoped_refptr<TestReceiverVideoCallback> test_receiver_video_callback_;
571 scoped_ptr<TestAudioBusFactory> audio_bus_factory_;
573 SimpleEventSubscriber event_subscriber_;
574 std::vector<FrameEvent> frame_events_;
575 std::vector<PacketEvent> packet_events_;
576 std::vector<GenericEvent> generic_events_;
579 #if defined(OS_WIN)
580 #define MAYBE_LoopNoLossPcm16 DISABLED_LoopNoLossPcm16
581 #else
582 #define MAYBE_LoopNoLossPcm16 LoopNoLossPcm16
583 #endif
584 // TODO(mikhal): Crashes in win bots (http://crbug.com/329563)
585 TEST_F(End2EndTest, MAYBE_LoopNoLossPcm16) {
586 SetupConfig(transport::kPcm16, 32000, false, 1);
587 // Reduce video resolution to allow processing multiple frames within a
588 // reasonable time frame.
589 video_sender_config_.width = kVideoQcifWidth;
590 video_sender_config_.height = kVideoQcifHeight;
591 Create();
593 int video_start = kVideoStart;
594 int audio_diff = kFrameTimerMs;
595 int i = 0;
597 for (; i < 300; ++i) {
598 int num_10ms_blocks = audio_diff / 10;
599 audio_diff -= num_10ms_blocks * 10;
601 scoped_ptr<AudioBus> audio_bus(audio_bus_factory_->NextAudioBus(
602 base::TimeDelta::FromMilliseconds(10) * num_10ms_blocks));
604 base::TimeTicks send_time = testing_clock_->NowTicks();
605 if (i != 0) {
606 // Due to the re-sampler and NetEq in the webrtc AudioCodingModule the
607 // first samples will be 0 and then slowly ramp up to its real
608 // amplitude;
609 // ignore the first frame.
610 test_receiver_audio_callback_->AddExpectedResult(
611 ToPcmAudioFrame(*audio_bus, audio_sender_config_.frequency),
612 num_10ms_blocks,
613 send_time);
616 AudioBus* const audio_bus_ptr = audio_bus.get();
617 frame_input_->InsertAudio(
618 audio_bus_ptr,
619 send_time,
620 base::Bind(&OwnThatAudioBus, base::Passed(&audio_bus)));
622 test_receiver_video_callback_->AddExpectedResult(
623 video_start,
624 video_sender_config_.width,
625 video_sender_config_.height,
626 send_time);
627 SendVideoFrame(video_start, send_time);
629 if (i == 0) {
630 frame_receiver_->GetRawAudioFrame(
631 num_10ms_blocks,
632 audio_sender_config_.frequency,
633 base::Bind(&TestReceiverAudioCallback::IgnoreAudioFrame,
634 test_receiver_audio_callback_));
635 } else {
636 frame_receiver_->GetRawAudioFrame(
637 num_10ms_blocks,
638 audio_sender_config_.frequency,
639 base::Bind(&TestReceiverAudioCallback::CheckPcmAudioFrame,
640 test_receiver_audio_callback_));
643 frame_receiver_->GetRawVideoFrame(
644 base::Bind(&TestReceiverVideoCallback::CheckVideoFrame,
645 test_receiver_video_callback_));
647 RunTasks(kFrameTimerMs);
648 audio_diff += kFrameTimerMs;
649 video_start++;
652 RunTasks(2 * kFrameTimerMs + 1); // Empty the receiver pipeline.
653 EXPECT_EQ(i - 1, test_receiver_audio_callback_->number_times_called());
654 EXPECT_EQ(i, test_receiver_video_callback_->number_times_called());
657 // TODO(mikhal): Crashes on the Win7 x64 bots. Re-enable.
658 // http://crbug.com/329563
659 #if defined(OS_WIN)
660 #define MAYBE_LoopNoLossPcm16ExternalDecoder \
661 DISABLED_LoopNoLossPcm16ExternalDecoder
662 #else
663 #define MAYBE_LoopNoLossPcm16ExternalDecoder LoopNoLossPcm16ExternalDecoder
664 #endif
665 // This tests our external decoder interface for Audio.
666 // Audio test without packet loss using raw PCM 16 audio "codec";
667 TEST_F(End2EndTest, MAYBE_LoopNoLossPcm16ExternalDecoder) {
668 SetupConfig(transport::kPcm16, 32000, true, 1);
669 Create();
671 int i = 0;
672 for (; i < 10; ++i) {
673 base::TimeTicks send_time = testing_clock_->NowTicks();
674 scoped_ptr<AudioBus> audio_bus(audio_bus_factory_->NextAudioBus(
675 base::TimeDelta::FromMilliseconds(10)));
676 test_receiver_audio_callback_->AddExpectedResult(
677 ToPcmAudioFrame(*audio_bus, audio_sender_config_.frequency),
679 send_time);
681 AudioBus* const audio_bus_ptr = audio_bus.get();
682 frame_input_->InsertAudio(
683 audio_bus_ptr,
684 send_time,
685 base::Bind(&OwnThatAudioBus, base::Passed(&audio_bus)));
687 RunTasks(10);
688 frame_receiver_->GetCodedAudioFrame(
689 base::Bind(&TestReceiverAudioCallback::CheckCodedPcmAudioFrame,
690 test_receiver_audio_callback_));
692 RunTasks(2 * kFrameTimerMs + 1); // Empty the receiver pipeline.
693 EXPECT_EQ(10, test_receiver_audio_callback_->number_times_called());
696 // TODO(mikhal): Crashes on the bots. Re-enable. http://crbug.com/329563
697 #if defined(OS_WIN)
698 #define MAYBE_LoopNoLossOpus DISABLED_LoopNoLossOpus
699 #else
700 #define MAYBE_LoopNoLossOpus LoopNoLossOpus
701 #endif
702 // This tests our Opus audio codec without video.
703 TEST_F(End2EndTest, MAYBE_LoopNoLossOpus) {
704 SetupConfig(transport::kOpus, kDefaultAudioSamplingRate, false, 1);
705 Create();
707 int i = 0;
708 for (; i < 10; ++i) {
709 int num_10ms_blocks = 3;
710 base::TimeTicks send_time = testing_clock_->NowTicks();
712 scoped_ptr<AudioBus> audio_bus(audio_bus_factory_->NextAudioBus(
713 base::TimeDelta::FromMilliseconds(10) * num_10ms_blocks));
715 if (i != 0) {
716 test_receiver_audio_callback_->AddExpectedResult(
717 ToPcmAudioFrame(*audio_bus, audio_sender_config_.frequency),
718 num_10ms_blocks,
719 send_time);
722 AudioBus* const audio_bus_ptr = audio_bus.get();
723 frame_input_->InsertAudio(
724 audio_bus_ptr,
725 send_time,
726 base::Bind(&OwnThatAudioBus, base::Passed(&audio_bus)));
728 RunTasks(30);
730 if (i == 0) {
731 frame_receiver_->GetRawAudioFrame(
732 num_10ms_blocks,
733 audio_sender_config_.frequency,
734 base::Bind(&TestReceiverAudioCallback::IgnoreAudioFrame,
735 test_receiver_audio_callback_));
736 } else {
737 frame_receiver_->GetRawAudioFrame(
738 num_10ms_blocks,
739 audio_sender_config_.frequency,
740 base::Bind(&TestReceiverAudioCallback::CheckPcmAudioFrame,
741 test_receiver_audio_callback_));
744 RunTasks(2 * kFrameTimerMs + 1); // Empty the receiver pipeline.
745 EXPECT_EQ(i - 1, test_receiver_audio_callback_->number_times_called());
748 // This tests start sending audio and video before the receiver is ready.
750 // TODO(miu): Test disabled because of non-determinism.
751 // http://crbug.com/314233
752 TEST_F(End2EndTest, DISABLED_StartSenderBeforeReceiver) {
753 SetupConfig(transport::kOpus, kDefaultAudioSamplingRate, false, 1);
754 Create();
756 int video_start = kVideoStart;
757 int audio_diff = kFrameTimerMs;
759 sender_to_receiver_.SetSendPackets(false);
761 for (int i = 0; i < 3; ++i) {
762 int num_10ms_blocks = audio_diff / 10;
763 audio_diff -= num_10ms_blocks * 10;
765 base::TimeTicks send_time = testing_clock_->NowTicks();
766 scoped_ptr<AudioBus> audio_bus(audio_bus_factory_->NextAudioBus(
767 base::TimeDelta::FromMilliseconds(10) * num_10ms_blocks));
769 AudioBus* const audio_bus_ptr = audio_bus.get();
770 frame_input_->InsertAudio(
771 audio_bus_ptr,
772 send_time,
773 base::Bind(&OwnThatAudioBus, base::Passed(&audio_bus)));
775 SendVideoFrame(video_start, send_time);
776 RunTasks(kFrameTimerMs);
777 audio_diff += kFrameTimerMs;
778 video_start++;
780 RunTasks(100);
781 sender_to_receiver_.SetSendPackets(true);
783 int j = 0;
784 const int number_of_audio_frames_to_ignore = 3;
785 for (; j < 10; ++j) {
786 int num_10ms_blocks = audio_diff / 10;
787 audio_diff -= num_10ms_blocks * 10;
788 base::TimeTicks send_time = testing_clock_->NowTicks();
790 scoped_ptr<AudioBus> audio_bus(audio_bus_factory_->NextAudioBus(
791 base::TimeDelta::FromMilliseconds(10) * num_10ms_blocks));
793 if (j >= number_of_audio_frames_to_ignore) {
794 test_receiver_audio_callback_->AddExpectedResult(
795 ToPcmAudioFrame(*audio_bus, audio_sender_config_.frequency),
796 num_10ms_blocks,
797 send_time);
800 AudioBus* const audio_bus_ptr = audio_bus.get();
801 frame_input_->InsertAudio(
802 audio_bus_ptr,
803 send_time,
804 base::Bind(&OwnThatAudioBus, base::Passed(&audio_bus)));
806 test_receiver_video_callback_->AddExpectedResult(
807 video_start,
808 video_sender_config_.width,
809 video_sender_config_.height,
810 send_time);
812 SendVideoFrame(video_start, send_time);
813 RunTasks(kFrameTimerMs);
814 audio_diff += kFrameTimerMs;
816 if (j < number_of_audio_frames_to_ignore) {
817 frame_receiver_->GetRawAudioFrame(
818 num_10ms_blocks,
819 audio_sender_config_.frequency,
820 base::Bind(&TestReceiverAudioCallback::IgnoreAudioFrame,
821 test_receiver_audio_callback_));
822 } else {
823 frame_receiver_->GetRawAudioFrame(
824 num_10ms_blocks,
825 audio_sender_config_.frequency,
826 base::Bind(&TestReceiverAudioCallback::CheckPcmAudioFrame,
827 test_receiver_audio_callback_));
829 frame_receiver_->GetRawVideoFrame(
830 base::Bind(&TestReceiverVideoCallback::CheckVideoFrame,
831 test_receiver_video_callback_));
832 video_start++;
834 RunTasks(2 * kFrameTimerMs + 1); // Empty the receiver pipeline.
835 EXPECT_EQ(j - number_of_audio_frames_to_ignore,
836 test_receiver_audio_callback_->number_times_called());
837 EXPECT_EQ(j, test_receiver_video_callback_->number_times_called());
840 // This tests a network glitch lasting for 10 video frames.
841 TEST_F(End2EndTest, GlitchWith3Buffers) {
842 SetupConfig(transport::kOpus, kDefaultAudioSamplingRate, false, 3);
843 video_sender_config_.rtp_config.max_delay_ms = 67;
844 video_receiver_config_.rtp_max_delay_ms = 67;
845 Create();
847 int video_start = kVideoStart;
848 base::TimeTicks send_time = testing_clock_->NowTicks();
849 SendVideoFrame(video_start, send_time);
850 RunTasks(kFrameTimerMs);
852 test_receiver_video_callback_->AddExpectedResult(video_start,
853 video_sender_config_.width,
854 video_sender_config_.height,
855 send_time);
856 frame_receiver_->GetRawVideoFrame(
857 base::Bind(&TestReceiverVideoCallback::CheckVideoFrame,
858 test_receiver_video_callback_));
860 RunTasks(750); // Make sure that we send a RTCP packet.
862 video_start++;
864 // Introduce a glitch lasting for 10 frames.
865 sender_to_receiver_.SetSendPackets(false);
866 for (int i = 0; i < 10; ++i) {
867 send_time = testing_clock_->NowTicks();
868 // First 3 will be sent and lost.
869 SendVideoFrame(video_start, send_time);
870 RunTasks(kFrameTimerMs);
871 video_start++;
873 sender_to_receiver_.SetSendPackets(true);
874 RunTasks(100);
875 send_time = testing_clock_->NowTicks();
877 // Frame 1 should be acked by now and we should have an opening to send 4.
878 SendVideoFrame(video_start, send_time);
879 RunTasks(kFrameTimerMs);
881 // Frames 1-3 are old frames by now, and therefore should be decoded, but
882 // not rendered. The next frame we expect to render is frame #4.
883 test_receiver_video_callback_->AddExpectedResult(video_start,
884 video_sender_config_.width,
885 video_sender_config_.height,
886 send_time);
888 frame_receiver_->GetRawVideoFrame(
889 base::Bind(&TestReceiverVideoCallback::CheckVideoFrame,
890 test_receiver_video_callback_));
892 RunTasks(2 * kFrameTimerMs + 1); // Empty the receiver pipeline.
893 EXPECT_EQ(2, test_receiver_video_callback_->number_times_called());
896 TEST_F(End2EndTest, DropEveryOtherFrame3Buffers) {
897 SetupConfig(transport::kOpus, kDefaultAudioSamplingRate, false, 3);
898 video_sender_config_.rtp_config.max_delay_ms = 67;
899 video_receiver_config_.rtp_max_delay_ms = 67;
900 Create();
901 sender_to_receiver_.DropAllPacketsBelongingToOddFrames();
903 int video_start = kVideoStart;
904 base::TimeTicks send_time;
906 int i = 0;
907 for (; i < 20; ++i) {
908 send_time = testing_clock_->NowTicks();
909 SendVideoFrame(video_start, send_time);
911 if (i % 2 == 0) {
912 test_receiver_video_callback_->AddExpectedResult(
913 video_start,
914 video_sender_config_.width,
915 video_sender_config_.height,
916 send_time);
918 // GetRawVideoFrame will not return the frame until we are close in
919 // time before we should render the frame.
920 frame_receiver_->GetRawVideoFrame(
921 base::Bind(&TestReceiverVideoCallback::CheckVideoFrame,
922 test_receiver_video_callback_));
924 RunTasks(kFrameTimerMs);
925 video_start++;
928 RunTasks(2 * kFrameTimerMs + 1); // Empty the pipeline.
929 EXPECT_EQ(i / 2, test_receiver_video_callback_->number_times_called());
932 TEST_F(End2EndTest, ResetReferenceFrameId) {
933 SetupConfig(transport::kOpus, kDefaultAudioSamplingRate, false, 3);
934 video_sender_config_.rtp_config.max_delay_ms = 67;
935 video_receiver_config_.rtp_max_delay_ms = 67;
936 Create();
937 sender_to_receiver_.AlwaysResetReferenceFrameId();
939 int frames_counter = 0;
940 for (; frames_counter < 20; ++frames_counter) {
941 const base::TimeTicks send_time = testing_clock_->NowTicks();
942 SendVideoFrame(frames_counter, send_time);
944 test_receiver_video_callback_->AddExpectedResult(
945 frames_counter,
946 video_sender_config_.width,
947 video_sender_config_.height,
948 send_time);
950 // GetRawVideoFrame will not return the frame until we are close to the
951 // time in which we should render the frame.
952 frame_receiver_->GetRawVideoFrame(
953 base::Bind(&TestReceiverVideoCallback::CheckVideoFrame,
954 test_receiver_video_callback_));
955 RunTasks(kFrameTimerMs);
957 RunTasks(2 * kFrameTimerMs + 1); // Empty the pipeline.
958 EXPECT_EQ(frames_counter,
959 test_receiver_video_callback_->number_times_called());
962 TEST_F(End2EndTest, CryptoVideo) {
963 SetupConfig(transport::kPcm16, 32000, false, 1);
965 transport_config_.aes_iv_mask =
966 ConvertFromBase16String("1234567890abcdeffedcba0987654321");
967 transport_config_.aes_key =
968 ConvertFromBase16String("deadbeefcafeb0b0b0b0cafedeadbeef");
970 video_receiver_config_.aes_iv_mask = transport_config_.aes_iv_mask;
971 video_receiver_config_.aes_key = transport_config_.aes_key;
973 Create();
975 int frames_counter = 0;
976 for (; frames_counter < 3; ++frames_counter) {
977 const base::TimeTicks send_time = testing_clock_->NowTicks();
979 SendVideoFrame(frames_counter, send_time);
981 test_receiver_video_callback_->AddExpectedResult(
982 frames_counter,
983 video_sender_config_.width,
984 video_sender_config_.height,
985 send_time);
987 // GetRawVideoFrame will not return the frame until we are close to the
988 // time in which we should render the frame.
989 frame_receiver_->GetRawVideoFrame(
990 base::Bind(&TestReceiverVideoCallback::CheckVideoFrame,
991 test_receiver_video_callback_));
992 RunTasks(kFrameTimerMs);
994 RunTasks(2 * kFrameTimerMs + 1); // Empty the pipeline.
995 EXPECT_EQ(frames_counter,
996 test_receiver_video_callback_->number_times_called());
999 // TODO(mikhal): Crashes on the bots. Re-enable. http://crbug.com/329563
1000 #if defined(OS_WIN)
1001 #define MAYBE_CryptoAudio DISABLED_CryptoAudio
1002 #else
1003 #define MAYBE_CryptoAudio CryptoAudio
1004 #endif
1005 TEST_F(End2EndTest, MAYBE_CryptoAudio) {
1006 SetupConfig(transport::kPcm16, 32000, false, 1);
1008 transport_config_.aes_iv_mask =
1009 ConvertFromBase16String("abcdeffedcba12345678900987654321");
1010 transport_config_.aes_key =
1011 ConvertFromBase16String("deadbeefcafecafedeadbeefb0b0b0b0");
1013 audio_receiver_config_.aes_iv_mask = transport_config_.aes_iv_mask;
1014 audio_receiver_config_.aes_key = transport_config_.aes_key;
1016 Create();
1018 int frames_counter = 0;
1019 for (; frames_counter < 3; ++frames_counter) {
1020 int num_10ms_blocks = 2;
1022 const base::TimeTicks send_time = testing_clock_->NowTicks();
1024 scoped_ptr<AudioBus> audio_bus(audio_bus_factory_->NextAudioBus(
1025 base::TimeDelta::FromMilliseconds(10) * num_10ms_blocks));
1027 if (frames_counter != 0) {
1028 // Due to the re-sampler and NetEq in the webrtc AudioCodingModule the
1029 // first samples will be 0 and then slowly ramp up to its real
1030 // amplitude;
1031 // ignore the first frame.
1032 test_receiver_audio_callback_->AddExpectedResult(
1033 ToPcmAudioFrame(*audio_bus, audio_sender_config_.frequency),
1034 num_10ms_blocks,
1035 send_time);
1037 AudioBus* const audio_bus_ptr = audio_bus.get();
1038 frame_input_->InsertAudio(
1039 audio_bus_ptr,
1040 send_time,
1041 base::Bind(&OwnThatAudioBus, base::Passed(&audio_bus)));
1043 RunTasks(num_10ms_blocks * 10);
1045 if (frames_counter == 0) {
1046 frame_receiver_->GetRawAudioFrame(
1047 num_10ms_blocks,
1048 32000,
1049 base::Bind(&TestReceiverAudioCallback::IgnoreAudioFrame,
1050 test_receiver_audio_callback_));
1051 } else {
1052 frame_receiver_->GetRawAudioFrame(
1053 num_10ms_blocks,
1054 32000,
1055 base::Bind(&TestReceiverAudioCallback::CheckPcmAudioFrame,
1056 test_receiver_audio_callback_));
1059 RunTasks(2 * kFrameTimerMs + 1); // Empty the pipeline.
1060 EXPECT_EQ(frames_counter - 1,
1061 test_receiver_audio_callback_->number_times_called());
1064 // Video test without packet loss - tests the logging aspects of the end2end,
1065 // but is basically equivalent to LoopNoLossPcm16.
1066 TEST_F(End2EndTest, VideoLogging) {
1067 SetupConfig(transport::kPcm16, 32000, false, 1);
1068 Create();
1070 int video_start = kVideoStart;
1071 const int num_frames = 1;
1072 for (int i = 0; i < num_frames; ++i) {
1073 base::TimeTicks send_time = testing_clock_->NowTicks();
1074 test_receiver_video_callback_->AddExpectedResult(
1075 video_start,
1076 video_sender_config_.width,
1077 video_sender_config_.height,
1078 send_time);
1080 SendVideoFrame(video_start, send_time);
1081 RunTasks(kFrameTimerMs);
1083 frame_receiver_->GetRawVideoFrame(
1084 base::Bind(&TestReceiverVideoCallback::CheckVideoFrame,
1085 test_receiver_video_callback_));
1087 video_start++;
1090 // Basic tests.
1091 RunTasks(2 * kFrameTimerMs + 1); // Empty the receiver pipeline.
1092 int num_callbacks_called =
1093 test_receiver_video_callback_->number_times_called();
1094 EXPECT_EQ(num_frames, num_callbacks_called);
1096 RunTasks(750); // Make sure that we send a RTCP message with the log.
1098 // Logging tests.
1099 // Frame logging.
1100 // Verify that all frames and all required events were logged.
1101 event_subscriber_.GetFrameEventsAndReset(&frame_events_);
1103 // For each frame, count the number of events that occurred for each event
1104 // for that frame.
1105 std::map<RtpTimestamp, LoggingEventCounts> event_counter_for_frame =
1106 GetEventCountForFrameEvents(frame_events_);
1108 // Verify that there are logs for expected number of frames.
1109 EXPECT_EQ(num_frames, static_cast<int>(event_counter_for_frame.size()));
1111 // Verify that each frame have the expected types of events logged.
1112 for (std::map<RtpTimestamp, LoggingEventCounts>::iterator map_it =
1113 event_counter_for_frame.begin();
1114 map_it != event_counter_for_frame.end();
1115 ++map_it) {
1116 int total_event_count_for_frame = 0;
1117 for (int i = 0; i < kNumOfLoggingEvents; ++i) {
1118 total_event_count_for_frame += map_it->second.counter[i];
1121 int expected_event_count_for_frame = 0;
1123 EXPECT_GT(map_it->second.counter[kVideoFrameSentToEncoder], 0);
1124 expected_event_count_for_frame +=
1125 map_it->second.counter[kVideoFrameSentToEncoder];
1127 EXPECT_GT(map_it->second.counter[kVideoFrameEncoded], 0);
1128 expected_event_count_for_frame +=
1129 map_it->second.counter[kVideoFrameEncoded];
1131 EXPECT_GT(map_it->second.counter[kVideoFrameReceived], 0);
1132 expected_event_count_for_frame +=
1133 map_it->second.counter[kVideoFrameReceived];
1135 EXPECT_GT(map_it->second.counter[kVideoRenderDelay], 0);
1136 expected_event_count_for_frame += map_it->second.counter[kVideoRenderDelay];
1138 EXPECT_GT(map_it->second.counter[kVideoFrameDecoded], 0);
1139 expected_event_count_for_frame +=
1140 map_it->second.counter[kVideoFrameDecoded];
1142 // Verify that there were no other events logged with respect to this
1143 // frame.
1144 // (i.e. Total event count = expected event count)
1145 EXPECT_EQ(total_event_count_for_frame, expected_event_count_for_frame);
1148 // Packet logging.
1149 // Verify that all packet related events were logged.
1150 event_subscriber_.GetPacketEventsAndReset(&packet_events_);
1151 std::map<uint16, LoggingEventCounts> event_count_for_packet =
1152 GetEventCountForPacketEvents(packet_events_);
1154 // Verify that each packet have the expected types of events logged.
1155 for (std::map<uint16, LoggingEventCounts>::iterator map_it =
1156 event_count_for_packet.begin();
1157 map_it != event_count_for_packet.end();
1158 ++map_it) {
1159 int total_event_count_for_packet = 0;
1160 for (int i = 0; i < kNumOfLoggingEvents; ++i) {
1161 total_event_count_for_packet += map_it->second.counter[i];
1164 int expected_event_count_for_packet = 0;
1165 EXPECT_GT(map_it->second.counter[kVideoPacketReceived], 0);
1166 expected_event_count_for_packet +=
1167 map_it->second.counter[kVideoPacketReceived];
1169 // Verify that there were no other events logged with respect to this
1170 // packet. (i.e. Total event count = expected event count)
1171 EXPECT_EQ(total_event_count_for_packet, expected_event_count_for_packet);
1175 // TODO(mikhal): Crashes on the bots. Re-enable. http://crbug.com/329563
1176 #if defined(OS_WIN)
1177 #define MAYBE_AudioLogging DISABLED_AudioLogging
1178 #else
1179 #define MAYBE_AudioLogging AudioLogging
1180 #endif
1181 // Audio test without packet loss - tests the logging aspects of the end2end,
1182 // but is basically equivalent to LoopNoLossPcm16.
1183 TEST_F(End2EndTest, MAYBE_AudioLogging) {
1184 SetupConfig(transport::kPcm16, 32000, false, 1);
1185 Create();
1187 int audio_diff = kFrameTimerMs;
1188 const int num_frames = 10;
1189 for (int i = 0; i < num_frames; ++i) {
1190 int num_10ms_blocks = audio_diff / 10;
1191 audio_diff -= num_10ms_blocks * 10;
1192 base::TimeTicks send_time = testing_clock_->NowTicks();
1194 scoped_ptr<AudioBus> audio_bus(audio_bus_factory_->NextAudioBus(
1195 base::TimeDelta::FromMilliseconds(10) * num_10ms_blocks));
1197 if (i != 0) {
1198 // Due to the re-sampler and NetEq in the webrtc AudioCodingModule the
1199 // first samples will be 0 and then slowly ramp up to its real
1200 // amplitude;
1201 // ignore the first frame.
1202 test_receiver_audio_callback_->AddExpectedResult(
1203 ToPcmAudioFrame(*audio_bus, audio_sender_config_.frequency),
1204 num_10ms_blocks,
1205 send_time);
1208 AudioBus* const audio_bus_ptr = audio_bus.get();
1209 frame_input_->InsertAudio(
1210 audio_bus_ptr,
1211 send_time,
1212 base::Bind(&OwnThatAudioBus, base::Passed(&audio_bus)));
1214 RunTasks(kFrameTimerMs);
1215 audio_diff += kFrameTimerMs;
1217 if (i == 0) {
1218 frame_receiver_->GetRawAudioFrame(
1219 num_10ms_blocks,
1220 audio_sender_config_.frequency,
1221 base::Bind(&TestReceiverAudioCallback::IgnoreAudioFrame,
1222 test_receiver_audio_callback_));
1223 } else {
1224 frame_receiver_->GetRawAudioFrame(
1225 num_10ms_blocks,
1226 audio_sender_config_.frequency,
1227 base::Bind(&TestReceiverAudioCallback::CheckPcmAudioFrame,
1228 test_receiver_audio_callback_));
1232 // Basic tests.
1233 RunTasks(2 * kFrameTimerMs + 1); // Empty the receiver pipeline.
1235 int num_times_called = test_receiver_audio_callback_->number_times_called();
1236 EXPECT_EQ(num_frames - 1, num_times_called);
1238 // Logging tests.
1239 // Verify that all frames and all required events were logged.
1240 event_subscriber_.GetFrameEventsAndReset(&frame_events_);
1242 // Construct a map from each frame (RTP timestamp) to a count of each event
1243 // type logged for that frame.
1244 std::map<RtpTimestamp, LoggingEventCounts> event_counter_for_frame =
1245 GetEventCountForFrameEvents(frame_events_);
1247 // Verify that each frame have the expected types of events logged.
1248 std::map<RtpTimestamp, LoggingEventCounts>::iterator map_it =
1249 event_counter_for_frame.begin();
1251 // TODO(imcheng): This only checks the first frame. This doesn't work
1252 // properly for all frames for two reasons:
1253 // 1. There is a loopback of kAudioPlayoutDelay and kAudioFrameDecoded
1254 // events due to shared CastEnvironment between sender and
1255 // receiver in the test setup. We will need to create separate
1256 // CastEnvironment once again to fix this.
1257 // 2. kAudioPlayoutDelay and kAudioFrameDecoded RTP timestamps aren't
1258 // exactly aligned with those of kAudioFrameReceived and kAudioFrameEncoded.
1259 // Note that these RTP timestamps are output from webrtc::AudioCodingModule
1260 // which are different from RTP timestamps that the cast library generates
1261 // during the encode step (and which are sent to receiver). The first frame
1262 // just happen to be aligned.
1263 int total_event_count_for_frame = 0;
1264 for (int j = 0; j < kNumOfLoggingEvents; ++j)
1265 total_event_count_for_frame += map_it->second.counter[j];
1267 int expected_event_count_for_frame = 0;
1269 EXPECT_GT(map_it->second.counter[kAudioFrameReceived], 0);
1270 expected_event_count_for_frame += map_it->second.counter[kAudioFrameReceived];
1272 EXPECT_GT(map_it->second.counter[kAudioFrameEncoded], 0);
1273 expected_event_count_for_frame += map_it->second.counter[kAudioFrameEncoded];
1275 // Note that this is a big positive number instead of just 1 due to loopback
1276 // described in TODO above.
1277 EXPECT_GT(map_it->second.counter[kAudioPlayoutDelay], 0);
1278 expected_event_count_for_frame += map_it->second.counter[kAudioPlayoutDelay];
1279 EXPECT_GT(map_it->second.counter[kAudioFrameDecoded], 0);
1280 expected_event_count_for_frame += map_it->second.counter[kAudioFrameDecoded];
1282 // Verify that there were no other events logged with respect to this frame.
1283 // (i.e. Total event count = expected event count)
1284 EXPECT_EQ(total_event_count_for_frame, expected_event_count_for_frame);
1287 // TODO(pwestin): Add repeatable packet loss test.
1288 // TODO(pwestin): Add test for misaligned send get calls.
1289 // TODO(pwestin): Add more tests that does not resample.
1290 // TODO(pwestin): Add test when we have starvation for our RunTask.
1292 } // namespace cast
1293 } // namespace media