OrbbecSDK 2.10.6
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#include <string>
19#include <unordered_map>
20#include <mutex>
21
22namespace ob {
23
24// forward declarations
25class Device;
26class DeviceInfo;
27class DeviceList;
28
29class Context {
30public:
37 typedef std::function<void(std::shared_ptr<DeviceList> removedList, std::shared_ptr<DeviceList> addedList)> DeviceChangedCallback;
38
45 typedef std::function<void(OBLogSeverity severity, const char *logMsg)> LogCallback;
46
52
53private:
54 ob_context *impl_ = nullptr;
55 std::mutex callbackMtx_;
56
57 std::mutex legacyCallbackMtx_;
58 OBCallbackId legacyCallbackId_ = INVALID_CALLBACK_ID;
59
60 std::unordered_map<OBCallbackId, std::unique_ptr<DeviceChangedCallbackContext>> devChangedCallbacks_;
61 // static LogCallback logCallback_;
62
63public:
72 explicit Context(const char *configPath = "") {
73 ob_error *error = nullptr;
74 impl_ = ob_create_context_with_config(configPath, &error);
75 Error::handle(&error);
76 }
77
81 ~Context() noexcept {
82 // delete contex of C API, which will auto unregister all callbacks
83 ob_error *error = nullptr;
84 ob_delete_context(impl_, &error);
85 Error::handle(&error, false);
86 }
87
98 ob_error *error = nullptr;
99 ob_context_set_timestamp_clock_type(impl_, type, &error);
100 Error::handle(&error);
101 }
102
109 ob_error *error = nullptr;
110 auto type = ob_context_get_timestamp_clock_type(impl_, &error);
111 Error::handle(&error);
112 return type;
113 }
114
120 std::shared_ptr<DeviceList> queryDeviceList() const {
121 ob_error *error = nullptr;
122 auto list = ob_query_device_list(impl_, &error);
123 Error::handle(&error);
124 return std::make_shared<DeviceList>(list);
125 }
126
136 void enableNetDeviceEnumeration(bool enable) const {
137 ob_error *error = nullptr;
138 ob_enable_net_device_enumeration(impl_, enable, &error);
139 Error::handle(&error);
140 }
141
153 bool forceIp(const char *macAddress, const OBNetIpConfig &config) {
154 ob_error *error = nullptr;
155 auto res = ob_force_ip_config(macAddress, config, &error);
156 Error::handle(&error);
157 return res;
158 }
159
171 bool sendActionCommand(uint32_t deviceKey, uint32_t groupKey, uint32_t groupMask, const char *destIp = "255.255.255.255", uint64_t scheduledTime = 0) {
172 ob_error *error = nullptr;
173 bool ok = ob_send_action_command(deviceKey, groupKey, groupMask, destIp, scheduledTime, &error);
174 Error::handle(&error);
175 return ok;
176 }
177
188 ob_error *error = nullptr;
189 ob_set_gvcp_port_scheme(impl_, scheme, &error);
190 Error::handle(&error);
191 }
192
199 ob_error *error = nullptr;
200 auto scheme = ob_get_gvcp_port_scheme(impl_, &error);
201 Error::handle(&error);
202 return scheme;
203 }
204
216 std::shared_ptr<Device> createNetDevice(const char *address, uint16_t port, OBDeviceAccessMode accessMode = OB_DEVICE_DEFAULT_ACCESS) const {
217 ob_error *error = nullptr;
218 auto device = ob_create_net_device_ex(impl_, address, port, accessMode, &error);
219 Error::handle(&error);
220 return std::make_shared<Device>(device);
221 }
222
233 std::lock_guard<std::mutex> lock(legacyCallbackMtx_);
234 // remove the last callback first
235 if(legacyCallbackId_ != INVALID_CALLBACK_ID) {
236 unregisterDeviceChangedCallback(legacyCallbackId_);
237 }
238 // register the new callback
239 legacyCallbackId_ = registerDeviceChangedCallback(callback);
240 }
241
243 ob_error *error = nullptr;
244 auto cbCtxHolder = std::unique_ptr<DeviceChangedCallbackContext>(new DeviceChangedCallbackContext({ this, 0, nullptr }));
245 auto id = ob_register_device_changed_callback(impl_, &Context::deviceChangedCallback, (void *)cbCtxHolder.get(), &error);
246 Error::handle(&error);
247
248 cbCtxHolder->callbackId = id;
249 cbCtxHolder->callback = callback;
250 {
251 std::unique_lock<std::mutex> lock(callbackMtx_);
252 devChangedCallbacks_[id] = std::move(cbCtxHolder);
253 }
254 return id;
255 }
256
258 ob_error *error = nullptr;
259 ob_unregister_device_changed_callback(impl_, id, &error);
260 {
261 std::unique_lock<std::mutex> lock(callbackMtx_);
262 if(devChangedCallbacks_.count(id)) {
263 devChangedCallbacks_.erase(id);
264 }
265 }
266 Error::handle(&error);
267 }
268
274 void enableDeviceClockSync(uint64_t repeatIntervalMsec) const {
275 ob_error *error = nullptr;
276 ob_enable_device_clock_sync(impl_, repeatIntervalMsec, &error);
277 Error::handle(&error);
278 }
279
285 void syncDeviceHardwarePPSTime(uint64_t hardwarePPSTime) const {
286 ob_error *error = nullptr;
287 ob_sync_device_hardware_pps_time(impl_, hardwarePPSTime, &error);
288 Error::handle(&error);
289 }
290
295 void freeIdleMemory() const {
296 ob_error *error = nullptr;
297 ob_free_idle_memory(impl_, &error);
298 Error::handle(&error);
299 }
300
310 ob_error *error = nullptr;
311 ob_set_uvc_backend_type(impl_, type, &error);
312 Error::handle(&error);
313 }
314
321 static void setLoggerSeverity(OBLogSeverity severity) {
322 ob_error *error = nullptr;
323 ob_set_logger_severity(severity, &error);
324 Error::handle(&error);
325 }
326
334 static void setLoggerToFile(OBLogSeverity severity, const char *directory) {
335 ob_error *error = nullptr;
336 ob_set_logger_to_file(severity, directory, &error);
337 Error::handle(&error);
338 }
339
347 static void setLoggerFileName(const std::string &fileName) {
348 ob_error *error = nullptr;
349 ob_set_logger_file_name(fileName.c_str(), &error);
350 Error::handle(&error);
351 }
352
358 static void setLoggerToConsole(OBLogSeverity severity) {
359 ob_error *error = nullptr;
360 ob_set_logger_to_console(severity, &error);
361 Error::handle(&error);
362 }
363
370 static void setLoggerToCallback(OBLogSeverity severity, LogCallback callback) {
371 ob_error *error = nullptr;
372 Context::getLogCallback() = callback;
373 ob_set_logger_to_callback(severity, &Context::logCallback, &Context::getLogCallback(), &error);
374 Error::handle(&error);
375 }
376
387 static void logExternalMessage(OBLogSeverity severity, const std::string &module, const std::string &message, const std::string &file,
388 const std::string &func, int line) {
389 ob_error *error = nullptr;
390 ob_log_external_message(severity, module.c_str(), message.c_str(), file.c_str(), func.c_str(), line, &error);
391 Error::handle(&error);
392 }
393
402 static void setExtensionsDirectory(const char *directory) {
403 ob_error *error = nullptr;
404 ob_set_extensions_directory(directory, &error);
405 Error::handle(&error);
406 }
407
408private:
409 static void deviceChangedCallback(ob_device_list *removedList, ob_device_list *addedList, void *userData) {
410 auto cbCtx = static_cast<DeviceChangedCallbackContext *>(userData);
411 if(cbCtx && cbCtx->ctx && cbCtx->callbackId != INVALID_CALLBACK_ID) {
412 DeviceChangedCallback callbackCopy;
413 {
414 // preventing the captured variables from being released.
415 std::unique_lock<std::mutex> lock(cbCtx->ctx->callbackMtx_);
416 if(cbCtx->ctx->devChangedCallbacks_.count(cbCtx->callbackId)) {
417 callbackCopy = cbCtx->callback;
418 }
419 }
420 auto removed = std::make_shared<DeviceList>(removedList);
421 auto added = std::make_shared<DeviceList>(addedList);
422 callbackCopy(removed, added);
423 }
424 }
425
426 static void logCallback(OBLogSeverity severity, const char *logMsg, void *userData) {
427 auto cb = static_cast<LogCallback *>(userData);
428 if(cb) {
429 (*cb)(severity, logMsg);
430 }
431 }
432
433 // Lazy initialization of the logcallback_. The purpose is to initialize logcallback_ in .hpp
434 static LogCallback &getLogCallback() {
435 static LogCallback logCallback_ = nullptr;
436 return logCallback_;
437 }
438
439// for backward compatibility
440#define enableMultiDeviceSync enableDeviceClockSync
441};
442} // namespace ob
Context is a management class that describes the runtime of the SDK and is responsible for resource a...
OB_EXPORT void ob_set_gvcp_port_scheme(ob_context *context, ob_gvcp_port_scheme scheme, ob_error **error)
Set the GVCP port scheme used for network device discovery and control.
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 ob_clock_type ob_context_get_timestamp_clock_type(const ob_context *context, ob_error **error)
Get the current host-side timestamp clock type for the context.
OB_EXPORT bool ob_force_ip_config(const char *macAddress, 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_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 bool ob_send_action_command(uint32_t deviceKey, uint32_t groupKey, uint32_t groupMask, const char *destIp, uint64_t scheduledTime, ob_error **error)
Send an Action Command to GVCP devices.
OB_EXPORT ob_gvcp_port_scheme ob_get_gvcp_port_scheme(ob_context *context, ob_error **error)
Get the current GVCP port scheme.
OB_EXPORT ob_device * ob_create_net_device_ex(ob_context *context, const char *address, uint16_t port, ob_device_access_mode accessMode, ob_error **error)
Create a network device object.
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_context_set_timestamp_clock_type(ob_context *context, ob_clock_type clock_type, ob_error **error)
Set the host-side timestamp clock type for the current context.
OB_EXPORT void ob_set_logger_severity(ob_log_severity severity, ob_error **error)
Set the global log level.
OB_EXPORT void ob_unregister_device_changed_callback(ob_context *context, ob_callback_id callback_id, ob_error **error)
Unregister a previously registered device plug-in callback function.
OB_EXPORT void ob_set_logger_to_console(ob_log_severity severity, ob_error **error)
Set the log output to the console.
OB_EXPORT void ob_log_external_message(ob_log_severity severity, const char *module, const char *message, const char *file, const char *func, int line, ob_error **error)
Logs a message with severity, file, function, and line info.
OB_EXPORT void ob_sync_device_hardware_pps_time(ob_context *context, uint64_t hardware_pps_time, ob_error **error)
Synchronize the device time (synchronize hardwarePPS time to all created devices).
OB_EXPORT ob_callback_id ob_register_device_changed_callback(ob_context *context, ob_device_changed_callback callback, void *user_data, ob_error **error)
Register a device plug-in callback function.
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 void ob_set_logger_file_name(const char *file_name, ob_error **error)
Set the log file name for file output.
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....
@ OB_DEVICE_DEFAULT_ACCESS
Default access: control access for capable devices, ignored otherwise.
Definition ObTypes.h:2060
struct ob_context_t ob_context
Definition ObTypes.h:22
OBGvcpPortScheme
GVCP port scheme.
Definition ObTypes.h:173
OBClockType
Host-side timestamp clock type for device.
Definition ObTypes.h:2089
enum ob_device_access_mode OBDeviceAccessMode
enum ob_uvc_backend_type OBUvcBackendType
#define INVALID_CALLBACK_ID
Definition ObTypes.h:2217
struct ob_device_list_t ob_device_list
Definition ObTypes.h:25
uint64_t OBCallbackId
Callback Id.
Definition ObTypes.h:2216
OBLogSeverity
log level, the higher the level, the stronger the log filter
Definition ObTypes.h:123
void enableNetDeviceEnumeration(bool enable) const
enable or disable net device enumeration.
Definition Context.hpp:136
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:37
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:321
void syncDeviceHardwarePPSTime(uint64_t hardwarePPSTime) const
Activates synchronization time to all created devices (if supported).
Definition Context.hpp:285
static void setLoggerToConsole(OBLogSeverity severity)
Set log output to the console.
Definition Context.hpp:358
void setDeviceChangedCallback(DeviceChangedCallback callback)
Set the device plug-in callback function.
Definition Context.hpp:232
static void setLoggerFileName(const std::string &fileName)
Set the log file name for file output.
Definition Context.hpp:347
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:153
std::function< void(OBLogSeverity severity, const char *logMsg)> LogCallback
Type definition for the log output callback function.
Definition Context.hpp:45
void enableDeviceClockSync(uint64_t repeatIntervalMsec) const
Activates device clock synchronization to synchronize the clock of the host and all created devices (...
Definition Context.hpp:274
static void logExternalMessage(OBLogSeverity severity, const std::string &module, const std::string &message, const std::string &file, const std::string &func, int line)
Logs a message with severity, file, function, and line info.
Definition Context.hpp:387
static void setLoggerToCallback(OBLogSeverity severity, LogCallback callback)
Set the logger to callback.
Definition Context.hpp:370
OBGvcpPortScheme getGvcpPortScheme() const
Get the current GVCP port scheme.
Definition Context.hpp:198
~Context() noexcept
Context destructor.
Definition Context.hpp:81
void setTimestampClockType(OBClockType type) const
Set the host-side timestamp clock type for the current context.
Definition Context.hpp:97
void unregisterDeviceChangedCallback(OBCallbackId id)
Definition Context.hpp:257
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:309
OBCallbackId registerDeviceChangedCallback(DeviceChangedCallback callback)
Definition Context.hpp:242
static void setExtensionsDirectory(const char *directory)
Set the extensions directory.
Definition Context.hpp:402
void freeIdleMemory() const
Frees idle memory from the internal frame memory pool.
Definition Context.hpp:295
bool sendActionCommand(uint32_t deviceKey, uint32_t groupKey, uint32_t groupMask, const char *destIp="255.255.255.255", uint64_t scheduledTime=0)
Send an Action Command to GVCP devices.
Definition Context.hpp:171
OBClockType getTimestampClockType() const
Get the current host-side timestamp clock type for the context.
Definition Context.hpp:108
Context(const char *configPath="")
Context constructor.
Definition Context.hpp:72
std::shared_ptr< Device > createNetDevice(const char *address, uint16_t port, OBDeviceAccessMode accessMode=OB_DEVICE_DEFAULT_ACCESS) const
Creates a network device with the specified IP address and port.
Definition Context.hpp:216
void setGvcpPortScheme(OBGvcpPortScheme scheme)
Set the GVCP port scheme used for network device discovery and control.
Definition Context.hpp:187
std::shared_ptr< DeviceList > queryDeviceList() const
Queries the enumerated device list.
Definition Context.hpp:120
static void setLoggerToFile(OBLogSeverity severity, const char *directory)
Set log output to a file.
Definition Context.hpp:334
A class describing device information, representing the name, id, serial number and other basic infor...
Definition Device.hpp:1114
Class representing a list of devices.
Definition Device.hpp:1355
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
IP address configuration for network devices (IPv4), used with property OB_STRUCT_DEVICE_IP_ADDR_CONF...
Definition ObTypes.h:1303
The error class exposed by the SDK, users can get detailed error information according to the error.
Definition ObTypes.h:162