clang optimizations and improved error handling
This commit is contained in:
@@ -13,30 +13,29 @@ std::string CameraController::processCommand(const std::string& jsonCommand) {
|
|||||||
// Simple JSON parsing (basic implementation)
|
// Simple JSON parsing (basic implementation)
|
||||||
// Format: {"command":"name","params":{...}}
|
// Format: {"command":"name","params":{...}}
|
||||||
|
|
||||||
size_t cmdPos = jsonCommand.find("\"command\"");
|
const size_t cmdPos = jsonCommand.find("\"command\"");
|
||||||
if (cmdPos == std::string::npos) {
|
if (cmdPos == std::string::npos) {
|
||||||
return createErrorResponse("Missing command field");
|
return createErrorResponse("Missing command field");
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t colonPos = jsonCommand.find(":", cmdPos);
|
const size_t colonPos = jsonCommand.find(":", cmdPos);
|
||||||
size_t quoteStart = jsonCommand.find("\"", colonPos);
|
const size_t quoteStart = jsonCommand.find("\"", colonPos);
|
||||||
size_t quoteEnd = jsonCommand.find("\"", quoteStart + 1);
|
const size_t quoteEnd = jsonCommand.find("\"", quoteStart + 1);
|
||||||
|
|
||||||
if (quoteStart == std::string::npos || quoteEnd == std::string::npos) {
|
if (quoteStart == std::string::npos || quoteEnd == std::string::npos) {
|
||||||
return createErrorResponse("Invalid command format");
|
return createErrorResponse("Invalid command format");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string command = jsonCommand.substr(quoteStart + 1, quoteEnd - quoteStart - 1);
|
const std::string command = jsonCommand.substr(quoteStart + 1, quoteEnd - quoteStart - 1);
|
||||||
|
|
||||||
// Helper lambda to extract parameter value
|
// Helper lambda to extract parameter value
|
||||||
auto getParam = [&jsonCommand](const std::string& paramName) -> std::string {
|
auto getParam = [&jsonCommand](const std::string& paramName) -> std::string {
|
||||||
size_t pos = jsonCommand.find("\"" + paramName + "\"");
|
const size_t pos = jsonCommand.find("\"" + paramName + "\"");
|
||||||
if (pos == std::string::npos) return "";
|
if (pos == std::string::npos) return "";
|
||||||
|
|
||||||
size_t colonPos = jsonCommand.find(":", pos);
|
const size_t colonPos = jsonCommand.find(":", pos);
|
||||||
size_t valueStart = jsonCommand.find_first_not_of(" \t\n\r", colonPos + 1);
|
|
||||||
|
|
||||||
if (jsonCommand[valueStart] == '\"') {
|
if (size_t valueStart = jsonCommand.find_first_not_of(" \t\n\r", colonPos + 1); jsonCommand[valueStart] == '\"') {
|
||||||
size_t valueEnd = jsonCommand.find("\"", valueStart + 1);
|
size_t valueEnd = jsonCommand.find("\"", valueStart + 1);
|
||||||
return jsonCommand.substr(valueStart + 1, valueEnd - valueStart - 1);
|
return jsonCommand.substr(valueStart + 1, valueEnd - valueStart - 1);
|
||||||
} else {
|
} else {
|
||||||
@@ -126,8 +125,8 @@ std::string CameraController::handleGetFormats() {
|
|||||||
|
|
||||||
std::string CameraController::handleSetExposure(const std::string& mode, const std::string& value) {
|
std::string CameraController::handleSetExposure(const std::string& mode, const std::string& value) {
|
||||||
try {
|
try {
|
||||||
int flag = (mode == "auto") ? 1 : 0;
|
const int flag = (mode == "auto") ? 1 : 0;
|
||||||
long expValue = value.empty() ? 0 : std::stol(value);
|
const long expValue = value.empty() ? 0 : std::stol(value);
|
||||||
|
|
||||||
if (VxSetUVCImageProcessing(camera_, VX_UVC_IMAGE_PROPERTIES::UVC_IMAGE_EXPOSURE,
|
if (VxSetUVCImageProcessing(camera_, VX_UVC_IMAGE_PROPERTIES::UVC_IMAGE_EXPOSURE,
|
||||||
expValue, flag) != 0) {
|
expValue, flag) != 0) {
|
||||||
@@ -142,8 +141,8 @@ std::string CameraController::handleSetExposure(const std::string& mode, const s
|
|||||||
|
|
||||||
std::string CameraController::handleSetWhiteBalance(const std::string& mode, const std::string& temperature) {
|
std::string CameraController::handleSetWhiteBalance(const std::string& mode, const std::string& temperature) {
|
||||||
try {
|
try {
|
||||||
int flag = (mode == "auto") ? 1 : 0;
|
const int flag = (mode == "auto") ? 1 : 0;
|
||||||
long tempValue = temperature.empty() ? 0 : std::stol(temperature);
|
const long tempValue = temperature.empty() ? 0 : std::stol(temperature);
|
||||||
|
|
||||||
if (VxSetUVCImageProcessing(camera_, VX_UVC_IMAGE_PROPERTIES::UVC_IMAGE_WHITEBALANCE,
|
if (VxSetUVCImageProcessing(camera_, VX_UVC_IMAGE_PROPERTIES::UVC_IMAGE_WHITEBALANCE,
|
||||||
tempValue, flag) != 0) {
|
tempValue, flag) != 0) {
|
||||||
@@ -158,7 +157,7 @@ std::string CameraController::handleSetWhiteBalance(const std::string& mode, con
|
|||||||
|
|
||||||
std::string CameraController::handleSetBrightness(const std::string& value) {
|
std::string CameraController::handleSetBrightness(const std::string& value) {
|
||||||
try {
|
try {
|
||||||
long val = std::stol(value);
|
const long val = std::stol(value);
|
||||||
if (VxSetUVCImageProcessing(camera_, VX_UVC_IMAGE_PROPERTIES::UVC_IMAGE_BRIGHTNESS,
|
if (VxSetUVCImageProcessing(camera_, VX_UVC_IMAGE_PROPERTIES::UVC_IMAGE_BRIGHTNESS,
|
||||||
val, 0) != 0) {
|
val, 0) != 0) {
|
||||||
return createErrorResponse("Failed to set brightness");
|
return createErrorResponse("Failed to set brightness");
|
||||||
@@ -171,7 +170,7 @@ std::string CameraController::handleSetBrightness(const std::string& value) {
|
|||||||
|
|
||||||
std::string CameraController::handleSetContrast(const std::string& value) {
|
std::string CameraController::handleSetContrast(const std::string& value) {
|
||||||
try {
|
try {
|
||||||
long val = std::stol(value);
|
const long val = std::stol(value);
|
||||||
if (VxSetUVCImageProcessing(camera_, VX_UVC_IMAGE_PROPERTIES::UVC_IMAGE_CONTRAST,
|
if (VxSetUVCImageProcessing(camera_, VX_UVC_IMAGE_PROPERTIES::UVC_IMAGE_CONTRAST,
|
||||||
val, 0) != 0) {
|
val, 0) != 0) {
|
||||||
return createErrorResponse("Failed to set contrast");
|
return createErrorResponse("Failed to set contrast");
|
||||||
@@ -184,7 +183,7 @@ std::string CameraController::handleSetContrast(const std::string& value) {
|
|||||||
|
|
||||||
std::string CameraController::handleSetSaturation(const std::string& value) {
|
std::string CameraController::handleSetSaturation(const std::string& value) {
|
||||||
try {
|
try {
|
||||||
long val = std::stol(value);
|
const long val = std::stol(value);
|
||||||
if (VxSetUVCImageProcessing(camera_, VX_UVC_IMAGE_PROPERTIES::UVC_IMAGE_SATURATION,
|
if (VxSetUVCImageProcessing(camera_, VX_UVC_IMAGE_PROPERTIES::UVC_IMAGE_SATURATION,
|
||||||
val, 0) != 0) {
|
val, 0) != 0) {
|
||||||
return createErrorResponse("Failed to set saturation");
|
return createErrorResponse("Failed to set saturation");
|
||||||
@@ -197,7 +196,7 @@ std::string CameraController::handleSetSaturation(const std::string& value) {
|
|||||||
|
|
||||||
std::string CameraController::handleSetSharpness(const std::string& value) {
|
std::string CameraController::handleSetSharpness(const std::string& value) {
|
||||||
try {
|
try {
|
||||||
long val = std::stol(value);
|
const long val = std::stol(value);
|
||||||
if (VxSetUVCImageProcessing(camera_, VX_UVC_IMAGE_PROPERTIES::UVC_IMAGE_SHARPNESS,
|
if (VxSetUVCImageProcessing(camera_, VX_UVC_IMAGE_PROPERTIES::UVC_IMAGE_SHARPNESS,
|
||||||
val, 0) != 0) {
|
val, 0) != 0) {
|
||||||
return createErrorResponse("Failed to set sharpness");
|
return createErrorResponse("Failed to set sharpness");
|
||||||
@@ -210,7 +209,7 @@ std::string CameraController::handleSetSharpness(const std::string& value) {
|
|||||||
|
|
||||||
std::string CameraController::handleSetGamma(const std::string& value) {
|
std::string CameraController::handleSetGamma(const std::string& value) {
|
||||||
try {
|
try {
|
||||||
long val = std::stol(value);
|
const long val = std::stol(value);
|
||||||
if (VxSetUVCImageProcessing(camera_, VX_UVC_IMAGE_PROPERTIES::UVC_IMAGE_GAMMA,
|
if (VxSetUVCImageProcessing(camera_, VX_UVC_IMAGE_PROPERTIES::UVC_IMAGE_GAMMA,
|
||||||
val, 0) != 0) {
|
val, 0) != 0) {
|
||||||
return createErrorResponse("Failed to set gamma");
|
return createErrorResponse("Failed to set gamma");
|
||||||
@@ -223,7 +222,7 @@ std::string CameraController::handleSetGamma(const std::string& value) {
|
|||||||
|
|
||||||
std::string CameraController::handleSetGain(const std::string& value) {
|
std::string CameraController::handleSetGain(const std::string& value) {
|
||||||
try {
|
try {
|
||||||
long val = std::stol(value);
|
const long val = std::stol(value);
|
||||||
if (VxSetUVCImageProcessing(camera_, VX_UVC_IMAGE_PROPERTIES::UVC_IMAGE_GAIN,
|
if (VxSetUVCImageProcessing(camera_, VX_UVC_IMAGE_PROPERTIES::UVC_IMAGE_GAIN,
|
||||||
val, 0) != 0) {
|
val, 0) != 0) {
|
||||||
return createErrorResponse("Failed to set gain");
|
return createErrorResponse("Failed to set gain");
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ bool GStreamerPipeline::start() {
|
|||||||
|
|
||||||
// Set callbacks
|
// Set callbacks
|
||||||
std::cout << "[DEBUG] Setting appsrc callbacks..." << std::endl;
|
std::cout << "[DEBUG] Setting appsrc callbacks..." << std::endl;
|
||||||
GstAppSrcCallbacks callbacks;
|
GstAppSrcCallbacks callbacks = {};
|
||||||
callbacks.need_data = onNeedData;
|
callbacks.need_data = onNeedData;
|
||||||
callbacks.enough_data = onEnoughData;
|
callbacks.enough_data = onEnoughData;
|
||||||
callbacks.seek_data = nullptr;
|
callbacks.seek_data = nullptr;
|
||||||
@@ -82,7 +82,7 @@ bool GStreamerPipeline::start() {
|
|||||||
|
|
||||||
// Start the pipeline
|
// Start the pipeline
|
||||||
std::cout << "[DEBUG] Setting pipeline state to PLAYING..." << std::endl;
|
std::cout << "[DEBUG] Setting pipeline state to PLAYING..." << std::endl;
|
||||||
GstStateChangeReturn ret = gst_element_set_state(pipeline_, GST_STATE_PLAYING);
|
const GstStateChangeReturn ret = gst_element_set_state(pipeline_, GST_STATE_PLAYING);
|
||||||
if (ret == GST_STATE_CHANGE_FAILURE) {
|
if (ret == GST_STATE_CHANGE_FAILURE) {
|
||||||
std::cerr << "[ERROR] Failed to set pipeline state to PLAYING" << std::endl;
|
std::cerr << "[ERROR] Failed to set pipeline state to PLAYING" << std::endl;
|
||||||
gst_object_unref(appsrc_);
|
gst_object_unref(appsrc_);
|
||||||
@@ -194,7 +194,7 @@ bool GStreamerPipeline::pushBuffer(const uint8_t* data, const size_t size, const
|
|||||||
gst_buffer_unmap(buffer, &map);
|
gst_buffer_unmap(buffer, &map);
|
||||||
|
|
||||||
// Push buffer to pipeline
|
// Push buffer to pipeline
|
||||||
GstFlowReturn ret = gst_app_src_push_buffer(GST_APP_SRC(appsrc_), buffer);
|
const GstFlowReturn ret = gst_app_src_push_buffer(GST_APP_SRC(appsrc_), buffer);
|
||||||
if (ret != GST_FLOW_OK) {
|
if (ret != GST_FLOW_OK) {
|
||||||
std::cerr << "[ERROR] Failed to push buffer to pipeline, flow return: " << ret << std::endl;
|
std::cerr << "[ERROR] Failed to push buffer to pipeline, flow return: " << ret << std::endl;
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ void SocketServer::serverLoop() {
|
|||||||
|
|
||||||
void SocketServer::handleClient(const int clientFd) {
|
void SocketServer::handleClient(const int clientFd) {
|
||||||
char buffer[4096];
|
char buffer[4096];
|
||||||
ssize_t bytesRead = recv(clientFd, buffer, sizeof(buffer) - 1, 0);
|
const ssize_t bytesRead = recv(clientFd, buffer, sizeof(buffer) - 1, 0);
|
||||||
|
|
||||||
if (bytesRead > 0) {
|
if (bytesRead > 0) {
|
||||||
buffer[bytesRead] = '\0';
|
buffer[bytesRead] = '\0';
|
||||||
|
|||||||
@@ -46,7 +46,8 @@ bool StreamingEngine::start(const std::string& gstPipeline) {
|
|||||||
currentFormat_ = fmtList[0];
|
currentFormat_ = fmtList[0];
|
||||||
|
|
||||||
// Allocate buffer (assume worst case: uncompressed)
|
// Allocate buffer (assume worst case: uncompressed)
|
||||||
bufferSize_ = currentFormat_.width * currentFormat_.height * 4;
|
const size_t calculatedBufferSize = currentFormat_.width * currentFormat_.height * 4;
|
||||||
|
bufferSize_ = calculatedBufferSize;
|
||||||
buffer_ = std::make_unique<uint8_t[]>(bufferSize_);
|
buffer_ = std::make_unique<uint8_t[]>(bufferSize_);
|
||||||
|
|
||||||
// Start GStreamer pipeline
|
// Start GStreamer pipeline
|
||||||
@@ -104,7 +105,7 @@ void StreamingEngine::acquisitionLoop() {
|
|||||||
|
|
||||||
while (running_) {
|
while (running_) {
|
||||||
int dataSize = 0;
|
int dataSize = 0;
|
||||||
VX_CAPTURE_RESULT result = VxGetImage(camera_, buffer_.get(), &dataSize, 1000);
|
const VX_CAPTURE_RESULT result = VxGetImage(camera_, buffer_.get(), &dataSize, 1000);
|
||||||
|
|
||||||
if (result == VX_CAPTURE_RESULT::VX_SUCCESS && dataSize > 0) {
|
if (result == VX_CAPTURE_RESULT::VX_SUCCESS && dataSize > 0) {
|
||||||
// Push frame to GStreamer pipeline
|
// Push frame to GStreamer pipeline
|
||||||
@@ -128,8 +129,8 @@ void StreamingEngine::acquisitionLoop() {
|
|||||||
framesInLastSecond++;
|
framesInLastSecond++;
|
||||||
|
|
||||||
// Print statistics every second
|
// Print statistics every second
|
||||||
auto now = std::chrono::steady_clock::now();
|
const auto now = std::chrono::steady_clock::now();
|
||||||
auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(now - lastStatsTime);
|
const auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(now - lastStatsTime);
|
||||||
if (elapsed.count() >= 1) {
|
if (elapsed.count() >= 1) {
|
||||||
std::cout << "FPS: " << framesInLastSecond
|
std::cout << "FPS: " << framesInLastSecond
|
||||||
<< " | Total frames: " << frameCount
|
<< " | Total frames: " << frameCount
|
||||||
|
|||||||
4
main.cpp
4
main.cpp
@@ -20,7 +20,7 @@ int main() {
|
|||||||
|
|
||||||
// List available cameras
|
// List available cameras
|
||||||
std::vector<std::string> devList;
|
std::vector<std::string> devList;
|
||||||
int deviceCount = VxDiscoverCameraDevices(devList);
|
const int deviceCount = VxDiscoverCameraDevices(devList);
|
||||||
|
|
||||||
if (deviceCount == 0) {
|
if (deviceCount == 0) {
|
||||||
std::cout << "No cameras found" << std::endl;
|
std::cout << "No cameras found" << std::endl;
|
||||||
@@ -72,7 +72,7 @@ int main() {
|
|||||||
<< " @ " << fmtList[0].framerate << " fps" << std::endl;
|
<< " @ " << fmtList[0].framerate << " fps" << std::endl;
|
||||||
|
|
||||||
// Create camera controller
|
// Create camera controller
|
||||||
auto controller = std::make_shared<CameraController>(cam);
|
const auto controller = std::make_shared<CameraController>(cam);
|
||||||
|
|
||||||
// Start Unix domain socket server
|
// Start Unix domain socket server
|
||||||
const std::string socketPath = "/tmp/vizion_control.sock";
|
const std::string socketPath = "/tmp/vizion_control.sock";
|
||||||
|
|||||||
Reference in New Issue
Block a user