video_frame_matcher.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. */
  10. #ifndef TEST_SCENARIO_VIDEO_FRAME_MATCHER_H_
  11. #define TEST_SCENARIO_VIDEO_FRAME_MATCHER_H_
  12. #include <deque>
  13. #include <map>
  14. #include <memory>
  15. #include <set>
  16. #include <string>
  17. #include <vector>
  18. #include "api/units/timestamp.h"
  19. #include "api/video/video_frame.h"
  20. #include "api/video/video_sink_interface.h"
  21. #include "api/video/video_source_interface.h"
  22. #include "rtc_base/ref_counted_object.h"
  23. #include "rtc_base/task_queue_for_test.h"
  24. #include "system_wrappers/include/clock.h"
  25. #include "test/scenario/performance_stats.h"
  26. namespace webrtc {
  27. namespace test {
  28. class VideoFrameMatcher {
  29. public:
  30. explicit VideoFrameMatcher(
  31. std::vector<std::function<void(const VideoFramePair&)>>
  32. frame_pair_handlers);
  33. ~VideoFrameMatcher();
  34. void RegisterLayer(int layer_id);
  35. void OnCapturedFrame(const VideoFrame& frame, Timestamp at_time);
  36. void OnDecodedFrame(const VideoFrame& frame,
  37. int layer_id,
  38. Timestamp render_time,
  39. Timestamp at_time);
  40. bool Active() const;
  41. private:
  42. struct DecodedFrameBase {
  43. int id;
  44. Timestamp decoded_time = Timestamp::PlusInfinity();
  45. Timestamp render_time = Timestamp::PlusInfinity();
  46. rtc::scoped_refptr<VideoFrameBuffer> frame;
  47. rtc::scoped_refptr<VideoFrameBuffer> thumb;
  48. int repeat_count = 0;
  49. };
  50. using DecodedFrame = rtc::RefCountedObject<DecodedFrameBase>;
  51. struct CapturedFrame {
  52. int id;
  53. Timestamp capture_time = Timestamp::PlusInfinity();
  54. rtc::scoped_refptr<VideoFrameBuffer> frame;
  55. rtc::scoped_refptr<VideoFrameBuffer> thumb;
  56. double best_score = INFINITY;
  57. rtc::scoped_refptr<DecodedFrame> best_decode;
  58. bool matched = false;
  59. };
  60. struct VideoLayer {
  61. int layer_id;
  62. std::deque<CapturedFrame> captured_frames;
  63. rtc::scoped_refptr<DecodedFrame> last_decode;
  64. int next_decoded_id = 1;
  65. };
  66. void HandleMatch(CapturedFrame captured, int layer_id);
  67. void Finalize();
  68. int next_capture_id_ = 1;
  69. std::vector<std::function<void(const VideoFramePair&)>> frame_pair_handlers_;
  70. std::map<int, VideoLayer> layers_;
  71. TaskQueueForTest task_queue_;
  72. };
  73. class CapturedFrameTap : public rtc::VideoSinkInterface<VideoFrame> {
  74. public:
  75. CapturedFrameTap(Clock* clock, VideoFrameMatcher* matcher);
  76. CapturedFrameTap(CapturedFrameTap&) = delete;
  77. CapturedFrameTap& operator=(CapturedFrameTap&) = delete;
  78. void OnFrame(const VideoFrame& frame) override;
  79. void OnDiscardedFrame() override;
  80. private:
  81. Clock* const clock_;
  82. VideoFrameMatcher* const matcher_;
  83. int discarded_count_ = 0;
  84. };
  85. class ForwardingCapturedFrameTap
  86. : public rtc::VideoSinkInterface<VideoFrame>,
  87. public rtc::VideoSourceInterface<VideoFrame> {
  88. public:
  89. ForwardingCapturedFrameTap(Clock* clock,
  90. VideoFrameMatcher* matcher,
  91. rtc::VideoSourceInterface<VideoFrame>* source);
  92. ForwardingCapturedFrameTap(ForwardingCapturedFrameTap&) = delete;
  93. ForwardingCapturedFrameTap& operator=(ForwardingCapturedFrameTap&) = delete;
  94. // VideoSinkInterface interface
  95. void OnFrame(const VideoFrame& frame) override;
  96. void OnDiscardedFrame() override;
  97. // VideoSourceInterface interface
  98. void AddOrUpdateSink(VideoSinkInterface<VideoFrame>* sink,
  99. const rtc::VideoSinkWants& wants) override;
  100. void RemoveSink(VideoSinkInterface<VideoFrame>* sink) override;
  101. private:
  102. Clock* const clock_;
  103. VideoFrameMatcher* const matcher_;
  104. rtc::VideoSourceInterface<VideoFrame>* const source_;
  105. VideoSinkInterface<VideoFrame>* sink_ = nullptr;
  106. int discarded_count_ = 0;
  107. };
  108. class DecodedFrameTap : public rtc::VideoSinkInterface<VideoFrame> {
  109. public:
  110. DecodedFrameTap(Clock* clock, VideoFrameMatcher* matcher, int layer_id);
  111. // VideoSinkInterface interface
  112. void OnFrame(const VideoFrame& frame) override;
  113. private:
  114. Clock* const clock_;
  115. VideoFrameMatcher* const matcher_;
  116. int layer_id_;
  117. };
  118. } // namespace test
  119. } // namespace webrtc
  120. #endif // TEST_SCENARIO_VIDEO_FRAME_MATCHER_H_