webrtc/modules/video_coding/codecs/test/video_codec_analyzer_unittest.cc
Sergey Silkin 5dd493b3da Do not use PostDelayedTask in video codec tester's pacer
PostDelayedTask doesn't guarantee task execution order. For example,
if you post two tasks, A and B, back-to-back using the same delay
there is no guarantee that A will be executed before B.

Re-implemented pacing using sleep(). Changed pacer to compute task
scheduled time instead of delay. Sleep time is calculated right before
task start. This provides better accuracy by accounting for any delays
that may happen after pacing time is computed and before task queue is
ready to run the task.

It is tricky to implement pacer tests using simulated clocks. The test
use system time which make them flacky on low performance bots. Keep
the test disabled by default.

Bug: b/261160916, webrtc:14852
Change-Id: I88e1a2001e6d33cf3bb7fe16730ec28abf90acc8
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/291804
Reviewed-by: Rasmus Brandt <brandtr@webrtc.org>
Commit-Queue: Sergey Silkin <ssilkin@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#39302}
2023-02-13 15:07:16 +00:00

127 lines
3.9 KiB
C++

/*
* Copyright (c) 2022 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "modules/video_coding/codecs/test/video_codec_analyzer.h"
#include "absl/types/optional.h"
#include "api/video/i420_buffer.h"
#include "test/gmock.h"
#include "test/gtest.h"
#include "third_party/libyuv/include/libyuv/planar_functions.h"
namespace webrtc {
namespace test {
namespace {
using ::testing::Return;
using ::testing::Values;
using Psnr = VideoCodecStats::Frame::Psnr;
const uint32_t kTimestamp = 3000;
const int kSpatialIdx = 2;
class MockReferenceVideoSource
: public VideoCodecAnalyzer::ReferenceVideoSource {
public:
MOCK_METHOD(VideoFrame, GetFrame, (uint32_t, Resolution), (override));
};
VideoFrame CreateVideoFrame(uint32_t timestamp_rtp,
uint8_t y = 0,
uint8_t u = 0,
uint8_t v = 0) {
rtc::scoped_refptr<I420Buffer> buffer(I420Buffer::Create(2, 2));
libyuv::I420Rect(buffer->MutableDataY(), buffer->StrideY(),
buffer->MutableDataU(), buffer->StrideU(),
buffer->MutableDataV(), buffer->StrideV(), 0, 0,
buffer->width(), buffer->height(), y, u, v);
return VideoFrame::Builder()
.set_video_frame_buffer(buffer)
.set_timestamp_rtp(timestamp_rtp)
.build();
}
EncodedImage CreateEncodedImage(uint32_t timestamp_rtp, int spatial_idx = 0) {
EncodedImage encoded_image;
encoded_image.SetTimestamp(timestamp_rtp);
encoded_image.SetSpatialIndex(spatial_idx);
return encoded_image;
}
} // namespace
TEST(VideoCodecAnalyzerTest, StartEncode) {
VideoCodecAnalyzer analyzer;
analyzer.StartEncode(CreateVideoFrame(kTimestamp));
auto fs = analyzer.GetStats()->Slice();
EXPECT_EQ(1u, fs.size());
EXPECT_EQ(fs[0].timestamp_rtp, kTimestamp);
}
TEST(VideoCodecAnalyzerTest, FinishEncode) {
VideoCodecAnalyzer analyzer;
analyzer.StartEncode(CreateVideoFrame(kTimestamp));
EncodedImage encoded_frame = CreateEncodedImage(kTimestamp, kSpatialIdx);
analyzer.FinishEncode(encoded_frame);
auto fs = analyzer.GetStats()->Slice();
EXPECT_EQ(2u, fs.size());
EXPECT_EQ(kSpatialIdx, fs[1].spatial_idx);
}
TEST(VideoCodecAnalyzerTest, StartDecode) {
VideoCodecAnalyzer analyzer;
analyzer.StartDecode(CreateEncodedImage(kTimestamp, kSpatialIdx));
auto fs = analyzer.GetStats()->Slice();
EXPECT_EQ(1u, fs.size());
EXPECT_EQ(kTimestamp, fs[0].timestamp_rtp);
}
TEST(VideoCodecAnalyzerTest, FinishDecode) {
VideoCodecAnalyzer analyzer;
analyzer.StartDecode(CreateEncodedImage(kTimestamp, kSpatialIdx));
VideoFrame decoded_frame = CreateVideoFrame(kTimestamp);
analyzer.FinishDecode(decoded_frame, kSpatialIdx);
auto fs = analyzer.GetStats()->Slice();
EXPECT_EQ(1u, fs.size());
EXPECT_EQ(decoded_frame.width(), fs[0].width);
EXPECT_EQ(decoded_frame.height(), fs[0].height);
}
TEST(VideoCodecAnalyzerTest, ReferenceVideoSource) {
MockReferenceVideoSource reference_video_source;
VideoCodecAnalyzer analyzer(&reference_video_source);
analyzer.StartDecode(CreateEncodedImage(kTimestamp, kSpatialIdx));
EXPECT_CALL(reference_video_source, GetFrame)
.WillOnce(Return(CreateVideoFrame(kTimestamp, /*y=*/0,
/*u=*/0, /*v=*/0)));
analyzer.FinishDecode(
CreateVideoFrame(kTimestamp, /*value_y=*/1, /*value_u=*/2, /*value_v=*/3),
kSpatialIdx);
auto fs = analyzer.GetStats()->Slice();
EXPECT_EQ(1u, fs.size());
EXPECT_TRUE(fs[0].psnr.has_value());
const Psnr& psnr = *fs[0].psnr;
EXPECT_NEAR(psnr.y, 48, 1);
EXPECT_NEAR(psnr.u, 42, 1);
EXPECT_NEAR(psnr.v, 38, 1);
}
} // namespace test
} // namespace webrtc