blob: 66d787322f3865557282a38e8acf1ea409177497 [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;
97 image._length = FrameSize(min_frame_size, max_frame_size, s, i);
98 image.capture_time_ms_ = current_timestamp;
Niels Möller23775882018-08-16 10:24:12 +020099 image.SetTimestamp(static_cast<uint32_t>(current_timestamp * 90));
Niels Möllerd3b8c632018-08-27 15:33:42 +0200100 image.SetSpatialIndex(s);
ilnik04f4d122017-06-19 07:18:55 -0700101 codec_specific.codecType = kVideoCodecGeneric;
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100102 callback.OnEncodeStarted(static_cast<uint32_t>(current_timestamp * 90),
103 current_timestamp, s);
ilnik04f4d122017-06-19 07:18:55 -0700104 if (dropped) {
105 result[s].push_back(FrameType::kDropped);
106 continue;
107 }
108 callback.OnEncodedImage(image, &codec_specific, nullptr);
109 if (sink.WasTimingFrame()) {
110 result[s].push_back(FrameType::kTiming);
111 } else {
112 result[s].push_back(FrameType::kNormal);
113 }
114 }
115 }
116 return result;
117}
118} // namespace
119
120TEST(TestVCMEncodedFrameCallback, MarksTimingFramesPeriodicallyTogether) {
121 const int64_t kDelayMs = 29;
122 const size_t kMinFrameSize = 10;
123 const size_t kMaxFrameSize = 20;
124 const int kNumFrames = 1000;
125 const int kNumStreams = 3;
126 // No outliers as 1000 is larger than anything from range [10,20].
127 const std::vector<size_t> kAverageSize = {1000, 1000, 1000};
128 auto frames = GetTimingFrames(kDelayMs, kMinFrameSize, kMaxFrameSize,
129 kAverageSize, kNumStreams, kNumFrames);
130 // Timing frames should be tirggered every delayMs.
131 // As no outliers are expected, frames on all streams have to be
132 // marked together.
133 int last_timing_frame = -1;
134 for (int i = 0; i < kNumFrames; ++i) {
135 int num_normal = 0;
136 int num_timing = 0;
137 int num_dropped = 0;
138 for (int s = 0; s < kNumStreams; ++s) {
139 if (frames[s][i] == FrameType::kTiming) {
140 ++num_timing;
141 } else if (frames[s][i] == FrameType::kNormal) {
142 ++num_normal;
143 } else {
144 ++num_dropped;
145 }
146 }
147 // Can't have both normal and timing frames at the same timstamp.
148 EXPECT_TRUE(num_timing == 0 || num_normal == 0);
149 if (num_dropped < kNumStreams) {
150 if (last_timing_frame == -1 || i >= last_timing_frame + kDelayMs) {
151 // If didn't have timing frames for a period, current sent frame has to
152 // be one. No normal frames should be sent.
153 EXPECT_EQ(num_normal, 0);
154 } else {
155 // No unneeded timing frames should be sent.
156 EXPECT_EQ(num_timing, 0);
157 }
158 }
159 if (num_timing > 0)
160 last_timing_frame = i;
161 }
162}
163
164TEST(TestVCMEncodedFrameCallback, MarksOutliers) {
165 const int64_t kDelayMs = 29;
166 const size_t kMinFrameSize = 2495;
167 const size_t kMaxFrameSize = 2505;
168 const int kNumFrames = 1000;
169 const int kNumStreams = 3;
170 // Possible outliers as 1000 lies in range [995, 1005].
171 const std::vector<size_t> kAverageSize = {998, 1000, 1004};
172 auto frames = GetTimingFrames(kDelayMs, kMinFrameSize, kMaxFrameSize,
173 kAverageSize, kNumStreams, kNumFrames);
174 // All outliers should be marked.
175 for (int i = 0; i < kNumFrames; ++i) {
176 for (int s = 0; s < kNumStreams; ++s) {
177 if (FrameSize(kMinFrameSize, kMaxFrameSize, s, i) >=
178 kAverageSize[s] * kDefaultOutlierFrameSizePercent / 100) {
179 // Too big frame. May be dropped or timing, but not normal.
180 EXPECT_NE(frames[s][i], FrameType::kNormal);
181 }
182 }
183 }
184}
185
sprangba050a62017-08-18 02:51:12 -0700186TEST(TestVCMEncodedFrameCallback, NoTimingFrameIfNoEncodeStartTime) {
187 EncodedImage image;
188 CodecSpecificInfo codec_specific;
189 int64_t timestamp = 1;
190 image._length = 500;
191 image.capture_time_ms_ = timestamp;
Niels Möller23775882018-08-16 10:24:12 +0200192 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
sprangba050a62017-08-18 02:51:12 -0700193 codec_specific.codecType = kVideoCodecGeneric;
sprangba050a62017-08-18 02:51:12 -0700194 FakeEncodedImageCallback sink;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100195 VCMEncodedFrameCallback callback(&sink);
sprangba050a62017-08-18 02:51:12 -0700196 VideoCodec::TimingFrameTriggerThresholds thresholds;
197 thresholds.delay_ms = 1; // Make all frames timing frames.
198 callback.SetTimingFramesThresholds(thresholds);
Ilya Nikolaevskiye0da9ea2017-11-08 14:39:02 +0100199 callback.OnTargetBitrateChanged(500, 0);
sprangba050a62017-08-18 02:51:12 -0700200
201 // Verify a single frame works with encode start time set.
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100202 callback.OnEncodeStarted(static_cast<uint32_t>(timestamp * 90), timestamp, 0);
sprangba050a62017-08-18 02:51:12 -0700203 callback.OnEncodedImage(image, &codec_specific, nullptr);
204 EXPECT_TRUE(sink.WasTimingFrame());
205
206 // New frame, now skip OnEncodeStarted. Should not result in timing frame.
207 image.capture_time_ms_ = ++timestamp;
Niels Möller23775882018-08-16 10:24:12 +0200208 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
sprangba050a62017-08-18 02:51:12 -0700209 callback.OnEncodedImage(image, &codec_specific, nullptr);
210 EXPECT_FALSE(sink.WasTimingFrame());
211}
212
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200213TEST(TestVCMEncodedFrameCallback, AdjustsCaptureTimeForInternalSourceEncoder) {
214 rtc::ScopedFakeClock clock;
215 clock.SetTimeMicros(1234567);
216 EncodedImage image;
217 CodecSpecificInfo codec_specific;
218 const int64_t kEncodeStartDelayMs = 2;
219 const int64_t kEncodeFinishDelayMs = 10;
220 int64_t timestamp = 1;
221 image._length = 500;
222 image.capture_time_ms_ = timestamp;
Niels Möller23775882018-08-16 10:24:12 +0200223 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200224 codec_specific.codecType = kVideoCodecGeneric;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200225 FakeEncodedImageCallback sink;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100226 VCMEncodedFrameCallback callback(&sink);
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200227 callback.SetInternalSource(true);
228 VideoCodec::TimingFrameTriggerThresholds thresholds;
229 thresholds.delay_ms = 1; // Make all frames timing frames.
230 callback.SetTimingFramesThresholds(thresholds);
231 callback.OnTargetBitrateChanged(500, 0);
232
233 // Verify a single frame without encode timestamps isn't a timing frame.
234 callback.OnEncodedImage(image, &codec_specific, nullptr);
235 EXPECT_FALSE(sink.WasTimingFrame());
236
237 // New frame, but this time with encode timestamps set in timing_.
238 // This should be a timing frame.
239 image.capture_time_ms_ = ++timestamp;
Niels Möller23775882018-08-16 10:24:12 +0200240 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200241 image.timing_.encode_start_ms = timestamp + kEncodeStartDelayMs;
242 image.timing_.encode_finish_ms = timestamp + kEncodeFinishDelayMs;
243 callback.OnEncodedImage(image, &codec_specific, nullptr);
244 EXPECT_TRUE(sink.WasTimingFrame());
245 // Frame is captured kEncodeFinishDelayMs before it's encoded, so restored
246 // capture timestamp should be kEncodeFinishDelayMs in the past.
247 EXPECT_EQ(
248 sink.GetLastCaptureTimestamp(),
249 clock.TimeNanos() / rtc::kNumNanosecsPerMillisec - kEncodeFinishDelayMs);
250}
251
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200252TEST(TestVCMEncodedFrameCallback, NotifiesAboutDroppedFrames) {
253 EncodedImage image;
254 CodecSpecificInfo codec_specific;
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100255 const int64_t kTimestampMs1 = 47721840;
256 const int64_t kTimestampMs2 = 47721850;
257 const int64_t kTimestampMs3 = 47721860;
258 const int64_t kTimestampMs4 = 47721870;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200259 codec_specific.codecType = kVideoCodecGeneric;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200260 FakeEncodedImageCallback sink;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100261 VCMEncodedFrameCallback callback(&sink);
Ilya Nikolaevskiye0da9ea2017-11-08 14:39:02 +0100262 // Any non-zero bitrate needed to be set before the first frame.
263 callback.OnTargetBitrateChanged(500, 0);
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100264 image.capture_time_ms_ = kTimestampMs1;
Niels Möller23775882018-08-16 10:24:12 +0200265 image.SetTimestamp(static_cast<uint32_t>(image.capture_time_ms_ * 90));
266 callback.OnEncodeStarted(image.Timestamp(), image.capture_time_ms_, 0);
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200267 EXPECT_EQ(0u, sink.GetNumFramesDropped());
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200268 callback.OnEncodedImage(image, &codec_specific, nullptr);
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100269
270 image.capture_time_ms_ = kTimestampMs2;
Niels Möller23775882018-08-16 10:24:12 +0200271 image.SetTimestamp(static_cast<uint32_t>(image.capture_time_ms_ * 90));
272 callback.OnEncodeStarted(image.Timestamp(), image.capture_time_ms_, 0);
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200273 // No OnEncodedImageCall for timestamp2. Yet, at this moment it's not known
274 // that frame with timestamp2 was dropped.
275 EXPECT_EQ(0u, sink.GetNumFramesDropped());
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100276
277 image.capture_time_ms_ = kTimestampMs3;
Niels Möller23775882018-08-16 10:24:12 +0200278 image.SetTimestamp(static_cast<uint32_t>(image.capture_time_ms_ * 90));
279 callback.OnEncodeStarted(image.Timestamp(), image.capture_time_ms_, 0);
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200280 callback.OnEncodedImage(image, &codec_specific, nullptr);
281 EXPECT_EQ(1u, sink.GetNumFramesDropped());
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100282
283 image.capture_time_ms_ = kTimestampMs4;
Niels Möller23775882018-08-16 10:24:12 +0200284 image.SetTimestamp(static_cast<uint32_t>(image.capture_time_ms_ * 90));
285 callback.OnEncodeStarted(image.Timestamp(), image.capture_time_ms_, 0);
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200286 callback.OnEncodedImage(image, &codec_specific, nullptr);
287 EXPECT_EQ(1u, sink.GetNumFramesDropped());
288}
289
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100290TEST(TestVCMEncodedFrameCallback, RestoresCaptureTimestamps) {
291 EncodedImage image;
292 CodecSpecificInfo codec_specific;
293 const int64_t kTimestampMs = 123456;
294 codec_specific.codecType = kVideoCodecGeneric;
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100295 FakeEncodedImageCallback sink;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100296 VCMEncodedFrameCallback callback(&sink);
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100297 // Any non-zero bitrate needed to be set before the first frame.
298 callback.OnTargetBitrateChanged(500, 0);
299 image.capture_time_ms_ = kTimestampMs; // Incorrect timesetamp.
Niels Möller23775882018-08-16 10:24:12 +0200300 image.SetTimestamp(static_cast<uint32_t>(image.capture_time_ms_ * 90));
301 callback.OnEncodeStarted(image.Timestamp(), image.capture_time_ms_, 0);
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100302 image.capture_time_ms_ = 0; // Incorrect timesetamp.
303 callback.OnEncodedImage(image, &codec_specific, nullptr);
304 EXPECT_EQ(kTimestampMs, sink.GetLastCaptureTimestamp());
305}
306
ilnik04f4d122017-06-19 07:18:55 -0700307} // namespace test
308} // namespace webrtc