new frame grabber handling (#137)

* - implement framegrabber type option
- framegrabber autoselect
- integrate x11 grabber in hyperiond

* add doxy

* v4l: select device by name
hyperiond: fix x11 grabber connection to kodichecker
config: tune default prios of boblight and v4l

* make v4l name finding case insensitive
This commit is contained in:
redPanther
2016-07-24 15:18:34 +02:00
committed by GitHub
parent 01ec4a3655
commit 30b9c20611
16 changed files with 548 additions and 163 deletions

View File

@@ -80,11 +80,24 @@ bool V4L2Grabber::init()
if (! _initialized)
{
getV4Ldevices();
if ( _deviceName == "auto" )
std::string v4lDevices_str;
// show list only once
if ( ! QString(_deviceName.c_str()).startsWith("/dev/") )
{
for (auto& dev: _v4lDevices)
{
Debug(_log, "check v4l2 device: %s (%s)",dev.first.c_str(), dev.second.c_str());
v4lDevices_str += "\t"+ dev.first + "\t" + dev.second + "\n";
}
Info(_log, "available V4L2 devices:\n%s", v4lDevices_str.c_str());
}
if ( _deviceName == "auto" )
{
_deviceName = "unknown";
for (auto& dev: _v4lDevices)
{
//Debug(_log, "check v4l2 device: %s (%s)",dev.first.c_str(), dev.second.c_str());
_deviceName = dev.first;
if ( init() )
{
@@ -93,6 +106,18 @@ bool V4L2Grabber::init()
}
}
}
else if ( ! QString(_deviceName.c_str()).startsWith("/dev/") )
{
for (auto& dev: _v4lDevices)
{
if ( QString(_deviceName.c_str()).toLower() == QString(dev.second.c_str()).toLower() )
{
_deviceName = dev.first;
Info(_log, "found v4l2 device with configured name: %s (%s)", dev.second.c_str(), dev.first.c_str() );
break;
}
}
}
else
{
Info(_log, "configured v4l device: %s", _deviceName.c_str());

View File

@@ -9,6 +9,9 @@ include_directories(
${QT_INCLUDES}
${X11_INCLUDES}
)
SET(X11_QT_HEADERS
${CURRENT_HEADER_DIR}/X11Wrapper.h
)
SET(X11_HEADERS
${CURRENT_HEADER_DIR}/X11Grabber.h
@@ -16,6 +19,7 @@ SET(X11_HEADERS
SET(X11_SOURCES
${CURRENT_SOURCE_DIR}/X11Grabber.cpp
${CURRENT_SOURCE_DIR}/X11Wrapper.cpp
)
QT5_WRAP_CPP(X11_HEADERS_MOC ${X11_QT_HEADERS})
@@ -29,5 +33,7 @@ add_library(x11-grabber
target_link_libraries(x11-grabber
hyperion
${QT_LIBRARIES}
${X11_LIBRARIES}
${X11_Xrender_LIB}
${QT_LIBRARIES}
)

View File

@@ -162,6 +162,49 @@ Image<ColorRgb> & X11Grabber::grab()
return _image;
}
int X11Grabber::grabFrame(Image<ColorRgb> & image)
{
if (_XRenderAvailable && !_useXGetImage) {
XRenderComposite( _x11Display, // *dpy,
PictOpSrc, // op,
_srcPicture, // src
None, // mask
_dstPicture, // dst
_cropLeft, // src_x
_cropTop, // src_y
0, // mask_x
0, // mask_y
0, // dst_x
0, // dst_y
_croppedWidth, // width
_croppedHeight); // height
XSync(_x11Display, False);
if (_XShmAvailable) {
XShmGetImage(_x11Display, _pixmap, _xImage, 0, 0, AllPlanes);
} else {
_xImage = XGetImage(_x11Display, _pixmap, 0, 0, _croppedWidth, _croppedHeight, AllPlanes, ZPixmap);
}
} else {
if (_XShmAvailable && !_useXGetImage) {
XShmGetImage(_x11Display, _window, _xImage, _cropLeft, _cropTop, AllPlanes);
} else {
_xImage = XGetImage(_x11Display, _window, _cropLeft, _cropTop, _croppedWidth, _croppedHeight, AllPlanes, ZPixmap);
}
}
if (_xImage == nullptr)
{
Error(_log, "Grab Failed!");
return -1;
}
_imageResampler.processImage(reinterpret_cast<const uint8_t *>(_xImage->data), _xImage->width, _xImage->height, _xImage->bytes_per_line, PIXELFORMAT_BGR32, image);
return 0;
}
int X11Grabber::updateScreenDimensions()
{
const Status status = XGetWindowAttributes(_x11Display, _window, &_windowAttr);
@@ -176,18 +219,15 @@ int X11Grabber::updateScreenDimensions()
// No update required
return 0;
}
Info(_log, "Update of screen resolution: [%dx%d]", _screenWidth, _screenHeight);
if (_screenWidth || _screenHeight) {
freeResources();
}
Info(_log, "Update of screen resolution: [%dx%d] to [%dx%d]", _screenWidth, _screenHeight, _windowAttr.width, _windowAttr.height);
_screenWidth = _windowAttr.width;
_screenHeight = _windowAttr.height;
Info(_log, " to [%dx%d]", _screenWidth, _screenHeight);
_croppedWidth = (_screenWidth > unsigned(_cropLeft + _cropRight))
? (_screenWidth - _cropLeft - _cropRight)
: _screenWidth;
@@ -204,5 +244,5 @@ int X11Grabber::updateScreenDimensions()
setupResources();
return 0;
return 1;
}

View File

@@ -0,0 +1,111 @@
// Hyperion includes
#include <hyperion/Hyperion.h>
#include <hyperion/ImageProcessorFactory.h>
#include <hyperion/ImageProcessor.h>
// X11 grabber includes
#include <grabber/X11Wrapper.h>
#include <grabber/X11Grabber.h>
X11Wrapper::X11Wrapper(bool useXGetImage, int cropLeft, int cropRight, int cropTop, int cropBottom, int horizontalPixelDecimation, int verticalPixelDecimation, const unsigned updateRate_Hz, const int priority)
: _updateInterval_ms(1000/updateRate_Hz)
, _timeout_ms(2 * _updateInterval_ms)
, _priority(priority)
, _timer()
// , _image(grabWidth, grabHeight)
, _grabber(new X11Grabber(useXGetImage, cropLeft, cropRight, cropTop, cropBottom, horizontalPixelDecimation, verticalPixelDecimation))
, _processor(ImageProcessorFactory::getInstance().newImageProcessor())
, _ledColors(Hyperion::getInstance()->getLedCount(), ColorRgb{0,0,0})
, _hyperion(Hyperion::getInstance())
, _init(false)
, _x11SetupSuccess(false)
{
// Configure the timer to generate events every n milliseconds
_timer.setInterval(_updateInterval_ms);
_timer.setSingleShot(false);
// Connect the QTimer to this
QObject::connect(&_timer, SIGNAL(timeout()), this, SLOT(action()));
}
X11Wrapper::~X11Wrapper()
{
// Cleanup used resources (ImageProcessor and FrameGrabber)
delete _processor;
delete _grabber;
}
void X11Wrapper::start()
{
if (! _init )
{
_init = true;
_x11SetupSuccess = _grabber->Setup();
if ( _x11SetupSuccess )
{
_x11SetupSuccess = (_grabber->updateScreenDimensions() >= 0);
_processor->setSize(_grabber->getImageWidth(), _grabber->getImageHeight());
_image.resize(_grabber->getImageWidth(), _grabber->getImageHeight());
}
}
// Start the timer with the pre configured interval
if ( _x11SetupSuccess )
{
_timer.start();
_hyperion->registerPriority("X11 Grabber", _priority);
}
ErrorIf( ! _x11SetupSuccess, Logger::getInstance("X11"), "X11 Grabber start failed");
}
void X11Wrapper::action()
{
int result = _grabber->updateScreenDimensions();
if (result < 0 )
{
return;
}
if ( result > 0 )
{
_processor->setSize(_grabber->getImageWidth(), _grabber->getImageHeight());
_image.resize(_grabber->getImageWidth(), _grabber->getImageHeight());
}
// Grab frame into the allocated image
_grabber->grabFrame(_image);
emit emitImage(_priority, _image, _timeout_ms);
_processor->process(_image, _ledColors);
_hyperion->setColors(_priority, _ledColors, _timeout_ms);
}
void X11Wrapper::stop()
{
// Stop the timer, effectivly stopping the process
_timer.stop();
_hyperion->unRegisterPriority("X11 Grabber");
}
void X11Wrapper::setGrabbingMode(const GrabbingMode mode)
{
switch (mode)
{
case GRABBINGMODE_VIDEO:
case GRABBINGMODE_PAUSE:
case GRABBINGMODE_AUDIO:
case GRABBINGMODE_PHOTO:
case GRABBINGMODE_MENU:
case GRABBINGMODE_SCREENSAVER:
case GRABBINGMODE_INVALID:
start();
break;
case GRABBINGMODE_OFF:
stop();
break;
}
}
void X11Wrapper::setVideoMode(const VideoMode mode)
{
_grabber->setVideoMode(mode);
}