| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <list> | 11 #include <list> |
| 12 #include <memory> | 12 #include <memory> |
| 13 | 13 |
| 14 #include "webrtc/modules/pacing/paced_sender.h" | 14 #include "webrtc/modules/pacing/paced_sender.h" |
| 15 #include "webrtc/system_wrappers/include/clock.h" | 15 #include "webrtc/system_wrappers/include/clock.h" |
| 16 #include "webrtc/test/gmock.h" | 16 #include "webrtc/test/gmock.h" |
| 17 #include "webrtc/test/gtest.h" | 17 #include "webrtc/test/gtest.h" |
| 18 | 18 |
| 19 using testing::_; | 19 using testing::_; |
| 20 using testing::Return; | 20 using testing::Return; |
| 21 | 21 |
| 22 namespace { |
| 23 constexpr unsigned kFirstClusterBps = 900000; |
| 24 constexpr int kFirstClusterCount = 6; |
| 25 constexpr unsigned kSecondClusterBps = 1800000; |
| 26 constexpr int kSecondClusterCount = 5; |
| 27 |
| 28 // The error stems from truncating the time interval of probe packets to integer |
| 29 // values. This results in probing slightly higher than the target bitrate. |
| 30 // For 1.8 Mbps, this comes to be about 120 kbps with 1200 probe packets. |
| 31 constexpr int kBitrateProbingError = 150000; |
| 32 } // namespace |
| 33 |
| 22 namespace webrtc { | 34 namespace webrtc { |
| 23 namespace test { | 35 namespace test { |
| 24 | 36 |
| 25 static const int kTargetBitrateBps = 800000; | 37 static const int kTargetBitrateBps = 800000; |
| 26 | 38 |
| 27 class MockPacedSenderCallback : public PacedSender::PacketSender { | 39 class MockPacedSenderCallback : public PacedSender::PacketSender { |
| 28 public: | 40 public: |
| 29 MOCK_METHOD5(TimeToSendPacket, | 41 MOCK_METHOD5(TimeToSendPacket, |
| 30 bool(uint32_t ssrc, | 42 bool(uint32_t ssrc, |
| 31 uint16_t sequence_number, | 43 uint16_t sequence_number, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 55 } | 67 } |
| 56 | 68 |
| 57 size_t padding_sent() { return padding_sent_; } | 69 size_t padding_sent() { return padding_sent_; } |
| 58 | 70 |
| 59 private: | 71 private: |
| 60 size_t padding_sent_; | 72 size_t padding_sent_; |
| 61 }; | 73 }; |
| 62 | 74 |
| 63 class PacedSenderProbing : public PacedSender::PacketSender { | 75 class PacedSenderProbing : public PacedSender::PacketSender { |
| 64 public: | 76 public: |
| 65 PacedSenderProbing(const std::list<int>& expected_deltas, Clock* clock) | 77 PacedSenderProbing() : packets_sent_(0), padding_sent_(0) {} |
| 66 : prev_packet_time_ms_(-1), | |
| 67 expected_deltas_(expected_deltas), | |
| 68 packets_sent_(0), | |
| 69 clock_(clock) {} | |
| 70 | 78 |
| 71 bool TimeToSendPacket(uint32_t ssrc, | 79 bool TimeToSendPacket(uint32_t ssrc, |
| 72 uint16_t sequence_number, | 80 uint16_t sequence_number, |
| 73 int64_t capture_time_ms, | 81 int64_t capture_time_ms, |
| 74 bool retransmission, | 82 bool retransmission, |
| 75 int probe_cluster_id) override { | 83 int probe_cluster_id) override { |
| 76 ExpectAndCountPacket(); | 84 packets_sent_++; |
| 77 return true; | 85 return true; |
| 78 } | 86 } |
| 79 | 87 |
| 80 size_t TimeToSendPadding(size_t bytes, int probe_cluster_id) override { | 88 size_t TimeToSendPadding(size_t bytes, int probe_cluster_id) override { |
| 81 ExpectAndCountPacket(); | 89 padding_sent_ += bytes; |
| 82 return bytes; | 90 return padding_sent_; |
| 83 } | |
| 84 | |
| 85 void ExpectAndCountPacket() { | |
| 86 ++packets_sent_; | |
| 87 EXPECT_FALSE(expected_deltas_.empty()); | |
| 88 if (expected_deltas_.empty()) | |
| 89 return; | |
| 90 int64_t now_ms = clock_->TimeInMilliseconds(); | |
| 91 if (prev_packet_time_ms_ >= 0) { | |
| 92 EXPECT_EQ(expected_deltas_.front(), now_ms - prev_packet_time_ms_); | |
| 93 expected_deltas_.pop_front(); | |
| 94 } | |
| 95 prev_packet_time_ms_ = now_ms; | |
| 96 } | 91 } |
| 97 | 92 |
| 98 int packets_sent() const { return packets_sent_; } | 93 int packets_sent() const { return packets_sent_; } |
| 99 | 94 |
| 95 int padding_sent() const { return padding_sent_; } |
| 96 |
| 100 private: | 97 private: |
| 101 int64_t prev_packet_time_ms_; | |
| 102 std::list<int> expected_deltas_; | |
| 103 int packets_sent_; | 98 int packets_sent_; |
| 104 Clock* clock_; | 99 int padding_sent_; |
| 105 }; | 100 }; |
| 106 | 101 |
| 107 class PacedSenderTest : public ::testing::Test { | 102 class PacedSenderTest : public ::testing::Test { |
| 108 protected: | 103 protected: |
| 109 PacedSenderTest() : clock_(123456) { | 104 PacedSenderTest() : clock_(123456) { |
| 110 srand(0); | 105 srand(0); |
| 111 // Need to initialize PacedSender after we initialize clock. | 106 // Need to initialize PacedSender after we initialize clock. |
| 112 send_bucket_.reset(new PacedSender(&clock_, &callback_)); | 107 send_bucket_.reset(new PacedSender(&clock_, &callback_)); |
| 113 send_bucket_->CreateProbeCluster(900000, 6); | 108 send_bucket_->CreateProbeCluster(kFirstClusterBps, kFirstClusterCount); |
| 114 send_bucket_->CreateProbeCluster(1800000, 5); | 109 send_bucket_->CreateProbeCluster(kSecondClusterBps, kSecondClusterCount); |
| 115 // Default to bitrate probing disabled for testing purposes. Probing tests | 110 // Default to bitrate probing disabled for testing purposes. Probing tests |
| 116 // have to enable probing, either by creating a new PacedSender instance or | 111 // have to enable probing, either by creating a new PacedSender instance or |
| 117 // by calling SetProbingEnabled(true). | 112 // by calling SetProbingEnabled(true). |
| 118 send_bucket_->SetProbingEnabled(false); | 113 send_bucket_->SetProbingEnabled(false); |
| 119 send_bucket_->SetEstimatedBitrate(kTargetBitrateBps); | 114 send_bucket_->SetEstimatedBitrate(kTargetBitrateBps); |
| 120 | 115 |
| 121 clock_.AdvanceTimeMilliseconds(send_bucket_->TimeUntilNextProcess()); | 116 clock_.AdvanceTimeMilliseconds(send_bucket_->TimeUntilNextProcess()); |
| 122 } | 117 } |
| 123 | 118 |
| 124 void SendAndExpectPacket(PacedSender::Priority priority, | 119 void SendAndExpectPacket(PacedSender::Priority priority, |
| (...skipping 671 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 796 clock_.TimeInMilliseconds(), | 791 clock_.TimeInMilliseconds(), |
| 797 1200, | 792 1200, |
| 798 false); | 793 false); |
| 799 | 794 |
| 800 clock_.AdvanceTimeMilliseconds(500); | 795 clock_.AdvanceTimeMilliseconds(500); |
| 801 EXPECT_EQ(500, send_bucket_->QueueInMs()); | 796 EXPECT_EQ(500, send_bucket_->QueueInMs()); |
| 802 send_bucket_->Process(); | 797 send_bucket_->Process(); |
| 803 EXPECT_EQ(0, send_bucket_->QueueInMs()); | 798 EXPECT_EQ(0, send_bucket_->QueueInMs()); |
| 804 } | 799 } |
| 805 | 800 |
| 806 TEST_F(PacedSenderTest, ProbingWithInitialFrame) { | 801 TEST_F(PacedSenderTest, ProbingWithInsertedPackets) { |
| 807 const int kNumPackets = 11; | |
| 808 const int kNumDeltas = kNumPackets - 1; | |
| 809 const size_t kPacketSize = 1200; | 802 const size_t kPacketSize = 1200; |
| 810 const int kInitialBitrateBps = 300000; | 803 const int kInitialBitrateBps = 300000; |
| 811 uint32_t ssrc = 12346; | 804 uint32_t ssrc = 12346; |
| 812 uint16_t sequence_number = 1234; | 805 uint16_t sequence_number = 1234; |
| 813 | 806 |
| 814 const int expected_deltas[kNumDeltas] = {10, 10, 10, 10, 10, 5, 5, 5, 5, 5}; | 807 PacedSenderProbing packet_sender; |
| 815 std::list<int> expected_deltas_list(expected_deltas, | 808 send_bucket_.reset(new PacedSender(&clock_, &packet_sender)); |
| 816 expected_deltas + kNumDeltas); | 809 send_bucket_->CreateProbeCluster(kFirstClusterBps, kFirstClusterCount); |
| 817 PacedSenderProbing callback(expected_deltas_list, &clock_); | 810 send_bucket_->CreateProbeCluster(kSecondClusterBps, kSecondClusterCount); |
| 818 send_bucket_.reset(new PacedSender(&clock_, &callback)); | |
| 819 send_bucket_->CreateProbeCluster(900000, 6); | |
| 820 send_bucket_->CreateProbeCluster(1800000, 5); | |
| 821 send_bucket_->SetEstimatedBitrate(kInitialBitrateBps); | 811 send_bucket_->SetEstimatedBitrate(kInitialBitrateBps); |
| 822 | 812 |
| 823 for (int i = 0; i < kNumPackets; ++i) { | 813 for (int i = 0; i < kFirstClusterCount + kSecondClusterCount; ++i) { |
| 824 send_bucket_->InsertPacket(PacedSender::kNormalPriority, ssrc, | 814 send_bucket_->InsertPacket(PacedSender::kNormalPriority, ssrc, |
| 825 sequence_number++, clock_.TimeInMilliseconds(), | 815 sequence_number++, clock_.TimeInMilliseconds(), |
| 826 kPacketSize, false); | 816 kPacketSize, false); |
| 827 } | 817 } |
| 828 | 818 |
| 829 while (callback.packets_sent() < kNumPackets) { | 819 int64_t start = clock_.TimeInMilliseconds(); |
| 820 while (packet_sender.packets_sent() < kFirstClusterCount) { |
| 830 int time_until_process = send_bucket_->TimeUntilNextProcess(); | 821 int time_until_process = send_bucket_->TimeUntilNextProcess(); |
| 831 if (time_until_process <= 0) { | 822 clock_.AdvanceTimeMilliseconds(time_until_process); |
| 832 send_bucket_->Process(); | 823 send_bucket_->Process(); |
| 833 } else { | |
| 834 clock_.AdvanceTimeMilliseconds(time_until_process); | |
| 835 } | |
| 836 } | 824 } |
| 825 int packets_sent = packet_sender.packets_sent(); |
| 826 // Validate first cluster bitrate. Note that we have to account for number |
| 827 // of intervals and hence (packets_sent - 1) on the first cluster. |
| 828 EXPECT_NEAR((packets_sent - 1) * kPacketSize * 8000 / |
| 829 (clock_.TimeInMilliseconds() - start), |
| 830 kFirstClusterBps, kBitrateProbingError); |
| 831 EXPECT_EQ(0, packet_sender.padding_sent()); |
| 832 |
| 833 start = clock_.TimeInMilliseconds(); |
| 834 while (packet_sender.packets_sent() < |
| 835 kFirstClusterCount + kSecondClusterCount) { |
| 836 int time_until_process = send_bucket_->TimeUntilNextProcess(); |
| 837 clock_.AdvanceTimeMilliseconds(time_until_process); |
| 838 send_bucket_->Process(); |
| 839 } |
| 840 packets_sent = packet_sender.packets_sent() - packets_sent; |
| 841 // Validate second cluster bitrate. |
| 842 EXPECT_NEAR( |
| 843 packets_sent * kPacketSize * 8000 / (clock_.TimeInMilliseconds() - start), |
| 844 kSecondClusterBps, kBitrateProbingError); |
| 837 } | 845 } |
| 838 | 846 |
| 839 TEST_F(PacedSenderTest, ProbingWithTooSmallInitialFrame) { | 847 TEST_F(PacedSenderTest, ProbingWithPaddingSupport) { |
| 840 const int kNumPackets = 11; | |
| 841 const int kNumDeltas = kNumPackets - 1; | |
| 842 const size_t kPacketSize = 1200; | 848 const size_t kPacketSize = 1200; |
| 843 const int kInitialBitrateBps = 300000; | 849 const int kInitialBitrateBps = 300000; |
| 844 uint32_t ssrc = 12346; | 850 uint32_t ssrc = 12346; |
| 845 uint16_t sequence_number = 1234; | 851 uint16_t sequence_number = 1234; |
| 846 const int expected_deltas[kNumDeltas] = {10, 10, 10, 10, 10, 5, 5, 5, 5, 5}; | 852 |
| 847 std::list<int> expected_deltas_list(expected_deltas, | 853 PacedSenderProbing packet_sender; |
| 848 expected_deltas + kNumDeltas); | 854 send_bucket_.reset(new PacedSender(&clock_, &packet_sender)); |
| 849 PacedSenderProbing callback(expected_deltas_list, &clock_); | 855 send_bucket_->CreateProbeCluster(kFirstClusterBps, kFirstClusterCount); |
| 850 send_bucket_.reset(new PacedSender(&clock_, &callback)); | |
| 851 send_bucket_->CreateProbeCluster(900000, 6); | |
| 852 send_bucket_->CreateProbeCluster(1800000, 5); | |
| 853 send_bucket_->SetEstimatedBitrate(kInitialBitrateBps); | 856 send_bucket_->SetEstimatedBitrate(kInitialBitrateBps); |
| 854 | 857 |
| 855 for (int i = 0; i < kNumPackets - 5; ++i) { | 858 for (int i = 0; i < kFirstClusterCount - 2; ++i) { |
| 856 send_bucket_->InsertPacket(PacedSender::kNormalPriority, ssrc, | 859 send_bucket_->InsertPacket(PacedSender::kNormalPriority, ssrc, |
| 857 sequence_number++, clock_.TimeInMilliseconds(), | 860 sequence_number++, clock_.TimeInMilliseconds(), |
| 858 kPacketSize, false); | 861 kPacketSize, false); |
| 859 } | 862 } |
| 860 while (callback.packets_sent() < kNumPackets) { | 863 |
| 864 int64_t start = clock_.TimeInMilliseconds(); |
| 865 int process_count = 0; |
| 866 while (process_count < kFirstClusterCount) { |
| 861 int time_until_process = send_bucket_->TimeUntilNextProcess(); | 867 int time_until_process = send_bucket_->TimeUntilNextProcess(); |
| 862 if (time_until_process <= 0) { | 868 clock_.AdvanceTimeMilliseconds(time_until_process); |
| 863 send_bucket_->Process(); | 869 send_bucket_->Process(); |
| 864 } else { | 870 ++process_count; |
| 865 clock_.AdvanceTimeMilliseconds(time_until_process); | |
| 866 } | |
| 867 } | 871 } |
| 868 | 872 int packets_sent = packet_sender.packets_sent(); |
| 869 // Process one more time and make sure we don't send any more probes. | 873 int padding_sent = packet_sender.padding_sent(); |
| 870 int time_until_process = send_bucket_->TimeUntilNextProcess(); | 874 EXPECT_GT(packets_sent, 0); |
| 871 clock_.AdvanceTimeMilliseconds(time_until_process); | 875 EXPECT_GT(padding_sent, 0); |
| 872 send_bucket_->Process(); | 876 // Note that the number of intervals here for kPacketSize is |
| 873 EXPECT_EQ(kNumPackets, callback.packets_sent()); | 877 // packets_sent due to padding in the same cluster. |
| 878 EXPECT_NEAR((packets_sent * kPacketSize * 8000 + padding_sent) / |
| 879 (clock_.TimeInMilliseconds() - start), |
| 880 kFirstClusterBps, kBitrateProbingError); |
| 874 } | 881 } |
| 875 | 882 |
| 876 TEST_F(PacedSenderTest, PriorityInversion) { | 883 TEST_F(PacedSenderTest, PriorityInversion) { |
| 877 uint32_t ssrc = 12346; | 884 uint32_t ssrc = 12346; |
| 878 uint16_t sequence_number = 1234; | 885 uint16_t sequence_number = 1234; |
| 879 const size_t kPacketSize = 1200; | 886 const size_t kPacketSize = 1200; |
| 880 | 887 |
| 881 send_bucket_->InsertPacket( | 888 send_bucket_->InsertPacket( |
| 882 PacedSender::kHighPriority, ssrc, sequence_number + 3, | 889 PacedSender::kHighPriority, ssrc, sequence_number + 3, |
| 883 clock_.TimeInMilliseconds() + 33, kPacketSize, true); | 890 clock_.TimeInMilliseconds() + 33, kPacketSize, true); |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1028 | 1035 |
| 1029 // No more probing packets. | 1036 // No more probing packets. |
| 1030 EXPECT_CALL(callback_, TimeToSendPadding(_, PacketInfo::kNotAProbe)) | 1037 EXPECT_CALL(callback_, TimeToSendPadding(_, PacketInfo::kNotAProbe)) |
| 1031 .Times(1) | 1038 .Times(1) |
| 1032 .WillRepeatedly(Return(500)); | 1039 .WillRepeatedly(Return(500)); |
| 1033 send_bucket_->Process(); | 1040 send_bucket_->Process(); |
| 1034 } | 1041 } |
| 1035 | 1042 |
| 1036 } // namespace test | 1043 } // namespace test |
| 1037 } // namespace webrtc | 1044 } // namespace webrtc |
| OLD | NEW |