Feature/xcb grabber (#912)

* Add Xcb grabber

* update compile instruction

Signed-off-by: Paulchen Panther <Paulchen-Panter@protonmail.com>

* Fix problem on resolution change + Make XCB default if X11 is not avaialable

* Fix decimation problem

Co-authored-by: Paulchen Panther <16664240+Paulchen-Panther@users.noreply.github.com>
Co-authored-by: Paulchen Panther <Paulchen-Panter@protonmail.com>
This commit is contained in:
Murat Seker
2020-08-03 12:31:39 +02:00
committed by GitHub
parent 11d7614591
commit 13205a9d11
38 changed files with 1545 additions and 20 deletions

View File

@@ -0,0 +1,38 @@
cmake_minimum_required(VERSION 3.0.0)
project(hyperion-xcb)
include_directories(
${CMAKE_CURRENT_BINARY_DIR}/../../libsrc/flatbufserver
${FLATBUFFERS_INCLUDE_DIRS}
)
set(Hyperion_XCB_HEADERS
XcbWrapper.h
)
set(Hyperion_XCB_SOURCES
hyperion-xcb.cpp
XcbWrapper.cpp
)
add_executable(${PROJECT_NAME}
${Hyperion_XCB_HEADERS}
${Hyperion_XCB_SOURCES}
)
target_link_libraries(${PROJECT_NAME}
commandline
hyperion-utils
flatbufserver
flatbuffers
xcb-grabber
ssdp
)
install (TARGETS ${PROJECT_NAME} DESTINATION "share/hyperion/bin" COMPONENT "hyperion_xcb")
if(CMAKE_HOST_UNIX)
install(CODE "EXECUTE_PROCESS(COMMAND ln -sf \"../share/hyperion/bin/${PROJECT_NAME}\" \"${CMAKE_BINARY_DIR}/symlink_${PROJECT_NAME}\" )" COMPONENT "hyperion_xcb" )
install(FILES "${CMAKE_BINARY_DIR}/symlink_${PROJECT_NAME}" DESTINATION "bin" RENAME "${PROJECT_NAME}" COMPONENT "hyperion_xcb" )
install(CODE "FILE (REMOVE ${CMAKE_BINARY_DIR}/symlink_${PROJECT_NAME} )" COMPONENT "hyperion_xcb" )
endif(CMAKE_HOST_UNIX)

View File

@@ -0,0 +1,47 @@
// Hyperion-Xcb includes
#include "XcbWrapper.h"
XcbWrapper::XcbWrapper(int grabInterval, int cropLeft, int cropRight, int cropTop, int cropBottom, int pixelDecimation) :
_timer(this),
_grabber(cropLeft, cropRight, cropTop, cropBottom, pixelDecimation)
{
_timer.setSingleShot(false);
_timer.setInterval(grabInterval);
// Connect capturing to the timeout signal of the timer
connect(&_timer, SIGNAL(timeout()), this, SLOT(capture()));
}
const Image<ColorRgb> & XcbWrapper::getScreenshot()
{
_grabber.grabFrame(_screenshot, true);
return _screenshot;
}
void XcbWrapper::start()
{
_timer.start();
}
void XcbWrapper::stop()
{
_timer.stop();
}
bool XcbWrapper::displayInit()
{
return _grabber.Setup();
}
void XcbWrapper::capture()
{
_grabber.grabFrame(_screenshot, !_inited);
emit sig_screenshot(_screenshot);
_inited = true;
}
void XcbWrapper::setVideoMode(const VideoMode mode)
{
_grabber.setVideoMode(mode);
}

View File

@@ -0,0 +1,57 @@
#pragma once
// QT includes
#include <QTimer>
// Hyperion-Xcb includes
#include <grabber/XcbGrabber.h>
//Utils includes
#include <utils/VideoMode.h>
class XcbWrapper : public QObject
{
Q_OBJECT
public:
XcbWrapper(int grabInterval, int cropLeft, int cropRight, int cropTop, int cropBottom, int pixelDecimation);
const Image<ColorRgb> & getScreenshot();
///
/// Starts the timed capturing of screenshots
///
void start();
void stop();
bool displayInit();
signals:
void sig_screenshot(const Image<ColorRgb> & screenshot);
public slots:
///
/// Set the video mode (2D/3D)
/// @param[in] mode The new video mode
///
void setVideoMode(const VideoMode videoMode);
private slots:
///
/// 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 grabber for creating screenshots
XcbGrabber _grabber;
Image<ColorRgb> _screenshot;
// prevent cont dimension updates
bool _inited = false;
};

View File

@@ -0,0 +1,115 @@
// QT includes
#include <QApplication>
#include <QImage>
#include <commandline/Parser.h>
#include <flatbufserver/FlatBufferConnection.h>
#include <utils/DefaultSignalHandler.h>
#include "XcbWrapper.h"
#include "HyperionConfig.h"
// ssdp discover
#include <ssdp/SSDPDiscover.h>
using namespace commandline;
// save the image as screenshot
void saveScreenshot(QString 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);
}
int main(int argc, char ** argv)
{
std::cout
<< "hyperion-xcb:" << std::endl
<< "\tVersion : " << HYPERION_VERSION << " (" << HYPERION_BUILD_ID << ")" << std::endl
<< "\tbuild time: " << __DATE__ << " " << __TIME__ << std::endl;
DefaultSignalHandler::install();
QApplication app(argc, argv);
try
{
// create the option parser and initialize all parameters
Parser parser("XCB capture application for Hyperion. Will automatically search a Hyperion server if -a option isn't used. Please note that if you have more than one server running it's more or less random which one will be used.");
IntOption & argFps = parser.add<IntOption> ('f', "framerate", "Capture frame rate [default: %1]", "10");
IntOption & argCropWidth = parser.add<IntOption> (0x0, "crop-width", "Number of pixels to crop from the left and right sides of the picture before decimation [default: %1]", "0");
IntOption & argCropHeight = parser.add<IntOption> (0x0, "crop-height", "Number of pixels to crop from the top and the bottom of the picture before decimation [default: %1]", "0");
IntOption & argCropLeft = parser.add<IntOption> (0x0, "crop-left", "Number of pixels to crop from the left of the picture before decimation (overrides --crop-width)");
IntOption & argCropRight = parser.add<IntOption> (0x0, "crop-right", "Number of pixels to crop from the right of the picture before decimation (overrides --crop-width)");
IntOption & argCropTop = parser.add<IntOption> (0x0, "crop-top", "Number of pixels to crop from the top of the picture before decimation (overrides --crop-height)");
IntOption & argCropBottom = parser.add<IntOption> (0x0, "crop-bottom", "Number of pixels to crop from the bottom of the picture before decimation (overrides --crop-height)");
IntOption & argSizeDecimation = parser.add<IntOption> ('s', "size-decimator", "Decimation factor for the output size [default=%1]", "8", 1);
BooleanOption & argScreenshot = parser.add<BooleanOption>(0x0, "screenshot", "Take a single screenshot, save it to file and quit");
Option & argAddress = parser.add<Option> ('a', "address", "Set the address of the hyperion server [default: %1]", "127.0.0.1:19400");
IntOption & argPriority = parser.add<IntOption> ('p', "priority", "Use the provided priority channel (suggested 100-199) [default: %1]", "150");
BooleanOption & argSkipReply = parser.add<BooleanOption>(0x0, "skip-reply", "Do not receive and check reply messages from Hyperion");
BooleanOption & argHelp = parser.add<BooleanOption>('h', "help", "Show this help message and exit");
// parse all options
parser.process(app);
// check if we need to display the usage. exit if we do.
if (parser.isSet(argHelp))
{
parser.showHelp(0);
}
// Create the XCB grabbing stuff
XcbWrapper xcbWrapper(
1000 / argFps.getInt(parser),
parser.isSet(argCropLeft) ? argCropLeft.getInt(parser) : argCropWidth.getInt(parser),
parser.isSet(argCropRight) ? argCropRight.getInt(parser) : argCropWidth.getInt(parser),
parser.isSet(argCropTop) ? argCropTop.getInt(parser) : argCropHeight.getInt(parser),
parser.isSet(argCropBottom) ? argCropBottom.getInt(parser) : argCropHeight.getInt(parser),
argSizeDecimation.getInt(parser)); // decimation
if (!xcbWrapper.displayInit())
return -1;
if (parser.isSet(argScreenshot))
{
// Capture a single screenshot and finish
const Image<ColorRgb> & screenshot = xcbWrapper.getScreenshot();
saveScreenshot("screenshot.png", screenshot);
}
else
{
// server searching by ssdp
QString address = argAddress.value(parser);
if(argAddress.value(parser) == "127.0.0.1:19400")
{
SSDPDiscover discover;
address = discover.getFirstService(searchType::STY_FLATBUFSERVER);
if(address.isEmpty())
{
address = argAddress.value(parser);
}
}
// Create the Flatbuf-connection
FlatBufferConnection flatbuf("XCB Standalone", address, argPriority.getInt(parser), parser.isSet(argSkipReply));
// Connect the screen capturing to flatbuf connection processing
QObject::connect(&xcbWrapper, SIGNAL(sig_screenshot(const Image<ColorRgb> &)), &flatbuf, SLOT(setImage(Image<ColorRgb>)));
// Start the capturing
xcbWrapper.start();
// Start the application
app.exec();
}
}
catch (const std::runtime_error & e)
{
// An error occured. Display error and quit
Error(Logger::getInstance("XCBGRABBER"), "%s", e.what());
return -1;
}
return 0;
}