OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "chrome/browser/media/cast_remoting_sender.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/macros.h" |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/test/simple_test_tick_clock.h" |
| 11 #include "media/base/fake_single_thread_task_runner.h" |
| 12 #include "media/cast/cast_environment.h" |
| 13 #include "media/cast/constants.h" |
| 14 #include "media/cast/logging/simple_event_subscriber.h" |
| 15 #include "media/cast/net/cast_transport_config.h" |
| 16 #include "media/cast/net/cast_transport_impl.h" |
| 17 #include "media/cast/net/pacing/paced_sender.h" |
| 18 #include "media/cast/test/utility/default_config.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 |
| 22 namespace cast { |
| 23 |
| 24 namespace { |
| 25 |
| 26 // Number of bytes in each faked frame. |
| 27 constexpr int kFrameSize = 5000; |
| 28 |
| 29 class TestPacketSender : public media::cast::PacketTransport { |
| 30 public: |
| 31 TestPacketSender() |
| 32 : number_of_rtp_packets_(0), number_of_rtcp_packets_(0), paused_(false) {} |
| 33 |
| 34 // A singular packet implies a RTCP packet. |
| 35 bool SendPacket(media::cast::PacketRef packet, |
| 36 const base::Closure& cb) final { |
| 37 if (paused_) { |
| 38 stored_packet_ = packet; |
| 39 callback_ = cb; |
| 40 return false; |
| 41 } |
| 42 if (media::cast::IsRtcpPacket(&packet->data[0], packet->data.size())) { |
| 43 ++number_of_rtcp_packets_; |
| 44 } else { |
| 45 ++number_of_rtp_packets_; |
| 46 } |
| 47 return true; |
| 48 } |
| 49 |
| 50 int64_t GetBytesSent() final { return 0; } |
| 51 |
| 52 void StartReceiving(const media::cast::PacketReceiverCallbackWithStatus& |
| 53 packet_receiver) final {} |
| 54 |
| 55 void StopReceiving() final {} |
| 56 |
| 57 int number_of_rtp_packets() const { return number_of_rtp_packets_; } |
| 58 |
| 59 int number_of_rtcp_packets() const { return number_of_rtcp_packets_; } |
| 60 |
| 61 void SetPause(bool paused) { |
| 62 paused_ = paused; |
| 63 if (!paused && stored_packet_.get()) { |
| 64 SendPacket(stored_packet_, callback_); |
| 65 callback_.Run(); |
| 66 } |
| 67 } |
| 68 |
| 69 private: |
| 70 int number_of_rtp_packets_; |
| 71 int number_of_rtcp_packets_; |
| 72 bool paused_; |
| 73 base::Closure callback_; |
| 74 media::cast::PacketRef stored_packet_; |
| 75 |
| 76 DISALLOW_COPY_AND_ASSIGN(TestPacketSender); |
| 77 }; |
| 78 |
| 79 class DummyClient : public media::cast::CastTransport::Client { |
| 80 public: |
| 81 DummyClient() {} |
| 82 |
| 83 void OnStatusChanged(media::cast::CastTransportStatus status) final { |
| 84 EXPECT_EQ(media::cast::TRANSPORT_STREAM_INITIALIZED, status); |
| 85 }; |
| 86 void OnLoggingEventsReceived( |
| 87 std::unique_ptr<std::vector<media::cast::FrameEvent>> frame_events, |
| 88 std::unique_ptr<std::vector<media::cast::PacketEvent>> packet_events) |
| 89 final {} |
| 90 void ProcessRtpPacket(std::unique_ptr<media::cast::Packet> packet) final {} |
| 91 |
| 92 DISALLOW_COPY_AND_ASSIGN(DummyClient); |
| 93 }; |
| 94 |
| 95 } // namespace |
| 96 |
| 97 class CastRemotingSenderTest : public ::testing::Test { |
| 98 protected: |
| 99 CastRemotingSenderTest() |
| 100 : testing_clock_(new base::SimpleTestTickClock()), |
| 101 task_runner_(new media::FakeSingleThreadTaskRunner(testing_clock_)), |
| 102 cast_environment_( |
| 103 new media::cast::CastEnvironment(base::WrapUnique(testing_clock_), |
| 104 task_runner_, |
| 105 task_runner_, |
| 106 task_runner_)) { |
| 107 testing_clock_->Advance(base::TimeTicks::Now() - base::TimeTicks()); |
| 108 transport_ = new TestPacketSender(); |
| 109 transport_sender_.reset(new media::cast::CastTransportImpl( |
| 110 testing_clock_, base::TimeDelta(), base::MakeUnique<DummyClient>(), |
| 111 base::WrapUnique(transport_), task_runner_)); |
| 112 media::cast::FrameSenderConfig video_config = |
| 113 media::cast::GetDefaultVideoSenderConfig(); |
| 114 video_config.rtp_payload_type = media::cast::RtpPayloadType::REMOTE_VIDEO; |
| 115 media::cast::CastTransportRtpConfig transport_config; |
| 116 transport_config.ssrc = video_config.sender_ssrc; |
| 117 transport_config.rtp_stream_id = 5; |
| 118 transport_config.feedback_ssrc = video_config.receiver_ssrc; |
| 119 transport_config.rtp_payload_type = video_config.rtp_payload_type; |
| 120 transport_config.aes_key = video_config.aes_key; |
| 121 transport_config.aes_iv_mask = video_config.aes_iv_mask; |
| 122 remoting_sender_.reset(new CastRemotingSender( |
| 123 cast_environment_, transport_sender_.get(), transport_config)); |
| 124 task_runner_->RunTasks(); |
| 125 } |
| 126 |
| 127 ~CastRemotingSenderTest() override {} |
| 128 |
| 129 void TearDown() final { |
| 130 remoting_sender_.reset(); |
| 131 task_runner_->RunTasks(); |
| 132 } |
| 133 |
| 134 void InsertFrame() { |
| 135 remoting_sender_->next_frame_data_.resize(kFrameSize); |
| 136 remoting_sender_->SendFrame(); |
| 137 } |
| 138 |
| 139 void OnReceivedCastMessage( |
| 140 const media::cast::RtcpCastMessage& cast_feedback) { |
| 141 remoting_sender_->OnReceivedCastMessage(cast_feedback); |
| 142 } |
| 143 |
| 144 int NumberOfFramesInFlight() { |
| 145 return remoting_sender_->NumberOfFramesInFlight(); |
| 146 } |
| 147 |
| 148 void CancelFramesInFlight() { remoting_sender_->CancelFramesInFlight(); } |
| 149 |
| 150 base::SimpleTestTickClock* const testing_clock_; // Owned by CastEnvironment. |
| 151 const scoped_refptr<media::FakeSingleThreadTaskRunner> task_runner_; |
| 152 const scoped_refptr<media::cast::CastEnvironment> cast_environment_; |
| 153 TestPacketSender* transport_; // Owned by CastTransport. |
| 154 std::unique_ptr<media::cast::CastTransportImpl> transport_sender_; |
| 155 std::unique_ptr<CastRemotingSender> remoting_sender_; |
| 156 |
| 157 private: |
| 158 DISALLOW_COPY_AND_ASSIGN(CastRemotingSenderTest); |
| 159 }; |
| 160 |
| 161 TEST_F(CastRemotingSenderTest, SendAndAckEncodedFrame) { |
| 162 // Send a fake video frame that will be decomposed into 4 packets. |
| 163 InsertFrame(); |
| 164 task_runner_->RunTasks(); |
| 165 EXPECT_EQ(4, transport_->number_of_rtp_packets()); |
| 166 EXPECT_EQ(1, NumberOfFramesInFlight()); |
| 167 |
| 168 // Ack the frame. |
| 169 media::cast::RtcpCastMessage cast_feedback(11); |
| 170 cast_feedback.ack_frame_id = media::cast::FrameId::first(); |
| 171 OnReceivedCastMessage(cast_feedback); |
| 172 EXPECT_EQ(0, NumberOfFramesInFlight()); |
| 173 |
| 174 // Send 4 frames. |
| 175 for (int i = 0; i < 4; ++i) |
| 176 InsertFrame(); |
| 177 EXPECT_EQ(4, NumberOfFramesInFlight()); |
| 178 // Ack one frame. |
| 179 cast_feedback.ack_frame_id = media::cast::FrameId::first() + 1; |
| 180 OnReceivedCastMessage(cast_feedback); |
| 181 EXPECT_EQ(3, NumberOfFramesInFlight()); |
| 182 // Ack all. |
| 183 cast_feedback.received_later_frames.clear(); |
| 184 cast_feedback.ack_frame_id = media::cast::FrameId::first() + 4; |
| 185 OnReceivedCastMessage(cast_feedback); |
| 186 EXPECT_EQ(0, NumberOfFramesInFlight()); |
| 187 } |
| 188 |
| 189 TEST_F(CastRemotingSenderTest, CancelFramesInFlight) { |
| 190 for (int i = 0; i < 10; ++i) |
| 191 InsertFrame(); |
| 192 EXPECT_EQ(10, NumberOfFramesInFlight()); |
| 193 |
| 194 media::cast::RtcpCastMessage cast_feedback(11); |
| 195 cast_feedback.ack_frame_id = media::cast::FrameId::first(); |
| 196 OnReceivedCastMessage(cast_feedback); |
| 197 EXPECT_EQ(9, NumberOfFramesInFlight()); |
| 198 |
| 199 CancelFramesInFlight(); |
| 200 EXPECT_EQ(0, NumberOfFramesInFlight()); |
| 201 } |
| 202 |
| 203 } // namespace cast |
OLD | NEW |