mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
Merge branch 'add_x11'
Former-commit-id: d80efce3aa0413e4f63e9171dab438d03db9021c
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
|
||||
if (ENABLE_DISPMANX)
|
||||
add_subdirectory(dispmanx)
|
||||
endif (ENABLE_DISPMANX)
|
||||
@@ -6,3 +5,7 @@ endif (ENABLE_DISPMANX)
|
||||
if (ENABLE_V4L2)
|
||||
add_subdirectory(v4l2)
|
||||
endif (ENABLE_V4L2)
|
||||
|
||||
if (ENABLE_X11)
|
||||
add_subdirectory(x11)
|
||||
endif()
|
||||
|
@@ -9,7 +9,6 @@ SET(V4L2_QT_HEADERS
|
||||
|
||||
SET(V4L2_HEADERS
|
||||
${CURRENT_HEADER_DIR}/VideoStandard.h
|
||||
${CURRENT_HEADER_DIR}/PixelFormat.h
|
||||
)
|
||||
|
||||
SET(V4L2_SOURCES
|
||||
|
File diff suppressed because it is too large
Load Diff
37
libsrc/grabber/x11/CMakeLists.txt
Normal file
37
libsrc/grabber/x11/CMakeLists.txt
Normal file
@@ -0,0 +1,37 @@
|
||||
# Define the current source locations
|
||||
SET(CURRENT_HEADER_DIR ${CMAKE_SOURCE_DIR}/include/grabber)
|
||||
SET(CURRENT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/libsrc/grabber/x11)
|
||||
|
||||
# Find X11
|
||||
find_package(X11 REQUIRED)
|
||||
|
||||
include_directories(
|
||||
${QT_INCLUDES}
|
||||
${X11_INCLUDES}
|
||||
)
|
||||
|
||||
SET(X11_QT_HEADERS
|
||||
${CURRENT_HEADER_DIR}/X11Grabber.h
|
||||
)
|
||||
|
||||
SET(X11_HEADERS
|
||||
${CURRENT_HEADER_DIR}/X11Grabber.h
|
||||
)
|
||||
|
||||
SET(X11_SOURCES
|
||||
${CURRENT_SOURCE_DIR}/X11Grabber.cpp
|
||||
)
|
||||
|
||||
QT4_WRAP_CPP(X11_HEADERS_MOC ${X11_QT_HEADERS})
|
||||
|
||||
add_library(x11-grabber
|
||||
${X11_HEADERS}
|
||||
${X11_SOURCES}
|
||||
${X11_QT_HEADERS}
|
||||
${X11_HEADERS_MOC}
|
||||
)
|
||||
|
||||
target_link_libraries(x11-grabber
|
||||
hyperion
|
||||
${QT_LIBRARIES}
|
||||
)
|
98
libsrc/grabber/x11/X11Grabber.cpp
Normal file
98
libsrc/grabber/x11/X11Grabber.cpp
Normal file
@@ -0,0 +1,98 @@
|
||||
// STL includes
|
||||
#include <iostream>
|
||||
#include <cstdint>
|
||||
|
||||
// X11 includes
|
||||
#include <X11/Xutil.h>
|
||||
|
||||
// X11Grabber includes
|
||||
#include <grabber/X11Grabber.h>
|
||||
|
||||
X11Grabber::X11Grabber(int cropLeft, int cropRight, int cropTop, int cropBottom, int horizontalPixelDecimation, int verticalPixelDecimation) :
|
||||
_imageResampler(),
|
||||
_cropLeft(cropLeft),
|
||||
_cropRight(cropRight),
|
||||
_cropTop(cropTop),
|
||||
_cropBottom(cropBottom),
|
||||
_x11Display(nullptr),
|
||||
_screenWidth(0),
|
||||
_screenHeight(0),
|
||||
_image(0,0)
|
||||
{
|
||||
_imageResampler.setHorizontalPixelDecimation(horizontalPixelDecimation);
|
||||
_imageResampler.setVerticalPixelDecimation(verticalPixelDecimation);
|
||||
_imageResampler.setCropping(0, 0, 0, 0); // cropping is performed by XGetImage
|
||||
}
|
||||
|
||||
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 unsigned croppedWidth = _screenWidth - _cropLeft - _cropRight;
|
||||
const unsigned croppedHeight = _screenHeight - _cropTop - _cropBottom;
|
||||
|
||||
// Capture the current screen
|
||||
XImage * xImage = XGetImage(_x11Display, DefaultRootWindow(_x11Display), _cropLeft, _cropTop, croppedWidth, croppedHeight, AllPlanes, ZPixmap);
|
||||
if (xImage == nullptr)
|
||||
{
|
||||
std::cerr << "Grab failed" << std::endl;
|
||||
return _image;
|
||||
}
|
||||
|
||||
_imageResampler.processImage(reinterpret_cast<const uint8_t *>(xImage->data), xImage->width, xImage->height, xImage->bytes_per_line, PIXELFORMAT_BGR32, _image);
|
||||
|
||||
// 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;
|
||||
|
||||
return 0;
|
||||
}
|
@@ -10,7 +10,9 @@ include_directories(
|
||||
# Group the headers that go through the MOC compiler
|
||||
set(ProtoServer_QT_HEADERS
|
||||
${CURRENT_HEADER_DIR}/ProtoServer.h
|
||||
${CURRENT_HEADER_DIR}/ProtoConnection.h
|
||||
${CURRENT_SOURCE_DIR}/ProtoClientConnection.h
|
||||
${CURRENT_HEADER_DIR}/ProtoConnectionWrapper.h
|
||||
)
|
||||
|
||||
set(ProtoServer_HEADERS
|
||||
@@ -19,6 +21,8 @@ set(ProtoServer_HEADERS
|
||||
set(ProtoServer_SOURCES
|
||||
${CURRENT_SOURCE_DIR}/ProtoServer.cpp
|
||||
${CURRENT_SOURCE_DIR}/ProtoClientConnection.cpp
|
||||
${CURRENT_SOURCE_DIR}/ProtoConnection.cpp
|
||||
${CURRENT_SOURCE_DIR}/ProtoConnectionWrapper.cpp
|
||||
)
|
||||
|
||||
set(ProtoServer_PROTOS
|
||||
@@ -44,5 +48,5 @@ add_library(protoserver
|
||||
target_link_libraries(protoserver
|
||||
hyperion
|
||||
hyperion-utils
|
||||
${PROTOBUF_LIBRARIES}
|
||||
${QT_LIBRARIES})
|
||||
${PROTOBUF_LIBRARIES}
|
||||
${QT_LIBRARIES})
|
||||
|
188
libsrc/protoserver/ProtoConnection.cpp
Normal file
188
libsrc/protoserver/ProtoConnection.cpp
Normal file
@@ -0,0 +1,188 @@
|
||||
// stl includes
|
||||
#include <stdexcept>
|
||||
|
||||
// Qt includes
|
||||
#include <QRgb>
|
||||
|
||||
// protoserver includes
|
||||
#include "protoserver/ProtoConnection.h"
|
||||
|
||||
ProtoConnection::ProtoConnection(const std::string & a) :
|
||||
_socket(),
|
||||
_skipReply(false)
|
||||
{
|
||||
QString address(a.c_str());
|
||||
QStringList parts = address.split(":");
|
||||
if (parts.size() != 2)
|
||||
{
|
||||
throw std::runtime_error(QString("Wrong address: unable to parse address (%1)").arg(address).toStdString());
|
||||
}
|
||||
_host = parts[0];
|
||||
|
||||
bool ok;
|
||||
_port = parts[1].toUShort(&ok);
|
||||
if (!ok)
|
||||
{
|
||||
throw std::runtime_error(QString("Wrong address: Unable to parse the port number (%1)").arg(parts[1]).toStdString());
|
||||
}
|
||||
|
||||
// try to connect to host
|
||||
std::cout << "Connecting to Hyperion: " << _host.toStdString() << ":" << _port << std::endl;
|
||||
connectToHost();
|
||||
}
|
||||
|
||||
ProtoConnection::~ProtoConnection()
|
||||
{
|
||||
_socket.close();
|
||||
}
|
||||
|
||||
void ProtoConnection::setSkipReply(bool skip)
|
||||
{
|
||||
_skipReply = skip;
|
||||
}
|
||||
|
||||
void ProtoConnection::setColor(const ColorRgb & color, int priority, int duration)
|
||||
{
|
||||
proto::HyperionRequest request;
|
||||
request.set_command(proto::HyperionRequest::COLOR);
|
||||
proto::ColorRequest * colorRequest = request.MutableExtension(proto::ColorRequest::colorRequest);
|
||||
colorRequest->set_rgbcolor((color.red << 16) | (color.green << 8) | color.blue);
|
||||
colorRequest->set_priority(priority);
|
||||
colorRequest->set_duration(duration);
|
||||
|
||||
// send command message
|
||||
sendMessage(request);
|
||||
}
|
||||
|
||||
void ProtoConnection::setImage(const Image<ColorRgb> &image, int priority, int duration)
|
||||
{
|
||||
proto::HyperionRequest request;
|
||||
request.set_command(proto::HyperionRequest::IMAGE);
|
||||
proto::ImageRequest * imageRequest = request.MutableExtension(proto::ImageRequest::imageRequest);
|
||||
imageRequest->set_imagedata(image.memptr(), image.width() * image.height() * 3);
|
||||
imageRequest->set_imagewidth(image.width());
|
||||
imageRequest->set_imageheight(image.height());
|
||||
imageRequest->set_priority(priority);
|
||||
imageRequest->set_duration(duration);
|
||||
|
||||
// send command message
|
||||
sendMessage(request);
|
||||
}
|
||||
|
||||
void ProtoConnection::clear(int priority)
|
||||
{
|
||||
proto::HyperionRequest request;
|
||||
request.set_command(proto::HyperionRequest::CLEAR);
|
||||
proto::ClearRequest * clearRequest = request.MutableExtension(proto::ClearRequest::clearRequest);
|
||||
clearRequest->set_priority(priority);
|
||||
|
||||
// send command message
|
||||
sendMessage(request);
|
||||
}
|
||||
|
||||
void ProtoConnection::clearAll()
|
||||
{
|
||||
proto::HyperionRequest request;
|
||||
request.set_command(proto::HyperionRequest::CLEARALL);
|
||||
|
||||
// send command message
|
||||
sendMessage(request);
|
||||
}
|
||||
|
||||
void ProtoConnection::connectToHost()
|
||||
{
|
||||
_socket.connectToHost(_host, _port);
|
||||
if (_socket.waitForConnected()) {
|
||||
std::cout << "Connected to Hyperion host" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void ProtoConnection::sendMessage(const proto::HyperionRequest &message)
|
||||
{
|
||||
if (_socket.state() == QAbstractSocket::UnconnectedState)
|
||||
{
|
||||
std::cout << "Currently disconnected: trying to connect to host" << std::endl;
|
||||
connectToHost();
|
||||
}
|
||||
|
||||
if (_socket.state() != QAbstractSocket::ConnectedState)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// We only get here if we are connected
|
||||
|
||||
// serialize message (FastWriter already appends a newline)
|
||||
std::string serializedMessage = message.SerializeAsString();
|
||||
|
||||
int length = serializedMessage.size();
|
||||
const uint8_t header[] = {
|
||||
uint8_t((length >> 24) & 0xFF),
|
||||
uint8_t((length >> 16) & 0xFF),
|
||||
uint8_t((length >> 8) & 0xFF),
|
||||
uint8_t((length ) & 0xFF)};
|
||||
|
||||
// write message
|
||||
int count = 0;
|
||||
count += _socket.write(reinterpret_cast<const char *>(header), 4);
|
||||
count += _socket.write(reinterpret_cast<const char *>(serializedMessage.data()), length);
|
||||
if (!_socket.waitForBytesWritten())
|
||||
{
|
||||
std::cerr << "Error while writing data to host" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_skipReply)
|
||||
{
|
||||
// read reply data
|
||||
QByteArray serializedReply;
|
||||
length = -1;
|
||||
while (length < 0 && serializedReply.size() < length+4)
|
||||
{
|
||||
// receive reply
|
||||
if (!_socket.waitForReadyRead())
|
||||
{
|
||||
std::cerr << "Error while reading data from host" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
serializedReply += _socket.readAll();
|
||||
|
||||
if (length < 0 && serializedReply.size() >= 4)
|
||||
{
|
||||
// read the message size
|
||||
length =
|
||||
((serializedReply[0]<<24) & 0xFF000000) |
|
||||
((serializedReply[1]<<16) & 0x00FF0000) |
|
||||
((serializedReply[2]<< 8) & 0x0000FF00) |
|
||||
((serializedReply[3] ) & 0x000000FF);
|
||||
}
|
||||
}
|
||||
|
||||
// parse reply data
|
||||
proto::HyperionReply reply;
|
||||
reply.ParseFromArray(serializedReply.constData()+4, length);
|
||||
|
||||
// parse reply message
|
||||
parseReply(reply);
|
||||
}
|
||||
}
|
||||
|
||||
bool ProtoConnection::parseReply(const proto::HyperionReply &reply)
|
||||
{
|
||||
bool success = false;
|
||||
|
||||
if (!reply.success())
|
||||
{
|
||||
if (reply.has_error())
|
||||
{
|
||||
throw std::runtime_error("Error: " + reply.error());
|
||||
}
|
||||
else
|
||||
{
|
||||
throw std::runtime_error("Error: No error info");
|
||||
}
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
19
libsrc/protoserver/ProtoConnectionWrapper.cpp
Normal file
19
libsrc/protoserver/ProtoConnectionWrapper.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
// protoserver includes
|
||||
#include "protoserver/ProtoConnectionWrapper.h"
|
||||
|
||||
ProtoConnectionWrapper::ProtoConnectionWrapper(const std::string & address, int priority, int duration_ms, bool skipProtoReply) :
|
||||
_priority(priority),
|
||||
_duration_ms(duration_ms),
|
||||
_connection(address)
|
||||
{
|
||||
_connection.setSkipReply(skipProtoReply);
|
||||
}
|
||||
|
||||
ProtoConnectionWrapper::~ProtoConnectionWrapper()
|
||||
{
|
||||
}
|
||||
|
||||
void ProtoConnectionWrapper::receiveImage(const Image<ColorRgb> & image)
|
||||
{
|
||||
_connection.setImage(image, _priority, _duration_ms);
|
||||
}
|
@@ -13,6 +13,12 @@ add_library(hyperion-utils
|
||||
${CURRENT_HEADER_DIR}/Image.h
|
||||
${CURRENT_HEADER_DIR}/Sleep.h
|
||||
|
||||
${CURRENT_HEADER_DIR}/PixelFormat.h
|
||||
${CURRENT_HEADER_DIR}/VideoMode.h
|
||||
|
||||
${CURRENT_HEADER_DIR}/ImageResampler.h
|
||||
${CURRENT_SOURCE_DIR}/ImageResampler.cpp
|
||||
|
||||
${CURRENT_HEADER_DIR}/HsvTransform.h
|
||||
${CURRENT_SOURCE_DIR}/HsvTransform.cpp
|
||||
${CURRENT_HEADER_DIR}/RgbChannelTransform.h
|
||||
|
133
libsrc/utils/ImageResampler.cpp
Normal file
133
libsrc/utils/ImageResampler.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
#include "utils/ImageResampler.h"
|
||||
|
||||
ImageResampler::ImageResampler() :
|
||||
_horizontalDecimation(1),
|
||||
_verticalDecimation(1),
|
||||
_cropLeft(0),
|
||||
_cropRight(0),
|
||||
_cropTop(0),
|
||||
_cropBottom(0),
|
||||
_videoMode(VIDEO_2D)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ImageResampler::~ImageResampler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void ImageResampler::setHorizontalPixelDecimation(int decimator)
|
||||
{
|
||||
_horizontalDecimation = decimator;
|
||||
}
|
||||
|
||||
void ImageResampler::setVerticalPixelDecimation(int decimator)
|
||||
{
|
||||
_verticalDecimation = decimator;
|
||||
}
|
||||
|
||||
void ImageResampler::setCropping(int cropLeft, int cropRight, int cropTop, int cropBottom)
|
||||
{
|
||||
_cropLeft = cropLeft;
|
||||
_cropRight = cropRight;
|
||||
_cropTop = cropTop;
|
||||
_cropBottom = cropBottom;
|
||||
}
|
||||
|
||||
void ImageResampler::set3D(VideoMode mode)
|
||||
{
|
||||
_videoMode = mode;
|
||||
}
|
||||
|
||||
void ImageResampler::processImage(const uint8_t * data, int width, int height, int lineLength, PixelFormat pixelFormat, Image<ColorRgb> &outputImage) const
|
||||
{
|
||||
int cropLeft = _cropLeft;
|
||||
int cropRight = _cropRight;
|
||||
int cropTop = _cropTop;
|
||||
int cropBottom = _cropBottom;
|
||||
|
||||
// handle 3D mode
|
||||
switch (_videoMode)
|
||||
{
|
||||
case VIDEO_3DSBS:
|
||||
cropRight = width/2;
|
||||
break;
|
||||
case VIDEO_3DTAB:
|
||||
cropBottom = height/2;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// calculate the output size
|
||||
int outputWidth = (width - cropLeft - cropRight - _horizontalDecimation/2 + _horizontalDecimation - 1) / _horizontalDecimation;
|
||||
int outputHeight = (height - cropTop - cropBottom - _verticalDecimation/2 + _verticalDecimation - 1) / _verticalDecimation;
|
||||
outputImage.resize(outputWidth, outputHeight);
|
||||
|
||||
for (int yDest = 0, ySource = cropTop + _verticalDecimation/2; yDest < outputHeight; ySource += _verticalDecimation, ++yDest)
|
||||
{
|
||||
for (int xDest = 0, xSource = cropLeft + _horizontalDecimation/2; xDest < outputWidth; xSource += _horizontalDecimation, ++xDest)
|
||||
{
|
||||
ColorRgb & rgb = outputImage(xDest, yDest);
|
||||
|
||||
switch (pixelFormat)
|
||||
{
|
||||
case PIXELFORMAT_UYVY:
|
||||
{
|
||||
int index = lineLength * ySource + xSource * 2;
|
||||
uint8_t y = data[index+1];
|
||||
uint8_t u = ((xSource&1) == 0) ? data[index ] : data[index-2];
|
||||
uint8_t v = ((xSource&1) == 0) ? data[index+2] : data[index ];
|
||||
yuv2rgb(y, u, v, rgb.red, rgb.green, rgb.blue);
|
||||
}
|
||||
break;
|
||||
case PIXELFORMAT_YUYV:
|
||||
{
|
||||
int index = lineLength * ySource + xSource * 2;
|
||||
uint8_t y = data[index];
|
||||
uint8_t u = ((xSource&1) == 0) ? data[index+1] : data[index-1];
|
||||
uint8_t v = ((xSource&1) == 0) ? data[index+3] : data[index+1];
|
||||
yuv2rgb(y, u, v, rgb.red, rgb.green, rgb.blue);
|
||||
}
|
||||
break;
|
||||
case PIXELFORMAT_RGB32:
|
||||
{
|
||||
int index = lineLength * ySource + xSource * 4;
|
||||
rgb.red = data[index ];
|
||||
rgb.green = data[index+1];
|
||||
rgb.blue = data[index+2];
|
||||
}
|
||||
break;
|
||||
case PIXELFORMAT_BGR32:
|
||||
{
|
||||
int index = lineLength * ySource + xSource * 4;
|
||||
rgb.blue = data[index ];
|
||||
rgb.green = data[index+1];
|
||||
rgb.red = data[index+2];
|
||||
}
|
||||
break;
|
||||
case PIXELFORMAT_NO_CHANGE:
|
||||
std::cerr << "Invalid pixel format given" << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t ImageResampler::clamp(int x)
|
||||
{
|
||||
return (x<0) ? 0 : ((x>255) ? 255 : uint8_t(x));
|
||||
}
|
||||
|
||||
void ImageResampler::yuv2rgb(uint8_t y, uint8_t u, uint8_t v, uint8_t &r, uint8_t &g, uint8_t &b)
|
||||
{
|
||||
// see: http://en.wikipedia.org/wiki/YUV#Y.27UV444_to_RGB888_conversion
|
||||
int c = y - 16;
|
||||
int d = u - 128;
|
||||
int e = v - 128;
|
||||
|
||||
r = clamp((298 * c + 409 * e + 128) >> 8);
|
||||
g = clamp((298 * c - 100 * d - 208 * e + 128) >> 8);
|
||||
b = clamp((298 * c + 516 * d + 128) >> 8);
|
||||
}
|
Reference in New Issue
Block a user