1 // Copyright 2014 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.
5 #include "base/callback.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/time/default_tick_clock.h"
8 #include "chrome/browser/media/cast_transport_host_filter.h"
9 #include "content/public/test/test_browser_thread_bundle.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 class CastTransportHostFilterTest : public testing::Test {
16 CastTransportHostFilterTest()
17 : browser_thread_bundle_(
18 content::TestBrowserThreadBundle::IO_MAINLOOP) {
21 virtual void SetUp() OVERRIDE {
22 filter_ = new cast::CastTransportHostFilter();
25 void FakeSend(const IPC::Message& message) {
27 EXPECT_TRUE(filter_->OnMessageReceived(message, &message_was_ok));
28 EXPECT_TRUE(message_was_ok);
31 net::IPEndPoint GetLocalEndPoint() {
32 return net::IPEndPoint(net::IPAddressNumber(4, 0), 0);
35 net::IPEndPoint GetRemoteEndPoint() {
36 // 127.0.0.1:7 is the local echo service port, which
37 // is probably not going to respond, but that's ok.
38 // TODO(hubbe): Open up an UDP port and make sure
39 // we can send and receive packets.
40 net::IPAddressNumber receiver_address(4, 0);
41 receiver_address[0] = 127;
42 receiver_address[3] = 1;
43 return net::IPEndPoint(receiver_address, 7);
46 content::TestBrowserThreadBundle browser_thread_bundle_;
47 scoped_refptr<content::BrowserMessageFilter> filter_;
48 net::IPAddressNumber receiver_address_;
51 TEST_F(CastTransportHostFilterTest, NewDelete) {
52 media::cast::transport::CastTransportConfig config;
53 config.local_endpoint = GetLocalEndPoint();
54 config.receiver_endpoint = GetRemoteEndPoint();
55 const int kChannelId = 17;
56 CastHostMsg_New new_msg(kChannelId, config);
57 CastHostMsg_Delete delete_msg(kChannelId);
59 // New, then delete, as expected.
67 // Now create/delete transport senders in the wrong order to make sure
68 // this doesn't crash.
77 TEST_F(CastTransportHostFilterTest, NewMany) {
78 media::cast::transport::CastTransportConfig config;
79 config.local_endpoint = GetLocalEndPoint();
80 config.receiver_endpoint = GetRemoteEndPoint();
82 for (int i = 0; i < 100; i++) {
83 CastHostMsg_New new_msg(i, config);
87 for (int i = 0; i < 60; i++) {
88 CastHostMsg_Delete delete_msg(i);
92 // Leave some open, see what happens.
95 TEST_F(CastTransportHostFilterTest, SimpleMessages) {
96 // Create a cast transport sender.
97 media::cast::transport::CastTransportConfig config;
98 config.local_endpoint = GetLocalEndPoint();
99 config.receiver_endpoint = GetRemoteEndPoint();
100 const int32 kChannelId = 42;
101 CastHostMsg_New new_msg(kChannelId, config);
104 media::cast::transport::EncodedAudioFrame audio_frame;
105 audio_frame.codec = media::cast::transport::kPcm16;
106 audio_frame.frame_id = 1;
107 audio_frame.rtp_timestamp = 47;
108 const int kSamples = 47;
109 const int kBytesPerSample = 2;
110 const int kChannels = 2;
111 audio_frame.data = std::string(kSamples * kBytesPerSample * kChannels, 'q');
112 CastHostMsg_InsertCodedAudioFrame insert_coded_audio_frame(
113 kChannelId, audio_frame, base::TimeTicks::Now());
116 media::cast::transport::EncodedVideoFrame video_frame;
117 video_frame.codec = media::cast::transport::kVp8;
118 video_frame.key_frame = true;
119 video_frame.frame_id = 1;
120 video_frame.last_referenced_frame_id = 0;
121 // Let's make sure we try a few kb so multiple packets
123 const int kVideoDataSize = 4711;
124 video_frame.data = std::string(kVideoDataSize, 'p');
125 CastHostMsg_InsertCodedVideoFrame insert_coded_video_frame(
126 kChannelId, video_frame, base::TimeTicks::Now());
129 media::cast::transport::SendRtcpFromRtpSenderData rtcp_data;
130 rtcp_data.packet_type_flags = 0;
131 rtcp_data.sending_ssrc = 0;
132 rtcp_data.c_name = "FNRD";
133 media::cast::transport::RtcpSenderInfo sender_info;
134 sender_info.ntp_seconds = 1;
135 sender_info.ntp_fraction = 2;
136 sender_info.rtp_timestamp = 3;
137 sender_info.send_packet_count = 4;
138 sender_info.send_octet_count = 5;
139 media::cast::transport::RtcpDlrrReportBlock dlrr;
141 dlrr.delay_since_last_rr = 8;
142 media::cast::transport::RtcpSenderLogMessage sender_log(1);
143 sender_log[0].frame_status =
144 media::cast::transport::kRtcpSenderFrameStatusSentToNetwork;
145 sender_log[0].rtp_timestamp = 9;
146 CastHostMsg_SendRtcpFromRtpSender rtcp_msg(
147 kChannelId, rtcp_data, sender_info, dlrr, sender_log);
150 media::cast::MissingFramesAndPacketsMap missing_packets;
151 missing_packets[1].insert(4);
152 missing_packets[3].insert(7);
153 CastHostMsg_ResendPackets resend_msg(
154 kChannelId, false, missing_packets);
155 FakeSend(resend_msg);
157 CastHostMsg_Delete delete_msg(kChannelId);
158 FakeSend(delete_msg);