OrbbecSDK 2.5.5
OrbbecSDK: Software-Development-Kit for Orbbec 3D Cameras
Loading...
Searching...
No Matches
Context.hpp
Go to the documentation of this file.
1// Copyright (c) Orbbec Inc. All Rights Reserved.
2// Licensed under the MIT License.
3
10#pragma once
11
13#include "Types.hpp"
14#include "Error.hpp"
15
16#include <functional>
17#include <memory>
18
19namespace ob {
20
21// forward declarations
22class Device;
23class DeviceInfo;
24class DeviceList;
25
26class Context {
27public:
34 typedef std::function<void(std::shared_ptr<DeviceList> removedList, std::shared_ptr<DeviceList> addedList)> DeviceChangedCallback;
35
42 typedef std::function<void(OBLogSeverity severity, const char *logMsg)> LogCallback;
43
44private:
45 ob_context *impl_ = nullptr;
46 DeviceChangedCallback deviceChangedCallback_;
47 // static LogCallback logCallback_;
48
49public:
58 explicit Context(const char *configPath = "") {
59 ob_error *error = nullptr;
60 impl_ = ob_create_context_with_config(configPath, &error);
61 Error::handle(&error);
62 }
63
67 ~Context() noexcept {
68 ob_error *error = nullptr;
69 ob_delete_context(impl_, &error);
70 Error::handle(&error, false);
71 }
72
78 std::shared_ptr<DeviceList> queryDeviceList() const {
79 ob_error *error = nullptr;
80 auto list = ob_query_device_list(impl_, &error);
81 Error::handle(&error);
82 return std::make_shared<DeviceList>(list);
83 }
84
94 void enableNetDeviceEnumeration(bool enable) const {
95 ob_error *error = nullptr;
96 ob_enable_net_device_enumeration(impl_, enable, &error);
97 Error::handle(&error);
98 }
99
111 bool forceIp(const char *macAddress, const OBNetIpConfig &config) {
112 ob_error *error = nullptr;
113 auto res = ob_force_ip_config(macAddress, config, &error);
114 Error::handle(&error);
115 return res;
116 }
117
125 std::shared_ptr<Device> createNetDevice(const char *address, uint16_t port) const {
126 ob_error *error = nullptr;
127 auto device = ob_create_net_device(impl_, address, port, &error);
128 Error::handle(&error);
129 return std::make_shared<Device>(device);
130 }
131
139 deviceChangedCallback_ = callback;
140 ob_error *error = nullptr;
141 ob_set_device_changed_callback(impl_, &Context::deviceChangedCallback, this, &error);
142 Error::handle(&error);
143 }
144
150 void enableDeviceClockSync(uint64_t repeatIntervalMsec) const {
151 ob_error *error = nullptr;
152 ob_enable_device_clock_sync(impl_, repeatIntervalMsec, &error);
153 Error::handle(&error);
154 }
155
160 void freeIdleMemory() const {
161 ob_error *error = nullptr;
162 ob_free_idle_memory(impl_, &error);
163 Error::handle(&error);
164 }
165
175 ob_error *error = nullptr;
176 ob_set_uvc_backend_type(impl_, type, &error);
177 Error::handle(&error);
178 }
179
186 static void setLoggerSeverity(OBLogSeverity severity) {
187 ob_error *error = nullptr;
188 ob_set_logger_severity(severity, &error);
189 Error::handle(&error);
190 }
191
199 static void setLoggerToFile(OBLogSeverity severity, const char *directory) {
200 ob_error *error = nullptr;
201 ob_set_logger_to_file(severity, directory, &error);
202 Error::handle(&error);
203 }
204
210 static void setLoggerToConsole(OBLogSeverity severity) {
211 ob_error *error = nullptr;
212 ob_set_logger_to_console(severity, &error);
213 Error::handle(&error);
214 }
215
222 static void setLoggerToCallback(OBLogSeverity severity, LogCallback callback) {
223 ob_error *error = nullptr;
224 Context::getLogCallback() = callback;
225 ob_set_logger_to_callback(severity, &Context::logCallback, &Context::getLogCallback(), &error);
226 Error::handle(&error);
227 }
228
237 static void setExtensionsDirectory(const char *directory) {
238 ob_error *error = nullptr;
239 ob_set_extensions_directory(directory, &error);
240 Error::handle(&error);
241 }
242
243private:
244 static void deviceChangedCallback(ob_device_list *removedList, ob_device_list *addedList, void *userData) {
245 auto ctx = static_cast<Context *>(userData);
246 if(ctx && ctx->deviceChangedCallback_) {
247 auto removed = std::make_shared<DeviceList>(removedList);
248 auto added = std::make_shared<DeviceList>(addedList);
249 ctx->deviceChangedCallback_(removed, added);
250 }
251 }
252
253 static void logCallback(OBLogSeverity severity, const char *logMsg, void *userData) {
254 auto cb = static_cast<LogCallback *>(userData);
255 if(cb) {
256 (*cb)(severity, logMsg);
257 }
258 }
259
260 // Lazy initialization of the logcallback_. The purpose is to initialize logcallback_ in .hpp
261 static LogCallback &getLogCallback() {
262 static LogCallback logCallback_ = nullptr;
263 return logCallback_;
264 }
265
266// for backward compatibility
267#define enableMultiDeviceSync enableDeviceClockSync
268};
269} // namespace ob
Context is a management class that describes the runtime of the SDK and is responsible for resource a...
OB_EXPORT void ob_enable_device_clock_sync(ob_context *context, uint64_t repeat_interval_msec, ob_error **error)
Activates device clock synchronization to synchronize the clock of the host and all created devices (...
OB_EXPORT void ob_set_logger_to_callback(ob_log_severity severity, ob_log_callback callback, void *user_data, ob_error **error)
Set the log callback function.
OB_EXPORT void ob_set_uvc_backend_type(ob_context *context, ob_uvc_backend_type backend_type, ob_error **error)
For linux, there are two ways to enable the UVC backend: libuvc and v4l2. This function is used to se...
OB_EXPORT ob_context * ob_create_context_with_config(const char *config_file_path, ob_error **error)
Create a context object with a specified configuration file.
OB_EXPORT void ob_set_device_changed_callback(ob_context *context, ob_device_changed_callback callback, void *user_data, ob_error **error)
Set a device plug-in callback function.
OB_EXPORT void ob_free_idle_memory(ob_context *context, ob_error **error)
Free idle memory from the internal frame memory pool.
OB_EXPORT void ob_set_logger_to_file(ob_log_severity severity, const char *directory, ob_error **error)
Set the log output to a file.
OB_EXPORT void ob_set_logger_severity(ob_log_severity severity, ob_error **error)
Set the global log level.
OB_EXPORT ob_device * ob_create_net_device(ob_context *context, const char *address, uint16_t port, ob_error **error)
Create a network device object.
OB_EXPORT void ob_set_logger_to_console(ob_log_severity severity, ob_error **error)
Set the log output to the console.
OB_EXPORT ob_device_list * ob_query_device_list(ob_context *context, ob_error **error)
Get a list of enumerated devices.
OB_EXPORT void ob_delete_context(ob_context *context, ob_error **error)
Delete a context object.
OB_EXPORT void ob_enable_net_device_enumeration(ob_context *context, bool enable, ob_error **error)
Enable or disable network device enumeration.
OB_EXPORT bool ob_force_ip_config(const char *deviceUid, ob_net_ip_config config, ob_error **error)
"Force" a static IP address configuration in a device identified by its MAC Address.
OB_EXPORT void ob_set_extensions_directory(const char *directory, ob_error **error)
Set the extensions directory.
This file defines the Error class, which describes abnormal errors within the SDK....
struct ob_context_t ob_context
Definition ObTypes.h:22
enum ob_uvc_backend_type OBUvcBackendType
struct ob_device_list_t ob_device_list
Definition ObTypes.h:25
OBLogSeverity
log level, the higher the level, the stronger the log filter
Definition ObTypes.h:84
void enableNetDeviceEnumeration(bool enable) const
enable or disable net device enumeration.
Definition Context.hpp:94
std::function< void(std::shared_ptr< DeviceList > removedList, std::shared_ptr< DeviceList > addedList)> DeviceChangedCallback
Type definition for the device changed callback function.
Definition Context.hpp:34
static void setLoggerSeverity(OBLogSeverity severity)
Set the level of the global log, which affects both the log level output to the console,...
Definition Context.hpp:186
static void setLoggerToConsole(OBLogSeverity severity)
Set log output to the console.
Definition Context.hpp:210
void setDeviceChangedCallback(DeviceChangedCallback callback)
Set the device plug-in callback function.
Definition Context.hpp:138
bool forceIp(const char *macAddress, const OBNetIpConfig &config)
"Force" a static IP address configuration in a device identified by its MAC Address.
Definition Context.hpp:111
std::function< void(OBLogSeverity severity, const char *logMsg)> LogCallback
Type definition for the log output callback function.
Definition Context.hpp:42
void enableDeviceClockSync(uint64_t repeatIntervalMsec) const
Activates device clock synchronization to synchronize the clock of the host and all created devices (...
Definition Context.hpp:150
static void setLoggerToCallback(OBLogSeverity severity, LogCallback callback)
Set the logger to callback.
Definition Context.hpp:222
~Context() noexcept
Context destructor.
Definition Context.hpp:67
void setUvcBackendType(OBUvcBackendType type) const
For linux, there are two ways to enable the UVC backend: libuvc and v4l2. This function is used to se...
Definition Context.hpp:174
static void setExtensionsDirectory(const char *directory)
Set the extensions directory.
Definition Context.hpp:237
void freeIdleMemory() const
Frees idle memory from the internal frame memory pool.
Definition Context.hpp:160
Context(const char *configPath="")
Context constructor.
Definition Context.hpp:58
std::shared_ptr< Device > createNetDevice(const char *address, uint16_t port) const
Creates a network device with the specified IP address and port.
Definition Context.hpp:125
std::shared_ptr< DeviceList > queryDeviceList() const
Queries the enumerated device list.
Definition Context.hpp:78
static void setLoggerToFile(OBLogSeverity severity, const char *directory)
Set log output to a file.
Definition Context.hpp:199
A class describing device information, representing the name, id, serial number and other basic infor...
Definition Device.hpp:872
Class representing a list of devices.
Definition Device.hpp:1109
static void handle(ob_error **error, bool throw_exception=true)
A static function to handle the ob_error and throw an exception if needed.
Definition Error.hpp:38
Definition Context.hpp:19
IP address configuration for network devices (IPv4)
Definition ObTypes.h:1098
The error class exposed by the SDK, users can get detailed error information according to the error.
Definition ObTypes.h:117