Refactor V4L2 and X11 grabbers to share more code

Former-commit-id: 46176e53d1acf39f9bd0c0ecbb8e5fb5ab4d45be
This commit is contained in:
poljvd
2014-11-21 21:24:33 +01:00
parent 01854a471e
commit d89f504d83
22 changed files with 724 additions and 748 deletions

View File

@@ -4,9 +4,6 @@ cmake_minimum_required(VERSION 2.8)
# Set the project name
project(hyperion-x11)
# add protocol buffers
find_package(Protobuf REQUIRED)
# find Qt4
find_package(Qt4 REQUIRED QtCore QtGui QtNetwork)
@@ -14,42 +11,27 @@ find_package(Qt4 REQUIRED QtCore QtGui QtNetwork)
find_package(X11 REQUIRED)
include_directories(
${CMAKE_CURRENT_BINARY_DIR}
${PROTOBUF_INCLUDE_DIRS}
${CMAKE_CURRENT_BINARY_DIR}/../../libsrc/protoserver
${QT_INCLUDES}
${X11_INCLUDES}
)
set(Hyperion_X11_QT_HEADERS
ProtoWrapper.h
X11Wrapper.h)
set(Hyperion_X11_HEADERS
X11Grabber.h
../hyperion-v4l2/ProtoConnection.h
)
set(Hyperion_X11_SOURCES
hyperion-x11.cpp
ProtoWrapper.cpp
X11Grabber.cpp
X11Wrapper.cpp
../hyperion-v4l2/ProtoConnection.cpp
)
set(Hyperion_X11_PROTOS
${CMAKE_CURRENT_SOURCE_DIR}/../../libsrc/protoserver/message.proto
)
QT4_WRAP_CPP(Hyperion_X11_HEADERS_MOC ${Hyperion_X11_QT_HEADERS})
protobuf_generate_cpp(Hyperion_X11_PROTO_SRCS Hyperion_X11_PROTO_HDRS ${Hyperion_X11_PROTOS})
add_executable(hyperion-x11
${Hyperion_X11_HEADERS}
${Hyperion_X11_SOURCES}
${Hyperion_X11_PROTO_SRCS}
${Hyperion_X11_PROTO_HDRS}
${Hyperion_X11_HEADERS_MOC}
)
@@ -57,7 +39,8 @@ target_link_libraries(hyperion-x11
getoptPlusPlus
blackborder
hyperion-utils
${PROTOBUF_LIBRARIES}
protoserver
x11-grabber
${X11_LIBRARIES}
pthread
)

View File

@@ -1,17 +0,0 @@
//
#include "ProtoWrapper.h"
ProtoWrapper::ProtoWrapper(const std::string & protoAddress, const bool skipReply) :
_priority(10),
_duration_ms(2000),
_connection(protoAddress)
{
_connection.setSkipReply(skipReply);
}
void ProtoWrapper::process(const Image<ColorRgb> & image)
{
std::cout << "Attempt to send screenshot" << std::endl;
_connection.setImage(image, _priority, _duration_ms);
}

View File

@@ -1,23 +0,0 @@
// QT includes
#include <QObject>
#include "../hyperion-v4l2/ProtoConnection.h"
class ProtoWrapper : public QObject
{
Q_OBJECT
public:
ProtoWrapper(const std::string & protoAddress, const bool skipReply);
public slots:
void process(const Image<ColorRgb> & image);
private:
int _priority;
int _duration_ms;
ProtoConnection _connection;
};

View File

