mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 1 | /* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 2 | * |
| 3 | * Use of this source code is governed by a BSD-style license |
| 4 | * that can be found in the LICENSE file in the root of the source |
| 5 | * tree. An additional intellectual property rights grant can be found |
| 6 | * in the file PATENTS. All contributing project authors may |
| 7 | * be found in the AUTHORS file in the root of the source tree. |
| 8 | */ |
| 9 | |
| 10 | #include <string.h> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame^] | 11 | #include <cstdint> |
kwiberg | 3f55dea | 2016-02-29 05:51:59 -0800 | [diff] [blame] | 12 | #include <memory> |
Qiang Chen | d4cec15 | 2015-06-19 09:17:00 -0700 | [diff] [blame] | 13 | #include <queue> |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 14 | #include <vector> |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 15 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame^] | 16 | #include "common_types.h" // NOLINT(build/include) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "modules/video_coding/encoded_frame.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame^] | 18 | #include "modules/video_coding/jitter_buffer_common.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "modules/video_coding/packet.h" |
| 20 | #include "modules/video_coding/receiver.h" |
| 21 | #include "modules/video_coding/test/stream_generator.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "modules/video_coding/timing.h" |
| 23 | #include "rtc_base/checks.h" |
| 24 | #include "system_wrappers/include/clock.h" |
| 25 | #include "test/gtest.h" |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | |
| 29 | class TestVCMReceiver : public ::testing::Test { |
| 30 | protected: |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 31 | TestVCMReceiver() |
| 32 | : clock_(new SimulatedClock(0)), |
| 33 | timing_(clock_.get()), |
Niels Möller | 689983f | 2018-11-07 16:36:22 +0100 | [diff] [blame] | 34 | receiver_(&timing_, clock_.get()) { |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 35 | stream_generator_.reset( |
| 36 | new StreamGenerator(0, clock_->TimeInMilliseconds())); |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 37 | } |
| 38 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 39 | virtual void SetUp() { receiver_.Reset(); } |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 40 | |
| 41 | int32_t InsertPacket(int index) { |
| 42 | VCMPacket packet; |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 43 | bool packet_available = stream_generator_->GetPacket(&packet, index); |
| 44 | EXPECT_TRUE(packet_available); |
| 45 | if (!packet_available) |
stefan@webrtc.org | 3417eb4 | 2013-05-21 15:25:53 +0000 | [diff] [blame] | 46 | return kGeneralError; // Return here to avoid crashes below. |
Johan Ahlers | 95348f7 | 2016-06-28 11:11:28 +0200 | [diff] [blame] | 47 | return receiver_.InsertPacket(packet); |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | int32_t InsertPacketAndPop(int index) { |
| 51 | VCMPacket packet; |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 52 | bool packet_available = stream_generator_->PopPacket(&packet, index); |
| 53 | EXPECT_TRUE(packet_available); |
| 54 | if (!packet_available) |
stefan@webrtc.org | 3417eb4 | 2013-05-21 15:25:53 +0000 | [diff] [blame] | 55 | return kGeneralError; // Return here to avoid crashes below. |
Johan Ahlers | 95348f7 | 2016-06-28 11:11:28 +0200 | [diff] [blame] | 56 | return receiver_.InsertPacket(packet); |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | int32_t InsertFrame(FrameType frame_type, bool complete) { |
| 60 | int num_of_packets = complete ? 1 : 2; |
| 61 | stream_generator_->GenerateFrame( |
pbos | 22993e1 | 2015-10-19 02:39:06 -0700 | [diff] [blame] | 62 | frame_type, (frame_type != kEmptyFrame) ? num_of_packets : 0, |
| 63 | (frame_type == kEmptyFrame) ? 1 : 0, clock_->TimeInMilliseconds()); |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 64 | int32_t ret = InsertPacketAndPop(0); |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 65 | if (!complete) { |
| 66 | // Drop the second packet. |
| 67 | VCMPacket packet; |
| 68 | stream_generator_->PopPacket(&packet, 0); |
| 69 | } |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 70 | clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs); |
| 71 | return ret; |
| 72 | } |
| 73 | |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 74 | bool DecodeNextFrame() { |
Johan Ahlers | 31b2ec4 | 2016-06-28 13:32:49 +0200 | [diff] [blame] | 75 | VCMEncodedFrame* frame = receiver_.FrameForDecoding(0, false); |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 76 | if (!frame) |
| 77 | return false; |
| 78 | receiver_.ReleaseFrame(frame); |
| 79 | return true; |
| 80 | } |
| 81 | |
kwiberg | 3f55dea | 2016-02-29 05:51:59 -0800 | [diff] [blame] | 82 | std::unique_ptr<SimulatedClock> clock_; |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 83 | VCMTiming timing_; |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 84 | VCMReceiver receiver_; |
kwiberg | 3f55dea | 2016-02-29 05:51:59 -0800 | [diff] [blame] | 85 | std::unique_ptr<StreamGenerator> stream_generator_; |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 88 | TEST_F(TestVCMReceiver, NonDecodableDuration_Empty) { |
| 89 | // Enable NACK and with no RTT thresholds for disabling retransmission delay. |
| 90 | receiver_.SetNackMode(kNack, -1, -1); |
| 91 | const size_t kMaxNackListSize = 1000; |
| 92 | const int kMaxPacketAgeToNack = 1000; |
| 93 | const int kMaxNonDecodableDuration = 500; |
| 94 | const int kMinDelayMs = 500; |
| 95 | receiver_.SetNackSettings(kMaxNackListSize, kMaxPacketAgeToNack, |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 96 | kMaxNonDecodableDuration); |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 97 | EXPECT_GE(InsertFrame(kVideoFrameKey, true), kNoError); |
| 98 | // Advance time until it's time to decode the key frame. |
| 99 | clock_->AdvanceTimeMilliseconds(kMinDelayMs); |
| 100 | EXPECT_TRUE(DecodeNextFrame()); |
Wan-Teh Chang | b1825a4 | 2015-06-03 15:03:35 -0700 | [diff] [blame] | 101 | bool request_key_frame = false; |
| 102 | std::vector<uint16_t> nack_list = receiver_.NackList(&request_key_frame); |
| 103 | EXPECT_FALSE(request_key_frame); |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | TEST_F(TestVCMReceiver, NonDecodableDuration_NoKeyFrame) { |
| 107 | // Enable NACK and with no RTT thresholds for disabling retransmission delay. |
| 108 | receiver_.SetNackMode(kNack, -1, -1); |
| 109 | const size_t kMaxNackListSize = 1000; |
| 110 | const int kMaxPacketAgeToNack = 1000; |
| 111 | const int kMaxNonDecodableDuration = 500; |
| 112 | receiver_.SetNackSettings(kMaxNackListSize, kMaxPacketAgeToNack, |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 113 | kMaxNonDecodableDuration); |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 114 | const int kNumFrames = kDefaultFrameRate * kMaxNonDecodableDuration / 1000; |
| 115 | for (int i = 0; i < kNumFrames; ++i) { |
| 116 | EXPECT_GE(InsertFrame(kVideoFrameDelta, true), kNoError); |
| 117 | } |
Wan-Teh Chang | b1825a4 | 2015-06-03 15:03:35 -0700 | [diff] [blame] | 118 | bool request_key_frame = false; |
| 119 | std::vector<uint16_t> nack_list = receiver_.NackList(&request_key_frame); |
| 120 | EXPECT_TRUE(request_key_frame); |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | TEST_F(TestVCMReceiver, NonDecodableDuration_OneIncomplete) { |
| 124 | // Enable NACK and with no RTT thresholds for disabling retransmission delay. |
| 125 | receiver_.SetNackMode(kNack, -1, -1); |
| 126 | const size_t kMaxNackListSize = 1000; |
| 127 | const int kMaxPacketAgeToNack = 1000; |
| 128 | const int kMaxNonDecodableDuration = 500; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 129 | const int kMaxNonDecodableDurationFrames = |
| 130 | (kDefaultFrameRate * kMaxNonDecodableDuration + 500) / 1000; |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 131 | const int kMinDelayMs = 500; |
| 132 | receiver_.SetNackSettings(kMaxNackListSize, kMaxPacketAgeToNack, |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 133 | kMaxNonDecodableDuration); |
Niels Möller | 4eb4b05 | 2018-08-14 10:31:58 +0200 | [diff] [blame] | 134 | timing_.set_min_playout_delay(kMinDelayMs); |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 135 | int64_t key_frame_inserted = clock_->TimeInMilliseconds(); |
| 136 | EXPECT_GE(InsertFrame(kVideoFrameKey, true), kNoError); |
| 137 | // Insert an incomplete frame. |
| 138 | EXPECT_GE(InsertFrame(kVideoFrameDelta, false), kNoError); |
| 139 | // Insert enough frames to have too long non-decodable sequence. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 140 | for (int i = 0; i < kMaxNonDecodableDurationFrames; ++i) { |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 141 | EXPECT_GE(InsertFrame(kVideoFrameDelta, true), kNoError); |
| 142 | } |
| 143 | // Advance time until it's time to decode the key frame. |
| 144 | clock_->AdvanceTimeMilliseconds(kMinDelayMs - clock_->TimeInMilliseconds() - |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 145 | key_frame_inserted); |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 146 | EXPECT_TRUE(DecodeNextFrame()); |
| 147 | // Make sure we get a key frame request. |
Wan-Teh Chang | b1825a4 | 2015-06-03 15:03:35 -0700 | [diff] [blame] | 148 | bool request_key_frame = false; |
| 149 | std::vector<uint16_t> nack_list = receiver_.NackList(&request_key_frame); |
| 150 | EXPECT_TRUE(request_key_frame); |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | TEST_F(TestVCMReceiver, NonDecodableDuration_NoTrigger) { |
| 154 | // Enable NACK and with no RTT thresholds for disabling retransmission delay. |
| 155 | receiver_.SetNackMode(kNack, -1, -1); |
| 156 | const size_t kMaxNackListSize = 1000; |
| 157 | const int kMaxPacketAgeToNack = 1000; |
| 158 | const int kMaxNonDecodableDuration = 500; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 159 | const int kMaxNonDecodableDurationFrames = |
| 160 | (kDefaultFrameRate * kMaxNonDecodableDuration + 500) / 1000; |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 161 | const int kMinDelayMs = 500; |
| 162 | receiver_.SetNackSettings(kMaxNackListSize, kMaxPacketAgeToNack, |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 163 | kMaxNonDecodableDuration); |
Niels Möller | 4eb4b05 | 2018-08-14 10:31:58 +0200 | [diff] [blame] | 164 | timing_.set_min_playout_delay(kMinDelayMs); |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 165 | int64_t key_frame_inserted = clock_->TimeInMilliseconds(); |
| 166 | EXPECT_GE(InsertFrame(kVideoFrameKey, true), kNoError); |
| 167 | // Insert an incomplete frame. |
| 168 | EXPECT_GE(InsertFrame(kVideoFrameDelta, false), kNoError); |
| 169 | // Insert all but one frame to not trigger a key frame request due to |
| 170 | // too long duration of non-decodable frames. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 171 | for (int i = 0; i < kMaxNonDecodableDurationFrames - 1; ++i) { |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 172 | EXPECT_GE(InsertFrame(kVideoFrameDelta, true), kNoError); |
| 173 | } |
| 174 | // Advance time until it's time to decode the key frame. |
| 175 | clock_->AdvanceTimeMilliseconds(kMinDelayMs - clock_->TimeInMilliseconds() - |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 176 | key_frame_inserted); |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 177 | EXPECT_TRUE(DecodeNextFrame()); |
| 178 | // Make sure we don't get a key frame request since we haven't generated |
| 179 | // enough frames. |
Wan-Teh Chang | b1825a4 | 2015-06-03 15:03:35 -0700 | [diff] [blame] | 180 | bool request_key_frame = false; |
| 181 | std::vector<uint16_t> nack_list = receiver_.NackList(&request_key_frame); |
| 182 | EXPECT_FALSE(request_key_frame); |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 183 | } |
| 184 | |
stefan@webrtc.org | 1673481 | 2013-05-14 12:00:47 +0000 | [diff] [blame] | 185 | TEST_F(TestVCMReceiver, NonDecodableDuration_NoTrigger2) { |
| 186 | // Enable NACK and with no RTT thresholds for disabling retransmission delay. |
| 187 | receiver_.SetNackMode(kNack, -1, -1); |
| 188 | const size_t kMaxNackListSize = 1000; |
| 189 | const int kMaxPacketAgeToNack = 1000; |
| 190 | const int kMaxNonDecodableDuration = 500; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 191 | const int kMaxNonDecodableDurationFrames = |
| 192 | (kDefaultFrameRate * kMaxNonDecodableDuration + 500) / 1000; |
stefan@webrtc.org | 1673481 | 2013-05-14 12:00:47 +0000 | [diff] [blame] | 193 | const int kMinDelayMs = 500; |
| 194 | receiver_.SetNackSettings(kMaxNackListSize, kMaxPacketAgeToNack, |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 195 | kMaxNonDecodableDuration); |
Niels Möller | 4eb4b05 | 2018-08-14 10:31:58 +0200 | [diff] [blame] | 196 | timing_.set_min_playout_delay(kMinDelayMs); |
stefan@webrtc.org | 1673481 | 2013-05-14 12:00:47 +0000 | [diff] [blame] | 197 | int64_t key_frame_inserted = clock_->TimeInMilliseconds(); |
| 198 | EXPECT_GE(InsertFrame(kVideoFrameKey, true), kNoError); |
| 199 | // Insert enough frames to have too long non-decodable sequence, except that |
| 200 | // we don't have any losses. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 201 | for (int i = 0; i < kMaxNonDecodableDurationFrames; ++i) { |
stefan@webrtc.org | 1673481 | 2013-05-14 12:00:47 +0000 | [diff] [blame] | 202 | EXPECT_GE(InsertFrame(kVideoFrameDelta, true), kNoError); |
| 203 | } |
| 204 | // Insert an incomplete frame. |
| 205 | EXPECT_GE(InsertFrame(kVideoFrameDelta, false), kNoError); |
| 206 | // Advance time until it's time to decode the key frame. |
| 207 | clock_->AdvanceTimeMilliseconds(kMinDelayMs - clock_->TimeInMilliseconds() - |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 208 | key_frame_inserted); |
stefan@webrtc.org | 1673481 | 2013-05-14 12:00:47 +0000 | [diff] [blame] | 209 | EXPECT_TRUE(DecodeNextFrame()); |
| 210 | // Make sure we don't get a key frame request since the non-decodable duration |
| 211 | // is only one frame. |
Wan-Teh Chang | b1825a4 | 2015-06-03 15:03:35 -0700 | [diff] [blame] | 212 | bool request_key_frame = false; |
| 213 | std::vector<uint16_t> nack_list = receiver_.NackList(&request_key_frame); |
| 214 | EXPECT_FALSE(request_key_frame); |
stefan@webrtc.org | 1673481 | 2013-05-14 12:00:47 +0000 | [diff] [blame] | 215 | } |
| 216 | |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 217 | TEST_F(TestVCMReceiver, NonDecodableDuration_KeyFrameAfterIncompleteFrames) { |
| 218 | // Enable NACK and with no RTT thresholds for disabling retransmission delay. |
| 219 | receiver_.SetNackMode(kNack, -1, -1); |
| 220 | const size_t kMaxNackListSize = 1000; |
| 221 | const int kMaxPacketAgeToNack = 1000; |
| 222 | const int kMaxNonDecodableDuration = 500; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 223 | const int kMaxNonDecodableDurationFrames = |
| 224 | (kDefaultFrameRate * kMaxNonDecodableDuration + 500) / 1000; |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 225 | const int kMinDelayMs = 500; |
| 226 | receiver_.SetNackSettings(kMaxNackListSize, kMaxPacketAgeToNack, |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 227 | kMaxNonDecodableDuration); |
Niels Möller | 4eb4b05 | 2018-08-14 10:31:58 +0200 | [diff] [blame] | 228 | timing_.set_min_playout_delay(kMinDelayMs); |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 229 | int64_t key_frame_inserted = clock_->TimeInMilliseconds(); |
| 230 | EXPECT_GE(InsertFrame(kVideoFrameKey, true), kNoError); |
| 231 | // Insert an incomplete frame. |
| 232 | EXPECT_GE(InsertFrame(kVideoFrameDelta, false), kNoError); |
| 233 | // Insert enough frames to have too long non-decodable sequence. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 234 | for (int i = 0; i < kMaxNonDecodableDurationFrames; ++i) { |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 235 | EXPECT_GE(InsertFrame(kVideoFrameDelta, true), kNoError); |
| 236 | } |
| 237 | EXPECT_GE(InsertFrame(kVideoFrameKey, true), kNoError); |
| 238 | // Advance time until it's time to decode the key frame. |
| 239 | clock_->AdvanceTimeMilliseconds(kMinDelayMs - clock_->TimeInMilliseconds() - |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 240 | key_frame_inserted); |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 241 | EXPECT_TRUE(DecodeNextFrame()); |
| 242 | // Make sure we don't get a key frame request since we have a key frame |
| 243 | // in the list. |
Wan-Teh Chang | b1825a4 | 2015-06-03 15:03:35 -0700 | [diff] [blame] | 244 | bool request_key_frame = false; |
| 245 | std::vector<uint16_t> nack_list = receiver_.NackList(&request_key_frame); |
| 246 | EXPECT_FALSE(request_key_frame); |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 247 | } |
Qiang Chen | d4cec15 | 2015-06-19 09:17:00 -0700 | [diff] [blame] | 248 | |
| 249 | // A simulated clock, when time elapses, will insert frames into the jitter |
| 250 | // buffer, based on initial settings. |
| 251 | class SimulatedClockWithFrames : public SimulatedClock { |
| 252 | public: |
| 253 | SimulatedClockWithFrames(StreamGenerator* stream_generator, |
| 254 | VCMReceiver* receiver) |
| 255 | : SimulatedClock(0), |
| 256 | stream_generator_(stream_generator), |
| 257 | receiver_(receiver) {} |
| 258 | virtual ~SimulatedClockWithFrames() {} |
| 259 | |
| 260 | // If |stop_on_frame| is true and next frame arrives between now and |
| 261 | // now+|milliseconds|, the clock will be advanced to the arrival time of next |
| 262 | // frame. |
| 263 | // Otherwise, the clock will be advanced by |milliseconds|. |
| 264 | // |
| 265 | // For both cases, a frame will be inserted into the jitter buffer at the |
| 266 | // instant when the clock time is timestamps_.front().arrive_time. |
| 267 | // |
| 268 | // Return true if some frame arrives between now and now+|milliseconds|. |
| 269 | bool AdvanceTimeMilliseconds(int64_t milliseconds, bool stop_on_frame) { |
| 270 | return AdvanceTimeMicroseconds(milliseconds * 1000, stop_on_frame); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 271 | } |
Qiang Chen | d4cec15 | 2015-06-19 09:17:00 -0700 | [diff] [blame] | 272 | |
| 273 | bool AdvanceTimeMicroseconds(int64_t microseconds, bool stop_on_frame) { |
| 274 | int64_t start_time = TimeInMicroseconds(); |
| 275 | int64_t end_time = start_time + microseconds; |
| 276 | bool frame_injected = false; |
| 277 | while (!timestamps_.empty() && |
| 278 | timestamps_.front().arrive_time <= end_time) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 279 | RTC_DCHECK(timestamps_.front().arrive_time >= start_time); |
Qiang Chen | d4cec15 | 2015-06-19 09:17:00 -0700 | [diff] [blame] | 280 | |
| 281 | SimulatedClock::AdvanceTimeMicroseconds(timestamps_.front().arrive_time - |
| 282 | TimeInMicroseconds()); |
| 283 | GenerateAndInsertFrame((timestamps_.front().render_time + 500) / 1000); |
| 284 | timestamps_.pop(); |
| 285 | frame_injected = true; |
| 286 | |
| 287 | if (stop_on_frame) |
| 288 | return frame_injected; |
| 289 | } |
| 290 | |
| 291 | if (TimeInMicroseconds() < end_time) { |
| 292 | SimulatedClock::AdvanceTimeMicroseconds(end_time - TimeInMicroseconds()); |
| 293 | } |
| 294 | return frame_injected; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 295 | } |
Qiang Chen | d4cec15 | 2015-06-19 09:17:00 -0700 | [diff] [blame] | 296 | |
| 297 | // Input timestamps are in unit Milliseconds. |
| 298 | // And |arrive_timestamps| must be positive and in increasing order. |
| 299 | // |arrive_timestamps| determine when we are going to insert frames into the |
| 300 | // jitter buffer. |
| 301 | // |render_timestamps| are the timestamps on the frame. |
| 302 | void SetFrames(const int64_t* arrive_timestamps, |
| 303 | const int64_t* render_timestamps, |
| 304 | size_t size) { |
| 305 | int64_t previous_arrive_timestamp = 0; |
| 306 | for (size_t i = 0; i < size; i++) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 307 | RTC_CHECK(arrive_timestamps[i] >= previous_arrive_timestamp); |
Qiang Chen | d4cec15 | 2015-06-19 09:17:00 -0700 | [diff] [blame] | 308 | timestamps_.push(TimestampPair(arrive_timestamps[i] * 1000, |
| 309 | render_timestamps[i] * 1000)); |
| 310 | previous_arrive_timestamp = arrive_timestamps[i]; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | private: |
| 315 | struct TimestampPair { |
| 316 | TimestampPair(int64_t arrive_timestamp, int64_t render_timestamp) |
| 317 | : arrive_time(arrive_timestamp), render_time(render_timestamp) {} |
| 318 | |
| 319 | int64_t arrive_time; |
| 320 | int64_t render_time; |
| 321 | }; |
| 322 | |
| 323 | void GenerateAndInsertFrame(int64_t render_timestamp_ms) { |
| 324 | VCMPacket packet; |
| 325 | stream_generator_->GenerateFrame(FrameType::kVideoFrameKey, |
| 326 | 1, // media packets |
| 327 | 0, // empty packets |
| 328 | render_timestamp_ms); |
| 329 | |
| 330 | bool packet_available = stream_generator_->PopPacket(&packet, 0); |
| 331 | EXPECT_TRUE(packet_available); |
| 332 | if (!packet_available) |
| 333 | return; // Return here to avoid crashes below. |
Johan Ahlers | 95348f7 | 2016-06-28 11:11:28 +0200 | [diff] [blame] | 334 | receiver_->InsertPacket(packet); |
Qiang Chen | d4cec15 | 2015-06-19 09:17:00 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | std::queue<TimestampPair> timestamps_; |
| 338 | StreamGenerator* stream_generator_; |
| 339 | VCMReceiver* receiver_; |
| 340 | }; |
| 341 | |
| 342 | // Use a SimulatedClockWithFrames |
| 343 | // Wait call will do either of these: |
| 344 | // 1. If |stop_on_frame| is true, the clock will be turned to the exact instant |
| 345 | // that the first frame comes and the frame will be inserted into the jitter |
| 346 | // buffer, or the clock will be turned to now + |max_time| if no frame comes in |
| 347 | // the window. |
| 348 | // 2. If |stop_on_frame| is false, the clock will be turn to now + |max_time|, |
| 349 | // and all the frames arriving between now and now + |max_time| will be |
| 350 | // inserted into the jitter buffer. |
| 351 | // |
| 352 | // This is used to simulate the JitterBuffer getting packets from internet as |
| 353 | // time elapses. |
| 354 | |
| 355 | class FrameInjectEvent : public EventWrapper { |
| 356 | public: |
| 357 | FrameInjectEvent(SimulatedClockWithFrames* clock, bool stop_on_frame) |
| 358 | : clock_(clock), stop_on_frame_(stop_on_frame) {} |
| 359 | |
| 360 | bool Set() override { return true; } |
| 361 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 362 | EventTypeWrapper Wait(unsigned long max_time) override { // NOLINT |
Qiang Chen | d4cec15 | 2015-06-19 09:17:00 -0700 | [diff] [blame] | 363 | if (clock_->AdvanceTimeMilliseconds(max_time, stop_on_frame_) && |
| 364 | stop_on_frame_) { |
| 365 | return EventTypeWrapper::kEventSignaled; |
| 366 | } else { |
| 367 | return EventTypeWrapper::kEventTimeout; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | private: |
| 372 | SimulatedClockWithFrames* clock_; |
| 373 | bool stop_on_frame_; |
| 374 | }; |
| 375 | |
| 376 | class VCMReceiverTimingTest : public ::testing::Test { |
| 377 | protected: |
Qiang Chen | d4cec15 | 2015-06-19 09:17:00 -0700 | [diff] [blame] | 378 | VCMReceiverTimingTest() |
| 379 | |
| 380 | : clock_(&stream_generator_, &receiver_), |
| 381 | stream_generator_(0, clock_.TimeInMilliseconds()), |
| 382 | timing_(&clock_), |
| 383 | receiver_( |
| 384 | &timing_, |
| 385 | &clock_, |
kwiberg | 3f55dea | 2016-02-29 05:51:59 -0800 | [diff] [blame] | 386 | std::unique_ptr<EventWrapper>(new FrameInjectEvent(&clock_, false)), |
| 387 | std::unique_ptr<EventWrapper>( |
Qiang Chen | d4cec15 | 2015-06-19 09:17:00 -0700 | [diff] [blame] | 388 | new FrameInjectEvent(&clock_, true))) {} |
| 389 | |
Qiang Chen | d4cec15 | 2015-06-19 09:17:00 -0700 | [diff] [blame] | 390 | virtual void SetUp() { receiver_.Reset(); } |
| 391 | |
| 392 | SimulatedClockWithFrames clock_; |
| 393 | StreamGenerator stream_generator_; |
| 394 | VCMTiming timing_; |
| 395 | VCMReceiver receiver_; |
| 396 | }; |
| 397 | |
| 398 | // Test whether VCMReceiver::FrameForDecoding handles parameter |
| 399 | // |max_wait_time_ms| correctly: |
| 400 | // 1. The function execution should never take more than |max_wait_time_ms|. |
| 401 | // 2. If the function exit before now + |max_wait_time_ms|, a frame must be |
| 402 | // returned. |
| 403 | TEST_F(VCMReceiverTimingTest, FrameForDecoding) { |
| 404 | const size_t kNumFrames = 100; |
| 405 | const int kFramePeriod = 40; |
| 406 | int64_t arrive_timestamps[kNumFrames]; |
| 407 | int64_t render_timestamps[kNumFrames]; |
Qiang Chen | d4cec15 | 2015-06-19 09:17:00 -0700 | [diff] [blame] | 408 | |
| 409 | // Construct test samples. |
| 410 | // render_timestamps are the timestamps stored in the Frame; |
| 411 | // arrive_timestamps controls when the Frame packet got received. |
| 412 | for (size_t i = 0; i < kNumFrames; i++) { |
| 413 | // Preset frame rate to 25Hz. |
| 414 | // But we add a reasonable deviation to arrive_timestamps to mimic Internet |
| 415 | // fluctuation. |
| 416 | arrive_timestamps[i] = |
| 417 | (i + 1) * kFramePeriod + (i % 10) * ((i % 2) ? 1 : -1); |
| 418 | render_timestamps[i] = (i + 1) * kFramePeriod; |
| 419 | } |
| 420 | |
| 421 | clock_.SetFrames(arrive_timestamps, render_timestamps, kNumFrames); |
| 422 | |
| 423 | // Record how many frames we finally get out of the receiver. |
| 424 | size_t num_frames_return = 0; |
| 425 | |
| 426 | const int64_t kMaxWaitTime = 30; |
| 427 | |
| 428 | // Ideally, we should get all frames that we input in InitializeFrames. |
| 429 | // In the case that FrameForDecoding kills frames by error, we rely on the |
| 430 | // build bot to kill the test. |
| 431 | while (num_frames_return < kNumFrames) { |
| 432 | int64_t start_time = clock_.TimeInMilliseconds(); |
Johan Ahlers | 31b2ec4 | 2016-06-28 13:32:49 +0200 | [diff] [blame] | 433 | VCMEncodedFrame* frame = receiver_.FrameForDecoding(kMaxWaitTime, false); |
Qiang Chen | d4cec15 | 2015-06-19 09:17:00 -0700 | [diff] [blame] | 434 | int64_t end_time = clock_.TimeInMilliseconds(); |
| 435 | |
| 436 | // In any case the FrameForDecoding should not wait longer than |
| 437 | // max_wait_time. |
| 438 | // In the case that we did not get a frame, it should have been waiting for |
| 439 | // exactly max_wait_time. (By the testing samples we constructed above, we |
| 440 | // are sure there is no timing error, so the only case it returns with NULL |
| 441 | // is that it runs out of time.) |
| 442 | if (frame) { |
| 443 | receiver_.ReleaseFrame(frame); |
| 444 | ++num_frames_return; |
| 445 | EXPECT_GE(kMaxWaitTime, end_time - start_time); |
| 446 | } else { |
| 447 | EXPECT_EQ(kMaxWaitTime, end_time - start_time); |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
perkj | 796cfaf | 2015-12-10 09:27:38 -0800 | [diff] [blame] | 452 | // Test whether VCMReceiver::FrameForDecoding handles parameter |
| 453 | // |prefer_late_decoding| and |max_wait_time_ms| correctly: |
| 454 | // 1. The function execution should never take more than |max_wait_time_ms|. |
| 455 | // 2. If the function exit before now + |max_wait_time_ms|, a frame must be |
| 456 | // returned and the end time must be equal to the render timestamp - delay |
| 457 | // for decoding and rendering. |
| 458 | TEST_F(VCMReceiverTimingTest, FrameForDecodingPreferLateDecoding) { |
| 459 | const size_t kNumFrames = 100; |
| 460 | const int kFramePeriod = 40; |
| 461 | |
| 462 | int64_t arrive_timestamps[kNumFrames]; |
| 463 | int64_t render_timestamps[kNumFrames]; |
perkj | 796cfaf | 2015-12-10 09:27:38 -0800 | [diff] [blame] | 464 | |
| 465 | int render_delay_ms; |
| 466 | int max_decode_ms; |
| 467 | int dummy; |
| 468 | timing_.GetTimings(&dummy, &max_decode_ms, &dummy, &dummy, &dummy, &dummy, |
| 469 | &render_delay_ms); |
| 470 | |
| 471 | // Construct test samples. |
| 472 | // render_timestamps are the timestamps stored in the Frame; |
| 473 | // arrive_timestamps controls when the Frame packet got received. |
| 474 | for (size_t i = 0; i < kNumFrames; i++) { |
| 475 | // Preset frame rate to 25Hz. |
| 476 | // But we add a reasonable deviation to arrive_timestamps to mimic Internet |
| 477 | // fluctuation. |
| 478 | arrive_timestamps[i] = |
| 479 | (i + 1) * kFramePeriod + (i % 10) * ((i % 2) ? 1 : -1); |
| 480 | render_timestamps[i] = (i + 1) * kFramePeriod; |
| 481 | } |
| 482 | |
| 483 | clock_.SetFrames(arrive_timestamps, render_timestamps, kNumFrames); |
| 484 | |
| 485 | // Record how many frames we finally get out of the receiver. |
| 486 | size_t num_frames_return = 0; |
| 487 | const int64_t kMaxWaitTime = 30; |
| 488 | bool prefer_late_decoding = true; |
| 489 | while (num_frames_return < kNumFrames) { |
| 490 | int64_t start_time = clock_.TimeInMilliseconds(); |
| 491 | |
Johan Ahlers | 31b2ec4 | 2016-06-28 13:32:49 +0200 | [diff] [blame] | 492 | VCMEncodedFrame* frame = |
| 493 | receiver_.FrameForDecoding(kMaxWaitTime, prefer_late_decoding); |
perkj | 796cfaf | 2015-12-10 09:27:38 -0800 | [diff] [blame] | 494 | int64_t end_time = clock_.TimeInMilliseconds(); |
| 495 | if (frame) { |
| 496 | EXPECT_EQ(frame->RenderTimeMs() - max_decode_ms - render_delay_ms, |
| 497 | end_time); |
| 498 | receiver_.ReleaseFrame(frame); |
| 499 | ++num_frames_return; |
| 500 | } else { |
| 501 | EXPECT_EQ(kMaxWaitTime, end_time - start_time); |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 506 | } // namespace webrtc |