Skip to content
This repository was archived by the owner on May 6, 2021. It is now read-only.

Commit 1bd70d0

Browse files
committed
Added signal detection option
1 parent 5900299 commit 1bd70d0

File tree

6 files changed

+140
-58
lines changed

6 files changed

+140
-58
lines changed

dependencies/build/getoptPlusPlus/getoptpp.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void OptionsParser::usage() const {
120120
for(i = parameters.parameters.begin();
121121
i != parameters.parameters.end(); i++)
122122
{
123-
cerr.width(31);
123+
cerr.width(33);
124124
cerr << std::left << " " + (*i)->usageLine();
125125

126126
cerr.width(40);

src/hyperion-v4l2/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ include_directories(
1717
set(Hyperion_V4L2_HEADERS
1818
V4L2Grabber.h
1919
ProtoConnection.h
20+
ImageHandler.h
21+
VideoStandardParameter.h
2022
)
2123

2224
set(Hyperion_V4L2_SOURCES
2325
hyperion-v4l2.cpp
2426
V4L2Grabber.cpp
2527
ProtoConnection.cpp
28+
ImageHandler.cpp
2629
)
2730

2831
set(Hyperion_V4L2_PROTOS
@@ -42,6 +45,7 @@ add_executable(hyperion-v4l2
4245

4346
target_link_libraries(hyperion-v4l2
4447
getoptPlusPlus
48+
blackborder
4549
hyperion-utils
4650
${PROTOBUF_LIBRARIES}
4751
pthread

src/hyperion-v4l2/ImageHandler.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// hyperion-v4l2 includes
2+
#include "ImageHandler.h"
3+
4+
ImageHandler::ImageHandler(const std::string &address, int priority, double signalThreshold, bool skipProtoReply) :
5+
_priority(priority),
6+
_connection(address),
7+
_signalThreshold(signalThreshold),
8+
_signalProcessor(100, 50, 0, uint8_t(std::min(255, std::max(0, int(255*signalThreshold)))))
9+
{
10+
_connection.setSkipReply(skipProtoReply);
11+
}
12+
13+
void ImageHandler::receiveImage(const Image<ColorRgb> &image)
14+
{
15+
// check if we should do signal detection
16+
if (_signalThreshold < 0)
17+
{
18+
_connection.setImage(image, _priority, 200);
19+
}
20+
else
21+
{
22+
if (_signalProcessor.process(image))
23+
{
24+
std::cout << "Signal state = " << (_signalProcessor.getCurrentBorder().unknown ? "off" : "on") << std::endl;
25+
}
26+
27+
// consider an unknown border as no signal
28+
// send the image to Hyperion if we have a signal
29+
if (!_signalProcessor.getCurrentBorder().unknown)
30+
{
31+
_connection.setImage(image, _priority, 200);
32+
}
33+
}
34+
}
35+
36+
void ImageHandler::imageCallback(void *arg, const Image<ColorRgb> &image)
37+
{
38+
ImageHandler * handler = static_cast<ImageHandler *>(arg);
39+
handler->receiveImage(image);
40+
}
41+

src/hyperion-v4l2/ImageHandler.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// blackborder includes
2+
#include <blackborder/BlackBorderProcessor.h>
3+
4+
// hyperion-v4l includes
5+
#include "ProtoConnection.h"
6+
7+
/// This class handles callbacks from the V4L2 grabber
8+
class ImageHandler
9+
{
10+
public:
11+
ImageHandler(const std::string & address, int priority, double signalThreshold, bool skipProtoReply);
12+
13+
/// Handle a single image
14+
/// @param image The image to process
15+
void receiveImage(const Image<ColorRgb> & image);
16+
17+
/// static function used to direct callbacks to a ImageHandler object
18+
/// @param arg This should be an ImageHandler instance
19+
/// @param image The image to process
20+
static void imageCallback(void * arg, const Image<ColorRgb> & image);
21+
22+
private:
23+
/// Priority for calls to Hyperion
24+
const int _priority;
25+
26+
/// Hyperion proto connection object
27+
ProtoConnection _connection;
28+
29+
/// Threshold used for signal detection
30+
double _signalThreshold;
31+
32+
/// Blackborder detector which is used as a signal detector (unknown border = no signal)
33+
hyperion::BlackBorderProcessor _signalProcessor;
34+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// getoptPlusPLus includes
2+
#include <getoptPlusPlus/getoptpp.h>
3+
4+
using namespace vlofgren;
5+
6+
/// Data parameter for the video standard
7+
typedef vlofgren::PODParameter<V4L2Grabber::VideoStandard> VideoStandardParameter;
8+
9+
namespace vlofgren {
10+
/// Translates a string (as passed on the commandline) to a color standard
11+
///
12+
/// @param[in] s The string (as passed on the commandline)
13+
/// @return The color standard
14+
/// @throws Parameter::ParameterRejected If the string did not result in a video standard
15+
template<>
16+
V4L2Grabber::VideoStandard VideoStandardParameter::validate(const std::string& s) throw (Parameter::ParameterRejected)
17+
{
18+
QString input = QString::fromStdString(s).toLower();
19+
20+
if (input == "pal")
21+
{
22+
return V4L2Grabber::PAL;
23+
}
24+
else if (input == "ntsc")
25+
{
26+
return V4L2Grabber::NTSC;
27+
}
28+
else if (input == "no-change")
29+
{
30+
return V4L2Grabber::NO_CHANGE;
31+
}
32+
33+
throw Parameter::ParameterRejected("Invalid value for video standard. Valid values are: PAL, NTSC, and NO-CHANGE");
34+
return V4L2Grabber::NO_CHANGE;
35+
}
36+
}

src/hyperion-v4l2/hyperion-v4l2.cpp

Lines changed: 24 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,17 @@
99
// getoptPlusPLus includes
1010
#include <getoptPlusPlus/getoptpp.h>
1111

12+
// blackborder includes
13+
#include <blackborder/BlackBorderProcessor.h>
14+
15+
// hyperion-v4l2 includes
1216
#include "V4L2Grabber.h"
1317
#include "ProtoConnection.h"
18+
#include "VideoStandardParameter.h"
19+
#include "ImageHandler.h"
1420

1521
using namespace vlofgren;
1622

17-
/// Data parameter for the video standard
18-
typedef vlofgren::PODParameter<V4L2Grabber::VideoStandard> VideoStandardParameter;
19-
20-
namespace vlofgren {
21-
/// Translates a string (as passed on the commandline) to a color standard
22-
///
23-
/// @param[in] s The string (as passed on the commandline)
24-
/// @return The color standard
25-
/// @throws Parameter::ParameterRejected If the string did not result in a video standard
26-
template<>
27-
V4L2Grabber::VideoStandard VideoStandardParameter::validate(const std::string& s) throw (Parameter::ParameterRejected)
28-
{
29-
QString input = QString::fromStdString(s).toLower();
30-
31-
if (input == "pal")
32-
{
33-
return V4L2Grabber::PAL;
34-
}
35-
else if (input == "ntsc")
36-
{
37-
return V4L2Grabber::NTSC;
38-
}
39-
else if (input == "no-change")
40-
{
41-
return V4L2Grabber::NO_CHANGE;
42-
}
43-
44-
throw Parameter::ParameterRejected("Invalid value for video standard. Valid values are: PAL, NTSC, and NO-CHANGE");
45-
return V4L2Grabber::NO_CHANGE;
46-
}
47-
}
48-
4923
// save the image as screenshot
5024
void saveScreenshot(void *, const Image<ColorRgb> & image)
5125
{
@@ -54,13 +28,6 @@ void saveScreenshot(void *, const Image<ColorRgb> & image)
5428
pngImage.save("screenshot.png");
5529
}
5630

57-
// send the image to Hyperion
58-
void sendImage(void * arg, const Image<ColorRgb> & image)
59-
{
60-
ProtoConnection * connection = static_cast<ProtoConnection *>(arg);
61-
connection->setImage(image, 50, 200);
62-
}
63-
6431
int main(int argc, char** argv)
6532
{
6633
try
@@ -69,20 +36,21 @@ int main(int argc, char** argv)
6936
OptionsParser optionParser("Simple application to send a command to hyperion using the Json interface");
7037
ParameterSet & parameters = optionParser.getParameters();
7138

72-
StringParameter & argDevice = parameters.add<StringParameter> ('d', "device", "The device to use [default=/dev/video0]");
73-
VideoStandardParameter & argVideoStandard = parameters.add<VideoStandardParameter>('v', "video-standard", "The used video standard. Valid values are PAL. NYSC, or NO-CHANGE [default=PAL]");
74-
IntParameter & argInput = parameters.add<IntParameter> (0x0, "input", "Input channel (optional)");
75-
IntParameter & argWidth = parameters.add<IntParameter> (0x0, "width", "Try to set the width of the video input (optional)");
76-
IntParameter & argHeight = parameters.add<IntParameter> (0x0, "height", "Try to set the height of the video input (optional)");
77-
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]");
78-
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]");
79-
IntParameter & argSizeDecimation = parameters.add<IntParameter> ('s', "size-decimator", "Decimation factor for the output size [default=1]");
80-
IntParameter & argFrameDecimation = parameters.add<IntParameter> ('f', "frame-decimator","Decimation factor for the video frames [default=1]");
81-
SwitchParameter<> & argScreenshot = parameters.add<SwitchParameter<>> (0x0, "screenshot", "Take a single screenshot, save it to file and quit");
82-
StringParameter & argAddress = parameters.add<StringParameter> ('a', "address", "Set the address of the hyperion server [default: 127.0.0.1:19445]");
83-
IntParameter & argPriority = parameters.add<IntParameter> ('p', "priority", "Use the provided priority channel (the lower the number, the higher the priority) [default: 800]");
84-
SwitchParameter<> & argSkipReply = parameters.add<SwitchParameter<>> (0x0, "skip-reply", "Do not receive and check reply messages from Hyperion");
85-
SwitchParameter<> & argHelp = parameters.add<SwitchParameter<>> ('h', "help", "Show this help message and exit");
39+
StringParameter & argDevice = parameters.add<StringParameter> ('d', "device", "The device to use [default=/dev/video0]");
40+
VideoStandardParameter & argVideoStandard = parameters.add<VideoStandardParameter>('v', "video-standard", "The used video standard. Valid values are PAL. NYSC, or NO-CHANGE [default=PAL]");
41+
IntParameter & argInput = parameters.add<IntParameter> (0x0, "input", "Input channel (optional)");
42+
IntParameter & argWidth = parameters.add<IntParameter> (0x0, "width", "Try to set the width of the video input (optional)");
43+
IntParameter & argHeight = parameters.add<IntParameter> (0x0, "height", "Try to set the height of the video input (optional)");
44+
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]");
45+
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]");
46+
IntParameter & argSizeDecimation = parameters.add<IntParameter> ('s', "size-decimator", "Decimation factor for the output size [default=1]");
47+
IntParameter & argFrameDecimation = parameters.add<IntParameter> ('f', "frame-decimator", "Decimation factor for the video frames [default=1]");
48+
SwitchParameter<> & argScreenshot = parameters.add<SwitchParameter<>> (0x0, "screenshot", "Take a single screenshot, save it to file and quit");
49+
DoubleParameter & argSignalThreshold = parameters.add<DoubleParameter> ('t', "signal-threshold", "The signal threshold for detecting the presence of a signal. Value should be between 0.0 and 1.0.");
50+
StringParameter & argAddress = parameters.add<StringParameter> ('a', "address", "Set the address of the hyperion server [default: 127.0.0.1:19445]");
51+
IntParameter & argPriority = parameters.add<IntParameter> ('p', "priority", "Use the provided priority channel (the lower the number, the higher the priority) [default: 800]");
52+
SwitchParameter<> & argSkipReply = parameters.add<SwitchParameter<>> (0x0, "skip-reply", "Do not receive and check reply messages from Hyperion");
53+
SwitchParameter<> & argHelp = parameters.add<SwitchParameter<>> ('h', "help", "Show this help message and exit");
8654

8755
// set defaults
8856
argDevice.setDefault("/dev/video0");
@@ -96,6 +64,7 @@ int main(int argc, char** argv)
9664
argFrameDecimation.setDefault(1);
9765
argAddress.setDefault("127.0.0.1:19445");
9866
argPriority.setDefault(800);
67+
argSignalThreshold.setDefault(-1);
9968

10069
// parse all options
10170
optionParser.parse(argc, const_cast<const char **>(argv));
@@ -126,10 +95,8 @@ int main(int argc, char** argv)
12695
}
12796
else
12897
{
129-
ProtoConnection connection(argAddress.getValue());
130-
connection.setSkipReply(argSkipReply.isSet());
131-
132-
grabber.setCallback(&sendImage, &connection);
98+
ImageHandler handler(argAddress.getValue(), argPriority.getValue(), argSignalThreshold.getValue(), argSkipReply.isSet());
99+
grabber.setCallback(&ImageHandler::imageCallback, &handler);
133100
grabber.capture();
134101
}
135102
grabber.stop();

0 commit comments

Comments
 (0)