@@ -1,115 +0,0 @@
// STL includes
#include <iostream>
#include <cstdint>
// X11 includes
#include <X11/Xutil.h>
// X11Grabber includes
#include "X11Grabber.h"
X11Grabber::X11Grabber(const unsigned cropHorizontal, const unsigned cropVertical, const unsigned pixelDecimation) :
_pixelDecimation(pixelDecimation),
_cropWidth(cropHorizontal),
_cropHeight(cropVertical),
_x11Display(nullptr),
_screenWidth(0),
_screenHeight(0),
_image(0,0)
{
// empty
}
X11Grabber::~X11Grabber()
{
if (_x11Display != nullptr)
{
XCloseDisplay(_x11Display);
}
}
int X11Grabber::open()
{
const char * display_name = nullptr;
_x11Display = XOpenDisplay(display_name);
if (_x11Display == nullptr)
{
std::cerr << "Failed to open the default X11-display" << std::endl;
return -1;
}
return 0;
}
Image<ColorRgb> & X11Grabber::grab()
{
if (_x11Display == nullptr)
{
open();
}
updateScreenDimensions();
const int croppedWidth = _screenWidth - 2*_cropWidth;
const int croppedHeight = _screenHeight - 2*_cropHeight;
// Capture the current screen
XImage * xImage = XGetImage(_x11Display, DefaultRootWindow(_x11Display), _cropWidth, _cropHeight, croppedWidth, croppedHeight, AllPlanes, ZPixmap);
if (xImage == nullptr)
{
std::cerr << "Grab failed" << std::endl;
return _image;
}
// Copy the capture XImage to the local image (and apply required decimation)
ColorRgb * outputPtr = _image.memptr();
for (int iY=(_pixelDecimation/2); iY<croppedHeight; iY+=_pixelDecimation)
{
for (int iX=(_pixelDecimation/2); iX<croppedWidth; iX+=_pixelDecimation)
{
// Extract the pixel from the X11-image
const uint32_t pixel = uint32_t(XGetPixel(xImage, iX, iY));
// Assign the color value
outputPtr->red = uint8_t((pixel >> 16) & 0xff);
outputPtr->green = uint8_t((pixel >> 8) & 0xff);
outputPtr->blue = uint8_t((pixel >> 0) & 0xff);
// Move to the next output pixel
++outputPtr;
}
}
// Cleanup allocated resources of the X11 grab
XDestroyImage(xImage);
return _image;
}
int X11Grabber::updateScreenDimensions()
{
XWindowAttributes window_attributes_return;
const Status status = XGetWindowAttributes(_x11Display, DefaultRootWindow(_x11Display), &window_attributes_return);
if (status == 0)
{
std::cerr << "Failed to obtain window attributes" << std::endl;
return -1;
}
if (_screenWidth == unsigned(window_attributes_return.width) && _screenHeight == unsigned(window_attributes_return.height))
{
// No update required
return 0;
}
std::cout << "Update of screen resolution: [" << _screenWidth << "x" << _screenHeight <<"] => ";
_screenWidth = window_attributes_return.width;
_screenHeight = window_attributes_return.height;
std::cout << "[" << _screenWidth << "x" << _screenHeight <<"]" << std::endl;
// Update the size of the buffer used to transfer the screenshot
int width = (_screenWidth - 2 * _cropWidth + _pixelDecimation/2) / _pixelDecimation;
int height = (_screenHeight - 2 * _cropHeight + _pixelDecimation/2) / _pixelDecimation;
_image.resize(width, height);
return 0;
}

View File

@@ -1,37 +0,0 @@
// Hyperion-utils includes
#include <utils/Image.h>
#include <utils/ColorRgb.h>
// X11 includes
#include <X11/Xlib.h>
class X11Grabber
{
public:
X11Grabber(const unsigned cropHorizontal, const unsigned cropVertical, const unsigned pixelDecimation);
virtual ~X11Grabber();
int open();
Image<ColorRgb> & grab();
private:
const unsigned _pixelDecimation;
const unsigned _cropWidth;
const unsigned _cropHeight;
/// Reference to the X11 display (nullptr if not opened)
Display * _x11Display;
unsigned _screenWidth;
unsigned _screenHeight;
Image<ColorRgb> _image;
int updateScreenDimensions();
};

View File

@@ -3,31 +3,31 @@
#include "X11Wrapper.h"
X11Wrapper::X11Wrapper(const unsigned cropHorizontal, const unsigned cropVertical, const unsigned pixelDecimation) :
_timer(this),
_grabber(cropHorizontal, cropVertical, pixelDecimation)
_timer(this),
_grabber(cropHorizontal, cropVertical, pixelDecimation)
{
// Connect capturing to the timeout signal of the timer
connect(&_timer, SIGNAL(timeout()), this, SLOT(capture()));
// Connect capturing to the timeout signal of the timer
connect(&_timer, SIGNAL(timeout()), this, SLOT(capture()));
}
const Image<ColorRgb> & X11Wrapper::getScreenshot()
{
const Image<ColorRgb> & screenshot = _grabber.grab();
return screenshot;
const Image<ColorRgb> & screenshot = _grabber.grab();
return screenshot;
}
void X11Wrapper::start()
{
_timer.start(200);
_timer.start(100);
}
void X11Wrapper::stop()
{
_timer.stop();
_timer.stop();
}
void X11Wrapper::capture()
{
const Image<ColorRgb> & screenshot = _grabber.grab();
emit sig_screenshot(screenshot);
const Image<ColorRgb> & screenshot = _grabber.grab();
emit sig_screenshot(screenshot);
}

