OpenShot Library | libopenshot  1.0.0
ScreenCaptureReader.h
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2026 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 #ifndef OPENSHOT_SCREENCAPTUREREADER_H
14 #define OPENSHOT_SCREENCAPTUREREADER_H
15 
16 #include "ReaderBase.h"
17 
18 #include <atomic>
19 #include <map>
20 #include <memory>
21 #include <string>
22 
23 #include "FFmpegUtilities.h"
24 
25 namespace openshot
26 {
28  {
34  };
35 
37  {
39  std::string display;
40  int x = 0;
41  int y = 0;
42  int width = 1280;
43  int height = 720;
45  bool include_cursor = true;
46  bool show_region = false;
47  bool capture_audio = false;
48  std::string audio_device;
49  int audio_sample_rate = 48000;
50  int audio_channels = 2;
51  std::map<std::string, std::string> options;
52  };
53 
55  {
56  bool is_open = false;
57  int64_t frames_read = 0;
58  int dropped_packets = 0;
59  double duration = 0.0;
60  };
61 
63  {
64  public:
65 #ifndef SWIG
67  {
68  public:
69  virtual ~CaptureBackendReader() = default;
70  virtual void Open() = 0;
71  virtual void Close() = 0;
72  virtual bool IsOpen() const = 0;
73  virtual std::shared_ptr<openshot::Frame> GetFrame(int64_t number) = 0;
75  };
76 #endif
77 
78  explicit ScreenCaptureReader(const ScreenCaptureSettings& settings);
79  ~ScreenCaptureReader() override;
80 
81  void Close() override;
82  CacheBase* GetCache() override { return NULL; };
83  std::shared_ptr<openshot::Frame> GetFrame(int64_t number) override;
84  bool IsOpen() override;
85  std::string Name() override { return "ScreenCaptureReader"; };
86  std::string Json() const override;
87  void SetJson(const std::string value) override;
88  Json::Value JsonValue() const override;
89  void SetJsonValue(const Json::Value root) override;
90  void Open() override;
91 
93  void AddSystemAudio(std::shared_ptr<openshot::Frame> frame, int64_t output_frame_number);
94  void ResetSystemAudio();
95  ScreenCaptureSettings GetSettings() const { return settings; };
96  static bool IsBackendSupported(ScreenCaptureBackend backend);
97  static bool IsSystemAudioSupported(ScreenCaptureBackend backend);
99 
100  private:
101  void ValidateSettings() const;
102  void OpenDevice();
103  void OpenDecoder();
104  std::string InputFormatName() const;
105  std::string InputName() const;
106  std::shared_ptr<openshot::Frame> DecodeNextFrame(int64_t number);
107  bool UsesFFmpegDevice() const;
108  bool UsesWaylandPortal() const;
109  void PopulateInfo();
110 
111  ScreenCaptureSettings settings;
112 #ifndef SWIG
113  std::unique_ptr<CaptureBackendReader> backend_reader;
114  // The Wayland backend is a runtime-loaded module. Keep it loaded until its
115  // reader is destroyed, since the reader's vtable lives in that module.
116  void* backend_module = nullptr;
117 #endif
118  bool is_open;
119  int video_stream;
120  int64_t frames_read;
121  int dropped_packets;
122  AVFormatContext* format_context;
123  AVCodecContext* codec_context;
124  AVFrame* source_frame;
125  AVFrame* rgba_frame;
126  AVPacket* packet;
127  SwsContext* sws_context;
128  std::atomic<bool> close_requested;
129  bool manual_system_audio = false;
130 #ifndef SWIG
131  class SystemAudioCapture;
132  std::unique_ptr<SystemAudioCapture> system_audio;
133 #endif
134  };
135 }
136 
137 #endif
Header file for FFmpegUtilities.
Header file for ReaderBase class.
All cache managers in libopenshot are based on this CacheBase class.
Definition: CacheBase.h:35
This class represents a fraction.
Definition: Fraction.h:30
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:76
virtual openshot::CaptureReaderStats GetStats() const =0
virtual std::shared_ptr< openshot::Frame > GetFrame(int64_t number)=0
void SetJson(const std::string value) override
Load JSON string into this object.
bool IsOpen() override
Determine if reader is open or closed.
openshot::CaptureReaderStats GetStats() const
std::string Name() override
Return the type name of the class.
void Close() override
Close the reader (and any resources it was consuming)
static ScreenCaptureBackend DefaultBackend()
static bool IsBackendSupported(ScreenCaptureBackend backend)
ScreenCaptureReader(const ScreenCaptureSettings &settings)
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
void AddSystemAudio(std::shared_ptr< openshot::Frame > frame, int64_t output_frame_number)
CacheBase * GetCache() override
Get the cache object used by this reader (note: not all readers use cache)
Json::Value JsonValue() const override
Generate Json::Value for this object.
std::shared_ptr< openshot::Frame > GetFrame(int64_t number) override
ScreenCaptureSettings GetSettings() const
std::string Json() const override
Generate JSON string of this object.
void Open() override
Open the reader (and start consuming resources, such as images or video files)
static bool IsSystemAudioSupported(ScreenCaptureBackend backend)
This namespace is the default namespace for all code in the openshot library.
Definition: AnimatedCurve.h:24
@ SCREEN_CAPTURE_MAC_AVFOUNDATION
@ SCREEN_CAPTURE_WINDOWS_GDI
std::map< std::string, std::string > options