X11ScreenCaptureImageProvider.cpp
Go to the documentation of this file.
2 
3 #include <ArmarXCore/core/time.h>
4 
6 
7 
8 namespace visionx
9 {
10 
11  const std::string X11ScreenCaptureImageProvider::defaultName = "X11ScreenCapture";
12 
14  {
15  clock = armarx::Clock(armarx::ClockType::Realtime);
16  }
17 
18 
19  std::string
21  {
22  return defaultName;
23  }
24 
25 
26  std::string
28  {
29  return defaultName;
30  }
31 
32 
33  void
35  {
36  ARMARX_INFO << "Initializing X11...";
37 
38  display = XOpenDisplay(nullptr);
39 
40  ARMARX_CHECK_GREATER_EQUAL(properties.screenNumber, 0);
41  ARMARX_CHECK_LESS(properties.screenNumber, XScreenCount(display));
42  window = RootWindow(display, properties.screenNumber);
43 
44  setNumberImages(1);
45 
46  // setImageFormat(ImageDimension(dim.width, dim.height), visionx::eRgb, visionx::eBayerPatternGr);
47  setImageFormat(ImageDimension(properties.width, properties.height),
48  visionx::eRgb,
49  visionx::eBayerPatternGr);
50 
51  out = cv::Mat(properties.height, properties.width, CV_8UC3);
52 
53  setImageSyncMode(visionx::eFpsSynchronization);
54  frameRate = properties.fps;
55  ARMARX_INFO << "Using " << frameRate << " FPS";
56  }
57 
58 
59  void
61  {
62  if (ximg != nullptr)
63  {
64  XDestroyImage(ximg);
65  }
66 
67  XCloseDisplay(display);
68  }
69 
70 
71  bool
73  {
74  if (ximg != nullptr)
75  {
76  XDestroyImage(ximg);
77  }
78 
79  ximg = XGetImage(display,
80  window,
81  properties.x,
82  properties.y,
83  properties.width,
84  properties.height,
85  AllPlanes,
86  ZPixmap);
87  ximg_cv = cv::Mat(properties.height, properties.width, CV_8UC4, ximg->data);
88 
89  auto tmpSharedMemoryProvider = sharedMemoryProvider;
90  if (tmpSharedMemoryProvider)
91  {
92  Ice::Byte* pixels = static_cast<Ice::Byte*>(out.data);
93  updateTimestamp(clock.now().toMicroSecondsSinceEpoch());
94  cv::cvtColor(ximg_cv, out, cv::COLOR_RGBA2BGR);
96  tmpSharedMemoryProvider->getScopedWriteLock());
97  memcpy(ppImageBuffers[0], pixels, out.cols * out.rows * out.channels());
98  }
99 
100  return true;
101  }
102 
103 
104  void
106  {
107  properties.fps = static_cast<int>(framesPerSecond);
108  }
109 
110 
111  void
113  {
114  ;
115  }
116 
117 
120  {
122  new armarx::ComponentPropertyDefinitions(getConfigIdentifier());
123 
124  defs->optional(properties.screenNumber, "screen.number", "Screen number to record. This does not correspond to "
125  "the monitor count, as this is transparent to X. You "
126  "don't want to change this in most cases.");
127  defs->optional(properties.fps, "fps", "Frames per second.");
128  defs->optional(properties.x, "region.x", "X coordinate of the region to record.");
129  defs->optional(properties.y, "region.y", "Y coordinate of the region to record.");
130  defs->optional(properties.width, "region.width", "Width of the region to record.");
131  defs->optional(properties.height, "region.height", "Height of the region to record.");
132 
133  return defs;
134  }
135 
136 
137  std::vector<imrec::ChannelPreferences>
139  {
140  ARMARX_TRACE;
141 
142  imrec::ChannelPreferences cp;
143  cp.requiresLossless = false;
144  cp.name = "screen";
145  return {cp};
146  }
147 } // namespace visionx
visionx::X11ScreenCaptureImageProvider::onInitCapturingImageProvider
void onInitCapturingImageProvider() override
This is called when the Component::onInitComponent() is called.
Definition: X11ScreenCaptureImageProvider.cpp:34
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
display
Use of this software is granted under one of the following two to be chosen freely by the user Boost Software License Version Marcin Kalicinski Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this display
Definition: license.txt:11
ARMARX_CHECK_LESS
#define ARMARX_CHECK_LESS(lhs, rhs)
This macro evaluates whether lhs is less (<) than rhs and if it turns out to be false it will throw a...
Definition: ExpressionException.h:102
armarx::SharedMemoryScopedWriteLockPtr
std::shared_ptr< SharedMemoryScopedWriteLock > SharedMemoryScopedWriteLockPtr
Definition: SharedMemoryProvider.h:46
visionx::X11ScreenCaptureImageProvider::onStartCapture
void onStartCapture(float framesPerSecond) override
This is called when the image provider capturing has been started.
Definition: X11ScreenCaptureImageProvider.cpp:105
visionx::X11ScreenCaptureImageProvider::getImageRecordingChannelPreferences
std::vector< imrec::ChannelPreferences > getImageRecordingChannelPreferences(const Ice::Current &) override
Definition: X11ScreenCaptureImageProvider.cpp:138
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
visionx::X11ScreenCaptureImageProvider::onExitCapturingImageProvider
void onExitCapturingImageProvider() override
This is called when the Component::onExitComponent() setup is called.
Definition: X11ScreenCaptureImageProvider.cpp:60
visionx::X11ScreenCaptureImageProvider::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: X11ScreenCaptureImageProvider.cpp:20
visionx::CapturingImageProvider::capture
virtual void capture()
Definition: CapturingImageProvider.cpp:106
visionx::X11ScreenCaptureImageProvider::onStopCapture
void onStopCapture() override
This is called when the image provider capturing has been stopped.
Definition: X11ScreenCaptureImageProvider.cpp:112
visionx::X11ScreenCaptureImageProvider::X11ScreenCaptureImageProvider
X11ScreenCaptureImageProvider()
Definition: X11ScreenCaptureImageProvider.cpp:13
visionx::X11ScreenCaptureImageProvider::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: X11ScreenCaptureImageProvider.cpp:119
ARMARX_CHECK_GREATER_EQUAL
#define ARMARX_CHECK_GREATER_EQUAL(lhs, rhs)
This macro evaluates whether lhs is greater or equal (>=) rhs and if it turns out to be false it will...
Definition: ExpressionException.h:123
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
IceUtil::Handle< class PropertyDefinitionContainer >
ImageUtil.h
time.h
visionx::X11ScreenCaptureImageProvider::GetDefaultName
static std::string GetDefaultName()
Definition: X11ScreenCaptureImageProvider.cpp:27
X11ScreenCaptureImageProvider.h
stopwatch::Clock
std::chrono::system_clock Clock
Definition: Stopwatch.h:10