View File

@@ -3,37 +3,37 @@
#include <QTimer>
// Hyperion-X11 includes
#include "X11Grabber.h"
#include <grabber/X11Grabber.h>
class X11Wrapper : public QObject
{
Q_OBJECT
Q_OBJECT
public:
X11Wrapper(const unsigned cropHorizontal, const unsigned cropVertical, const unsigned pixelDecimation);
X11Wrapper(const unsigned cropHorizontal, const unsigned cropVertical, const unsigned pixelDecimation);
const Image<ColorRgb> & getScreenshot();
const Image<ColorRgb> & getScreenshot();
///
/// Starts the timed capturing of screenshots
///
void start();
///
/// Starts the timed capturing of screenshots
///
void start();
void stop();
void stop();
signals:
void sig_screenshot(const Image<ColorRgb> & screenshot);
void sig_screenshot(const Image<ColorRgb> & screenshot);
private slots:
///
/// Performs a single screenshot capture and publishes the capture screenshot on the screenshot
/// signal.
///
void capture();
///
/// Performs a single screenshot capture and publishes the capture screenshot on the screenshot
/// signal.
///
void capture();
private:
/// The QT timer to generate capture-publish events
QTimer _timer;
/// The QT timer to generate capture-publish events
QTimer _timer;
/// The grabber for creating screenshots
X11Grabber _grabber;
/// The grabber for creating screenshots
X11Grabber _grabber;
};

View File

