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 | |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 11 | #include "video/frame_encode_metadata_writer.h" |
| 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <cstddef> |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 16 | #include "api/video/i420_buffer.h" |
| 17 | #include "api/video/video_frame.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 18 | #include "api/video/video_timing.h" |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 19 | #include "common_video/test/utilities.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "modules/video_coding/include/video_coding_defines.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "rtc_base/time_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "test/gtest.h" |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | namespace test { |
| 26 | namespace { |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 27 | |
| 28 | const rtc::scoped_refptr<I420Buffer> kFrameBuffer = I420Buffer::Create(4, 4); |
| 29 | |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 30 | inline size_t FrameSize(const size_t& min_frame_size, |
| 31 | const size_t& max_frame_size, |
| 32 | const int& s, |
| 33 | const int& i) { |
| 34 | return min_frame_size + (s + 1) * i % (max_frame_size - min_frame_size); |
| 35 | } |
| 36 | |
| 37 | class FakeEncodedImageCallback : public EncodedImageCallback { |
| 38 | public: |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 39 | FakeEncodedImageCallback() : num_frames_dropped_(0) {} |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 40 | Result OnEncodedImage(const EncodedImage& encoded_image, |
| 41 | const CodecSpecificInfo* codec_specific_info, |
| 42 | const RTPFragmentationHeader* fragmentation) override { |
mflodman | 351424e | 2017-08-10 02:43:14 -0700 | [diff] [blame] | 43 | return Result(Result::OK); |
Mirko Bonadei | c4dd730 | 2019-02-25 09:12:02 +0100 | [diff] [blame] | 44 | } |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 45 | void OnDroppedFrame(DropReason reason) override { ++num_frames_dropped_; } |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 46 | size_t GetNumFramesDropped() { return num_frames_dropped_; } |
| 47 | |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 48 | private: |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 49 | size_t num_frames_dropped_; |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | enum class FrameType { |
| 53 | kNormal, |
| 54 | kTiming, |
| 55 | kDropped, |
| 56 | }; |
| 57 | |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 58 | bool IsTimingFrame(const EncodedImage& image) { |
| 59 | return image.timing_.flags != VideoSendTiming::kInvalid && |
| 60 | image.timing_.flags != VideoSendTiming::kNotTriggered; |
| 61 | } |
| 62 | |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 63 | // Emulates |num_frames| on |num_streams| frames with capture timestamps |
| 64 | // increased by 1 from 0. Size of each frame is between |
| 65 | // |min_frame_size| and |max_frame_size|, outliers are counted relatevely to |
| 66 | // |average_frame_sizes[]| for each stream. |
| 67 | std::vector<std::vector<FrameType>> GetTimingFrames( |
| 68 | const int64_t delay_ms, |
| 69 | const size_t min_frame_size, |
| 70 | const size_t max_frame_size, |
| 71 | std::vector<size_t> average_frame_sizes, |
| 72 | const int num_streams, |
| 73 | const int num_frames) { |
| 74 | FakeEncodedImageCallback sink; |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 75 | FrameEncodeMetadataWriter encode_timer(&sink); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 76 | VideoCodec codec_settings; |
| 77 | codec_settings.numberOfSimulcastStreams = num_streams; |
| 78 | codec_settings.timing_frame_thresholds = {delay_ms, |
| 79 | kDefaultOutlierFrameSizePercent}; |
| 80 | encode_timer.OnEncoderInit(codec_settings, false); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 81 | const size_t kFramerate = 30; |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 82 | VideoBitrateAllocation bitrate_allocation; |
| 83 | for (int si = 0; si < num_streams; ++si) { |
| 84 | bitrate_allocation.SetBitrate(si, 0, |
| 85 | average_frame_sizes[si] * 8 * kFramerate); |
| 86 | } |
| 87 | encode_timer.OnSetRates(bitrate_allocation, kFramerate); |
| 88 | |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 89 | std::vector<std::vector<FrameType>> result(num_streams); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 90 | int64_t current_timestamp = 0; |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 91 | for (int i = 0; i < num_frames; ++i) { |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 92 | current_timestamp += 1; |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 93 | VideoFrame frame = VideoFrame::Builder() |
| 94 | .set_timestamp_rtp(current_timestamp * 90) |
| 95 | .set_timestamp_ms(current_timestamp) |
| 96 | .set_video_frame_buffer(kFrameBuffer) |
| 97 | .build(); |
| 98 | encode_timer.OnEncodeStarted(frame); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 99 | for (int si = 0; si < num_streams; ++si) { |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 100 | // 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] | 101 | bool dropped = i % (5 + si) == 0; |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 102 | |
| 103 | EncodedImage image; |
Niels Möller | 663844d | 2019-02-14 16:15:54 +0100 | [diff] [blame] | 104 | image.Allocate(max_frame_size); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 105 | image.set_size(FrameSize(min_frame_size, max_frame_size, si, i)); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 106 | image.capture_time_ms_ = current_timestamp; |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 107 | image.SetTimestamp(static_cast<uint32_t>(current_timestamp * 90)); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 108 | image.SetSpatialIndex(si); |
| 109 | |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 110 | if (dropped) { |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 111 | result[si].push_back(FrameType::kDropped); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 112 | continue; |
| 113 | } |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 114 | |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 115 | encode_timer.FillTimingInfo(si, &image); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 116 | |
| 117 | if (IsTimingFrame(image)) { |
| 118 | result[si].push_back(FrameType::kTiming); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 119 | } else { |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 120 | result[si].push_back(FrameType::kNormal); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | } |
| 124 | return result; |
| 125 | } |
| 126 | } // namespace |
| 127 | |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 128 | TEST(FrameEncodeTimerTest, MarksTimingFramesPeriodicallyTogether) { |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 129 | const int64_t kDelayMs = 29; |
| 130 | const size_t kMinFrameSize = 10; |
| 131 | const size_t kMaxFrameSize = 20; |
| 132 | const int kNumFrames = 1000; |
| 133 | const int kNumStreams = 3; |
| 134 | // No outliers as 1000 is larger than anything from range [10,20]. |
| 135 | const std::vector<size_t> kAverageSize = {1000, 1000, 1000}; |
| 136 | auto frames = GetTimingFrames(kDelayMs, kMinFrameSize, kMaxFrameSize, |
| 137 | kAverageSize, kNumStreams, kNumFrames); |
| 138 | // Timing frames should be tirggered every delayMs. |
| 139 | // As no outliers are expected, frames on all streams have to be |
| 140 | // marked together. |
| 141 | int last_timing_frame = -1; |
| 142 | for (int i = 0; i < kNumFrames; ++i) { |
| 143 | int num_normal = 0; |
| 144 | int num_timing = 0; |
| 145 | int num_dropped = 0; |
| 146 | for (int s = 0; s < kNumStreams; ++s) { |
| 147 | if (frames[s][i] == FrameType::kTiming) { |
| 148 | ++num_timing; |
| 149 | } else if (frames[s][i] == FrameType::kNormal) { |
| 150 | ++num_normal; |
| 151 | } else { |
| 152 | ++num_dropped; |
| 153 | } |
| 154 | } |
| 155 | // Can't have both normal and timing frames at the same timstamp. |
| 156 | EXPECT_TRUE(num_timing == 0 || num_normal == 0); |
| 157 | if (num_dropped < kNumStreams) { |
| 158 | if (last_timing_frame == -1 || i >= last_timing_frame + kDelayMs) { |
| 159 | // If didn't have timing frames for a period, current sent frame has to |
| 160 | // be one. No normal frames should be sent. |
| 161 | EXPECT_EQ(num_normal, 0); |
| 162 | } else { |
| 163 | // No unneeded timing frames should be sent. |
| 164 | EXPECT_EQ(num_timing, 0); |
| 165 | } |
| 166 | } |
| 167 | if (num_timing > 0) |
| 168 | last_timing_frame = i; |
| 169 | } |
| 170 | } |
| 171 | |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 172 | TEST(FrameEncodeTimerTest, MarksOutliers) { |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 173 | const int64_t kDelayMs = 29; |
| 174 | const size_t kMinFrameSize = 2495; |
| 175 | const size_t kMaxFrameSize = 2505; |
| 176 | const int kNumFrames = 1000; |
| 177 | const int kNumStreams = 3; |
| 178 | // Possible outliers as 1000 lies in range [995, 1005]. |
| 179 | const std::vector<size_t> kAverageSize = {998, 1000, 1004}; |
| 180 | auto frames = GetTimingFrames(kDelayMs, kMinFrameSize, kMaxFrameSize, |
| 181 | kAverageSize, kNumStreams, kNumFrames); |
| 182 | // All outliers should be marked. |
| 183 | for (int i = 0; i < kNumFrames; ++i) { |
| 184 | for (int s = 0; s < kNumStreams; ++s) { |
| 185 | if (FrameSize(kMinFrameSize, kMaxFrameSize, s, i) >= |
| 186 | kAverageSize[s] * kDefaultOutlierFrameSizePercent / 100) { |
| 187 | // Too big frame. May be dropped or timing, but not normal. |
| 188 | EXPECT_NE(frames[s][i], FrameType::kNormal); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 194 | TEST(FrameEncodeTimerTest, NoTimingFrameIfNoEncodeStartTime) { |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 195 | int64_t timestamp = 1; |
Niels Möller | 663844d | 2019-02-14 16:15:54 +0100 | [diff] [blame] | 196 | constexpr size_t kFrameSize = 500; |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 197 | EncodedImage image; |
Niels Möller | 663844d | 2019-02-14 16:15:54 +0100 | [diff] [blame] | 198 | image.Allocate(kFrameSize); |
| 199 | image.set_size(kFrameSize); |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 200 | image.capture_time_ms_ = timestamp; |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 201 | image.SetTimestamp(static_cast<uint32_t>(timestamp * 90)); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 202 | |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 203 | FakeEncodedImageCallback sink; |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 204 | FrameEncodeMetadataWriter encode_timer(&sink); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 205 | VideoCodec codec_settings; |
| 206 | // Make all frames timing frames. |
| 207 | codec_settings.timing_frame_thresholds.delay_ms = 1; |
| 208 | encode_timer.OnEncoderInit(codec_settings, false); |
| 209 | VideoBitrateAllocation bitrate_allocation; |
| 210 | bitrate_allocation.SetBitrate(0, 0, 500000); |
| 211 | encode_timer.OnSetRates(bitrate_allocation, 30); |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 212 | |
| 213 | // Verify a single frame works with encode start time set. |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 214 | VideoFrame frame = VideoFrame::Builder() |
| 215 | .set_timestamp_ms(timestamp) |
| 216 | .set_timestamp_rtp(timestamp * 90) |
| 217 | .set_video_frame_buffer(kFrameBuffer) |
| 218 | .build(); |
| 219 | encode_timer.OnEncodeStarted(frame); |
| 220 | encode_timer.FillTimingInfo(0, &image); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 221 | EXPECT_TRUE(IsTimingFrame(image)); |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 222 | |
| 223 | // New frame, now skip OnEncodeStarted. Should not result in timing frame. |
| 224 | image.capture_time_ms_ = ++timestamp; |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 225 | image.SetTimestamp(static_cast<uint32_t>(timestamp * 90)); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 226 | image.timing_ = EncodedImage::Timing(); |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 227 | encode_timer.FillTimingInfo(0, &image); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 228 | EXPECT_FALSE(IsTimingFrame(image)); |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 231 | TEST(FrameEncodeTimerTest, AdjustsCaptureTimeForInternalSourceEncoder) { |
Ilya Nikolaevskiy | 764aeb7 | 2018-04-03 10:01:52 +0200 | [diff] [blame] | 232 | const int64_t kEncodeStartDelayMs = 2; |
| 233 | const int64_t kEncodeFinishDelayMs = 10; |
Niels Möller | 663844d | 2019-02-14 16:15:54 +0100 | [diff] [blame] | 234 | constexpr size_t kFrameSize = 500; |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 235 | |
| 236 | int64_t timestamp = 1; |
| 237 | EncodedImage image; |
Niels Möller | 663844d | 2019-02-14 16:15:54 +0100 | [diff] [blame] | 238 | image.Allocate(kFrameSize); |
| 239 | image.set_size(kFrameSize); |
Ilya Nikolaevskiy | 764aeb7 | 2018-04-03 10:01:52 +0200 | [diff] [blame] | 240 | image.capture_time_ms_ = timestamp; |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 241 | image.SetTimestamp(static_cast<uint32_t>(timestamp * 90)); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 242 | |
Ilya Nikolaevskiy | 764aeb7 | 2018-04-03 10:01:52 +0200 | [diff] [blame] | 243 | FakeEncodedImageCallback sink; |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 244 | FrameEncodeMetadataWriter encode_timer(&sink); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 245 | |
| 246 | VideoCodec codec_settings; |
| 247 | // Make all frames timing frames. |
| 248 | codec_settings.timing_frame_thresholds.delay_ms = 1; |
| 249 | encode_timer.OnEncoderInit(codec_settings, true); |
| 250 | |
| 251 | VideoBitrateAllocation bitrate_allocation; |
| 252 | bitrate_allocation.SetBitrate(0, 0, 500000); |
| 253 | encode_timer.OnSetRates(bitrate_allocation, 30); |
Ilya Nikolaevskiy | 764aeb7 | 2018-04-03 10:01:52 +0200 | [diff] [blame] | 254 | |
| 255 | // Verify a single frame without encode timestamps isn't a timing frame. |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 256 | encode_timer.FillTimingInfo(0, &image); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 257 | EXPECT_FALSE(IsTimingFrame(image)); |
Ilya Nikolaevskiy | 764aeb7 | 2018-04-03 10:01:52 +0200 | [diff] [blame] | 258 | |
| 259 | // New frame, but this time with encode timestamps set in timing_. |
| 260 | // This should be a timing frame. |
| 261 | image.capture_time_ms_ = ++timestamp; |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 262 | image.SetTimestamp(static_cast<uint32_t>(timestamp * 90)); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 263 | image.timing_ = EncodedImage::Timing(); |
Ilya Nikolaevskiy | 764aeb7 | 2018-04-03 10:01:52 +0200 | [diff] [blame] | 264 | image.timing_.encode_start_ms = timestamp + kEncodeStartDelayMs; |
| 265 | image.timing_.encode_finish_ms = timestamp + kEncodeFinishDelayMs; |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 266 | |
| 267 | encode_timer.FillTimingInfo(0, &image); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 268 | EXPECT_TRUE(IsTimingFrame(image)); |
| 269 | |
Ilya Nikolaevskiy | 764aeb7 | 2018-04-03 10:01:52 +0200 | [diff] [blame] | 270 | // Frame is captured kEncodeFinishDelayMs before it's encoded, so restored |
| 271 | // capture timestamp should be kEncodeFinishDelayMs in the past. |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 272 | EXPECT_NEAR(image.capture_time_ms_, rtc::TimeMillis() - kEncodeFinishDelayMs, |
| 273 | 1); |
Ilya Nikolaevskiy | 764aeb7 | 2018-04-03 10:01:52 +0200 | [diff] [blame] | 274 | } |
| 275 | |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 276 | TEST(FrameEncodeTimerTest, NotifiesAboutDroppedFrames) { |
Ilya Nikolaevskiy | 76f2a85 | 2017-11-16 14:33:53 +0100 | [diff] [blame] | 277 | const int64_t kTimestampMs1 = 47721840; |
| 278 | const int64_t kTimestampMs2 = 47721850; |
| 279 | const int64_t kTimestampMs3 = 47721860; |
| 280 | const int64_t kTimestampMs4 = 47721870; |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 281 | |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 282 | FakeEncodedImageCallback sink; |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 283 | FrameEncodeMetadataWriter encode_timer(&sink); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 284 | encode_timer.OnEncoderInit(VideoCodec(), false); |
Ilya Nikolaevskiy | e0da9ea | 2017-11-08 14:39:02 +0100 | [diff] [blame] | 285 | // 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] | 286 | VideoBitrateAllocation bitrate_allocation; |
| 287 | bitrate_allocation.SetBitrate(0, 0, 500000); |
| 288 | encode_timer.OnSetRates(bitrate_allocation, 30); |
| 289 | |
| 290 | EncodedImage image; |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 291 | VideoFrame frame = VideoFrame::Builder() |
| 292 | .set_timestamp_rtp(kTimestampMs1 * 90) |
| 293 | .set_timestamp_ms(kTimestampMs1) |
| 294 | .set_video_frame_buffer(kFrameBuffer) |
| 295 | .build(); |
| 296 | |
Ilya Nikolaevskiy | 76f2a85 | 2017-11-16 14:33:53 +0100 | [diff] [blame] | 297 | image.capture_time_ms_ = kTimestampMs1; |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 298 | image.SetTimestamp(static_cast<uint32_t>(image.capture_time_ms_ * 90)); |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 299 | frame.set_timestamp(image.capture_time_ms_ * 90); |
| 300 | frame.set_timestamp_us(image.capture_time_ms_ * 1000); |
| 301 | encode_timer.OnEncodeStarted(frame); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 302 | |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 303 | EXPECT_EQ(0u, sink.GetNumFramesDropped()); |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 304 | encode_timer.FillTimingInfo(0, &image); |
Ilya Nikolaevskiy | 76f2a85 | 2017-11-16 14:33:53 +0100 | [diff] [blame] | 305 | |
| 306 | image.capture_time_ms_ = kTimestampMs2; |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 307 | image.SetTimestamp(static_cast<uint32_t>(image.capture_time_ms_ * 90)); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 308 | image.timing_ = EncodedImage::Timing(); |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 309 | frame.set_timestamp(image.capture_time_ms_ * 90); |
| 310 | frame.set_timestamp_us(image.capture_time_ms_ * 1000); |
| 311 | encode_timer.OnEncodeStarted(frame); |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 312 | // No OnEncodedImageCall for timestamp2. Yet, at this moment it's not known |
| 313 | // that frame with timestamp2 was dropped. |
| 314 | EXPECT_EQ(0u, sink.GetNumFramesDropped()); |
Ilya Nikolaevskiy | 76f2a85 | 2017-11-16 14:33:53 +0100 | [diff] [blame] | 315 | |
| 316 | image.capture_time_ms_ = kTimestampMs3; |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 317 | image.SetTimestamp(static_cast<uint32_t>(image.capture_time_ms_ * 90)); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 318 | image.timing_ = EncodedImage::Timing(); |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 319 | frame.set_timestamp(image.capture_time_ms_ * 90); |
| 320 | frame.set_timestamp_us(image.capture_time_ms_ * 1000); |
| 321 | encode_timer.OnEncodeStarted(frame); |
| 322 | encode_timer.FillTimingInfo(0, &image); |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 323 | EXPECT_EQ(1u, sink.GetNumFramesDropped()); |
Ilya Nikolaevskiy | 76f2a85 | 2017-11-16 14:33:53 +0100 | [diff] [blame] | 324 | |
| 325 | image.capture_time_ms_ = kTimestampMs4; |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 326 | image.SetTimestamp(static_cast<uint32_t>(image.capture_time_ms_ * 90)); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 327 | image.timing_ = EncodedImage::Timing(); |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 328 | frame.set_timestamp(image.capture_time_ms_ * 90); |
| 329 | frame.set_timestamp_us(image.capture_time_ms_ * 1000); |
| 330 | encode_timer.OnEncodeStarted(frame); |
| 331 | encode_timer.FillTimingInfo(0, &image); |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 332 | EXPECT_EQ(1u, sink.GetNumFramesDropped()); |
| 333 | } |
| 334 | |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 335 | TEST(FrameEncodeTimerTest, RestoresCaptureTimestamps) { |
Ilya Nikolaevskiy | 76f2a85 | 2017-11-16 14:33:53 +0100 | [diff] [blame] | 336 | EncodedImage image; |
Ilya Nikolaevskiy | 76f2a85 | 2017-11-16 14:33:53 +0100 | [diff] [blame] | 337 | const int64_t kTimestampMs = 123456; |
Ilya Nikolaevskiy | 76f2a85 | 2017-11-16 14:33:53 +0100 | [diff] [blame] | 338 | FakeEncodedImageCallback sink; |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 339 | |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 340 | FrameEncodeMetadataWriter encode_timer(&sink); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 341 | encode_timer.OnEncoderInit(VideoCodec(), false); |
Ilya Nikolaevskiy | 76f2a85 | 2017-11-16 14:33:53 +0100 | [diff] [blame] | 342 | // 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] | 343 | VideoBitrateAllocation bitrate_allocation; |
| 344 | bitrate_allocation.SetBitrate(0, 0, 500000); |
| 345 | encode_timer.OnSetRates(bitrate_allocation, 30); |
| 346 | |
| 347 | image.capture_time_ms_ = kTimestampMs; // Correct timestamp. |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 348 | image.SetTimestamp(static_cast<uint32_t>(image.capture_time_ms_ * 90)); |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 349 | VideoFrame frame = VideoFrame::Builder() |
| 350 | .set_timestamp_ms(image.capture_time_ms_) |
| 351 | .set_timestamp_rtp(image.capture_time_ms_ * 90) |
| 352 | .set_video_frame_buffer(kFrameBuffer) |
| 353 | .build(); |
| 354 | encode_timer.OnEncodeStarted(frame); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 355 | image.capture_time_ms_ = 0; // Incorrect timestamp. |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 356 | encode_timer.FillTimingInfo(0, &image); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 357 | EXPECT_EQ(kTimestampMs, image.capture_time_ms_); |
Ilya Nikolaevskiy | 76f2a85 | 2017-11-16 14:33:53 +0100 | [diff] [blame] | 358 | } |
| 359 | |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 360 | TEST(FrameEncodeTimerTest, CopiesRotation) { |
| 361 | EncodedImage image; |
| 362 | const int64_t kTimestampMs = 123456; |
| 363 | FakeEncodedImageCallback sink; |
| 364 | |
| 365 | FrameEncodeMetadataWriter encode_timer(&sink); |
| 366 | encode_timer.OnEncoderInit(VideoCodec(), false); |
| 367 | // Any non-zero bitrate needed to be set before the first frame. |
| 368 | VideoBitrateAllocation bitrate_allocation; |
| 369 | bitrate_allocation.SetBitrate(0, 0, 500000); |
| 370 | encode_timer.OnSetRates(bitrate_allocation, 30); |
| 371 | |
| 372 | image.SetTimestamp(static_cast<uint32_t>(kTimestampMs * 90)); |
| 373 | VideoFrame frame = VideoFrame::Builder() |
| 374 | .set_timestamp_ms(kTimestampMs) |
| 375 | .set_timestamp_rtp(kTimestampMs * 90) |
| 376 | .set_rotation(kVideoRotation_180) |
| 377 | .set_video_frame_buffer(kFrameBuffer) |
| 378 | .build(); |
| 379 | encode_timer.OnEncodeStarted(frame); |
| 380 | encode_timer.FillTimingInfo(0, &image); |
| 381 | EXPECT_EQ(kVideoRotation_180, image.rotation_); |
| 382 | } |
| 383 | |
| 384 | TEST(FrameEncodeTimerTest, SetsContentType) { |
| 385 | EncodedImage image; |
| 386 | const int64_t kTimestampMs = 123456; |
| 387 | FakeEncodedImageCallback sink; |
| 388 | |
| 389 | FrameEncodeMetadataWriter encode_timer(&sink); |
| 390 | VideoCodec codec; |
| 391 | codec.mode = VideoCodecMode::kScreensharing; |
| 392 | encode_timer.OnEncoderInit(codec, false); |
| 393 | // Any non-zero bitrate needed to be set before the first frame. |
| 394 | VideoBitrateAllocation bitrate_allocation; |
| 395 | bitrate_allocation.SetBitrate(0, 0, 500000); |
| 396 | encode_timer.OnSetRates(bitrate_allocation, 30); |
| 397 | |
| 398 | image.SetTimestamp(static_cast<uint32_t>(kTimestampMs * 90)); |
| 399 | VideoFrame frame = VideoFrame::Builder() |
| 400 | .set_timestamp_ms(kTimestampMs) |
| 401 | .set_timestamp_rtp(kTimestampMs * 90) |
| 402 | .set_rotation(kVideoRotation_180) |
| 403 | .set_video_frame_buffer(kFrameBuffer) |
| 404 | .build(); |
| 405 | encode_timer.OnEncodeStarted(frame); |
| 406 | encode_timer.FillTimingInfo(0, &image); |
| 407 | EXPECT_EQ(VideoContentType::SCREENSHARE, image.content_type_); |
| 408 | } |
| 409 | |
| 410 | TEST(FrameEncodeTimerTest, CopiesColorSpace) { |
| 411 | EncodedImage image; |
| 412 | const int64_t kTimestampMs = 123456; |
| 413 | FakeEncodedImageCallback sink; |
| 414 | |
| 415 | FrameEncodeMetadataWriter encode_timer(&sink); |
| 416 | encode_timer.OnEncoderInit(VideoCodec(), false); |
| 417 | // Any non-zero bitrate needed to be set before the first frame. |
| 418 | VideoBitrateAllocation bitrate_allocation; |
| 419 | bitrate_allocation.SetBitrate(0, 0, 500000); |
| 420 | encode_timer.OnSetRates(bitrate_allocation, 30); |
| 421 | |
| 422 | webrtc::ColorSpace color_space = |
| 423 | CreateTestColorSpace(/*with_hdr_metadata=*/true); |
| 424 | image.SetTimestamp(static_cast<uint32_t>(kTimestampMs * 90)); |
| 425 | VideoFrame frame = VideoFrame::Builder() |
| 426 | .set_timestamp_ms(kTimestampMs) |
| 427 | .set_timestamp_rtp(kTimestampMs * 90) |
| 428 | .set_color_space(color_space) |
| 429 | .set_video_frame_buffer(kFrameBuffer) |
| 430 | .build(); |
| 431 | encode_timer.OnEncodeStarted(frame); |
| 432 | encode_timer.FillTimingInfo(0, &image); |
| 433 | ASSERT_NE(image.ColorSpace(), nullptr); |
| 434 | EXPECT_EQ(color_space, *image.ColorSpace()); |
| 435 | } |
| 436 | |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 437 | } // namespace test |
| 438 | } // namespace webrtc |