Skip to content

Commit 9216058

Browse files
authored
[RSDK-13421] Improve C++ code quality: constexpr, consistent logging, namespace cleanup (#39)
* Clean up namespace polution * Remove all couts * Use constexpr instead of defines * Use string for device type env
1 parent 7e214d5 commit 9216058

File tree

5 files changed

+53
-55
lines changed

5 files changed

+53
-55
lines changed

constraints.h

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,42 @@
11
#pragma once
22

3+
#include <string>
4+
35
// Module
4-
#define DEFAULT_SOCKET_PATH "/tmp/viam.csi.sock"
5-
#define RESOURCE_TYPE "CSICamera"
6-
#define API_NAMESPACE "viam"
7-
#define API_TYPE "camera"
8-
#define DEFAULT_API_SUBTYPE "csi"
6+
constexpr const char* DEFAULT_SOCKET_PATH = "/tmp/viam.csi.sock";
7+
constexpr const char* RESOURCE_TYPE = "CSICamera";
8+
constexpr const char* API_NAMESPACE = "viam";
9+
constexpr const char* API_TYPE = "camera";
10+
constexpr const char* DEFAULT_API_SUBTYPE = "csi";
911

1012
// GST
11-
#define GST_GET_STATE_TIMEOUT 1
12-
#define GST_CHANGE_STATE_TIMEOUT 5
13+
constexpr int GST_GET_STATE_TIMEOUT = 1;
14+
constexpr int GST_CHANGE_STATE_TIMEOUT = 5;
1315

1416
// Pipeline
15-
#define DEFAULT_INPUT_SOURCE "libcamerasrc"
16-
#define DEFAULT_INPUT_SENSOR "0"
17-
#define DEFAULT_INPUT_FORMAT "video/x-raw"
18-
#define DEFAULT_INPUT_WIDTH 1920
19-
#define DEFAULT_INPUT_HEIGHT 1080
20-
#define DEFAULT_INPUT_FRAMERATE 30
21-
#define DEFAULT_VIDEO_CONVERTER "videoconvert"
22-
#define DEFAULT_OUTPUT_ENCODER "nvjpegenc"
23-
#define DEFAULT_OUTPUT_MIMETYPE "image/jpeg"
17+
constexpr const char* DEFAULT_INPUT_SOURCE = "libcamerasrc";
18+
constexpr const char* DEFAULT_INPUT_SENSOR = "0";
19+
constexpr const char* DEFAULT_INPUT_FORMAT = "video/x-raw";
20+
constexpr int DEFAULT_INPUT_WIDTH = 1920;
21+
constexpr int DEFAULT_INPUT_HEIGHT = 1080;
22+
constexpr int DEFAULT_INPUT_FRAMERATE = 30;
23+
constexpr const char* DEFAULT_VIDEO_CONVERTER = "videoconvert";
24+
constexpr const char* DEFAULT_OUTPUT_ENCODER = "nvjpegenc";
25+
constexpr const char* DEFAULT_OUTPUT_MIMETYPE = "image/jpeg";
2426

2527
// Jetson
26-
#define JETSON_API_SUBTYPE "csi"
27-
#define JETSON_INPUT_SOURCE "nvarguscamerasrc"
28-
#define JETSON_INPUT_FORMAT "video/x-raw(memory:NVMM)"
29-
#define JETSON_VIDEO_CONVERTER "nvvidconv"
30-
#define JETSON_OUTPUT_ENCODER "nvjpegenc"
28+
constexpr const char* JETSON_API_SUBTYPE = "csi";
29+
constexpr const char* JETSON_INPUT_SOURCE = "nvarguscamerasrc";
30+
constexpr const char* JETSON_INPUT_FORMAT = "video/x-raw(memory:NVMM)";
31+
constexpr const char* JETSON_VIDEO_CONVERTER = "nvvidconv";
32+
constexpr const char* JETSON_OUTPUT_ENCODER = "nvjpegenc";
3133

3234
// Pi
33-
#define PI_API_SUBTYPE "csi-pi"
34-
#define PI_INPUT_SOURCE "libcamerasrc"
35-
#define PI_INPUT_FORMAT "video/x-raw,format=NV12"
36-
#define PI_VIDEO_CONVERTER "videoconvert"
37-
#define PI_OUTPUT_ENCODER "jpegenc"
35+
constexpr const char* PI_API_SUBTYPE = "csi-pi";
36+
constexpr const char* PI_INPUT_SOURCE = "libcamerasrc";
37+
constexpr const char* PI_INPUT_FORMAT = "video/x-raw,format=NV12";
38+
constexpr const char* PI_VIDEO_CONVERTER = "videoconvert";
39+
constexpr const char* PI_OUTPUT_ENCODER = "jpegenc";
3840

3941
// Integration Tests
4042
inline const std::string TEST_GST_PIPELINE =

csi_camera.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include <chrono>
22
#include <cstring>
33
#include <fstream>
4-
#include <iostream>
54
#include <sstream>
65
#include <thread>
76

@@ -116,8 +115,7 @@ void CSICamera::init_csi(const std::string pipeline_args) {
116115
GError* error = nullptr;
117116
pipeline = gst_parse_launch(pipeline_args.c_str(), &error);
118117
if (!pipeline) {
119-
std::cerr << "Failed to create the pipeline" << std::endl;
120-
g_print("Error: %s\n", error->message);
118+
VIAM_SDK_LOG(error) << "Failed to create the pipeline: " << error->message;
121119
g_error_free(error);
122120
throw Exception("Failed to create the pipeline");
123121
}
@@ -171,14 +169,14 @@ void CSICamera::wait_pipeline() {
171169
}
172170

173171
if (ret == GST_STATE_CHANGE_SUCCESS) {
174-
std::cout << "GST pipeline state change success" << std::endl;
172+
VIAM_SDK_LOG(debug) << "GST pipeline state change success";
175173
} else if (ret == GST_STATE_CHANGE_FAILURE) {
176-
std::cerr << "GST pipeline failed to change state" << std::endl;
174+
VIAM_SDK_LOG(error) << "GST pipeline failed to change state";
177175
throw Exception("GST pipeline failed to change state");
178176
} else if (ret == GST_STATE_CHANGE_NO_PREROLL) {
179-
std::cout << "GST pipeline changed but not enough data for preroll" << std::endl;
177+
VIAM_SDK_LOG(warn) << "GST pipeline changed but not enough data for preroll";
180178
} else {
181-
std::cerr << "GST pipeline failed to change state" << std::endl;
179+
VIAM_SDK_LOG(error) << "GST pipeline failed to change state";
182180
throw Exception("GST pipeline failed to change state");
183181
}
184182
}

csi_camera.h

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717

1818
#include "utils.h"
1919

20-
using namespace viam::sdk;
21-
22-
class CSICamera : public Camera, public Reconfigurable {
20+
class CSICamera : public viam::sdk::Camera, public viam::sdk::Reconfigurable {
2321
private:
2422
// Device
2523
device_type device;
@@ -37,22 +35,22 @@ class CSICamera : public Camera, public Reconfigurable {
3735

3836
public:
3937
// Module
40-
explicit CSICamera(const std::string name, const ProtoStruct& attrs);
38+
explicit CSICamera(const std::string name, const viam::sdk::ProtoStruct& attrs);
4139
~CSICamera();
42-
void init(const ProtoStruct& attrs);
40+
void init(const viam::sdk::ProtoStruct& attrs);
4341
void init_csi(const std::string pipeline_args);
44-
void validate_attrs(const ProtoStruct& attrs);
42+
void validate_attrs(const viam::sdk::ProtoStruct& attrs);
4543
template <typename T>
46-
void set_attr(const ProtoStruct& attrs, const std::string& name, T CSICamera::* member, T de);
44+
void set_attr(const viam::sdk::ProtoStruct& attrs, const std::string& name, T CSICamera::* member, T de);
4745

4846
// Camera
4947
// overrides camera component interface
50-
void reconfigure(const Dependencies& deps, const ResourceConfig& cfg) override;
51-
raw_image get_image(const std::string mime_type, const ProtoStruct& extra) override;
52-
image_collection get_images(std::vector<std::string> /* filter_source_names */, const ProtoStruct& /* extra */) override;
53-
ProtoStruct do_command(const ProtoStruct& command) override;
54-
point_cloud get_point_cloud(const std::string mime_type, const ProtoStruct& extra) override;
55-
std::vector<GeometryConfig> get_geometries(const ProtoStruct& extra) override;
48+
void reconfigure(const viam::sdk::Dependencies& deps, const viam::sdk::ResourceConfig& cfg) override;
49+
raw_image get_image(const std::string mime_type, const viam::sdk::ProtoStruct& extra) override;
50+
image_collection get_images(std::vector<std::string> /* filter_source_names */, const viam::sdk::ProtoStruct& /* extra */) override;
51+
viam::sdk::ProtoStruct do_command(const viam::sdk::ProtoStruct& command) override;
52+
point_cloud get_point_cloud(const std::string mime_type, const viam::sdk::ProtoStruct& extra) override;
53+
std::vector<viam::sdk::GeometryConfig> get_geometries(const viam::sdk::ProtoStruct& extra) override;
5654
properties get_properties() override;
5755

5856
// GST

utils.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55

66
device_type get_device_type() {
77
// Check for environment variable override
8-
const char* env_device = std::getenv("VIAM_CSI_DEVICE");
9-
if (env_device != nullptr) {
10-
std::string device_str(env_device);
11-
std::transform(device_str.begin(), device_str.end(), device_str.begin(), ::tolower);
8+
const char* env_raw = std::getenv("VIAM_CSI_DEVICE");
9+
std::string env_device = env_raw ? env_raw : "";
10+
if (!env_device.empty()) {
11+
std::transform(env_device.begin(), env_device.end(), env_device.begin(), ::tolower);
1212

13-
if (device_str == "jetson") {
13+
if (env_device == "jetson") {
1414
return device_type(device_type::jetson, "Jetson");
15-
} else if (device_str == "pi") {
15+
} else if (env_device == "pi") {
1616
return device_type(device_type::pi, "Raspberry Pi");
17-
} else if (device_str == "test") {
17+
} else if (env_device == "test") {
1818
return device_type(device_type::test, "Test");
1919
}
2020
}
@@ -37,7 +37,7 @@ device_type get_device_type() {
3737
device_name.close();
3838
}
3939

40-
return device_type(device_type::unknown, "unkwnown");
40+
return device_type(device_type::unknown, "unknown");
4141
}
4242

4343
device_params get_device_params(device_type device) {

utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include "constraints.h"
66

7-
#define DEVICE_PATH "/proc/device-tree/model"
7+
const std::string DEVICE_PATH = "/proc/device-tree/model";
88

99
struct device_type {
1010
enum type { unknown, jetson, pi, test };

0 commit comments

Comments
 (0)