ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 1 | /* |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved. |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 11 | #include <cstddef> |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include "api/video/video_timing.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "modules/video_coding/include/video_coding_defines.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 16 | #include "rtc_base/time_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "test/gtest.h" |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 18 | #include "video/frame_encode_timer.h" |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | namespace test { |
| 22 | namespace { |
| 23 | inline size_t FrameSize(const size_t& min_frame_size, |
| 24 | const size_t& max_frame_size, |
| 25 | const int& s, |
| 26 | const int& i) { |
| 27 | return min_frame_size + (s + 1) * i % (max_frame_size - min_frame_size); |
| 28 | } |
| 29 | |
| 30 | class FakeEncodedImageCallback : public EncodedImageCallback { |
| 31 | public: |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 32 | FakeEncodedImageCallback() : num_frames_dropped_(0) {} |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 33 | Result OnEncodedImage(const EncodedImage& encoded_image, |
| 34 | const CodecSpecificInfo* codec_specific_info, |
| 35 | const RTPFragmentationHeader* fragmentation) override { |
mflodman | 351424e | 2017-08-10 02:43:14 -0700 | [diff] [blame] | 36 | return Result(Result::OK); |
Mirko Bonadei | c4dd730 | 2019-02-25 09:12:02 +0100 | [diff] [blame] | 37 | } |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 38 | void OnDroppedFrame(DropReason reason) override { ++num_frames_dropped_; } |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 39 | size_t GetNumFramesDropped() { return num_frames_dropped_; } |
| 40 | |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 41 | private: |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 42 | size_t num_frames_dropped_; |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | enum class FrameType { |
| 46 | kNormal, |
| 47 | kTiming, |
| 48 | kDropped, |
| 49 | }; |
| 50 | |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 51 | bool IsTimingFrame(const EncodedImage& image) { |
| 52 | return image.timing_.flags != VideoSendTiming::kInvalid && |
| 53 | image.timing_.flags != VideoSendTiming::kNotTriggered; |
| 54 | } |
| 55 | |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 56 | // Emulates |num_frames| on |num_streams| frames with capture timestamps |
| 57 | // increased by 1 from 0. Size of each frame is between |
| 58 | // |min_frame_size| and |max_frame_size|, outliers are counted relatevely to |
| 59 | // |average_frame_sizes[]| for each stream. |
| 60 | std::vector<std::vector<FrameType>> GetTimingFrames( |
| 61 | const int64_t delay_ms, |
| 62 | const size_t min_frame_size, |
| 63 | const size_t max_frame_size, |
| 64 | std::vector<size_t> average_frame_sizes, |
| 65 | const int num_streams, |
| 66 | const int num_frames) { |
| 67 | FakeEncodedImageCallback sink; |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 68 | FrameEncodeTimer encode_timer(&sink); |
| 69 | VideoCodec codec_settings; |
| 70 | codec_settings.numberOfSimulcastStreams = num_streams; |
| 71 | codec_settings.timing_frame_thresholds = {delay_ms, |
| 72 | kDefaultOutlierFrameSizePercent}; |
| 73 | encode_timer.OnEncoderInit(codec_settings, false); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 74 | const size_t kFramerate = 30; |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 75 | VideoBitrateAllocation bitrate_allocation; |
| 76 | for (int si = 0; si < num_streams; ++si) { |
| 77 | bitrate_allocation.SetBitrate(si, 0, |
| 78 | average_frame_sizes[si] * 8 * kFramerate); |
| 79 | } |
| 80 | encode_timer.OnSetRates(bitrate_allocation, kFramerate); |
| 81 | |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 82 | std::vector<std::vector<FrameType>> result(num_streams); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 83 | int64_t current_timestamp = 0; |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 84 | for (int i = 0; i < num_frames; ++i) { |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 85 | current_timestamp += 1; |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 86 | encode_timer.OnEncodeStarted(static_cast<uint32_t>(current_timestamp * 90), |
| 87 | current_timestamp); |
| 88 | for (int si = 0; si < num_streams; ++si) { |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 89 | // every (5+s)-th frame is dropped on s-th stream by design. |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 90 | bool dropped = i % (5 + si) == 0; |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 91 | |
| 92 | EncodedImage image; |
Niels Möller | 663844d | 2019-02-14 16:15:54 +0100 | [diff] [blame] | 93 | image.Allocate(max_frame_size); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 94 | image.set_size(FrameSize(min_frame_size, max_frame_size, si, i)); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 95 | image.capture_time_ms_ = current_timestamp; |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 96 | image.SetTimestamp(static_cast<uint32_t>(current_timestamp * 90)); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 97 | image.SetSpatialIndex(si); |
| 98 | |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 99 | if (dropped) { |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 100 | result[si].push_back(FrameType::kDropped); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 101 | continue; |
| 102 | } |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 103 | |
| 104 | encode_timer.FillTimingInfo(si, &image, current_timestamp); |
| 105 | |
| 106 | if (IsTimingFrame(image)) { |
| 107 | result[si].push_back(FrameType::kTiming); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 108 | } else { |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 109 | result[si].push_back(FrameType::kNormal); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | } |
| 113 | return result; |
| 114 | } |
| 115 | } // namespace |
| 116 | |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 117 | TEST(FrameEncodeTimerTest, MarksTimingFramesPeriodicallyTogether) { |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 118 | const int64_t kDelayMs = 29; |
| 119 | const size_t kMinFrameSize = 10; |
| 120 | const size_t kMaxFrameSize = 20; |
| 121 | const int kNumFrames = 1000; |
| 122 | const int kNumStreams = 3; |
| 123 | // No outliers as 1000 is larger than anything from range [10,20]. |
| 124 | const std::vector<size_t> kAverageSize = {1000, 1000, 1000}; |
| 125 | auto frames = GetTimingFrames(kDelayMs, kMinFrameSize, kMaxFrameSize, |
| 126 | kAverageSize, kNumStreams, kNumFrames); |
| 127 | // Timing frames should be tirggered every delayMs. |
| 128 | // As no outliers are expected, frames on all streams have to be |
| 129 | // marked together. |
| 130 | int last_timing_frame = -1; |
| 131 | for (int i = 0; i < kNumFrames; ++i) { |
| 132 | int num_normal = 0; |
| 133 | int num_timing = 0; |
| 134 | int num_dropped = 0; |
| 135 | for (int s = 0; s < kNumStreams; ++s) { |
| 136 | if (frames[s][i] == FrameType::kTiming) { |
| 137 | ++num_timing; |
| 138 | } else if (frames[s][i] == FrameType::kNormal) { |
| 139 | ++num_normal; |
| 140 | } else { |
| 141 | ++num_dropped; |
| 142 | } |
| 143 | } |
| 144 | // Can't have both normal and timing frames at the same timstamp. |
| 145 | EXPECT_TRUE(num_timing == 0 || num_normal == 0); |
| 146 | if (num_dropped < kNumStreams) { |
| 147 | if (last_timing_frame == -1 || i >= last_timing_frame + kDelayMs) { |
| 148 | // If didn't have timing frames for a period, current sent frame has to |
| 149 | // be one. No normal frames should be sent. |
| 150 | EXPECT_EQ(num_normal, 0); |
| 151 | } else { |
| 152 | // No unneeded timing frames should be sent. |
| 153 | EXPECT_EQ(num_timing, 0); |
| 154 | } |
| 155 | } |
| 156 | if (num_timing > 0) |
| 157 | last_timing_frame = i; |
| 158 | } |
| 159 | } |
| 160 | |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 161 | TEST(FrameEncodeTimerTest, MarksOutliers) { |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 162 | const int64_t kDelayMs = 29; |
| 163 | const size_t kMinFrameSize = 2495; |
| 164 | const size_t kMaxFrameSize = 2505; |
| 165 | const int kNumFrames = 1000; |
| 166 | const int kNumStreams = 3; |
| 167 | // Possible outliers as 1000 lies in range [995, 1005]. |
| 168 | const std::vector<size_t> kAverageSize = {998, 1000, 1004}; |
| 169 | auto frames = GetTimingFrames(kDelayMs, kMinFrameSize, kMaxFrameSize, |
| 170 | kAverageSize, kNumStreams, kNumFrames); |
| 171 | // All outliers should be marked. |
| 172 | for (int i = 0; i < kNumFrames; ++i) { |
| 173 | for (int s = 0; s < kNumStreams; ++s) { |
| 174 | if (FrameSize(kMinFrameSize, kMaxFrameSize, s, i) >= |
| 175 | kAverageSize[s] * kDefaultOutlierFrameSizePercent / 100) { |
| 176 | // Too big frame. May be dropped or timing, but not normal. |
| 177 | EXPECT_NE(frames[s][i], FrameType::kNormal); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 183 | TEST(FrameEncodeTimerTest, NoTimingFrameIfNoEncodeStartTime) { |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 184 | int64_t timestamp = 1; |
Niels Möller | 663844d | 2019-02-14 16:15:54 +0100 | [diff] [blame] | 185 | constexpr size_t kFrameSize = 500; |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 186 | EncodedImage image; |
Niels Möller | 663844d | 2019-02-14 16:15:54 +0100 | [diff] [blame] | 187 | image.Allocate(kFrameSize); |
| 188 | image.set_size(kFrameSize); |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 189 | image.capture_time_ms_ = timestamp; |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 190 | image.SetTimestamp(static_cast<uint32_t>(timestamp * 90)); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 191 | |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 192 | FakeEncodedImageCallback sink; |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 193 | FrameEncodeTimer encode_timer(&sink); |
| 194 | VideoCodec codec_settings; |
| 195 | // Make all frames timing frames. |
| 196 | codec_settings.timing_frame_thresholds.delay_ms = 1; |
| 197 | encode_timer.OnEncoderInit(codec_settings, false); |
| 198 | VideoBitrateAllocation bitrate_allocation; |
| 199 | bitrate_allocation.SetBitrate(0, 0, 500000); |
| 200 | encode_timer.OnSetRates(bitrate_allocation, 30); |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 201 | |
| 202 | // Verify a single frame works with encode start time set. |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 203 | encode_timer.OnEncodeStarted(static_cast<uint32_t>(timestamp * 90), |
| 204 | timestamp); |
| 205 | encode_timer.FillTimingInfo(0, &image, timestamp); |
| 206 | EXPECT_TRUE(IsTimingFrame(image)); |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 207 | |
| 208 | // New frame, now skip OnEncodeStarted. Should not result in timing frame. |
| 209 | image.capture_time_ms_ = ++timestamp; |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 210 | image.SetTimestamp(static_cast<uint32_t>(timestamp * 90)); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 211 | image.timing_ = EncodedImage::Timing(); |
| 212 | encode_timer.FillTimingInfo(0, &image, timestamp); |
| 213 | EXPECT_FALSE(IsTimingFrame(image)); |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 216 | TEST(FrameEncodeTimerTest, AdjustsCaptureTimeForInternalSourceEncoder) { |
Ilya Nikolaevskiy | 764aeb7 | 2018-04-03 10:01:52 +0200 | [diff] [blame] | 217 | const int64_t kEncodeStartDelayMs = 2; |
| 218 | const int64_t kEncodeFinishDelayMs = 10; |
Niels Möller | 663844d | 2019-02-14 16:15:54 +0100 | [diff] [blame] | 219 | constexpr size_t kFrameSize = 500; |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 220 | |
| 221 | int64_t timestamp = 1; |
| 222 | EncodedImage image; |
Niels Möller | 663844d | 2019-02-14 16:15:54 +0100 | [diff] [blame] | 223 | image.Allocate(kFrameSize); |
| 224 | image.set_size(kFrameSize); |
Ilya Nikolaevskiy | 764aeb7 | 2018-04-03 10:01:52 +0200 | [diff] [blame] | 225 | image.capture_time_ms_ = timestamp; |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 226 | image.SetTimestamp(static_cast<uint32_t>(timestamp * 90)); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 227 | |
Ilya Nikolaevskiy | 764aeb7 | 2018-04-03 10:01:52 +0200 | [diff] [blame] | 228 | FakeEncodedImageCallback sink; |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 229 | FrameEncodeTimer encode_timer(&sink); |
| 230 | |
| 231 | VideoCodec codec_settings; |
| 232 | // Make all frames timing frames. |
| 233 | codec_settings.timing_frame_thresholds.delay_ms = 1; |
| 234 | encode_timer.OnEncoderInit(codec_settings, true); |
| 235 | |
| 236 | VideoBitrateAllocation bitrate_allocation; |
| 237 | bitrate_allocation.SetBitrate(0, 0, 500000); |
| 238 | encode_timer.OnSetRates(bitrate_allocation, 30); |
Ilya Nikolaevskiy | 764aeb7 | 2018-04-03 10:01:52 +0200 | [diff] [blame] | 239 | |
| 240 | // Verify a single frame without encode timestamps isn't a timing frame. |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 241 | encode_timer.FillTimingInfo(0, &image, timestamp); |
| 242 | EXPECT_FALSE(IsTimingFrame(image)); |
Ilya Nikolaevskiy | 764aeb7 | 2018-04-03 10:01:52 +0200 | [diff] [blame] | 243 | |
| 244 | // New frame, but this time with encode timestamps set in timing_. |
| 245 | // This should be a timing frame. |
| 246 | image.capture_time_ms_ = ++timestamp; |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 247 | image.SetTimestamp(static_cast<uint32_t>(timestamp * 90)); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 248 | image.timing_ = EncodedImage::Timing(); |
Ilya Nikolaevskiy | 764aeb7 | 2018-04-03 10:01:52 +0200 | [diff] [blame] | 249 | image.timing_.encode_start_ms = timestamp + kEncodeStartDelayMs; |
| 250 | image.timing_.encode_finish_ms = timestamp + kEncodeFinishDelayMs; |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 251 | const int64_t kEncodeDoneTimestamp = 1234567; |
| 252 | encode_timer.FillTimingInfo(0, &image, kEncodeDoneTimestamp); |
| 253 | EXPECT_TRUE(IsTimingFrame(image)); |
| 254 | |
Ilya Nikolaevskiy | 764aeb7 | 2018-04-03 10:01:52 +0200 | [diff] [blame] | 255 | // Frame is captured kEncodeFinishDelayMs before it's encoded, so restored |
| 256 | // capture timestamp should be kEncodeFinishDelayMs in the past. |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 257 | EXPECT_EQ(image.capture_time_ms_, |
| 258 | kEncodeDoneTimestamp - kEncodeFinishDelayMs); |
Ilya Nikolaevskiy | 764aeb7 | 2018-04-03 10:01:52 +0200 | [diff] [blame] | 259 | } |
| 260 | |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 261 | TEST(FrameEncodeTimerTest, NotifiesAboutDroppedFrames) { |
Ilya Nikolaevskiy | 76f2a85 | 2017-11-16 14:33:53 +0100 | [diff] [blame] | 262 | const int64_t kTimestampMs1 = 47721840; |
| 263 | const int64_t kTimestampMs2 = 47721850; |
| 264 | const int64_t kTimestampMs3 = 47721860; |
| 265 | const int64_t kTimestampMs4 = 47721870; |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 266 | |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 267 | FakeEncodedImageCallback sink; |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 268 | FrameEncodeTimer encode_timer(&sink); |
| 269 | encode_timer.OnEncoderInit(VideoCodec(), false); |
Ilya Nikolaevskiy | e0da9ea | 2017-11-08 14:39:02 +0100 | [diff] [blame] | 270 | // Any non-zero bitrate needed to be set before the first frame. |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 271 | VideoBitrateAllocation bitrate_allocation; |
| 272 | bitrate_allocation.SetBitrate(0, 0, 500000); |
| 273 | encode_timer.OnSetRates(bitrate_allocation, 30); |
| 274 | |
| 275 | EncodedImage image; |
Ilya Nikolaevskiy | 76f2a85 | 2017-11-16 14:33:53 +0100 | [diff] [blame] | 276 | image.capture_time_ms_ = kTimestampMs1; |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 277 | image.SetTimestamp(static_cast<uint32_t>(image.capture_time_ms_ * 90)); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 278 | encode_timer.OnEncodeStarted(image.Timestamp(), image.capture_time_ms_); |
| 279 | |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 280 | EXPECT_EQ(0u, sink.GetNumFramesDropped()); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 281 | encode_timer.FillTimingInfo(0, &image, kTimestampMs1); |
Ilya Nikolaevskiy | 76f2a85 | 2017-11-16 14:33:53 +0100 | [diff] [blame] | 282 | |
| 283 | image.capture_time_ms_ = kTimestampMs2; |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 284 | image.SetTimestamp(static_cast<uint32_t>(image.capture_time_ms_ * 90)); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 285 | image.timing_ = EncodedImage::Timing(); |
| 286 | encode_timer.OnEncodeStarted(image.Timestamp(), image.capture_time_ms_); |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 287 | // No OnEncodedImageCall for timestamp2. Yet, at this moment it's not known |
| 288 | // that frame with timestamp2 was dropped. |
| 289 | EXPECT_EQ(0u, sink.GetNumFramesDropped()); |
Ilya Nikolaevskiy | 76f2a85 | 2017-11-16 14:33:53 +0100 | [diff] [blame] | 290 | |
| 291 | image.capture_time_ms_ = kTimestampMs3; |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 292 | image.SetTimestamp(static_cast<uint32_t>(image.capture_time_ms_ * 90)); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 293 | image.timing_ = EncodedImage::Timing(); |
| 294 | encode_timer.OnEncodeStarted(image.Timestamp(), image.capture_time_ms_); |
| 295 | encode_timer.FillTimingInfo(0, &image, kTimestampMs3); |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 296 | EXPECT_EQ(1u, sink.GetNumFramesDropped()); |
Ilya Nikolaevskiy | 76f2a85 | 2017-11-16 14:33:53 +0100 | [diff] [blame] | 297 | |
| 298 | image.capture_time_ms_ = kTimestampMs4; |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 299 | image.SetTimestamp(static_cast<uint32_t>(image.capture_time_ms_ * 90)); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 300 | image.timing_ = EncodedImage::Timing(); |
| 301 | encode_timer.OnEncodeStarted(image.Timestamp(), image.capture_time_ms_); |
| 302 | encode_timer.FillTimingInfo(0, &image, kTimestampMs4); |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 303 | EXPECT_EQ(1u, sink.GetNumFramesDropped()); |
| 304 | } |
| 305 | |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 306 | TEST(FrameEncodeTimerTest, RestoresCaptureTimestamps) { |
Ilya Nikolaevskiy | 76f2a85 | 2017-11-16 14:33:53 +0100 | [diff] [blame] | 307 | EncodedImage image; |
Ilya Nikolaevskiy | 76f2a85 | 2017-11-16 14:33:53 +0100 | [diff] [blame] | 308 | const int64_t kTimestampMs = 123456; |
Ilya Nikolaevskiy | 76f2a85 | 2017-11-16 14:33:53 +0100 | [diff] [blame] | 309 | FakeEncodedImageCallback sink; |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 310 | |
| 311 | FrameEncodeTimer encode_timer(&sink); |
| 312 | encode_timer.OnEncoderInit(VideoCodec(), false); |
Ilya Nikolaevskiy | 76f2a85 | 2017-11-16 14:33:53 +0100 | [diff] [blame] | 313 | // Any non-zero bitrate needed to be set before the first frame. |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 314 | VideoBitrateAllocation bitrate_allocation; |
| 315 | bitrate_allocation.SetBitrate(0, 0, 500000); |
| 316 | encode_timer.OnSetRates(bitrate_allocation, 30); |
| 317 | |
| 318 | image.capture_time_ms_ = kTimestampMs; // Correct timestamp. |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 319 | image.SetTimestamp(static_cast<uint32_t>(image.capture_time_ms_ * 90)); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame^] | 320 | encode_timer.OnEncodeStarted(image.Timestamp(), image.capture_time_ms_); |
| 321 | image.capture_time_ms_ = 0; // Incorrect timestamp. |
| 322 | encode_timer.FillTimingInfo(0, &image, kTimestampMs); |
| 323 | EXPECT_EQ(kTimestampMs, image.capture_time_ms_); |
Ilya Nikolaevskiy | 76f2a85 | 2017-11-16 14:33:53 +0100 | [diff] [blame] | 324 | } |
| 325 | |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 326 | } // namespace test |
| 327 | } // namespace webrtc |