xref: /aosp_15_r20/external/webrtc/pc/test/frame_generator_capturer_video_track_source.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 
11 #ifndef PC_TEST_FRAME_GENERATOR_CAPTURER_VIDEO_TRACK_SOURCE_H_
12 #define PC_TEST_FRAME_GENERATOR_CAPTURER_VIDEO_TRACK_SOURCE_H_
13 
14 #include <memory>
15 #include <utility>
16 
17 #include "api/task_queue/default_task_queue_factory.h"
18 #include "api/task_queue/task_queue_factory.h"
19 #include "api/test/create_frame_generator.h"
20 #include "pc/video_track_source.h"
21 #include "test/frame_generator_capturer.h"
22 
23 namespace webrtc {
24 
25 // Implements a VideoTrackSourceInterface to be used for creating VideoTracks.
26 // The video source is generated using a FrameGeneratorCapturer, specifically
27 // a SquareGenerator that generates frames with randomly sized and colored
28 // squares.
29 class FrameGeneratorCapturerVideoTrackSource : public VideoTrackSource {
30  public:
31   static const int kDefaultFramesPerSecond = 30;
32   static const int kDefaultWidth = 640;
33   static const int kDefaultHeight = 480;
34   static const int kNumSquaresGenerated = 50;
35 
36   struct Config {
37     int frames_per_second = kDefaultFramesPerSecond;
38     int width = kDefaultWidth;
39     int height = kDefaultHeight;
40     int num_squares_generated = 50;
41   };
42 
FrameGeneratorCapturerVideoTrackSource(Config config,Clock * clock,bool is_screencast)43   FrameGeneratorCapturerVideoTrackSource(Config config,
44                                          Clock* clock,
45                                          bool is_screencast)
46       : VideoTrackSource(false /* remote */),
47         task_queue_factory_(CreateDefaultTaskQueueFactory()),
48         is_screencast_(is_screencast) {
49     video_capturer_ = std::make_unique<test::FrameGeneratorCapturer>(
50         clock,
51         test::CreateSquareFrameGenerator(config.width, config.height,
52                                          absl::nullopt,
53                                          config.num_squares_generated),
54         config.frames_per_second, *task_queue_factory_);
55     video_capturer_->Init();
56   }
57 
FrameGeneratorCapturerVideoTrackSource(std::unique_ptr<test::FrameGeneratorCapturer> video_capturer,bool is_screencast)58   FrameGeneratorCapturerVideoTrackSource(
59       std::unique_ptr<test::FrameGeneratorCapturer> video_capturer,
60       bool is_screencast)
61       : VideoTrackSource(false /* remote */),
62         video_capturer_(std::move(video_capturer)),
63         is_screencast_(is_screencast) {}
64 
65   ~FrameGeneratorCapturerVideoTrackSource() = default;
66 
Start()67   void Start() { SetState(kLive); }
68 
Stop()69   void Stop() { SetState(kMuted); }
70 
is_screencast()71   bool is_screencast() const override { return is_screencast_; }
72 
73  protected:
source()74   rtc::VideoSourceInterface<VideoFrame>* source() override {
75     return video_capturer_.get();
76   }
77 
78  private:
79   const std::unique_ptr<TaskQueueFactory> task_queue_factory_;
80   std::unique_ptr<test::FrameGeneratorCapturer> video_capturer_;
81   const bool is_screencast_;
82 };
83 
84 }  // namespace webrtc
85 
86 #endif  // PC_TEST_FRAME_GENERATOR_CAPTURER_VIDEO_TRACK_SOURCE_H_
87