blob: 0be0c7591395d7ebd40ea2283a5513530eeded76 [file] [log] [blame]
ilnik04f4d122017-06-19 07:18:55 -07001/*
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
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 Gerey3e707812018-11-28 16:47:49 +010011#include <cstddef>
ilnik04f4d122017-06-19 07:18:55 -070012#include <vector>
13
Yves Gerey3e707812018-11-28 16:47:49 +010014#include "api/video/video_timing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/video_coding/generic_encoder.h"
16#include "modules/video_coding/include/video_coding_defines.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/fake_clock.h"
18#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "test/gtest.h"
ilnik04f4d122017-06-19 07:18:55 -070020
21namespace webrtc {
22namespace test {
23namespace {
24inline size_t FrameSize(const size_t& min_frame_size,
25 const size_t& max_frame_size,
26 const int& s,
27 const int& i) {
28 return min_frame_size + (s + 1) * i % (max_frame_size - min_frame_size);
29}
30
31class FakeEncodedImageCallback : public EncodedImageCallback {
32 public:
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020033 FakeEncodedImageCallback()
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +010034 : last_frame_was_timing_(false),
35 num_frames_dropped_(0),
36 last_capture_timestamp_(-1) {}
ilnik04f4d122017-06-19 07:18:55 -070037 Result OnEncodedImage(const EncodedImage& encoded_image,
38 const CodecSpecificInfo* codec_specific_info,
39 const RTPFragmentationHeader* fragmentation) override {
sprangba050a62017-08-18 02:51:12 -070040 last_frame_was_timing_ =
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +020041 encoded_image.timing_.flags != VideoSendTiming::kInvalid &&
42 encoded_image.timing_.flags != VideoSendTiming::kNotTriggered;
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +010043 last_capture_timestamp_ = encoded_image.capture_time_ms_;
mflodman351424e2017-08-10 02:43:14 -070044 return Result(Result::OK);
ilnik04f4d122017-06-19 07:18:55 -070045 };
46
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020047 void OnDroppedFrame(DropReason reason) override { ++num_frames_dropped_; }
48
ilnik04f4d122017-06-19 07:18:55 -070049 bool WasTimingFrame() { return last_frame_was_timing_; }
50
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020051 size_t GetNumFramesDropped() { return num_frames_dropped_; }
52
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +010053 int64_t GetLastCaptureTimestamp() { return last_capture_timestamp_; }
54
ilnik04f4d122017-06-19 07:18:55 -070055 private:
56 bool last_frame_was_timing_;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020057 size_t num_frames_dropped_;
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +010058 int64_t last_capture_timestamp_;
ilnik04f4d122017-06-19 07:18:55 -070059};
60
61enum class FrameType {
62 kNormal,
63 kTiming,
64 kDropped,
65};
66
67// Emulates |num_frames| on |num_streams| frames with capture timestamps
68// increased by 1 from 0. Size of each frame is between
69// |min_frame_size| and |max_frame_size|, outliers are counted relatevely to
70// |average_frame_sizes[]| for each stream.
71std::vector<std::vector<FrameType>> GetTimingFrames(
72 const int64_t delay_ms,
73 const size_t min_frame_size,
74 const size_t max_frame_size,
75 std::vector<size_t> average_frame_sizes,
76 const int num_streams,
77 const int num_frames) {
78 FakeEncodedImageCallback sink;
Niels Möller6bb5ab92019-01-11 11:11:10 +010079 VCMEncodedFrameCallback callback(&sink);
ilnik04f4d122017-06-19 07:18:55 -070080 const size_t kFramerate = 30;
81 callback.SetTimingFramesThresholds(
82 {delay_ms, kDefaultOutlierFrameSizePercent});
83 callback.OnFrameRateChanged(kFramerate);
84 int s, i;
85 std::vector<std::vector<FrameType>> result(num_streams);
86 for (s = 0; s < num_streams; ++s)
87 callback.OnTargetBitrateChanged(average_frame_sizes[s] * kFramerate, s);
88 int64_t current_timestamp = 0;
89 for (i = 0; i < num_frames; ++i) {
90 current_timestamp += 1;
91 for (s = 0; s < num_streams; ++s) {
92 // every (5+s)-th frame is dropped on s-th stream by design.
93 bool dropped = i % (5 + s) == 0;
94
95 EncodedImage image;
96 CodecSpecificInfo codec_specific;
Niels Möller663844d2019-02-14 16:15:54 +010097 image.Allocate(max_frame_size);
Niels Möller77536a22019-01-15 08:50:01 +010098 image.set_size(FrameSize(min_frame_size, max_frame_size, s, i));
ilnik04f4d122017-06-19 07:18:55 -070099 image.capture_time_ms_ = current_timestamp;
Niels Möller23775882018-08-16 10:24:12 +0200100 image.SetTimestamp(static_cast<uint32_t>(current_timestamp * 90));
Niels Möllerd3b8c632018-08-27 15:33:42 +0200101 image.SetSpatialIndex(s);
ilnik04f4d122017-06-19 07:18:55 -0700102 codec_specific.codecType = kVideoCodecGeneric;
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100103 callback.OnEncodeStarted(static_cast<uint32_t>(current_timestamp * 90),
104 current_timestamp, s);
ilnik04f4d122017-06-19 07:18:55 -0700105 if (dropped) {
106 result[s].push_back(FrameType::kDropped);
107 continue;
108 }
109 callback.OnEncodedImage(image, &codec_specific, nullptr);
110 if (sink.WasTimingFrame()) {
111 result[s].push_back(FrameType::kTiming);
112 } else {
113 result[s].push_back(FrameType::kNormal);
114 }
115 }
116 }
117 return result;
118}
119} // namespace
120
121TEST(TestVCMEncodedFrameCallback, MarksTimingFramesPeriodicallyTogether) {
122 const int64_t kDelayMs = 29;
123 const size_t kMinFrameSize = 10;
124 const size_t kMaxFrameSize = 20;
125 const int kNumFrames = 1000;
126 const int kNumStreams = 3;
127 // No outliers as 1000 is larger than anything from range [10,20].
128 const std::vector<size_t> kAverageSize = {1000, 1000, 1000};
129 auto frames = GetTimingFrames(kDelayMs, kMinFrameSize, kMaxFrameSize,
130 kAverageSize, kNumStreams, kNumFrames);
131 // Timing frames should be tirggered every delayMs.
132 // As no outliers are expected, frames on all streams have to be
133 // marked together.
134 int last_timing_frame = -1;
135 for (int i = 0; i < kNumFrames; ++i) {
136 int num_normal = 0;
137 int num_timing = 0;
138 int num_dropped = 0;
139 for (int s = 0; s < kNumStreams; ++s) {
140 if (frames[s][i] == FrameType::kTiming) {
141 ++num_timing;
142 } else if (frames[s][i] == FrameType::kNormal) {
143 ++num_normal;
144 } else {
145 ++num_dropped;
146 }
147 }
148 // Can't have both normal and timing frames at the same timstamp.
149 EXPECT_TRUE(num_timing == 0 || num_normal == 0);
150 if (num_dropped < kNumStreams) {
151 if (last_timing_frame == -1 || i >= last_timing_frame + kDelayMs) {
152 // If didn't have timing frames for a period, current sent frame has to
153 // be one. No normal frames should be sent.
154 EXPECT_EQ(num_normal, 0);
155 } else {
156 // No unneeded timing frames should be sent.
157 EXPECT_EQ(num_timing, 0);
158 }
159 }
160 if (num_timing > 0)
161 last_timing_frame = i;
162 }
163}
164
165TEST(TestVCMEncodedFrameCallback, MarksOutliers) {
166 const int64_t kDelayMs = 29;
167 const size_t kMinFrameSize = 2495;
168 const size_t kMaxFrameSize = 2505;
169 const int kNumFrames = 1000;
170 const int kNumStreams = 3;
171 // Possible outliers as 1000 lies in range [995, 1005].
172 const std::vector<size_t> kAverageSize = {998, 1000, 1004};
173 auto frames = GetTimingFrames(kDelayMs, kMinFrameSize, kMaxFrameSize,
174 kAverageSize, kNumStreams, kNumFrames);
175 // All outliers should be marked.
176 for (int i = 0; i < kNumFrames; ++i) {
177 for (int s = 0; s < kNumStreams; ++s) {
178 if (FrameSize(kMinFrameSize, kMaxFrameSize, s, i) >=
179 kAverageSize[s] * kDefaultOutlierFrameSizePercent / 100) {
180 // Too big frame. May be dropped or timing, but not normal.
181 EXPECT_NE(frames[s][i], FrameType::kNormal);
182 }
183 }
184 }
185}
186
sprangba050a62017-08-18 02:51:12 -0700187TEST(TestVCMEncodedFrameCallback, NoTimingFrameIfNoEncodeStartTime) {
188 EncodedImage image;
189 CodecSpecificInfo codec_specific;
190 int64_t timestamp = 1;
Niels Möller663844d2019-02-14 16:15:54 +0100191 constexpr size_t kFrameSize = 500;
192 image.Allocate(kFrameSize);
193 image.set_size(kFrameSize);
sprangba050a62017-08-18 02:51:12 -0700194 image.capture_time_ms_ = timestamp;
Niels Möller23775882018-08-16 10:24:12 +0200195 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
sprangba050a62017-08-18 02:51:12 -0700196 codec_specific.codecType = kVideoCodecGeneric;
sprangba050a62017-08-18 02:51:12 -0700197 FakeEncodedImageCallback sink;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100198 VCMEncodedFrameCallback callback(&sink);
sprangba050a62017-08-18 02:51:12 -0700199 VideoCodec::TimingFrameTriggerThresholds thresholds;
200 thresholds.delay_ms = 1; // Make all frames timing frames.
201 callback.SetTimingFramesThresholds(thresholds);
Ilya Nikolaevskiye0da9ea2017-11-08 14:39:02 +0100202 callback.OnTargetBitrateChanged(500, 0);
sprangba050a62017-08-18 02:51:12 -0700203
204 // Verify a single frame works with encode start time set.
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100205 callback.OnEncodeStarted(static_cast<uint32_t>(timestamp * 90), timestamp, 0);
sprangba050a62017-08-18 02:51:12 -0700206 callback.OnEncodedImage(image, &codec_specific, nullptr);
207 EXPECT_TRUE(sink.WasTimingFrame());
208
209 // New frame, now skip OnEncodeStarted. Should not result in timing frame.
210 image.capture_time_ms_ = ++timestamp;
Niels Möller23775882018-08-16 10:24:12 +0200211 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
sprangba050a62017-08-18 02:51:12 -0700212 callback.OnEncodedImage(image, &codec_specific, nullptr);
213 EXPECT_FALSE(sink.WasTimingFrame());
214}
215
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200216TEST(TestVCMEncodedFrameCallback, AdjustsCaptureTimeForInternalSourceEncoder) {
217 rtc::ScopedFakeClock clock;
218 clock.SetTimeMicros(1234567);
219 EncodedImage image;
220 CodecSpecificInfo codec_specific;
221 const int64_t kEncodeStartDelayMs = 2;
222 const int64_t kEncodeFinishDelayMs = 10;
223 int64_t timestamp = 1;
Niels Möller663844d2019-02-14 16:15:54 +0100224 constexpr size_t kFrameSize = 500;
225 image.Allocate(kFrameSize);
226 image.set_size(kFrameSize);
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200227 image.capture_time_ms_ = timestamp;
Niels Möller23775882018-08-16 10:24:12 +0200228 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200229 codec_specific.codecType = kVideoCodecGeneric;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200230 FakeEncodedImageCallback sink;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100231 VCMEncodedFrameCallback callback(&sink);
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200232 callback.SetInternalSource(true);
233 VideoCodec::TimingFrameTriggerThresholds thresholds;
234 thresholds.delay_ms = 1; // Make all frames timing frames.
235 callback.SetTimingFramesThresholds(thresholds);
236 callback.OnTargetBitrateChanged(500, 0);
237
238 // Verify a single frame without encode timestamps isn't a timing frame.
239 callback.OnEncodedImage(image, &codec_specific, nullptr);
240 EXPECT_FALSE(sink.WasTimingFrame());
241
242 // New frame, but this time with encode timestamps set in timing_.
243 // This should be a timing frame.
244 image.capture_time_ms_ = ++timestamp;
Niels Möller23775882018-08-16 10:24:12 +0200245 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200246 image.timing_.encode_start_ms = timestamp + kEncodeStartDelayMs;
247 image.timing_.encode_finish_ms = timestamp + kEncodeFinishDelayMs;
248 callback.OnEncodedImage(image, &codec_specific, nullptr);
249 EXPECT_TRUE(sink.WasTimingFrame());
250 // Frame is captured kEncodeFinishDelayMs before it's encoded, so restored
251 // capture timestamp should be kEncodeFinishDelayMs in the past.
252 EXPECT_EQ(
253 sink.GetLastCaptureTimestamp(),
254 clock.TimeNanos() / rtc::kNumNanosecsPerMillisec - kEncodeFinishDelayMs);
255}
256
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200257TEST(TestVCMEncodedFrameCallback, NotifiesAboutDroppedFrames) {
258 EncodedImage image;
259 CodecSpecificInfo codec_specific;
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100260 const int64_t kTimestampMs1 = 47721840;
261 const int64_t kTimestampMs2 = 47721850;
262 const int64_t kTimestampMs3 = 47721860;
263 const int64_t kTimestampMs4 = 47721870;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200264 codec_specific.codecType = kVideoCodecGeneric;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200265 FakeEncodedImageCallback sink;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100266 VCMEncodedFrameCallback callback(&sink);
Ilya Nikolaevskiye0da9ea2017-11-08 14:39:02 +0100267 // Any non-zero bitrate needed to be set before the first frame.
268 callback.OnTargetBitrateChanged(500, 0);
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100269 image.capture_time_ms_ = kTimestampMs1;
Niels Möller23775882018-08-16 10:24:12 +0200270 image.SetTimestamp(static_cast<uint32_t>(image.capture_time_ms_ * 90));
271 callback.OnEncodeStarted(image.Timestamp(), image.capture_time_ms_, 0);
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200272 EXPECT_EQ(0u, sink.GetNumFramesDropped());
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200273 callback.OnEncodedImage(image, &codec_specific, nullptr);
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100274
275 image.capture_time_ms_ = kTimestampMs2;
Niels Möller23775882018-08-16 10:24:12 +0200276 image.SetTimestamp(static_cast<uint32_t>(image.capture_time_ms_ * 90));
277 callback.OnEncodeStarted(image.Timestamp(), image.capture_time_ms_, 0);
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200278 // No OnEncodedImageCall for timestamp2. Yet, at this moment it's not known
279 // that frame with timestamp2 was dropped.
280 EXPECT_EQ(0u, sink.GetNumFramesDropped());
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100281
282 image.capture_time_ms_ = kTimestampMs3;
Niels Möller23775882018-08-16 10:24:12 +0200283 image.SetTimestamp(static_cast<uint32_t>(image.capture_time_ms_ * 90));
284 callback.OnEncodeStarted(image.Timestamp(), image.capture_time_ms_, 0);
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200285 callback.OnEncodedImage(image, &codec_specific, nullptr);
286 EXPECT_EQ(1u, sink.GetNumFramesDropped());
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100287
288 image.capture_time_ms_ = kTimestampMs4;
Niels Möller23775882018-08-16 10:24:12 +0200289 image.SetTimestamp(static_cast<uint32_t>(image.capture_time_ms_ * 90));
290 callback.OnEncodeStarted(image.Timestamp(), image.capture_time_ms_, 0);
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200291 callback.OnEncodedImage(image, &codec_specific, nullptr);
292 EXPECT_EQ(1u, sink.GetNumFramesDropped());
293}
294
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100295TEST(TestVCMEncodedFrameCallback, RestoresCaptureTimestamps) {
296 EncodedImage image;
297 CodecSpecificInfo codec_specific;
298 const int64_t kTimestampMs = 123456;
299 codec_specific.codecType = kVideoCodecGeneric;
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100300 FakeEncodedImageCallback sink;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100301 VCMEncodedFrameCallback callback(&sink);
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100302 // Any non-zero bitrate needed to be set before the first frame.
303 callback.OnTargetBitrateChanged(500, 0);
304 image.capture_time_ms_ = kTimestampMs; // Incorrect timesetamp.
Niels Möller23775882018-08-16 10:24:12 +0200305 image.SetTimestamp(static_cast<uint32_t>(image.capture_time_ms_ * 90));
306 callback.OnEncodeStarted(image.Timestamp(), image.capture_time_ms_, 0);
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100307 image.capture_time_ms_ = 0; // Incorrect timesetamp.
308 callback.OnEncodedImage(image, &codec_specific, nullptr);
309 EXPECT_EQ(kTimestampMs, sink.GetLastCaptureTimestamp());
310}
311
ilnik04f4d122017-06-19 07:18:55 -0700312} // namespace test
313} // namespace webrtc