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 "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "webrtc/modules/pacing/paced_sender.h" | 16 #include "webrtc/modules/pacing/paced_sender.h" |
17 #include "webrtc/system_wrappers/include/clock.h" | 17 #include "webrtc/system_wrappers/include/clock.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 | |
832 start = clock_.TimeInMilliseconds(); | |
833 while (packet_sender.packets_sent() < | |
834 kFirstClusterCount + kSecondClusterCount) { | |
835 int time_until_process = send_bucket_->TimeUntilNextProcess(); | |
836 clock_.AdvanceTimeMilliseconds(time_until_process); | |
837 send_bucket_->Process(); | |
838 } | |
839 packets_sent = packet_sender.packets_sent() - packets_sent; | |
840 // Validate second cluster bitrate. | |
841 EXPECT_NEAR( | |
842 packets_sent * kPacketSize * 8000 / (clock_.TimeInMilliseconds() - start), | |
843 kSecondClusterBps, kBitrateProbingError); | |
837 } | 844 } |
838 | 845 |
839 TEST_F(PacedSenderTest, ProbingWithTooSmallInitialFrame) { | 846 TEST_F(PacedSenderTest, ProbingWithPaddingSupport) { |
840 const int kNumPackets = 11; | |
841 const int kNumDeltas = kNumPackets - 1; | |
842 const size_t kPacketSize = 1200; | 847 const size_t kPacketSize = 1200; |
843 const int kInitialBitrateBps = 300000; | 848 const int kInitialBitrateBps = 300000; |
844 uint32_t ssrc = 12346; | 849 uint32_t ssrc = 12346; |
845 uint16_t sequence_number = 1234; | 850 uint16_t sequence_number = 1234; |
846 const int expected_deltas[kNumDeltas] = {10, 10, 10, 10, 10, 5, 5, 5, 5, 5}; | 851 |
847 std::list<int> expected_deltas_list(expected_deltas, | 852 PacedSenderProbing packet_sender; |
848 expected_deltas + kNumDeltas); | 853 send_bucket_.reset(new PacedSender(&clock_, &packet_sender)); |
849 PacedSenderProbing callback(expected_deltas_list, &clock_); | 854 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); | 855 send_bucket_->SetEstimatedBitrate(kInitialBitrateBps); |
854 | 856 |
855 for (int i = 0; i < kNumPackets - 5; ++i) { | 857 for (int i = 0; i < kFirstClusterCount - 2; ++i) { |
856 send_bucket_->InsertPacket(PacedSender::kNormalPriority, ssrc, | 858 send_bucket_->InsertPacket(PacedSender::kNormalPriority, ssrc, |
857 sequence_number++, clock_.TimeInMilliseconds(), | 859 sequence_number++, clock_.TimeInMilliseconds(), |
858 kPacketSize, false); | 860 kPacketSize, false); |
859 } | 861 } |
860 while (callback.packets_sent() < kNumPackets) { | 862 |
863 int64_t start = clock_.TimeInMilliseconds(); | |
864 int process_count = 0; | |
865 while (process_count < kFirstClusterCount) { | |
861 int time_until_process = send_bucket_->TimeUntilNextProcess(); | 866 int time_until_process = send_bucket_->TimeUntilNextProcess(); |
862 if (time_until_process <= 0) { | 867 clock_.AdvanceTimeMilliseconds(time_until_process); |
863 send_bucket_->Process(); | 868 send_bucket_->Process(); |
864 } else { | 869 process_count++; |
stefan-webrtc
2016/09/23 13:29:11
++process_count;
Irfan
2016/09/26 23:00:29
Done.
| |
865 clock_.AdvanceTimeMilliseconds(time_until_process); | 870 } |
866 } | 871 int packets_sent = packet_sender.packets_sent(); |
872 int padding_sent = packet_sender.padding_sent(); | |
873 EXPECT_GT(packets_sent, 0); | |
874 EXPECT_GT(padding_sent, 0); | |
875 // Note that the number of intervals here for kPacketSize is | |
876 // packets_sent due to padding in the same cluster. | |
877 EXPECT_NEAR((packets_sent * kPacketSize * 8000 + padding_sent) / | |
878 (clock_.TimeInMilliseconds() - start), | |
879 kFirstClusterBps, kBitrateProbingError); | |
880 } | |
881 | |
882 TEST_F(PacedSenderTest, ProbingUsesRetransmissions) { | |
stefan-webrtc
2016/09/23 13:29:11
This was not the test I was after in my earlier co
Irfan
2016/09/26 23:00:29
I have added a test that measures the padding over
stefan-webrtc
2016/09/27 14:18:32
Right, that's expected in some situations. But in
| |
883 const size_t kPacketSize = 1200; | |
884 const int kInitialBitrateBps = 300000; | |
885 uint32_t ssrc = 12346; | |
886 uint16_t sequence_number = 1234; | |
887 | |
888 PacedSenderProbing packet_sender; | |
889 send_bucket_.reset(new PacedSender(&clock_, &packet_sender)); | |
890 send_bucket_->CreateProbeCluster(kFirstClusterBps, kFirstClusterCount); | |
891 send_bucket_->SetEstimatedBitrate(kInitialBitrateBps); | |
892 | |
893 // All packets inserted are retransmissions. | |
894 for (int i = 0; i < kFirstClusterCount; ++i) { | |
895 send_bucket_->InsertPacket(PacedSender::kNormalPriority, ssrc, | |
896 sequence_number++, clock_.TimeInMilliseconds(), | |
897 kPacketSize, true); | |
867 } | 898 } |
868 | 899 |
869 // Process one more time and make sure we don't send any more probes. | 900 int64_t start = clock_.TimeInMilliseconds(); |
870 int time_until_process = send_bucket_->TimeUntilNextProcess(); | 901 while (packet_sender.packets_sent() < kFirstClusterCount) { |
871 clock_.AdvanceTimeMilliseconds(time_until_process); | 902 int time_until_process = send_bucket_->TimeUntilNextProcess(); |
872 send_bucket_->Process(); | 903 clock_.AdvanceTimeMilliseconds(time_until_process); |
873 EXPECT_EQ(kNumPackets, callback.packets_sent()); | 904 send_bucket_->Process(); |
905 } | |
906 int packets_sent = packet_sender.packets_sent(); | |
907 int padding_sent = packet_sender.padding_sent(); | |
908 EXPECT_EQ(padding_sent, 0); | |
909 // Validate first cluster bitrate. | |
910 EXPECT_NEAR(((packets_sent - 1) * kPacketSize * 8000) / | |
911 (clock_.TimeInMilliseconds() - start), | |
912 kFirstClusterBps, kBitrateProbingError); | |
874 } | 913 } |
875 | 914 |
876 TEST_F(PacedSenderTest, PriorityInversion) { | 915 TEST_F(PacedSenderTest, PriorityInversion) { |
877 uint32_t ssrc = 12346; | 916 uint32_t ssrc = 12346; |
878 uint16_t sequence_number = 1234; | 917 uint16_t sequence_number = 1234; |
879 const size_t kPacketSize = 1200; | 918 const size_t kPacketSize = 1200; |
880 | 919 |
881 send_bucket_->InsertPacket( | 920 send_bucket_->InsertPacket( |
882 PacedSender::kHighPriority, ssrc, sequence_number + 3, | 921 PacedSender::kHighPriority, ssrc, sequence_number + 3, |
883 clock_.TimeInMilliseconds() + 33, kPacketSize, true); | 922 clock_.TimeInMilliseconds() + 33, kPacketSize, true); |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1028 | 1067 |
1029 // No more probing packets. | 1068 // No more probing packets. |
1030 EXPECT_CALL(callback_, TimeToSendPadding(_, PacketInfo::kNotAProbe)) | 1069 EXPECT_CALL(callback_, TimeToSendPadding(_, PacketInfo::kNotAProbe)) |
1031 .Times(1) | 1070 .Times(1) |
1032 .WillRepeatedly(Return(500)); | 1071 .WillRepeatedly(Return(500)); |
1033 send_bucket_->Process(); | 1072 send_bucket_->Process(); |
1034 } | 1073 } |
1035 | 1074 |
1036 } // namespace test | 1075 } // namespace test |
1037 } // namespace webrtc | 1076 } // namespace webrtc |
OLD | NEW |