blob: a3aa85447d42effeb4f528a3cc180a6360a2ffd5 [file] [log] [blame]
Sebastian Jansson9a4f38e2018-12-19 12:14:411/*
2 * Copyright 2018 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 */
Sebastian Jansson7150d8c2019-04-09 12:18:0910#ifndef TEST_SCENARIO_VIDEO_FRAME_MATCHER_H_
11#define TEST_SCENARIO_VIDEO_FRAME_MATCHER_H_
Sebastian Jansson9a4f38e2018-12-19 12:14:4112
13#include <deque>
Sebastian Janssoncf2df2f2019-04-02 09:51:2814#include <map>
Sebastian Jansson52de8b02019-01-16 16:25:4415#include <memory>
Sebastian Janssoncf2df2f2019-04-02 09:51:2816#include <set>
Sebastian Jansson9a4f38e2018-12-19 12:14:4117#include <string>
18#include <vector>
19
Sebastian Jansson9a4f38e2018-12-19 12:14:4120#include "api/units/timestamp.h"
21#include "api/video/video_frame.h"
22#include "api/video/video_sink_interface.h"
23#include "api/video/video_source_interface.h"
Sebastian Janssoncf2df2f2019-04-02 09:51:2824#include "rtc_base/ref_counted_object.h"
Sebastian Jansson123f3452019-03-13 10:22:5225#include "rtc_base/task_queue_for_test.h"
Sebastian Jansson9a4f38e2018-12-19 12:14:4126#include "system_wrappers/include/clock.h"
Sebastian Jansson7150d8c2019-04-09 12:18:0927#include "test/scenario/performance_stats.h"
Sebastian Jansson9a4f38e2018-12-19 12:14:4128
29namespace webrtc {
30namespace test {
31
Sebastian Janssoncf2df2f2019-04-02 09:51:2832class VideoFrameMatcher {
Sebastian Jansson9a4f38e2018-12-19 12:14:4133 public:
Sebastian Janssoncf2df2f2019-04-02 09:51:2834 explicit VideoFrameMatcher(
35 std::vector<std::function<void(const VideoFramePair&)>>
36 frame_pair_handlers);
37 ~VideoFrameMatcher();
38 void RegisterLayer(int layer_id);
39 void OnCapturedFrame(const VideoFrame& frame, Timestamp at_time);
40 void OnDecodedFrame(const VideoFrame& frame,
Sebastian Janssone9cac4f2019-06-24 15:10:5541 int layer_id,
Sebastian Janssoncf2df2f2019-04-02 09:51:2842 Timestamp render_time,
Sebastian Janssone9cac4f2019-06-24 15:10:5543 Timestamp at_time);
Sebastian Jansson9a4f38e2018-12-19 12:14:4144 bool Active() const;
Sebastian Jansson9a4f38e2018-12-19 12:14:4145
46 private:
Sebastian Janssoncf2df2f2019-04-02 09:51:2847 struct DecodedFrameBase {
48 int id;
Sebastian Janssone9cac4f2019-06-24 15:10:5549 Timestamp decoded_time = Timestamp::PlusInfinity();
Sebastian Janssoncf2df2f2019-04-02 09:51:2850 Timestamp render_time = Timestamp::PlusInfinity();
51 rtc::scoped_refptr<VideoFrameBuffer> frame;
52 rtc::scoped_refptr<VideoFrameBuffer> thumb;
53 int repeat_count = 0;
54 };
Tomas Gunnarssone249d192021-04-26 09:46:5455 using DecodedFrame = rtc::FinalRefCountedObject<DecodedFrameBase>;
Sebastian Janssoncf2df2f2019-04-02 09:51:2856 struct CapturedFrame {
57 int id;
58 Timestamp capture_time = Timestamp::PlusInfinity();
59 rtc::scoped_refptr<VideoFrameBuffer> frame;
60 rtc::scoped_refptr<VideoFrameBuffer> thumb;
61 double best_score = INFINITY;
62 rtc::scoped_refptr<DecodedFrame> best_decode;
63 bool matched = false;
64 };
65 struct VideoLayer {
66 int layer_id;
67 std::deque<CapturedFrame> captured_frames;
68 rtc::scoped_refptr<DecodedFrame> last_decode;
69 int next_decoded_id = 1;
70 };
Sebastian Jansson7150d8c2019-04-09 12:18:0971 void HandleMatch(CapturedFrame captured, int layer_id);
Sebastian Janssoncf2df2f2019-04-02 09:51:2872 void Finalize();
73 int next_capture_id_ = 1;
74 std::vector<std::function<void(const VideoFramePair&)>> frame_pair_handlers_;
75 std::map<int, VideoLayer> layers_;
Danil Chapovalovd26a9162019-03-19 17:08:3776 TaskQueueForTest task_queue_;
Sebastian Jansson9a4f38e2018-12-19 12:14:4177};
78
Sebastian Janssone05ae5b2019-07-30 16:12:5479class CapturedFrameTap : public rtc::VideoSinkInterface<VideoFrame> {
80 public:
81 CapturedFrameTap(Clock* clock, VideoFrameMatcher* matcher);
82 CapturedFrameTap(CapturedFrameTap&) = delete;
83 CapturedFrameTap& operator=(CapturedFrameTap&) = delete;
84
85 void OnFrame(const VideoFrame& frame) override;
86 void OnDiscardedFrame() override;
87
88 private:
89 Clock* const clock_;
90 VideoFrameMatcher* const matcher_;
91 int discarded_count_ = 0;
92};
93
Sebastian Jansson9a4f38e2018-12-19 12:14:4194class ForwardingCapturedFrameTap
95 : public rtc::VideoSinkInterface<VideoFrame>,
96 public rtc::VideoSourceInterface<VideoFrame> {
97 public:
Sebastian Janssonaa01f272019-01-30 10:28:5998 ForwardingCapturedFrameTap(Clock* clock,
Sebastian Janssoncf2df2f2019-04-02 09:51:2899 VideoFrameMatcher* matcher,
Sebastian Jansson9a4f38e2018-12-19 12:14:41100 rtc::VideoSourceInterface<VideoFrame>* source);
101 ForwardingCapturedFrameTap(ForwardingCapturedFrameTap&) = delete;
102 ForwardingCapturedFrameTap& operator=(ForwardingCapturedFrameTap&) = delete;
Sebastian Jansson9a4f38e2018-12-19 12:14:41103
104 // VideoSinkInterface interface
105 void OnFrame(const VideoFrame& frame) override;
106 void OnDiscardedFrame() override;
107
108 // VideoSourceInterface interface
109 void AddOrUpdateSink(VideoSinkInterface<VideoFrame>* sink,
110 const rtc::VideoSinkWants& wants) override;
111 void RemoveSink(VideoSinkInterface<VideoFrame>* sink) override;
Sebastian Jansson9a4f38e2018-12-19 12:14:41112
113 private:
Sebastian Janssonaa01f272019-01-30 10:28:59114 Clock* const clock_;
Sebastian Janssoncf2df2f2019-04-02 09:51:28115 VideoFrameMatcher* const matcher_;
Sebastian Jansson9a4f38e2018-12-19 12:14:41116 rtc::VideoSourceInterface<VideoFrame>* const source_;
Sebastian Jansson7fa42772019-08-28 18:49:55117 VideoSinkInterface<VideoFrame>* sink_ = nullptr;
Sebastian Jansson9a4f38e2018-12-19 12:14:41118 int discarded_count_ = 0;
119};
120
121class DecodedFrameTap : public rtc::VideoSinkInterface<VideoFrame> {
122 public:
Sebastian Janssone9cac4f2019-06-24 15:10:55123 DecodedFrameTap(Clock* clock, VideoFrameMatcher* matcher, int layer_id);
Sebastian Jansson9a4f38e2018-12-19 12:14:41124 // VideoSinkInterface interface
125 void OnFrame(const VideoFrame& frame) override;
126
127 private:
Sebastian Janssone9cac4f2019-06-24 15:10:55128 Clock* const clock_;
Sebastian Janssoncf2df2f2019-04-02 09:51:28129 VideoFrameMatcher* const matcher_;
130 int layer_id_;
131};
Sebastian Jansson9a4f38e2018-12-19 12:14:41132} // namespace test
133} // namespace webrtc
Sebastian Jansson7150d8c2019-04-09 12:18:09134#endif // TEST_SCENARIO_VIDEO_FRAME_MATCHER_H_