• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "media/cast/net/rtp/rtp_packetizer.h"
6 
7 #include <stdint.h>
8 
9 #include "base/memory/scoped_ptr.h"
10 #include "base/test/simple_test_tick_clock.h"
11 #include "media/cast/logging/logging_impl.h"
12 #include "media/cast/logging/simple_event_subscriber.h"
13 #include "media/cast/net/pacing/paced_sender.h"
14 #include "media/cast/net/rtp/packet_storage.h"
15 #include "media/cast/net/rtp/rtp_header_parser.h"
16 #include "media/cast/test/fake_single_thread_task_runner.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 
19 namespace media {
20 namespace cast {
21 
22 namespace {
23 static const int kPayload = 127;
24 static const uint32 kTimestampMs = 10;
25 static const uint16 kSeqNum = 33;
26 static const int kMaxPacketLength = 1500;
27 static const int kSsrc = 0x12345;
28 static const unsigned int kFrameSize = 5000;
29 }
30 
31 class TestRtpPacketTransport : public PacketSender {
32  public:
TestRtpPacketTransport(RtpPacketizerConfig config)33   explicit TestRtpPacketTransport(RtpPacketizerConfig config)
34       : config_(config),
35         sequence_number_(kSeqNum),
36         packets_sent_(0),
37         expected_number_of_packets_(0),
38         expected_packet_id_(0),
39         expected_frame_id_(0),
40         expectd_rtp_timestamp_(0) {}
41 
VerifyRtpHeader(const RtpCastTestHeader & rtp_header)42   void VerifyRtpHeader(const RtpCastTestHeader& rtp_header) {
43     VerifyCommonRtpHeader(rtp_header);
44     VerifyCastRtpHeader(rtp_header);
45   }
46 
VerifyCommonRtpHeader(const RtpCastTestHeader & rtp_header)47   void VerifyCommonRtpHeader(const RtpCastTestHeader& rtp_header) {
48     EXPECT_EQ(kPayload, rtp_header.payload_type);
49     EXPECT_EQ(sequence_number_, rtp_header.sequence_number);
50     EXPECT_EQ(expectd_rtp_timestamp_, rtp_header.rtp_timestamp);
51     EXPECT_EQ(config_.ssrc, rtp_header.ssrc);
52     EXPECT_EQ(0, rtp_header.num_csrcs);
53   }
54 
VerifyCastRtpHeader(const RtpCastTestHeader & rtp_header)55   void VerifyCastRtpHeader(const RtpCastTestHeader& rtp_header) {
56     EXPECT_FALSE(rtp_header.is_key_frame);
57     EXPECT_EQ(expected_frame_id_, rtp_header.frame_id);
58     EXPECT_EQ(expected_packet_id_, rtp_header.packet_id);
59     EXPECT_EQ(expected_number_of_packets_ - 1, rtp_header.max_packet_id);
60     EXPECT_TRUE(rtp_header.is_reference);
61     EXPECT_EQ(expected_frame_id_ - 1u, rtp_header.reference_frame_id);
62   }
63 
SendPacket(PacketRef packet,const base::Closure & cb)64   virtual bool SendPacket(PacketRef packet, const base::Closure& cb) OVERRIDE {
65     ++packets_sent_;
66     RtpHeaderParser parser(&packet->data[0], packet->data.size());
67     RtpCastTestHeader rtp_header;
68     parser.Parse(&rtp_header);
69     VerifyRtpHeader(rtp_header);
70     ++sequence_number_;
71     ++expected_packet_id_;
72     return true;
73   }
74 
GetBytesSent()75   virtual int64 GetBytesSent() OVERRIDE {
76     return 0;
77   }
78 
number_of_packets_received() const79   size_t number_of_packets_received() const { return packets_sent_; }
80 
set_expected_number_of_packets(size_t expected_number_of_packets)81   void set_expected_number_of_packets(size_t expected_number_of_packets) {
82     expected_number_of_packets_ = expected_number_of_packets;
83   }
84 
set_rtp_timestamp(uint32 rtp_timestamp)85   void set_rtp_timestamp(uint32 rtp_timestamp) {
86     expectd_rtp_timestamp_ = rtp_timestamp;
87   }
88 
89   RtpPacketizerConfig config_;
90   uint32 sequence_number_;
91   size_t packets_sent_;
92   size_t number_of_packets_;
93   size_t expected_number_of_packets_;
94   // Assuming packets arrive in sequence.
95   int expected_packet_id_;
96   uint32 expected_frame_id_;
97   uint32 expectd_rtp_timestamp_;
98 
99   DISALLOW_COPY_AND_ASSIGN(TestRtpPacketTransport);
100 };
101 
102 class RtpPacketizerTest : public ::testing::Test {
103  protected:
RtpPacketizerTest()104   RtpPacketizerTest()
105       : task_runner_(new test::FakeSingleThreadTaskRunner(&testing_clock_)) {
106     config_.sequence_number = kSeqNum;
107     config_.ssrc = kSsrc;
108     config_.payload_type = kPayload;
109     config_.max_payload_length = kMaxPacketLength;
110     transport_.reset(new TestRtpPacketTransport(config_));
111     pacer_.reset(new PacedSender(kTargetBurstSize,
112                                  kMaxBurstSize,
113                                  &testing_clock_,
114                                  &logging_,
115                                  transport_.get(),
116                                  task_runner_));
117     pacer_->RegisterVideoSsrc(config_.ssrc);
118     rtp_packetizer_.reset(new RtpPacketizer(
119         pacer_.get(), &packet_storage_, config_));
120     video_frame_.dependency = EncodedFrame::DEPENDENT;
121     video_frame_.frame_id = 0;
122     video_frame_.referenced_frame_id = kStartFrameId;
123     video_frame_.data.assign(kFrameSize, 123);
124     video_frame_.rtp_timestamp =
125         GetVideoRtpTimestamp(testing_clock_.NowTicks());
126   }
127 
RunTasks(int during_ms)128   void RunTasks(int during_ms) {
129     for (int i = 0; i < during_ms; ++i) {
130       // Call process the timers every 1 ms.
131       testing_clock_.Advance(base::TimeDelta::FromMilliseconds(1));
132       task_runner_->RunTasks();
133     }
134   }
135 
136   base::SimpleTestTickClock testing_clock_;
137   scoped_refptr<test::FakeSingleThreadTaskRunner> task_runner_;
138   EncodedFrame video_frame_;
139   PacketStorage packet_storage_;
140   RtpPacketizerConfig config_;
141   scoped_ptr<TestRtpPacketTransport> transport_;
142   LoggingImpl logging_;
143   scoped_ptr<PacedSender> pacer_;
144   scoped_ptr<RtpPacketizer> rtp_packetizer_;
145 
146   DISALLOW_COPY_AND_ASSIGN(RtpPacketizerTest);
147 };
148 
TEST_F(RtpPacketizerTest,SendStandardPackets)149 TEST_F(RtpPacketizerTest, SendStandardPackets) {
150   size_t expected_num_of_packets = kFrameSize / kMaxPacketLength + 1;
151   transport_->set_expected_number_of_packets(expected_num_of_packets);
152   transport_->set_rtp_timestamp(video_frame_.rtp_timestamp);
153 
154   testing_clock_.Advance(base::TimeDelta::FromMilliseconds(kTimestampMs));
155   video_frame_.reference_time = testing_clock_.NowTicks();
156   rtp_packetizer_->SendFrameAsPackets(video_frame_);
157   RunTasks(33 + 1);
158   EXPECT_EQ(expected_num_of_packets, transport_->number_of_packets_received());
159 }
160 
TEST_F(RtpPacketizerTest,Stats)161 TEST_F(RtpPacketizerTest, Stats) {
162   EXPECT_FALSE(rtp_packetizer_->send_packet_count());
163   EXPECT_FALSE(rtp_packetizer_->send_octet_count());
164   // Insert packets at varying lengths.
165   size_t expected_num_of_packets = kFrameSize / kMaxPacketLength + 1;
166   transport_->set_expected_number_of_packets(expected_num_of_packets);
167   transport_->set_rtp_timestamp(video_frame_.rtp_timestamp);
168 
169   testing_clock_.Advance(base::TimeDelta::FromMilliseconds(kTimestampMs));
170   video_frame_.reference_time = testing_clock_.NowTicks();
171   rtp_packetizer_->SendFrameAsPackets(video_frame_);
172   RunTasks(33 + 1);
173   EXPECT_EQ(expected_num_of_packets, rtp_packetizer_->send_packet_count());
174   EXPECT_EQ(kFrameSize, rtp_packetizer_->send_octet_count());
175   EXPECT_EQ(expected_num_of_packets, transport_->number_of_packets_received());
176 }
177 
178 }  // namespace cast
179 }  // namespace media
180