Skip to content

Commit

Permalink
Move parameter handling to own class (#19)
Browse files Browse the repository at this point in the history
This makes the source code easier to read and maintain.

Signed-off-by: Joakim Roubert <[email protected]>
  • Loading branch information
joakimr-axis authored Feb 20, 2025
1 parent 93fcccf commit c3092eb
Show file tree
Hide file tree
Showing 9 changed files with 517 additions and 385 deletions.
12 changes: 3 additions & 9 deletions include/CgiHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,15 @@
* limitations under the License.
*/

/**
* This header file handles the vdo part of the application.
*/

#pragma once

#include <axhttp.h>
#include <opencv2/core/core.hpp>

class CgiHandler
{
public:
CgiHandler(
gboolean (*GetColorAreaValue)(),
gboolean (*GetParamDouble)(const gchar *, double &),
gboolean (*PickCurrentCallback)());
CgiHandler(cv::Scalar (*GetColor)(), gboolean (*GetColorAreaValue)(), gboolean (*PickCurrentCallback)());
~CgiHandler();
static void RequestHandler(
const gchar *path,
Expand All @@ -44,8 +38,8 @@ class CgiHandler
static void WriteBadRequest(GDataOutputStream &dos, const gchar *msg);
static void WriteInternalError(GDataOutputStream &dos, const gchar *msg);

cv::Scalar (*GetColor_)();
gboolean (*GetColorAreaValue_)();
gboolean (*GetParamDouble_)(const gchar *, double &);
gboolean (*PickCurrentCallback_)();

AXHttpHandler *http_handler_;
Expand Down
4 changes: 0 additions & 4 deletions include/EventHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
* limitations under the License.
*/

/**
* This header file handles the vdo part of the application.
*/

#pragma once

#include <axevent.h>
Expand Down
6 changes: 3 additions & 3 deletions include/ImageProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ class ImageProvider
static bool StartFrameFetch(ImageProvider &provider);
static bool StopFrameFetch(ImageProvider &provider);

/// Keeping track of frames' statuses.
// Keeping track of frames' statuses.
GQueue *delivered_frames_;
GQueue *processed_frames_;

/// To support fetching frames asynchonously with VDO.
// To support fetching frames asynchonously with VDO.
pthread_mutex_t frame_mutex_;
pthread_cond_t frame_deliver_cond_;
pthread_t fetcher_thread_;
Expand All @@ -74,7 +74,7 @@ class ImageProvider
bool initialized_;
unsigned int width_;
unsigned int height_;
/// Number of frames to keep in the delivered_frames queue.
// Number of frames to keep in the delivered_frames queue.
unsigned int num_app_frames_;
// Stream configuration parameters.
VdoFormat vdo_format_;
Expand Down
82 changes: 82 additions & 0 deletions include/ParamHandler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Copyright (C) 2025, Axis Communications AB, Lund, Sweden
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <axparameter.h>
#include <opencv2/core/core.hpp>

class ParamHandler
{
public:
ParamHandler(const gchar *app_name, void (*PurgeColorArea)(), void (*RestartOpcUaServer)(unsigned int));
~ParamHandler();
gboolean SetColor(const cv::Scalar color);
gboolean SetResolution(const gint32 w, const gint32 h);

static void ParamCallbackDouble(const gchar *name, const gchar *value, void *data);
static void ParamCallbackInt(const gchar *name, const gchar *value, void *data);

cv::Point GetCenterPoint() const
{
return center_point_;
};
cv::Scalar GetColor() const
{
return color_;
};
guint32 GetMarkerWidth() const
{
return markerwidth_;
};
guint32 GetMarkerHeight() const
{
return markerheight_;
};
guint8 GetMarkerShape() const
{
return markershape_;
};
guint8 GetTolerance() const
{
return tolerance_;
};

private:
gboolean SetParam(const gchar *name, const gchar &value, gboolean do_sync);
gboolean SetParam(const gchar *name, const gdouble value, gboolean do_sync);
gboolean SetParam(const gchar *name, const gint32 value, gboolean do_sync);
gchar *GetParam(const gchar *name) const;
gboolean GetParam(const gchar *name, gdouble &val) const;
gboolean GetParam(const gchar *name, gint32 &val) const;
void UpdateLocalParam(const gchar *name, const gdouble val);
void UpdateLocalParam(const gchar *name, const gint32 val);
gboolean SetupParam(const gchar *name, AXParameterCallback callbackfn);
gboolean SetupParamDouble(const gchar *name, AXParameterCallback callbackfn);
gboolean SetupParamInt(const gchar *name, AXParameterCallback callbackfn);

void (*PurgeColorArea_)();
void (*RestartOpcUaServer_)(const guint32);

AXParameter *axparameter_;
cv::Point center_point_;
cv::Scalar color_;
guint32 markerwidth_;
guint32 markerheight_;
guint8 markershape_;
guint8 tolerance_;
mutable GMutex mtx_;
};
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"vendorUrl": "https://www.axis.com/",
"runMode": "respawn",
"version": "1.1.2"
"version": "1.1.3"
},
"configuration": {
"settingPage": "settings.html",
Expand Down
23 changes: 6 additions & 17 deletions src/CgiHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,17 @@
#include <string>

#include "CgiHandler.hpp"
#include "ColorArea.hpp"
#include "common.hpp"

using namespace std;

CgiHandler::CgiHandler(
gboolean (*GetColorAreaValue)(),
gboolean (*GetParamDouble)(const gchar *, double &),
gboolean (*PickCurrentCallback)())
: GetColorAreaValue_(GetColorAreaValue), GetParamDouble_(GetParamDouble), PickCurrentCallback_(PickCurrentCallback),
CgiHandler::CgiHandler(cv::Scalar (*GetColor)(), gboolean (*GetColorAreaValue)(), gboolean (*PickCurrentCallback)())
: GetColor_(GetColor), GetColorAreaValue_(GetColorAreaValue), PickCurrentCallback_(PickCurrentCallback),
http_handler_(ax_http_handler_new(this->RequestHandler, this))
{
assert(nullptr != GetColor_);
assert(nullptr != GetColorAreaValue_);
assert(nullptr != GetParamDouble_);
assert(nullptr != PickCurrentCallback_);
assert(nullptr != http_handler_);
}
Expand Down Expand Up @@ -109,20 +107,11 @@ void CgiHandler::RequestHandler(
goto http_exit;
}

double blue;
double green;
double red;
assert(nullptr != cgi_handler->GetParamDouble_);
if (!cgi_handler->GetParamDouble_("ColorB", blue) || !cgi_handler->GetParamDouble_("ColorG", green) ||
!cgi_handler->GetParamDouble_("ColorR", red))
{
WriteInternalError(*dos, "Inner parameter retrieval error");
goto http_exit;
}
const auto color = cgi_handler->GetColor_();
g_data_output_stream_put_string(dos, "Status: 200 OK\r\n", nullptr, nullptr);
g_data_output_stream_put_string(dos, "Content-Type: application/json\r\n\r\n", nullptr, nullptr);
ostringstream ss;
ss << "{\"R\":" << red << ", \"G\":" << green << ", \"B\":" << blue << "}" << endl;
ss << "{\"R\":" << color[R] << ", \"G\":" << color[G] << ", \"B\":" << color[B] << "}" << endl;
g_data_output_stream_put_string(dos, ss.str().c_str(), nullptr, nullptr);
}
else
Expand Down
32 changes: 13 additions & 19 deletions src/ImageProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,17 @@ bool ImageProvider::ChooseStreamResolution(
unsigned int &chosenWidth,
unsigned int &chosenHeight)
{
VdoResolutionSet *set = nullptr;
VdoChannel *channel = nullptr;
GError *error = nullptr;

// Retrieve channel resolutions
channel = vdo_channel_get(VDO_CHANNEL, &error);
if (!channel)
auto channel = vdo_channel_get(VDO_CHANNEL, &error);
if (nullptr == channel)
{
LOG_E("%s: Failed vdo_channel_get(): %s", __func__, (error != nullptr) ? error->message : "N/A");
g_clear_object(&channel);
g_clear_error(&error);
}
set = vdo_channel_get_resolutions(channel, nullptr, &error);
auto set = vdo_channel_get_resolutions(channel, nullptr, &error);
g_clear_object(&channel);
if (nullptr == set)
{
Expand Down Expand Up @@ -221,9 +219,9 @@ bool ImageProvider::ChooseStreamResolution(
bool ImageProvider::CreateStream(ImageProvider &provider)
{
assert(!provider.initialized_);
VdoMap *vdoMap = vdo_map_new();
auto vdoMap = vdo_map_new();
GError *error = nullptr;
bool ret = false;
auto ret = false;

if (nullptr == vdoMap)
{
Expand All @@ -241,7 +239,7 @@ bool ImageProvider::CreateStream(ImageProvider &provider)
LOG_I("Dump of vdo stream settings map =====");
vdo_map_dump(vdoMap);

VdoStream *vdo_stream_ = vdo_stream_new(vdoMap, nullptr, &error);
auto vdo_stream_ = vdo_stream_new(vdoMap, nullptr, &error);
if (nullptr == vdo_stream_)
{
LOG_E("%s: Failed creating VDO stream (%s)", __func__, (error != nullptr) ? error->message : "N/A");
Expand Down Expand Up @@ -285,7 +283,7 @@ bool ImageProvider::AllocateVdoBuffers(ImageProvider &provider, VdoStream &vdoSt
{
assert(!provider.initialized_);
GError *error = nullptr;
bool ret = false;
auto ret = false;

for (size_t i = 0; i < NUM_VDO_BUFFERS; i++)
{
Expand All @@ -303,8 +301,8 @@ bool ImageProvider::AllocateVdoBuffers(ImageProvider &provider, VdoStream &vdoSt
// Make a 'speculative' vdo_buffer_get_data() call to trigger a
// memory mapping of the buffer. The mapping is cached in the VDO
// implementation.
void *dummyPtr = vdo_buffer_get_data(provider.vdo_buffers_[i]);
if (!dummyPtr)
auto dummyPtr = vdo_buffer_get_data(provider.vdo_buffers_[i]);
if (nullptr == dummyPtr)
{
LOG_E(
"%s/%s: Failed initializing buffer memmap: %s",
Expand Down Expand Up @@ -394,16 +392,12 @@ void ImageProvider::RunLoopIteration()
assert(initialized_);
GError *error = nullptr;
// Block waiting for a frame from VDO
VdoBuffer *newBuffer = vdo_stream_get_buffer(vdo_stream_, &error);
auto newBuffer = vdo_stream_get_buffer(vdo_stream_, &error);

if (!newBuffer)
if (nullptr == newBuffer)
{
// Fail but we continue anyway hoping for the best.
syslog(
LOG_WARNING,
"%s: Failed fetching frame from vdo: %s",
__func__,
(error != nullptr) ? error->message : "N/A");
LOG_I("%s: WARNING, failed fetching frame from vdo: %s", __func__, (error != nullptr) ? error->message : "N/A");
g_clear_error(&error);
return;
}
Expand Down Expand Up @@ -479,7 +473,7 @@ void ImageProvider::RunLoopIteration()
void *ImageProvider::threadEntry(void *data)
{
assert(nullptr != data);
ImageProvider *provider = (ImageProvider *)data;
auto provider = static_cast<ImageProvider *>(data);
assert(provider->initialized_);

while (!provider->shutdown_)
Expand Down
Loading

0 comments on commit c3092eb

Please sign in to comment.