@@ -6,7 +6,7 @@
// getoptPlusPLus includes
#include <getoptPlusPlus/getoptpp.h>
#include "ProtoWrapper.h"
#include "protoserver/ProtoConnectionWrapper.h"
#include "X11Wrapper.h"
using namespace vlofgren;
@@ -14,77 +14,77 @@ using namespace vlofgren;
// save the image as screenshot
void saveScreenshot(const char * filename, const Image<ColorRgb> & image)
{
// store as PNG
QImage pngImage((const uint8_t *) image.memptr(), image.width(), image.height(), 3*image.width(), QImage::Format_RGB888);
pngImage.save(filename);
// store as PNG
QImage pngImage((const uint8_t *) image.memptr(), image.width(), image.height(), 3*image.width(), QImage::Format_RGB888);
pngImage.save(filename);
}
int main(int argc, char ** argv)
{
QCoreApplication app(argc, argv);
QCoreApplication app(argc, argv);
try
{
// create the option parser and initialize all parameters
OptionsParser optionParser("X11 capture application for Hyperion");
ParameterSet & parameters = optionParser.getParameters();
try
{
// create the option parser and initialize all parameters
OptionsParser optionParser("X11 capture application for Hyperion");
ParameterSet & parameters = optionParser.getParameters();
IntParameter & argCropWidth = parameters.add<IntParameter> (0x0, "crop-width", "Number of pixels to crop from the left and right sides in the picture before decimation [default=0]");
IntParameter & argCropHeight = parameters.add<IntParameter> (0x0, "crop-height", "Number of pixels to crop from the top and the bottom in the picture before decimation [default=0]");
IntParameter & argSizeDecimation = parameters.add<IntParameter> ('s', "size-decimator", "Decimation factor for the output size [default=16]");
SwitchParameter<> & argScreenshot = parameters.add<SwitchParameter<>> (0x0, "screenshot", "Take a single screenshot, save it to file and quit");
StringParameter & argAddress = parameters.add<StringParameter> ('a', "address", "Set the address of the hyperion server [default: 127.0.0.1:19445]");
IntParameter & argPriority = parameters.add<IntParameter> ('p', "priority", "Use the provided priority channel (the lower the number, the higher the priority) [default: 800]");
SwitchParameter<> & argSkipReply = parameters.add<SwitchParameter<>> (0x0, "skip-reply", "Do not receive and check reply messages from Hyperion");
SwitchParameter<> & argHelp = parameters.add<SwitchParameter<>> ('h', "help", "Show this help message and exit");
IntParameter & argCropWidth = parameters.add<IntParameter> (0x0, "crop-width", "Number of pixels to crop from the left and right sides in the picture before decimation [default=0]");
IntParameter & argCropHeight = parameters.add<IntParameter> (0x0, "crop-height", "Number of pixels to crop from the top and the bottom in the picture before decimation [default=0]");
IntParameter & argSizeDecimation = parameters.add<IntParameter> ('s', "size-decimator", "Decimation factor for the output size [default=16]");
SwitchParameter<> & argScreenshot = parameters.add<SwitchParameter<>> (0x0, "screenshot", "Take a single screenshot, save it to file and quit");
StringParameter & argAddress = parameters.add<StringParameter> ('a', "address", "Set the address of the hyperion server [default: 127.0.0.1:19445]");
IntParameter & argPriority = parameters.add<IntParameter> ('p', "priority", "Use the provided priority channel (the lower the number, the higher the priority) [default: 800]");
SwitchParameter<> & argSkipReply = parameters.add<SwitchParameter<>> (0x0, "skip-reply", "Do not receive and check reply messages from Hyperion");
SwitchParameter<> & argHelp = parameters.add<SwitchParameter<>> ('h', "help", "Show this help message and exit");
// set defaults
argCropWidth.setDefault(0);
argCropHeight.setDefault(0);
argSizeDecimation.setDefault(16);
argAddress.setDefault("127.0.0.1:19445");
argPriority.setDefault(800);
// set defaults
argCropWidth.setDefault(0);
argCropHeight.setDefault(0);
argSizeDecimation.setDefault(16);
argAddress.setDefault("127.0.0.1:19445");
argPriority.setDefault(800);
// parse all options
optionParser.parse(argc, const_cast<const char **>(argv));
// parse all options
optionParser.parse(argc, const_cast<const char **>(argv));
// check if we need to display the usage. exit if we do.
if (argHelp.isSet())
{
optionParser.usage();
return 0;
}
// check if we need to display the usage. exit if we do.
if (argHelp.isSet())
{
optionParser.usage();
return 0;
}
// Create the X11 grabbing stuff
X11Wrapper x11Wrapper(argCropWidth.getValue(), argCropHeight.getValue(), argSizeDecimation.getValue());
// Create the X11 grabbing stuff
X11Wrapper x11Wrapper(argCropWidth.getValue(), argCropHeight.getValue(), argSizeDecimation.getValue());
if (argScreenshot.isSet())
{
// Capture a single screenshot and finish
const Image<ColorRgb> & screenshot = x11Wrapper.getScreenshot();
saveScreenshot("screenshot.png", screenshot);
}
else
{
// Create the Proto-connection with hyperiond
ProtoWrapper protoWrapper(argAddress.getValue(), argSkipReply.isSet());
if (argScreenshot.isSet())
{
// Capture a single screenshot and finish
const Image<ColorRgb> & screenshot = x11Wrapper.getScreenshot();
saveScreenshot("screenshot.png", screenshot);
}
else
{
// Create the Proto-connection with hyperiond
ProtoConnectionWrapper protoWrapper(argAddress.getValue(), argPriority.getValue(), 1000, argSkipReply.isSet());
// Connect the screen capturing to the proto processing
QObject::connect(&x11Wrapper, SIGNAL(sig_screenshot(const Image<ColorRgb> &)), &protoWrapper, SLOT(process(Image<ColorRgb>)));
// Connect the screen capturing to the proto processing
QObject::connect(&x11Wrapper, SIGNAL(sig_screenshot(const Image<ColorRgb> &)), &protoWrapper, SLOT(process(Image<ColorRgb>)));
// Start the capturing
x11Wrapper.start();
// Start the capturing
x11Wrapper.start();
// Start the application
app.exec();
}
}
catch (const std::runtime_error & e)
{
// An error occured. Display error and quit
std::cerr << e.what() << std::endl;
return -1;
}
// Start the application
app.exec();
}
}
catch (const std::runtime_error & e)
{
// An error occured. Display error and quit
std::cerr << e.what() << std::endl;
return -1;
}
return 0;
return 0;
}