diff --git a/CompileHowto.txt b/CompileHowto.txt index 621cf8fd..9f419709 100644 --- a/CompileHowto.txt +++ b/CompileHowto.txt @@ -24,11 +24,11 @@ mkdir "$HYPERION_DIR/build" cd "$HYPERION_DIR/build" # run cmake to generate make files on the raspberry pi -cmake .. +cmake -DCMAKE_BUILD_TYPE=Release -Wno-dev .. # or if you are not compiling on the raspberry pi and need to disable the Dispmanx grabber and support for spi devices -cmake -DENABLE_DISPMANX=OFF -DENABLE_SPIDEV=OFF -DENABLE_X11=ON .. +cmake -DENABLE_DISPMANX=OFF -DENABLE_SPIDEV=OFF -DENABLE_X11=ON -DCMAKE_BUILD_TYPE=Release -Wno-dev .. # as an alternative for the dispmanx grabber on non-rpi devices (e.g. cubox-i) you could try the framebuffer grabber -cmake -DENABLE_DISPMANX=OFF -DENABLE_SPIDEV=OFF -DENABLE_FB=ON .. +cmake -DENABLE_DISPMANX=OFF -DENABLE_SPIDEV=OFF -DENABLE_FB=ON -DCMAKE_BUILD_TYPE=Release -Wno-dev .. # for OSX build you need XCode, qt4 libraries and cmake. You can use macport (homebrew might work too) to install them sudo port install qt4-mac sudo port install cmake @@ -36,6 +36,12 @@ cmake -DENABLE_DISPMANX=OFF -DENABLE_SPIDEV=OFF -DENABLE_V4L2=OFF -DENABLE_OSX=O # run make to build Hyperion make +# or if you have a system with more then 1 cpu core +make -j 4 +# "4" is the number of cpu cores (e.g. 4 on RPi2), this makes compile faster + +#after compile, to remove any stuff not needed for a release version. +strip bin/* # The binaries are build in "$HYPERION_DIR/build/bin". You could copy those to /usr/bin sudo cp ./bin/hyperion-remote /usr/bin/ diff --git a/config/hyperion.config.json b/config/hyperion.config.json index 0f8404a9..291086db 100644 --- a/config/hyperion.config.json +++ b/config/hyperion.config.json @@ -23,6 +23,20 @@ "colorOrder" : "rgb" }, + /// Configuration for message forwarding to other hyperions + /// protobuffer and json remote interface are forwarded to configured hosts + /// 'proto' is mostly used for video streams and 'json' for effects + /// + /// ** pay attention which port you use. use correct ports for protols ** + /// + /// * 'proto' : list of host in form of : + /// * 'json' : list of host in form of : +/// "forwarder" : +/// { +/// "proto" : [ "127.0.0.1:19445","192.168.178.88:19445" ], +/// "json" : [ "127.0.0.1:19444","192.168.178.88:19444" ] +/// }, + /// Color manipulation configuration used to tune the output colors to specific surroundings. /// The configuration contains a list of color-transforms. Each transform contains the /// following fields: diff --git a/config/hyperion_x86.config.json b/config/hyperion_x86.config.json index 4121c499..ec21a35b 100644 --- a/config/hyperion_x86.config.json +++ b/config/hyperion_x86.config.json @@ -19,6 +19,21 @@ "colorOrder" : "rgb" }, + /// Configuration for message forwarding to other hyperions + /// protobuffer and json remote interface are forwarded to configured hosts + /// 'proto' is mostly used for video streams and 'json' for effects + /// + /// ** pay attention which port you use. use correct ports for protols ** + /// + /// * 'proto' : list of host in form of : + /// * 'json' : list of host in form of : +/// "forwarder" : +/// { +/// "proto" : [ "127.0.0.1:19445","192.168.178.88:19445" ], +/// "json" : [ "127.0.0.1:19444","192.168.178.88:19444" ] +/// }, + + /// Color manipulation configuration used to tune the output colors to specific surroundings. /// The configuration contains a list of color-transforms. Each transform contains the /// following fields: diff --git a/libsrc/grabber/amlogic/AmlogicGrabber.h b/include/grabber/AmlogicGrabber.h similarity index 100% rename from libsrc/grabber/amlogic/AmlogicGrabber.h rename to include/grabber/AmlogicGrabber.h diff --git a/include/grabber/AmlogicWrapper.h b/include/grabber/AmlogicWrapper.h index 0ce70b80..c712b60e 100644 --- a/include/grabber/AmlogicWrapper.h +++ b/include/grabber/AmlogicWrapper.h @@ -68,6 +68,9 @@ public slots: /// void setVideoMode(const VideoMode videoMode); +signals: + void emitImage(int priority, const Image & image, const int timeout_ms); + private: /// The update rate [Hz] const int _updateInterval_ms; @@ -91,4 +94,7 @@ private: /// Pointer to Hyperion for writing led values Hyperion * _hyperion; + + // forwarding enabled + bool _forward; }; diff --git a/libsrc/grabber/dispmanx/DispmanxFrameGrabber.h b/include/grabber/DispmanxFrameGrabber.h similarity index 100% rename from libsrc/grabber/dispmanx/DispmanxFrameGrabber.h rename to include/grabber/DispmanxFrameGrabber.h diff --git a/include/grabber/DispmanxWrapper.h b/include/grabber/DispmanxWrapper.h index 4868c1a0..e64adf47 100644 --- a/include/grabber/DispmanxWrapper.h +++ b/include/grabber/DispmanxWrapper.h @@ -68,6 +68,9 @@ public slots: /// void setVideoMode(const VideoMode videoMode); +signals: + void emitImage(int priority, const Image & image, const int timeout_ms); + private: /// The update rate [Hz] const int _updateInterval_ms; @@ -91,4 +94,7 @@ private: /// Pointer to Hyperion for writing led values Hyperion * _hyperion; + + // forwarding enabled + bool _forward; }; diff --git a/include/grabber/FramebufferWrapper.h b/include/grabber/FramebufferWrapper.h index f633ae55..75252d24 100644 --- a/include/grabber/FramebufferWrapper.h +++ b/include/grabber/FramebufferWrapper.h @@ -68,6 +68,9 @@ public slots: /// void setVideoMode(const VideoMode videoMode); +signals: + void emitImage(int priority, const Image & image, const int timeout_ms); + private: /// The update rate [Hz] const int _updateInterval_ms; diff --git a/include/grabber/OsxWrapper.h b/include/grabber/OsxWrapper.h index 39ac1a7e..fa597c67 100644 --- a/include/grabber/OsxWrapper.h +++ b/include/grabber/OsxWrapper.h @@ -69,6 +69,9 @@ public slots: /// void setVideoMode(const VideoMode videoMode); +signals: + void emitImage(int priority, const Image & image, const int timeout_ms); + private: /// The update rate [Hz] const int _updateInterval_ms; diff --git a/include/grabber/V4L2Wrapper.h b/include/grabber/V4L2Wrapper.h index 2a9ece2c..2b8dc74a 100644 --- a/include/grabber/V4L2Wrapper.h +++ b/include/grabber/V4L2Wrapper.h @@ -44,6 +44,7 @@ public slots: signals: void emitColors(int priority, const std::vector &ledColors, const int timeout_ms); + void emitImage(int priority, const Image & image, const int timeout_ms); private slots: void newFrame(const Image & image); diff --git a/include/hyperion/Hyperion.h b/include/hyperion/Hyperion.h index b6ed0946..811f0199 100644 --- a/include/hyperion/Hyperion.h +++ b/include/hyperion/Hyperion.h @@ -13,6 +13,7 @@ // Hyperion includes #include #include +#include // Effect engine includes #include @@ -125,6 +126,10 @@ public slots: /// Tell Hyperion that the transforms have changed and the leds need to be updated void transformsUpdated(); + /// Returns MessageForwarder Object + /// @return instance of message forwarder object + MessageForwarder * getForwarder(); + /// /// Clears the given priority channel. This will switch the led-colors to the colors of the next /// lower priority channel (or off if no more channels are set) @@ -168,6 +173,7 @@ public: static RgbChannelTransform * createRgbChannelTransform(const Json::Value& colorConfig); static LedDevice * createColorSmoothing(const Json::Value & smoothingConfig, LedDevice * ledDevice); + static MessageForwarder * createMessageForwarder(const Json::Value & forwarderConfig); signals: /// Signal which is emitted when a priority channel is actively cleared @@ -201,6 +207,9 @@ private: /// Effect engine EffectEngine * _effectEngine; + // proto and json Message forwarder + MessageForwarder * _messageForwarder; + /// The timer for handling priority channel timeouts QTimer _timer; }; diff --git a/include/hyperion/MessageForwarder.h b/include/hyperion/MessageForwarder.h new file mode 100644 index 00000000..a762ae41 --- /dev/null +++ b/include/hyperion/MessageForwarder.h @@ -0,0 +1,38 @@ +#pragma once + +// STL includes +#include +#include +#include +#include + +// QT includes +#include +#include +#include + +// Utils includes +#include +class MessageForwarder +{ +public: + + struct JsonSlaveAddress { + QHostAddress addr; + quint16 port; + }; + + MessageForwarder(); + ~MessageForwarder(); + + void addJsonSlave(std::string slave); + void addProtoSlave(std::string slave); + + bool protoForwardingEnabled(); + QStringList getProtoSlaves(); + QList getJsonSlaves(); + +private: + QStringList _protoSlaves; + QList _jsonSlaves; +}; diff --git a/include/protoserver/ProtoConnection.h b/include/protoserver/ProtoConnection.h index bb41e1b7..7ecd8f36 100644 --- a/include/protoserver/ProtoConnection.h +++ b/include/protoserver/ProtoConnection.h @@ -7,6 +7,7 @@ #include #include #include +#include #include // hyperion util @@ -19,8 +20,11 @@ /// /// Connection class to setup an connection to the hyperion server and execute commands /// -class ProtoConnection +class ProtoConnection : public QObject { + +Q_OBJECT + public: /// /// Constructor @@ -67,10 +71,6 @@ public: /// void clearAll(); -private: - /// Try to connect to the Hyperion host - void connectToHost(); - /// /// Send a command message and receive its reply /// @@ -78,6 +78,13 @@ private: /// void sendMessage(const proto::HyperionRequest & message); +private slots: + /// Try to connect to the Hyperion host + void connectToHost(); + + +private: + /// /// Parse a reply message /// @@ -99,4 +106,7 @@ private: /// Skip receiving reply messages from Hyperion if set bool _skipReply; + + QTimer _timer; + QAbstractSocket::SocketState _prevSocketState; }; diff --git a/include/protoserver/ProtoServer.h b/include/protoserver/ProtoServer.h index dae57481..520848a9 100644 --- a/include/protoserver/ProtoServer.h +++ b/include/protoserver/ProtoServer.h @@ -6,11 +6,23 @@ // Qt includes #include #include +#include +#include // Hyperion includes #include +// hyperion includes +#include +#include + +// forward decl class ProtoClientConnection; +class ProtoConnection; + +namespace proto { +class HyperionRequest; +} /// /// This class creates a TCP server which accepts connections wich can then send @@ -35,6 +47,9 @@ public: /// uint16_t getPort() const; +public slots: + void sendImageToProtoSlaves(int priority, const Image & image, int duration_ms); + private slots: /// /// Slot which is called when a client tries to create a new connection @@ -47,6 +62,8 @@ private slots: /// void closedConnection(ProtoClientConnection * connection); + void newMessage(const proto::HyperionRequest * message); + private: /// Hyperion instance Hyperion * _hyperion; @@ -56,4 +73,9 @@ private: /// List with open connections QSet _openConnections; + QStringList _forwardClients; + + /// Hyperion proto connection object for forwarding + QList _proxy_connections; + }; diff --git a/include/utils/Image.h b/include/utils/Image.h index 0a8ca31a..ece6155a 100644 --- a/include/utils/Image.h +++ b/include/utils/Image.h @@ -5,6 +5,8 @@ #include #include #include +#include + template class Image @@ -183,6 +185,25 @@ public: { return _pixels; } + + + /// + /// Convert image of any color order to a RGB image. + /// + /// @param[out] image The image that buffers the output + /// + void toRgb(Image& image) + { + image.resize(_width, _height); + const unsigned imageSize = _width * _height; + + for (unsigned idx=0; idx // Local includes -#include "AmlogicGrabber.h" +#include // Flags copied from 'include/linux/amlogic/amports/amvideocap.h' at https://github.com/codesnake/linux-amlogic #define AMVIDEOCAP_IOC_MAGIC 'V' diff --git a/libsrc/grabber/amlogic/AmlogicWrapper.cpp b/libsrc/grabber/amlogic/AmlogicWrapper.cpp index 1dca6764..29022afe 100644 --- a/libsrc/grabber/amlogic/AmlogicWrapper.cpp +++ b/libsrc/grabber/amlogic/AmlogicWrapper.cpp @@ -9,7 +9,7 @@ // Amlogic grabber includes #include -#include "AmlogicGrabber.h" +#include AmlogicWrapper::AmlogicWrapper(const unsigned grabWidth, const unsigned grabHeight, const unsigned updateRate_Hz, Hyperion * hyperion) : @@ -26,6 +26,7 @@ AmlogicWrapper::AmlogicWrapper(const unsigned grabWidth, const unsigned grabHeig // Configure the timer to generate events every n milliseconds _timer.setInterval(_updateInterval_ms); _timer.setSingleShot(false); + _forward = _hyperion->getForwarder()->protoForwardingEnabled(); _processor->setSize(grabWidth, grabHeight); @@ -55,8 +56,14 @@ void AmlogicWrapper::action() return; } - _processor->process(_image, _ledColors); + if ( _forward ) + { + Image image_rgb; + _image.toRgb(image_rgb); + emit emitImage(_priority, image_rgb, _timeout_ms); + } + _processor->process(_image, _ledColors); _hyperion->setColors(_priority, _ledColors, _timeout_ms); } diff --git a/libsrc/grabber/amlogic/CMakeLists.txt b/libsrc/grabber/amlogic/CMakeLists.txt index be6cb97d..9ec98bfe 100644 --- a/libsrc/grabber/amlogic/CMakeLists.txt +++ b/libsrc/grabber/amlogic/CMakeLists.txt @@ -7,7 +7,7 @@ SET(CURRENT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/libsrc/grabber/amlogic) SET(AmlogicQT_HEADERS ${CURRENT_HEADER_DIR}/AmlogicWrapper.h) SET(AmlogicHEADERS - ${CURRENT_SOURCE_DIR}/AmlogicGrabber.h + ${CURRENT_HEADER_DIR}/AmlogicGrabber.h ) SET(AmlogicSOURCES diff --git a/libsrc/grabber/dispmanx/CMakeLists.txt b/libsrc/grabber/dispmanx/CMakeLists.txt index d4d64b72..27c1f11c 100644 --- a/libsrc/grabber/dispmanx/CMakeLists.txt +++ b/libsrc/grabber/dispmanx/CMakeLists.txt @@ -1,40 +1,41 @@ - -# Find the BCM-package (VC control) -find_package(BCM REQUIRED) -include_directories(${BCM_INCLUDE_DIRS}) - -# Define the current source locations -SET(CURRENT_HEADER_DIR ${CMAKE_SOURCE_DIR}/include/grabber) -SET(CURRENT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/libsrc/grabber/dispmanx) - -# Group the headers that go through the MOC compiler -SET(DispmanxGrabberQT_HEADERS - ${CURRENT_HEADER_DIR}/DispmanxWrapper.h -) - -SET(DispmanxGrabberHEADERS - ${CURRENT_SOURCE_DIR}/DispmanxFrameGrabber.h -) - -SET(DispmanxGrabberSOURCES - ${CURRENT_SOURCE_DIR}/DispmanxWrapper.cpp - ${CURRENT_SOURCE_DIR}/DispmanxFrameGrabber.cpp -) - -if(ENABLE_QT5) -QT5_WRAP_CPP(DispmanxGrabberHEADERS_MOC ${DispmanxGrabberQT_HEADERS}) -else(ENABLE_QT5) -QT4_WRAP_CPP(DispmanxGrabberHEADERS_MOC ${DispmanxGrabberQT_HEADERS}) -endif(ENABLE_QT5) - -add_library(dispmanx-grabber - ${DispmanxGrabberHEADERS} - ${DispmanxGrabberQT_HEADERS} - ${DispmanxGrabberHEADERS_MOC} - ${DispmanxGrabberSOURCES} -) - -target_link_libraries(dispmanx-grabber - hyperion - ${QT_LIBRARIES} - ${BCM_LIBRARIES}) + +# Find the BCM-package (VC control) +find_package(BCM REQUIRED) +include_directories(${BCM_INCLUDE_DIRS}) + +# Define the current source locations +SET(CURRENT_HEADER_DIR ${CMAKE_SOURCE_DIR}/include/grabber) +SET(CURRENT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/libsrc/grabber/dispmanx) + +# Group the headers that go through the MOC compiler +SET(DispmanxGrabberQT_HEADERS + ${CURRENT_HEADER_DIR}/DispmanxWrapper.h +) + +SET(DispmanxGrabberHEADERS + ${CURRENT_HEADER_DIR}/DispmanxFrameGrabber.h +) + +SET(DispmanxGrabberSOURCES + ${CURRENT_SOURCE_DIR}/DispmanxWrapper.cpp + ${CURRENT_SOURCE_DIR}/DispmanxFrameGrabber.cpp +) + +if(ENABLE_QT5) + QT5_WRAP_CPP(DispmanxGrabberHEADERS_MOC ${DispmanxGrabberQT_HEADERS}) +else(ENABLE_QT5) + QT4_WRAP_CPP(DispmanxGrabberHEADERS_MOC ${DispmanxGrabberQT_HEADERS}) +endif(ENABLE_QT5) + +add_library(dispmanx-grabber + ${DispmanxGrabberHEADERS} + ${DispmanxGrabberQT_HEADERS} + ${DispmanxGrabberHEADERS_MOC} + ${DispmanxGrabberSOURCES} +) + +target_link_libraries(dispmanx-grabber + hyperion + ${QT_LIBRARIES} + ${BCM_LIBRARIES} +) diff --git a/libsrc/grabber/dispmanx/DispmanxFrameGrabber.cpp b/libsrc/grabber/dispmanx/DispmanxFrameGrabber.cpp index 1f6516a4..53397acf 100644 --- a/libsrc/grabber/dispmanx/DispmanxFrameGrabber.cpp +++ b/libsrc/grabber/dispmanx/DispmanxFrameGrabber.cpp @@ -4,7 +4,7 @@ #include // Local includes -#include "DispmanxFrameGrabber.h" +#include "grabber/DispmanxFrameGrabber.h" DispmanxFrameGrabber::DispmanxFrameGrabber(const unsigned width, const unsigned height) : _vc_display(0), diff --git a/libsrc/grabber/dispmanx/DispmanxWrapper.cpp b/libsrc/grabber/dispmanx/DispmanxWrapper.cpp index c9d2b22c..7868ce34 100644 --- a/libsrc/grabber/dispmanx/DispmanxWrapper.cpp +++ b/libsrc/grabber/dispmanx/DispmanxWrapper.cpp @@ -9,7 +9,7 @@ // Dispmanx grabber includes #include -#include "DispmanxFrameGrabber.h" +#include DispmanxWrapper::DispmanxWrapper(const unsigned grabWidth, const unsigned grabHeight, const unsigned updateRate_Hz, Hyperion * hyperion) : @@ -28,6 +28,7 @@ DispmanxWrapper::DispmanxWrapper(const unsigned grabWidth, const unsigned grabHe _timer.setSingleShot(false); _processor->setSize(grabWidth, grabHeight); + _forward = _hyperion->getForwarder()->protoForwardingEnabled(); // Connect the QTimer to this QObject::connect(&_timer, SIGNAL(timeout()), this, SLOT(action())); @@ -51,10 +52,17 @@ void DispmanxWrapper::action() // Grab frame into the allocated image _frameGrabber->grabFrame(_image); - _processor->process(_image, _ledColors); + if ( _forward ) + { + Image image_rgb; + _image.toRgb(image_rgb); + emit emitImage(_priority, image_rgb, _timeout_ms); + } + _processor->process(_image, _ledColors); _hyperion->setColors(_priority, _ledColors, _timeout_ms); } + void DispmanxWrapper::stop() { // Stop the timer, effectivly stopping the process diff --git a/libsrc/grabber/framebuffer/FramebufferWrapper.cpp b/libsrc/grabber/framebuffer/FramebufferWrapper.cpp index abc6400c..96506d55 100644 --- a/libsrc/grabber/framebuffer/FramebufferWrapper.cpp +++ b/libsrc/grabber/framebuffer/FramebufferWrapper.cpp @@ -46,8 +46,9 @@ void FramebufferWrapper::action() // Grab frame into the allocated image _frameGrabber->grabFrame(_image); - _processor->process(_image, _ledColors); + emit emitImage(_priority, _image, _timeout_ms); + _processor->process(_image, _ledColors); _hyperion->setColors(_priority, _ledColors, _timeout_ms); } void FramebufferWrapper::stop() diff --git a/libsrc/grabber/osx/OsxWrapper.cpp b/libsrc/grabber/osx/OsxWrapper.cpp index 6d6f9a00..da8b039e 100644 --- a/libsrc/grabber/osx/OsxWrapper.cpp +++ b/libsrc/grabber/osx/OsxWrapper.cpp @@ -46,8 +46,9 @@ void OsxWrapper::action() // Grab frame into the allocated image _frameGrabber->grabFrame(_image); - _processor->process(_image, _ledColors); + emit emitImage(_priority, _image, _timeout_ms); + _processor->process(_image, _ledColors); _hyperion->setColors(_priority, _ledColors, _timeout_ms); } void OsxWrapper::stop() diff --git a/libsrc/grabber/v4l2/V4L2Wrapper.cpp b/libsrc/grabber/v4l2/V4L2Wrapper.cpp index 76b16ab7..cbccf41b 100644 --- a/libsrc/grabber/v4l2/V4L2Wrapper.cpp +++ b/libsrc/grabber/v4l2/V4L2Wrapper.cpp @@ -94,6 +94,9 @@ void V4L2Wrapper::newFrame(const Image &image) // process the new image _processor->process(image, _ledColors); + // forward to other hyperions + emit emitImage(_priority, image, _timeout_ms); + // send colors to Hyperion emit emitColors(_priority, _ledColors, _timeout_ms); } diff --git a/libsrc/hyperion/CMakeLists.txt b/libsrc/hyperion/CMakeLists.txt index 1434f105..ebd25254 100644 --- a/libsrc/hyperion/CMakeLists.txt +++ b/libsrc/hyperion/CMakeLists.txt @@ -1,66 +1,68 @@ - -# Define the current source locations -SET(CURRENT_HEADER_DIR ${CMAKE_SOURCE_DIR}/include/hyperion) -SET(CURRENT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/libsrc/hyperion) - -# Group the headers that go through the MOC compiler -SET(Hyperion_QT_HEADERS - ${CURRENT_HEADER_DIR}/Hyperion.h - - ${CURRENT_SOURCE_DIR}/LinearColorSmoothing.h -) - -SET(Hyperion_HEADERS - ${CURRENT_HEADER_DIR}/ImageProcessor.h - ${CURRENT_HEADER_DIR}/ImageProcessorFactory.h - ${CURRENT_HEADER_DIR}/ImageToLedsMap.h - ${CURRENT_HEADER_DIR}/LedString.h - ${CURRENT_HEADER_DIR}/PriorityMuxer.h - - ${CURRENT_SOURCE_DIR}/MultiColorTransform.h -) - -SET(Hyperion_SOURCES - ${CURRENT_SOURCE_DIR}/Hyperion.cpp - ${CURRENT_SOURCE_DIR}/ImageProcessor.cpp - ${CURRENT_SOURCE_DIR}/ImageProcessorFactory.cpp - ${CURRENT_SOURCE_DIR}/LedString.cpp - ${CURRENT_SOURCE_DIR}/PriorityMuxer.cpp - - ${CURRENT_SOURCE_DIR}/ImageToLedsMap.cpp - ${CURRENT_SOURCE_DIR}/MultiColorTransform.cpp - ${CURRENT_SOURCE_DIR}/LinearColorSmoothing.cpp -) - -set(Hyperion_RESOURCES - ${CURRENT_SOURCE_DIR}/resource.qrc -) - -if(ENABLE_QT5) -QT5_WRAP_CPP(Hyperion_HEADERS_MOC ${Hyperion_QT_HEADERS}) -QT5_ADD_RESOURCES(Hyperion_RESOURCES_RCC ${Hyperion_RESOURCES} OPTIONS "-no-compress") -else(ENABLE_QT5) -QT4_WRAP_CPP(Hyperion_HEADERS_MOC ${Hyperion_QT_HEADERS}) -QT4_ADD_RESOURCES(Hyperion_RESOURCES_RCC ${Hyperion_RESOURCES} OPTIONS "-no-compress") -endif(ENABLE_QT5) - -add_library(hyperion - ${Hyperion_HEADERS} - ${Hyperion_QT_HEADERS} - ${Hyperion_HEADERS_MOC} - ${Hyperion_SOURCES} - ${Hyperion_RESOURCES_RCC} -) - -if(ENABLE_QT5) -qt5_use_modules(hyperion Widgets) -endif(ENABLE_QT5) - -target_link_libraries(hyperion - blackborder - hyperion-utils - leddevice - effectengine - serialport - ${QT_LIBRARIES} -) + +# Define the current source locations +SET(CURRENT_HEADER_DIR ${CMAKE_SOURCE_DIR}/include/hyperion) +SET(CURRENT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/libsrc/hyperion) + +# Group the headers that go through the MOC compiler +SET(Hyperion_QT_HEADERS + ${CURRENT_HEADER_DIR}/Hyperion.h + + ${CURRENT_SOURCE_DIR}/LinearColorSmoothing.h +) + +SET(Hyperion_HEADERS + ${CURRENT_HEADER_DIR}/ImageProcessor.h + ${CURRENT_HEADER_DIR}/ImageProcessorFactory.h + ${CURRENT_HEADER_DIR}/ImageToLedsMap.h + ${CURRENT_HEADER_DIR}/LedString.h + ${CURRENT_HEADER_DIR}/PriorityMuxer.h + + ${CURRENT_SOURCE_DIR}/MultiColorTransform.h + ${CURRENT_HEADER_DIR}/MessageForwarder.h +) + +SET(Hyperion_SOURCES + ${CURRENT_SOURCE_DIR}/Hyperion.cpp + ${CURRENT_SOURCE_DIR}/ImageProcessor.cpp + ${CURRENT_SOURCE_DIR}/ImageProcessorFactory.cpp + ${CURRENT_SOURCE_DIR}/LedString.cpp + ${CURRENT_SOURCE_DIR}/PriorityMuxer.cpp + + ${CURRENT_SOURCE_DIR}/ImageToLedsMap.cpp + ${CURRENT_SOURCE_DIR}/MultiColorTransform.cpp + ${CURRENT_SOURCE_DIR}/LinearColorSmoothing.cpp + ${CURRENT_SOURCE_DIR}/MessageForwarder.cpp +) + +set(Hyperion_RESOURCES + ${CURRENT_SOURCE_DIR}/resource.qrc +) + +if(ENABLE_QT5) +QT5_WRAP_CPP(Hyperion_HEADERS_MOC ${Hyperion_QT_HEADERS}) +QT5_ADD_RESOURCES(Hyperion_RESOURCES_RCC ${Hyperion_RESOURCES} OPTIONS "-no-compress") +else(ENABLE_QT5) +QT4_WRAP_CPP(Hyperion_HEADERS_MOC ${Hyperion_QT_HEADERS}) +QT4_ADD_RESOURCES(Hyperion_RESOURCES_RCC ${Hyperion_RESOURCES} OPTIONS "-no-compress") +endif(ENABLE_QT5) + +add_library(hyperion + ${Hyperion_HEADERS} + ${Hyperion_QT_HEADERS} + ${Hyperion_HEADERS_MOC} + ${Hyperion_SOURCES} + ${Hyperion_RESOURCES_RCC} +) + +if(ENABLE_QT5) +qt5_use_modules(hyperion Widgets) +endif(ENABLE_QT5) + +target_link_libraries(hyperion + blackborder + hyperion-utils + leddevice + effectengine + serialport + ${QT_LIBRARIES} +) diff --git a/libsrc/hyperion/Hyperion.cpp b/libsrc/hyperion/Hyperion.cpp index 37c77927..2463ed15 100644 --- a/libsrc/hyperion/Hyperion.cpp +++ b/libsrc/hyperion/Hyperion.cpp @@ -267,12 +267,45 @@ LedDevice * Hyperion::createColorSmoothing(const Json::Value & smoothingConfig, } +MessageForwarder * Hyperion::createMessageForwarder(const Json::Value & forwarderConfig) +{ + MessageForwarder * forwarder = new MessageForwarder(); + if ( ! forwarderConfig.isNull() ) + { + if ( ! forwarderConfig["json"].isNull() && forwarderConfig["json"].isArray() ) + { + for (const Json::Value& addr : forwarderConfig["json"]) + { + std::cout << "Json forward to " << addr.asString() << std::endl; + forwarder->addJsonSlave(addr.asString()); + } + } + + if ( ! forwarderConfig["proto"].isNull() && forwarderConfig["proto"].isArray() ) + { + for (const Json::Value& addr : forwarderConfig["proto"]) + { + std::cout << "Proto forward to " << addr.asString() << std::endl; + forwarder->addProtoSlave(addr.asString()); + } + } + } + + return forwarder; +} + +MessageForwarder * Hyperion::getForwarder() +{ + return _messageForwarder; +} + Hyperion::Hyperion(const Json::Value &jsonConfig) : _ledString(createLedString(jsonConfig["leds"], createColorOrder(jsonConfig["device"]))), _muxer(_ledString.leds().size()), _raw2ledTransform(createLedColorsTransform(_ledString.leds().size(), jsonConfig["color"])), _device(LedDeviceFactory::construct(jsonConfig["device"])), _effectEngine(nullptr), + _messageForwarder(createMessageForwarder(jsonConfig["forwarder"])), _timer() { if (!_raw2ledTransform->verifyTransforms()) @@ -314,6 +347,9 @@ Hyperion::~Hyperion() // delete the color transform delete _raw2ledTransform; + + // delete the message forwarder + delete _messageForwarder; } unsigned Hyperion::getLedCount() const diff --git a/libsrc/hyperion/MessageForwarder.cpp b/libsrc/hyperion/MessageForwarder.cpp new file mode 100644 index 00000000..aa6e4e0c --- /dev/null +++ b/libsrc/hyperion/MessageForwarder.cpp @@ -0,0 +1,51 @@ +// STL includes +#include + +#include + + +MessageForwarder::MessageForwarder() +{ +} + +MessageForwarder::~MessageForwarder() +{ +} + + +void MessageForwarder::addJsonSlave(std::string slave) +{ + QStringList parts = QString(slave.c_str()).split(":"); + if (parts.size() != 2) + throw std::runtime_error(QString("Wrong address: unable to parse address (%1)").arg(slave.c_str()).toStdString()); + + bool ok; + quint16 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()); + + JsonSlaveAddress c; + c.addr = QHostAddress(parts[0]); + c.port = port; + _jsonSlaves << c; +} + +void MessageForwarder::addProtoSlave(std::string slave) +{ + _protoSlaves << QString(slave.c_str()); +} + +QStringList MessageForwarder::getProtoSlaves() +{ + return _protoSlaves; +} + +QList MessageForwarder::getJsonSlaves() +{ + return _jsonSlaves; +} + +bool MessageForwarder::protoForwardingEnabled() +{ + return ! _protoSlaves.empty(); +} diff --git a/libsrc/jsonserver/JsonClientConnection.cpp b/libsrc/jsonserver/JsonClientConnection.cpp index 87fb7c1f..e4dbc096 100644 --- a/libsrc/jsonserver/JsonClientConnection.cpp +++ b/libsrc/jsonserver/JsonClientConnection.cpp @@ -16,6 +16,7 @@ // hyperion util includes #include #include +#include #include #include @@ -250,8 +251,27 @@ void JsonClientConnection::handleMessage(const std::string &messageString) handleNotImplemented(); } + +void JsonClientConnection::forwardJsonMessage(const Json::Value & message) +{ + QTcpSocket client; + QList list = _hyperion->getForwarder()->getJsonSlaves(); + + for ( int i=0; iclearall(); @@ -530,10 +558,51 @@ void JsonClientConnection::sendMessage(const Json::Value &message) response.append(serializedReply.c_str(), serializedReply.length()); - _socket->write(response.data(), response.length()); + _socket->write(response.data(), response.length()); } } + +void JsonClientConnection::sendMessage(const Json::Value & message, QTcpSocket * socket) +{ + // serialize message (FastWriter already appends a newline) + std::string serializedMessage = Json::FastWriter().write(message); + + // write message + socket->write(serializedMessage.c_str()); + if (!socket->waitForBytesWritten()) + { + //std::cout << "Error while writing data to host" << std::endl; + return; + } + + // read reply data + QByteArray serializedReply; + while (!serializedReply.contains('\n')) + { + // receive reply + if (!socket->waitForReadyRead()) + { + //std::cout << "Error while reading data from host" << std::endl; + return; + } + + serializedReply += socket->readAll(); + } + int bytes = serializedReply.indexOf('\n') + 1; // Find the end of message + + // parse reply data + Json::Reader jsonReader; + Json::Value reply; + if (!jsonReader.parse(serializedReply.constData(), serializedReply.constData() + bytes, reply)) + { + //std::cout << "Error while parsing reply: invalid json" << std::endl; + return; + } + +} + + void JsonClientConnection::sendSuccessReply() { // create reply diff --git a/libsrc/jsonserver/JsonClientConnection.h b/libsrc/jsonserver/JsonClientConnection.h index 6575388a..01db54b1 100644 --- a/libsrc/jsonserver/JsonClientConnection.h +++ b/libsrc/jsonserver/JsonClientConnection.h @@ -124,6 +124,7 @@ private: /// @param message The JSON message to send /// void sendMessage(const Json::Value & message); + void sendMessage(const Json::Value & message, QTcpSocket * socket); /// /// Send a standard reply indicating success @@ -147,6 +148,11 @@ private: /// void handleWebSocketFrame(); + /// + /// forward json message + /// + void forwardJsonMessage(const Json::Value & message); + private: /// /// Check if a JSON messag is valid according to a given JSON schema diff --git a/libsrc/jsonserver/JsonServer.cpp b/libsrc/jsonserver/JsonServer.cpp index 4efe855c..4cd7f411 100644 --- a/libsrc/jsonserver/JsonServer.cpp +++ b/libsrc/jsonserver/JsonServer.cpp @@ -16,6 +16,14 @@ JsonServer::JsonServer(Hyperion *hyperion, uint16_t port) : throw std::runtime_error("Json server could not bind to port"); } + QList list = _hyperion->getForwarder()->getJsonSlaves(); + for ( int i=0; i #include +#include // Hyperion includes #include // proto includes #include "message.pb.h" +#include "protoserver/ProtoConnection.h" class ImageProcessor; @@ -41,6 +43,7 @@ signals: /// @param connection This connection object /// void connectionClosed(ProtoClientConnection * connection); + void newMessage(const proto::HyperionRequest * message); private slots: /// diff --git a/libsrc/protoserver/ProtoConnection.cpp b/libsrc/protoserver/ProtoConnection.cpp index d7a6037e..5eb5e53c 100644 --- a/libsrc/protoserver/ProtoConnection.cpp +++ b/libsrc/protoserver/ProtoConnection.cpp @@ -9,7 +9,8 @@ ProtoConnection::ProtoConnection(const std::string & a) : _socket(), - _skipReply(false) + _skipReply(false), + _prevSocketState(QAbstractSocket::UnconnectedState) { QString address(a.c_str()); QStringList parts = address.split(":"); @@ -29,10 +30,18 @@ ProtoConnection::ProtoConnection(const std::string & a) : // try to connect to host std::cout << "Connecting to Hyperion: " << _host.toStdString() << ":" << _port << std::endl; connectToHost(); + + // start the connection timer + _timer.setInterval(5000); + _timer.setSingleShot(false); + + connect(&_timer,SIGNAL(timeout()), this, SLOT(connectToHost()) ); + _timer.start(); } ProtoConnection::~ProtoConnection() { + _timer.stop(); _socket.close(); } @@ -91,20 +100,37 @@ void ProtoConnection::clearAll() void ProtoConnection::connectToHost() { - _socket.connectToHost(_host, _port); - if (_socket.waitForConnected()) { - std::cout << "Connected to Hyperion host" << std::endl; + // try connection only when + if (_socket.state() == QAbstractSocket::UnconnectedState) + { + _socket.connectToHost(_host, _port); + //_socket.waitForConnected(1000); } } void ProtoConnection::sendMessage(const proto::HyperionRequest &message) { - if (_socket.state() == QAbstractSocket::UnconnectedState) + // print out connection message only when state is changed + if (_socket.state() != _prevSocketState ) { - std::cout << "Currently disconnected: trying to connect to host" << std::endl; - connectToHost(); + switch (_socket.state() ) + { + case QAbstractSocket::UnconnectedState: + std::cout << "No connection to Hyperion: " << _host.toStdString() << ":" << _port << std::endl; + break; + + case QAbstractSocket::ConnectedState: + std::cout << "Connected to Hyperion: " << _host.toStdString() << ":" << _port << std::endl; + break; + + default: + //std::cout << "Connecting to Hyperion: " << _host.toStdString() << ":" << _port << std::endl; + break; + } + _prevSocketState = _socket.state(); } + if (_socket.state() != QAbstractSocket::ConnectedState) { return; diff --git a/libsrc/protoserver/ProtoServer.cpp b/libsrc/protoserver/ProtoServer.cpp index a1afa30c..c4eb82ca 100644 --- a/libsrc/protoserver/ProtoServer.cpp +++ b/libsrc/protoserver/ProtoServer.cpp @@ -2,7 +2,9 @@ #include // project includes +#include #include +#include "protoserver/ProtoConnection.h" #include "ProtoClientConnection.h" ProtoServer::ProtoServer(Hyperion *hyperion, uint16_t port) : @@ -11,6 +13,20 @@ ProtoServer::ProtoServer(Hyperion *hyperion, uint16_t port) : _server(), _openConnections() { + + MessageForwarder * forwarder = hyperion->getForwarder(); + QStringList slaves = forwarder->getProtoSlaves(); + + for (int i = 0; i < slaves.size(); ++i) { + if ( QString("127.0.0.1:%1").arg(port) == slaves.at(i) ) { + throw std::runtime_error("Loop between proto server and forwarder detected. Fix your config!"); + } + + ProtoConnection* p = new ProtoConnection(slaves.at(i).toLocal8Bit().constData()); + p->setSkipReply(true); + _proxy_connections << p; + } + if (!_server.listen(QHostAddress::Any, port)) { throw std::runtime_error("Proto server could not bind to port"); @@ -25,6 +41,9 @@ ProtoServer::~ProtoServer() foreach (ProtoClientConnection * connection, _openConnections) { delete connection; } + + while (!_proxy_connections.isEmpty()) + delete _proxy_connections.takeFirst(); } uint16_t ProtoServer::getPort() const @@ -44,9 +63,23 @@ void ProtoServer::newConnection() // register slot for cleaning up after the connection closed connect(connection, SIGNAL(connectionClosed(ProtoClientConnection*)), this, SLOT(closedConnection(ProtoClientConnection*))); + connect(connection, SIGNAL(newMessage(const proto::HyperionRequest*)), this, SLOT(newMessage(const proto::HyperionRequest*))); + } } +void ProtoServer::newMessage(const proto::HyperionRequest * message) +{ + for (int i = 0; i < _proxy_connections.size(); ++i) + _proxy_connections.at(i)->sendMessage(*message); +} + +void ProtoServer::sendImageToProtoSlaves(int priority, const Image & image, int duration_ms) +{ + for (int i = 0; i < _proxy_connections.size(); ++i) + _proxy_connections.at(i)->setImage(image, priority, duration_ms); +} + void ProtoServer::closedConnection(ProtoClientConnection *connection) { std::cout << "Proto connection closed" << std::endl; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2a291850..f55af750 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,4 +15,9 @@ if(ENABLE_PROTOBUF) if(ENABLE_X11) add_subdirectory(hyperion-x11) endif() + + if(ENABLE_DISPMANX) + add_subdirectory(hyperion-dispmanx) + endif() + endif() diff --git a/src/hyperion-aml/AmlogicWrapper.cpp b/src/hyperion-aml/AmlogicWrapper.cpp new file mode 100644 index 00000000..e4c1f7f1 --- /dev/null +++ b/src/hyperion-aml/AmlogicWrapper.cpp @@ -0,0 +1,38 @@ + +// Hyperion-AmLogic includes +#include "AmlogicWrapper.h" + +AmlogicWrapper::AmlogicWrapper(const unsigned grabWidth, const unsigned grabHeight, const unsigned updateRate_Hz) : + _timer(this), + _grabber(grabWidth, grabHeight) +{ + _timer.setSingleShot(false); + _timer.setInterval(updateRate_Hz); + + // Connect capturing to the timeout signal of the timer + connect(&_timer, SIGNAL(timeout()), this, SLOT(capture())); +} + +const Image & AmlogicWrapper::getScreenshot() +{ + capture(); + return _screenshot_rgb; +} + +void AmlogicWrapper::start() +{ + _timer.start(); +} + +void AmlogicWrapper::stop() +{ + _timer.stop(); +} + +void AmlogicWrapper::capture() +{ + _grabber.grabFrame(_screenshot); + _screenshot.toRgb(_screenshot_rgb); + + emit sig_screenshot(_screenshot_rgb); +} diff --git a/src/hyperion-aml/AmlogicWrapper.h b/src/hyperion-aml/AmlogicWrapper.h new file mode 100644 index 00000000..3f5b0bc8 --- /dev/null +++ b/src/hyperion-aml/AmlogicWrapper.h @@ -0,0 +1,43 @@ + +// QT includes +#include + +// Hyperion-Dispmanx includes +#include + +class AmlogicWrapper : public QObject +{ + Q_OBJECT +public: + AmlogicWrapper(const unsigned grabWidth, const unsigned grabHeight, const unsigned updateRate_Hz); + + const Image & getScreenshot(); + + /// + /// Starts the timed capturing of screenshots + /// + void start(); + + void stop(); + +signals: + void sig_screenshot(const Image & screenshot); + +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 + AmlogicGrabber _grabber; + + // image buffers + Image _screenshot_rgb; + Image _screenshot; + +}; diff --git a/src/hyperion-aml/CMakeLists.txt b/src/hyperion-aml/CMakeLists.txt index 5d45222a..cd27c046 100644 --- a/src/hyperion-aml/CMakeLists.txt +++ b/src/hyperion-aml/CMakeLists.txt @@ -14,6 +14,7 @@ include_directories( ) set(Hyperion_AML_QT_HEADERS + AmlogicWrapper.h ) set(Hyperion_AML_HEADERS @@ -21,12 +22,13 @@ set(Hyperion_AML_HEADERS set(Hyperion_AML_SOURCES hyperion-aml.cpp + AmlogicWrapper.cpp ) if(ENABLE_QT5) -QT5_WRAP_CPP(Hyperion_AML_HEADERS_MOC ${Hyperion_AML_QT_HEADERS}) + QT5_WRAP_CPP(Hyperion_AML_HEADERS_MOC ${Hyperion_AML_QT_HEADERS}) else(ENABLE_QT5) -QT4_WRAP_CPP(Hyperion_AML_HEADERS_MOC ${Hyperion_AML_QT_HEADERS}) + QT4_WRAP_CPP(Hyperion_AML_HEADERS_MOC ${Hyperion_AML_QT_HEADERS}) endif(ENABLE_QT5) add_executable(hyperion-amlogic diff --git a/src/hyperion-aml/hyperion-aml.cpp b/src/hyperion-aml/hyperion-aml.cpp index 13da6686..cca5e688 100644 --- a/src/hyperion-aml/hyperion-aml.cpp +++ b/src/hyperion-aml/hyperion-aml.cpp @@ -7,16 +7,16 @@ // getoptPlusPLus includes #include -#include "../../libsrc/grabber/amlogic/AmlogicGrabber.h" +#include +#include "AmlogicWrapper.h" using namespace vlofgren; // save the image as screenshot -void saveScreenshot(const char * filename, const Image & image) +void saveScreenshot(const char * filename, const Image & image) { // store as PNG QImage pngImage((const uint8_t *) image.memptr(), image.width(), image.height(), 3*image.width(), QImage::Format_RGB888); - pngImage = pngImage.rgbSwapped(); pngImage.save(filename); } @@ -30,16 +30,17 @@ int main(int argc, char ** argv) OptionsParser optionParser("X11 capture application for Hyperion"); ParameterSet & parameters = optionParser.getParameters(); - //IntParameter & argFps = parameters.add ('f', "framerate", "Capture frame rate [default=10]"); + IntParameter & argFps = parameters.add ('f', "framerate", "Capture frame rate [default=10]"); IntParameter & argWidth = parameters.add (0x0, "width", "Width of the captured image [default=128]"); IntParameter & argHeight = parameters.add (0x0, "height", "Height of the captured image [default=128]"); SwitchParameter<> & argScreenshot = parameters.add> (0x0, "screenshot", "Take a single screenshot, save it to file and quit"); StringParameter & argAddress = parameters.add ('a', "address", "Set the address of the hyperion server [default: 127.0.0.1:19445]"); IntParameter & argPriority = parameters.add ('p', "priority", "Use the provided priority channel (the lower the number, the higher the priority) [default: 800]"); - //SwitchParameter<> & argSkipReply = parameters.add> (0x0, "skip-reply", "Do not receive and check reply messages from Hyperion"); + SwitchParameter<> & argSkipReply = parameters.add> (0x0, "skip-reply", "Do not receive and check reply messages from Hyperion"); SwitchParameter<> & argHelp = parameters.add> ('h', "help", "Show this help message and exit"); // set defaults + argFps.setDefault(10); argWidth.setDefault(160); argHeight.setDefault(160); argAddress.setDefault("127.0.0.1:19445"); @@ -57,29 +58,36 @@ int main(int argc, char ** argv) int width = argWidth.getValue(); int height = argHeight.getValue(); - if (width < 160 || height < 60) + if (width < 160 || height < 160) { std::cout << "Minimum width and height is 160" << std::endl; width = std::max(160, width); height = std::max(160, height); } + + int grabInterval = 1000 / argFps.getValue(); + AmlogicWrapper amlWrapper(argWidth.getValue(),argHeight.getValue(),grabInterval); + if (argScreenshot.isSet()) { - - // Create the grabber - AmlogicGrabber amlGrabber(width, height); - // Capture a single screenshot and finish - Image screenshot; - amlGrabber.grabFrame(screenshot); + const Image & screenshot = amlWrapper.getScreenshot(); saveScreenshot("screenshot.png", screenshot); } else { - // TODO[TvdZ]: Implement the proto-client mechanisme - std::cerr << "The PROTO-interface has not been implemented yet" << std::endl; - } + // 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(&amlWrapper, SIGNAL(sig_screenshot(const Image &)), &protoWrapper, SLOT(receiveImage(Image))); + + // Start the capturing + amlWrapper.start(); + + // Start the application + app.exec(); + } } catch (const std::runtime_error & e) { diff --git a/src/hyperion-dispmanx/CMakeLists.txt b/src/hyperion-dispmanx/CMakeLists.txt new file mode 100644 index 00000000..130cb661 --- /dev/null +++ b/src/hyperion-dispmanx/CMakeLists.txt @@ -0,0 +1,65 @@ +# Configure minimum CMAKE version +cmake_minimum_required(VERSION 2.8) + +# Set the project name +project(hyperion-dispmanx) + +if(ENABLE_QT5) + find_package(Qt5Widgets REQUIRED) +else(ENABLE_QT5) + # find Qt4 + find_package(Qt4 REQUIRED QtCore QtGui QtNetwork ) +endif(ENABLE_QT5) + +# Find the BCM-package (VC control) +find_package(BCM REQUIRED) + +include_directories( + ${CMAKE_CURRENT_BINARY_DIR}/../../libsrc/protoserver + ${QT_INCLUDES} + ${BCM_INCLUDE_DIRS} + ${PROTOBUF_INCLUDE_DIRS} +) + +set(Hyperion_Dispmanx_QT_HEADERS + DispmanxWrapper.h) + +set(Hyperion_Dispmanx_HEADERS +) + +set(Hyperion_Dispmanx_SOURCES + hyperion-dispmanx.cpp + DispmanxWrapper.cpp +) + +if(ENABLE_QT5) + QT5_WRAP_CPP(Hyperion_Dispmanx_HEADERS_MOC ${Hyperion_Dispmanx_QT_HEADERS}) +else(ENABLE_QT5) + QT4_WRAP_CPP(Hyperion_Dispmanx_HEADERS_MOC ${Hyperion_Dispmanx_QT_HEADERS}) +endif(ENABLE_QT5) + +add_executable( ${PROJECT_NAME} + ${Hyperion_Dispmanx_HEADERS} + ${Hyperion_Dispmanx_SOURCES} + ${Hyperion_Dispmanx_HEADERS_MOC} +) + +target_link_libraries( ${PROJECT_NAME} + getoptPlusPlus + blackborder + hyperion-utils + protoserver + dispmanx-grabber + ${Dispmanx_LIBRARIES} + pthread +) + +if(ENABLE_QT5) + qt5_use_modules(hyperion-dispmanx Widgets Core Gui Network) +else(ENABLE_QT5) + qt4_use_modules(hyperion-dispmanx + Core + Gui + Network + ) +endif(ENABLE_QT5) diff --git a/src/hyperion-dispmanx/DispmanxWrapper.cpp b/src/hyperion-dispmanx/DispmanxWrapper.cpp new file mode 100644 index 00000000..7d8702d2 --- /dev/null +++ b/src/hyperion-dispmanx/DispmanxWrapper.cpp @@ -0,0 +1,38 @@ + +// Hyperion-Dispmanx includes +#include "DispmanxWrapper.h" + +DispmanxWrapper::DispmanxWrapper(const unsigned grabWidth, const unsigned grabHeight, const unsigned updateRate_Hz) : + _timer(this), + _grabber(grabWidth, grabHeight) +{ + _timer.setSingleShot(false); + _timer.setInterval(updateRate_Hz); + + // Connect capturing to the timeout signal of the timer + connect(&_timer, SIGNAL(timeout()), this, SLOT(capture())); +} + +const Image & DispmanxWrapper::getScreenshot() +{ + capture(); + return _screenshot_rgb; +} + +void DispmanxWrapper::start() +{ + _timer.start(); +} + +void DispmanxWrapper::stop() +{ + _timer.stop(); +} + +void DispmanxWrapper::capture() +{ + _grabber.grabFrame(_screenshot); + _screenshot.toRgb(_screenshot_rgb); + + emit sig_screenshot(_screenshot_rgb); +} diff --git a/src/hyperion-dispmanx/DispmanxWrapper.h b/src/hyperion-dispmanx/DispmanxWrapper.h new file mode 100644 index 00000000..e8526b02 --- /dev/null +++ b/src/hyperion-dispmanx/DispmanxWrapper.h @@ -0,0 +1,42 @@ + +// QT includes +#include + +// Hyperion-Dispmanx includes +#include + +class DispmanxWrapper : public QObject +{ + Q_OBJECT +public: + DispmanxWrapper(const unsigned grabWidth, const unsigned grabHeight, const unsigned updateRate_Hz); + + const Image & getScreenshot(); + + /// + /// Starts the timed capturing of screenshots + /// + void start(); + + void stop(); + +signals: + void sig_screenshot(const Image & screenshot); + +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 + DispmanxFrameGrabber _grabber; + Image _screenshot_rgb; + Image _screenshot; + +}; diff --git a/src/hyperion-dispmanx/hyperion-dispmanx.cpp b/src/hyperion-dispmanx/hyperion-dispmanx.cpp new file mode 100644 index 00000000..abf78c8c --- /dev/null +++ b/src/hyperion-dispmanx/hyperion-dispmanx.cpp @@ -0,0 +1,92 @@ + +// QT includes +#include +#include + +// getoptPlusPLus includes +#include + +#include +#include "DispmanxWrapper.h" + +using namespace vlofgren; + +// save the image as screenshot +void saveScreenshot(const char * filename, const Image & 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) +{ + QCoreApplication app(argc, argv); + + try + { + // create the option parser and initialize all parameters + OptionsParser optionParser("Dispmanx capture application for Hyperion"); + ParameterSet & parameters = optionParser.getParameters(); + + IntParameter & argFps = parameters.add ('f', "framerate", "Capture frame rate [default=10]"); + IntParameter & argWidth = parameters.add (0x0, "width", "The width of the grabbed frames [pixels]"); + IntParameter & argHeight = parameters.add (0x0, "height", "The height of the grabbed frames"); + + SwitchParameter<> & argScreenshot = parameters.add> (0x0, "screenshot", "Take a single screenshot, save it to file and quit"); + StringParameter & argAddress = parameters.add ('a', "address", "Set the address of the hyperion server [default: 127.0.0.1:19445]"); + IntParameter & argPriority = parameters.add ('p', "priority", "Use the provided priority channel (the lower the number, the higher the priority) [default: 800]"); + SwitchParameter<> & argSkipReply = parameters.add> (0x0, "skip-reply", "Do not receive and check reply messages from Hyperion"); + SwitchParameter<> & argHelp = parameters.add> ('h', "help", "Show this help message and exit"); + + // set defaults + argFps.setDefault(10); + argWidth.setDefault(64); + argHeight.setDefault(64); + argAddress.setDefault("127.0.0.1:19445"); + argPriority.setDefault(800); + + // parse all options + optionParser.parse(argc, const_cast(argv)); + + // check if we need to display the usage. exit if we do. + if (argHelp.isSet()) + { + optionParser.usage(); + return 0; + } + + // Create the dispmanx grabbing stuff + int grabInterval = 1000 / argFps.getValue(); + DispmanxWrapper dispmanxWrapper(argWidth.getValue(),argHeight.getValue(),grabInterval); + + if (argScreenshot.isSet()) + { + // Capture a single screenshot and finish + const Image & screenshot = dispmanxWrapper.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(&dispmanxWrapper, SIGNAL(sig_screenshot(const Image &)), &protoWrapper, SLOT(receiveImage(Image))); + + // Start the capturing + dispmanxWrapper.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; + } + + return 0; +} diff --git a/src/hyperiond/hyperiond.cpp b/src/hyperiond/hyperiond.cpp index 032d44db..8af26879 100644 --- a/src/hyperiond/hyperiond.cpp +++ b/src/hyperiond/hyperiond.cpp @@ -1,6 +1,7 @@ // C++ includes #include #include +#include // QT includes #include @@ -188,6 +189,39 @@ int main(int argc, char** argv) std::cout << "XBMC video checker created and started" << std::endl; } +// ---- network services ----- + + // Create Json server if configuration is present + JsonServer * jsonServer = nullptr; + if (config.isMember("jsonServer")) + { + const Json::Value & jsonServerConfig = config["jsonServer"]; + jsonServer = new JsonServer(&hyperion, jsonServerConfig["port"].asUInt()); + std::cout << "Json server created and started on port " << jsonServer->getPort() << std::endl; + } + +#ifdef ENABLE_PROTOBUF + // Create Proto server if configuration is present + ProtoServer * protoServer = nullptr; + if (config.isMember("protoServer")) + { + const Json::Value & protoServerConfig = config["protoServer"]; + protoServer = new ProtoServer(&hyperion, protoServerConfig["port"].asUInt() ); + std::cout << "Proto server created and started on port " << protoServer->getPort() << std::endl; + } +#endif + + // Create Boblight server if configuration is present + BoblightServer * boblightServer = nullptr; + if (config.isMember("boblightServer")) + { + const Json::Value & boblightServerConfig = config["boblightServer"]; + boblightServer = new BoblightServer(&hyperion, boblightServerConfig["port"].asUInt()); + std::cout << "Boblight server created and started on port " << boblightServer->getPort() << std::endl; + } + +// ---- grabber ----- + #ifdef ENABLE_DISPMANX // Construct and start the frame-grabber if the configuration is present DispmanxWrapper * dispmanx = nullptr; @@ -206,6 +240,10 @@ int main(int argc, char** argv) QObject::connect(xbmcVideoChecker, SIGNAL(videoMode(VideoMode)), dispmanx, SLOT(setVideoMode(VideoMode))); } + #ifdef ENABLE_PROTOBUF + QObject::connect(dispmanx, SIGNAL(emitImage(int, const Image&, const int)), protoServer, SLOT(sendImageToProtoSlaves(int, const Image&, const int)) ); + #endif + dispmanx->start(); std::cout << "Frame grabber created and started" << std::endl; } @@ -213,7 +251,7 @@ int main(int argc, char** argv) #if !defined(ENABLE_OSX) && !defined(ENABLE_FB) if (config.isMember("framegrabber")) { - std::cerr << "The dispmanx framegrabber can not be instantiated, becuse it has been left out from the build" << std::endl; + std::cerr << "The dispmanx framegrabber can not be instantiated, because it has been left out from the build" << std::endl; } #endif #endif @@ -245,14 +283,19 @@ int main(int argc, char** argv) grabberConfig.get("cropTop", 0).asInt(), grabberConfig.get("cropBottom", 0).asInt()); + #ifdef ENABLE_PROTOBUF + QObject::connect(v4l2Grabber, SIGNAL(emitImage(int, const Image&, const int)), protoServer, SLOT(sendImageToProtoSlaves(int, const Image&, const int)) ); + #endif + v4l2Grabber->start(); std::cout << "V4l2 grabber created and started" << std::endl; } #else if (config.isMember("grabber-v4l2")) { - std::cerr << "The v4l2 grabber can not be instantiated, becuse it has been left out from the build" << std::endl; + std::cerr << "The v4l2 grabber can not be instantiated, because it has been left out from the build" << std::endl; } + #endif #ifdef ENABLE_AMLOGIC @@ -273,6 +316,10 @@ int main(int argc, char** argv) QObject::connect(xbmcVideoChecker, SIGNAL(videoMode(VideoMode)), amlGrabber, SLOT(setVideoMode(VideoMode))); } + #ifdef ENABLE_PROTOBUF + QObject::connect(amlGrabber, SIGNAL(emitImage(int, const Image&, const int)), protoServer, SLOT(sendImageToProtoSlaves(int, const Image&, const int)) ); + #endif + amlGrabber->start(); std::cout << "AMLOGIC grabber created and started" << std::endl; } @@ -302,18 +349,22 @@ int main(int argc, char** argv) QObject::connect(xbmcVideoChecker, SIGNAL(videoMode(VideoMode)), fbGrabber, SLOT(setVideoMode(VideoMode))); } + #ifdef ENABLE_PROTOBUF + QObject::connect(fbGrabber, SIGNAL(emitImage(int, const Image&, const int)), protoServer, SLOT(sendImageToProtoSlaves(int, const Image&, const int)) ); + #endif + fbGrabber->start(); std::cout << "Framebuffer grabber created and started" << std::endl; } #else if (config.isMember("framebuffergrabber")) { - std::cerr << "The framebuffer grabber can not be instantiated, becuse it has been left out from the build" << std::endl; + std::cerr << "The framebuffer grabber can not be instantiated, because it has been left out from the build" << std::endl; } #if !defined(ENABLE_DISPMANX) && !defined(ENABLE_OSX) else if (config.isMember("framegrabber")) { - std::cerr << "The framebuffer grabber can not be instantiated, becuse it has been left out from the build" << std::endl; + std::cerr << "The framebuffer grabber can not be instantiated, because it has been left out from the build" << std::endl; } #endif #endif @@ -336,6 +387,10 @@ int main(int argc, char** argv) QObject::connect(xbmcVideoChecker, SIGNAL(grabbingMode(GrabbingMode)), osxGrabber, SLOT(setGrabbingMode(GrabbingMode))); QObject::connect(xbmcVideoChecker, SIGNAL(videoMode(VideoMode)), osxGrabber, SLOT(setVideoMode(VideoMode))); } + + #ifdef ENABLE_PROTOBUF + QObject::connect(osxGrabber, SIGNAL(emitImage(int, const Image&, const int)), protoServer, SLOT(sendImageToProtoSlaves(int, const Image&, const int)) ); + #endif osxGrabber->start(); std::cout << "OSX grabber created and started" << std::endl; @@ -343,44 +398,16 @@ int main(int argc, char** argv) #else if (config.isMember("osxgrabber")) { - std::cerr << "The osx grabber can not be instantiated, becuse it has been left out from the build" << std::endl; + std::cerr << "The osx grabber can not be instantiated, because it has been left out from the build" << std::endl; } #if !defined(ENABLE_DISPMANX) && !defined(ENABLE_FB) else if (config.isMember("framegrabber")) { - std::cerr << "The osx grabber can not be instantiated, becuse it has been left out from the build" << std::endl; + std::cerr << "The osx grabber can not be instantiated, because it has been left out from the build" << std::endl; } #endif #endif - // Create Json server if configuration is present - JsonServer * jsonServer = nullptr; - if (config.isMember("jsonServer")) - { - const Json::Value & jsonServerConfig = config["jsonServer"]; - jsonServer = new JsonServer(&hyperion, jsonServerConfig["port"].asUInt()); - std::cout << "Json server created and started on port " << jsonServer->getPort() << std::endl; - } - -#ifdef ENABLE_PROTOBUF - // Create Proto server if configuration is present - ProtoServer * protoServer = nullptr; - if (config.isMember("protoServer")) - { - const Json::Value & protoServerConfig = config["protoServer"]; - protoServer = new ProtoServer(&hyperion, protoServerConfig["port"].asUInt()); - std::cout << "Proto server created and started on port " << protoServer->getPort() << std::endl; - } -#endif - - // Create Boblight server if configuration is present - BoblightServer * boblightServer = nullptr; - if (config.isMember("boblightServer")) - { - const Json::Value & boblightServerConfig = config["boblightServer"]; - boblightServer = new BoblightServer(&hyperion, boblightServerConfig["port"].asUInt()); - std::cout << "Boblight server created and started on port " << boblightServer->getPort() << std::endl; - } // run the application int rc = app.exec(); diff --git a/test/TestRgbImage.cpp b/test/TestRgbImage.cpp index 374408f5..67773763 100644 --- a/test/TestRgbImage.cpp +++ b/test/TestRgbImage.cpp @@ -4,22 +4,42 @@ // Utils includes #include +#include #include +#include +#include int main() { std::cout << "Constructing image" << std::endl; - Image image(64, 64, ColorRgb::BLACK); + int width = 64; + int height = 64; + Image image_rgb(width, height, ColorRgb::BLACK); + Image image_bgr(image_rgb.width(), image_rgb.height(), ColorBgr::BLACK); std::cout << "Writing image" << std::endl; - for (unsigned y=0; y<64; ++y) + unsigned l = width * height; + + // BGR + for (unsigned i=0; i // Dispmanx grabber includes -#include +#include using namespace vlofgren;