Sebastian Jansson | 9a4f38e | 2018-12-19 12:14:41 | [diff] [blame] | 1 | /* |
| 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 Jansson | 7150d8c | 2019-04-09 12:18:09 | [diff] [blame] | 10 | #ifndef TEST_SCENARIO_VIDEO_FRAME_MATCHER_H_ |
| 11 | #define TEST_SCENARIO_VIDEO_FRAME_MATCHER_H_ |
Sebastian Jansson | 9a4f38e | 2018-12-19 12:14:41 | [diff] [blame] | 12 | |
| 13 | #include <deque> |
Sebastian Jansson | cf2df2f | 2019-04-02 09:51:28 | [diff] [blame] | 14 | #include <map> |
Sebastian Jansson | 52de8b0 | 2019-01-16 16:25:44 | [diff] [blame] | 15 | #include <memory> |
Sebastian Jansson | cf2df2f | 2019-04-02 09:51:28 | [diff] [blame] | 16 | #include <set> |
Sebastian Jansson | 9a4f38e | 2018-12-19 12:14:41 | [diff] [blame] | 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
Sebastian Jansson | 9a4f38e | 2018-12-19 12:14:41 | [diff] [blame] | 20 | #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 Jansson | cf2df2f | 2019-04-02 09:51:28 | [diff] [blame] | 24 | #include "rtc_base/ref_counted_object.h" |
Sebastian Jansson | 123f345 | 2019-03-13 10:22:52 | [diff] [blame] | 25 | #include "rtc_base/task_queue_for_test.h" |
Sebastian Jansson | 9a4f38e | 2018-12-19 12:14:41 | [diff] [blame] | 26 | #include "system_wrappers/include/clock.h" |
Sebastian Jansson | 7150d8c | 2019-04-09 12:18:09 | [diff] [blame] | 27 | #include "test/scenario/performance_stats.h" |
Sebastian Jansson | 9a4f38e | 2018-12-19 12:14:41 | [diff] [blame] | 28 | |
| 29 | namespace webrtc { |
| 30 | namespace test { |
| 31 | |
Sebastian Jansson | cf2df2f | 2019-04-02 09:51:28 | [diff] [blame] | 32 | class VideoFrameMatcher { |
Sebastian Jansson | 9a4f38e | 2018-12-19 12:14:41 | [diff] [blame] | 33 | public: |
Sebastian Jansson | cf2df2f | 2019-04-02 09:51:28 | [diff] [blame] | 34 | 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 Jansson | e9cac4f | 2019-06-24 15:10:55 | [diff] [blame] | 41 | int layer_id, |
Sebastian Jansson | cf2df2f | 2019-04-02 09:51:28 | [diff] [blame] | 42 | Timestamp render_time, |
Sebastian Jansson | e9cac4f | 2019-06-24 15:10:55 | [diff] [blame] | 43 | Timestamp at_time); |
Sebastian Jansson | 9a4f38e | 2018-12-19 12:14:41 | [diff] [blame] | 44 | bool Active() const; |
Sebastian Jansson | 9a4f38e | 2018-12-19 12:14:41 | [diff] [blame] | 45 | |
| 46 | private: |
Sebastian Jansson | cf2df2f | 2019-04-02 09:51:28 | [diff] [blame] | 47 | struct DecodedFrameBase { |
| 48 | int id; |
Sebastian Jansson | e9cac4f | 2019-06-24 15:10:55 | [diff] [blame] | 49 | Timestamp decoded_time = Timestamp::PlusInfinity(); |
Sebastian Jansson | cf2df2f | 2019-04-02 09:51:28 | [diff] [blame] | 50 | Timestamp render_time = Timestamp::PlusInfinity(); |
| 51 | rtc::scoped_refptr<VideoFrameBuffer> frame; |
| 52 | rtc::scoped_refptr<VideoFrameBuffer> thumb; |
| 53 | int repeat_count = 0; |
| 54 | }; |
Tomas Gunnarsson | e249d19 | 2021-04-26 09:46:54 | [diff] [blame] | 55 | using DecodedFrame = rtc::FinalRefCountedObject<DecodedFrameBase>; |
Sebastian Jansson | cf2df2f | 2019-04-02 09:51:28 | [diff] [blame] | 56 | 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 Jansson | 7150d8c | 2019-04-09 12:18:09 | [diff] [blame] | 71 | void HandleMatch(CapturedFrame captured, int layer_id); |
Sebastian Jansson | cf2df2f | 2019-04-02 09:51:28 | [diff] [blame] | 72 | 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 Chapovalov | d26a916 | 2019-03-19 17:08:37 | [diff] [blame] | 76 | TaskQueueForTest task_queue_; |
Sebastian Jansson | 9a4f38e | 2018-12-19 12:14:41 | [diff] [blame] | 77 | }; |
| 78 | |
Sebastian Jansson | e05ae5b | 2019-07-30 16:12:54 | [diff] [blame] | 79 | class 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 Jansson | 9a4f38e | 2018-12-19 12:14:41 | [diff] [blame] | 94 | class ForwardingCapturedFrameTap |
| 95 | : public rtc::VideoSinkInterface<VideoFrame>, |
| 96 | public rtc::VideoSourceInterface<VideoFrame> { |
| 97 | public: |
Sebastian Jansson | aa01f27 | 2019-01-30 10:28:59 | [diff] [blame] | 98 | ForwardingCapturedFrameTap(Clock* clock, |
Sebastian Jansson | cf2df2f | 2019-04-02 09:51:28 | [diff] [blame] | 99 | VideoFrameMatcher* matcher, |
Sebastian Jansson | 9a4f38e | 2018-12-19 12:14:41 | [diff] [blame] | 100 | rtc::VideoSourceInterface<VideoFrame>* source); |
| 101 | ForwardingCapturedFrameTap(ForwardingCapturedFrameTap&) = delete; |
| 102 | ForwardingCapturedFrameTap& operator=(ForwardingCapturedFrameTap&) = delete; |
Sebastian Jansson | 9a4f38e | 2018-12-19 12:14:41 | [diff] [blame] | 103 | |
| 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 Jansson | 9a4f38e | 2018-12-19 12:14:41 | [diff] [blame] | 112 | |
| 113 | private: |
Sebastian Jansson | aa01f27 | 2019-01-30 10:28:59 | [diff] [blame] | 114 | Clock* const clock_; |
Sebastian Jansson | cf2df2f | 2019-04-02 09:51:28 | [diff] [blame] | 115 | VideoFrameMatcher* const matcher_; |
Sebastian Jansson | 9a4f38e | 2018-12-19 12:14:41 | [diff] [blame] | 116 | rtc::VideoSourceInterface<VideoFrame>* const source_; |
Sebastian Jansson | 7fa4277 | 2019-08-28 18:49:55 | [diff] [blame] | 117 | VideoSinkInterface<VideoFrame>* sink_ = nullptr; |
Sebastian Jansson | 9a4f38e | 2018-12-19 12:14:41 | [diff] [blame] | 118 | int discarded_count_ = 0; |
| 119 | }; |
| 120 | |
| 121 | class DecodedFrameTap : public rtc::VideoSinkInterface<VideoFrame> { |
| 122 | public: |
Sebastian Jansson | e9cac4f | 2019-06-24 15:10:55 | [diff] [blame] | 123 | DecodedFrameTap(Clock* clock, VideoFrameMatcher* matcher, int layer_id); |
Sebastian Jansson | 9a4f38e | 2018-12-19 12:14:41 | [diff] [blame] | 124 | // VideoSinkInterface interface |
| 125 | void OnFrame(const VideoFrame& frame) override; |
| 126 | |
| 127 | private: |
Sebastian Jansson | e9cac4f | 2019-06-24 15:10:55 | [diff] [blame] | 128 | Clock* const clock_; |
Sebastian Jansson | cf2df2f | 2019-04-02 09:51:28 | [diff] [blame] | 129 | VideoFrameMatcher* const matcher_; |
| 130 | int layer_id_; |
| 131 | }; |
Sebastian Jansson | 9a4f38e | 2018-12-19 12:14:41 | [diff] [blame] | 132 | } // namespace test |
| 133 | } // namespace webrtc |
Sebastian Jansson | 7150d8c | 2019-04-09 12:18:09 | [diff] [blame] | 134 | #endif // TEST_SCENARIO_VIDEO_FRAME_MATCHER_H_ |