1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/functional/bind.h"
8 #include "media/base/video_codecs.h"
9 #include "media/mojo/mojom/media_types.mojom.h"
10 #include "media/mojo/services/webrtc_video_perf_recorder.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
20 // Aliases for readability.
21 const bool kDecode = true;
22 const bool kEncode = false;
23 const VideoCodecProfile kProfileA = H264PROFILE_MIN;
24 const VideoCodecProfile kProfileB = VP9PROFILE_MIN;
25 const VideoCodecProfile kProfileC = VP8PROFILE_MIN;
26 const VideoCodecProfile kProfileUnknown = VIDEO_CODEC_PROFILE_UNKNOWN;
27 const int kPixelSizeA = 1280 * 720;
28 const int kPixelSizeB = 1920 * 1080;
29 const bool kHw = true;
30 const bool kSw = false;
32 MATCHER_P(MojoEq, value, "") {
33 return arg.Equals(value);
38 using Features = media::mojom::WebrtcPredictionFeatures;
39 using VideoStats = media::mojom::WebrtcVideoStats;
41 class WebrtcVideoPerfRecorderTest : public ::testing::Test {
44 recorder_ = std::make_unique<WebrtcVideoPerfRecorder>(
45 base::BindRepeating(&WebrtcVideoPerfRecorderTest::SavePerfCallback,
46 base::Unretained(this)));
48 ~WebrtcVideoPerfRecorderTest() override = default;
50 MOCK_METHOD3(SavePerfCallback,
51 void(media::mojom::WebrtcPredictionFeatures features,
52 media::mojom::WebrtcVideoStats targets,
53 base::OnceClosure save_done_cb));
56 std::unique_ptr<WebrtcVideoPerfRecorder> recorder_;
59 TEST_F(WebrtcVideoPerfRecorderTest, SaveOnNewConfig) {
61 // Update decode entry.
62 recorder_->UpdateRecord(Features::New(kDecode, kProfileA, kPixelSizeA, kSw),
63 VideoStats::New(11, 3, 12.0f));
64 // New data for the same decode entry should not result in a callback.
65 recorder_->UpdateRecord(Features::New(kDecode, kProfileA, kPixelSizeA, kSw),
66 VideoStats::New(111, 8, 11.7f));
68 // Update encode entry. This should not result in a callback either since
69 // encode and decode states are tracked individually.
70 recorder_->UpdateRecord(Features::New(kEncode, kProfileC, kPixelSizeA, kSw),
71 VideoStats::New(13, 7, 17.0f));
73 // Expect save with the previous state upon changing to a HW decoder.
75 SavePerfCallback((Features(kDecode, kProfileA, kPixelSizeA, kSw)),
76 (VideoStats(111, 8, 11.7f)), _))
78 recorder_->UpdateRecord(Features::New(kDecode, kProfileA, kPixelSizeA, kHw),
79 VideoStats::New(15, 4, 9.0f));
81 // Expect save with the previous state upon changing decode pixel size.
83 SavePerfCallback((Features(kDecode, kProfileA, kPixelSizeA, kHw)),
84 (VideoStats(15, 4, 9.0f)), _))
86 recorder_->UpdateRecord(Features::New(kDecode, kProfileA, kPixelSizeB, kHw),
87 VideoStats::New(17, 6, 14.0f));
89 // Expect save with the previous state upon changing decode codec profile.
91 SavePerfCallback((Features(kDecode, kProfileA, kPixelSizeB, kHw)),
92 (VideoStats(17, 6, 14.0f)), _))
94 recorder_->UpdateRecord(Features::New(kDecode, kProfileB, kPixelSizeB, kHw),
95 VideoStats::New(21, 5, 13.0f));
97 // An empty record saves the current state.
99 SavePerfCallback((Features(kDecode, kProfileB, kPixelSizeB, kHw)),
100 (VideoStats(21, 5, 13.0f)), _))
102 recorder_->UpdateRecord(Features::New(kDecode, kProfileUnknown, 0, kHw),
103 VideoStats::New(0, 0, 0.0f));
106 SavePerfCallback((Features(kEncode, kProfileC, kPixelSizeA, kSw)),
107 (VideoStats(13, 7, 17.0f)), _))
109 recorder_->UpdateRecord(Features::New(kEncode, kProfileUnknown, 0, kHw),
110 VideoStats::New(0, 0, 0.0f));
113 TEST_F(WebrtcVideoPerfRecorderTest, SaveOnDestruction) {
115 // Update decode entry.
116 recorder_->UpdateRecord(Features::New(kDecode, kProfileA, kPixelSizeA, kSw),
117 VideoStats::New(11, 3, 12.0f));
118 // Update encode entry. This should not result in a callback either since
119 // encode and decode states are tracked individually.
120 recorder_->UpdateRecord(Features::New(kEncode, kProfileB, kPixelSizeB, kHw),
121 VideoStats::New(13, 7, 17.0f));
123 // Expect save for both encode and decode upon destruction.
124 EXPECT_CALL(*this, SavePerfCallback(
125 MojoEq(Features(kDecode, kProfileA, kPixelSizeA, kSw)),
126 MojoEq(VideoStats(11, 3, 12.0f)), _))
128 EXPECT_CALL(*this, SavePerfCallback(
129 MojoEq(Features(kEncode, kProfileB, kPixelSizeB, kHw)),
130 MojoEq(VideoStats(13, 7, 17.0f)), _))