#pragma once
#include "Types.hpp"
#include "libobsensor/hpp/Filter.hpp"
#include "libobsensor/h/Sensor.h"
#include "libobsensor/h/Filter.h"
#include "Error.hpp"
#include "StreamProfile.hpp"
#include "Device.hpp"
#include "Frame.hpp"
#include <functional>
#include <memory>
#include <vector>
namespace ob {
class Sensor {
public:
typedef std::function<void(std::shared_ptr<Frame> frame)> FrameCallback;
protected:
ob_sensor_t *impl_;
FrameCallback callback_;
public:
explicit Sensor(ob_sensor_t *impl) : impl_(impl) {}
Sensor(Sensor &&sensor) noexcept : impl_(sensor.impl_) {
sensor.impl_ = nullptr;
}
Sensor &operator=(Sensor &&sensor) noexcept {
if(this != &sensor) {
ob_error *error = nullptr;
ob_delete_sensor(impl_, &error);
Error::handle(&error);
impl_ = sensor.impl_;
sensor.impl_ = nullptr;
}
return *this;
}
Sensor(const Sensor &sensor) = delete;
Sensor &operator=(const Sensor &sensor) = delete;
virtual ~Sensor() noexcept {
ob_error *error = nullptr;
ob_delete_sensor(impl_, &error);
Error::handle(&error, false);
}
OBSensorType getType() const {
ob_error *error = nullptr;
auto type = ob_sensor_get_type(impl_, &error);
Error::handle(&error);
return type;
}
std::shared_ptr<StreamProfileList> getStreamProfileList() const {
ob_error *error = nullptr;
auto list = ob_sensor_get_stream_profile_list(impl_, &error);
Error::handle(&error);
return std::make_shared<StreamProfileList>(list);
}
std::vector<std::shared_ptr<Filter>> createRecommendedFilters() const {
ob_error *error = nullptr;
auto list = ob_sensor_create_recommended_filter_list(impl_, &error);
Error::handle(&error);
auto filter_count = ob_filter_list_get_count(list, &error);
std::vector<std::shared_ptr<Filter>> filters;
for(uint32_t i = 0; i < filter_count; i++) {
auto filterImpl = ob_filter_list_get_filter(list, i, &error);
Error::handle(&error);
filters.push_back(std::make_shared<Filter>(filterImpl));
}
ob_delete_filter_list(list, &error);
Error::handle(&error, false);
return filters;
}
void start(std::shared_ptr<StreamProfile> streamProfile, FrameCallback callback) {
ob_error *error = nullptr;
callback_ = std::move(callback);
ob_sensor_start(impl_, const_cast<ob_stream_profile_t *>(streamProfile->getImpl()), &Sensor::frameCallback, this, &error);
Error::handle(&error);
}
void stop() const {
ob_error *error = nullptr;
ob_sensor_stop(impl_, &error);
Error::handle(&error);
}
void switchProfile(std::shared_ptr<StreamProfile> streamProfile) {
ob_error *error = nullptr;
ob_sensor_switch_profile(impl_, const_cast<ob_stream_profile_t *>(streamProfile->getImpl()), &error);
Error::handle(&error);
}
private:
static void frameCallback(ob_frame *frame, void *userData) {
auto sensor = static_cast<Sensor *>(userData);
sensor->callback_(std::make_shared<Frame>(frame));
}
public:
OBSensorType type() const {
return getType();
}
std::vector<std::shared_ptr<Filter>> getRecommendedFilters() const {
return createRecommendedFilters();
}
};
class SensorList {
private:
ob_sensor_list_t *impl_ = nullptr;
public:
explicit SensorList(ob_sensor_list_t *impl) : impl_(impl) {}
~SensorList() noexcept {
ob_error *error = nullptr;
ob_delete_sensor_list(impl_, &error);
Error::handle(&error, false);
}
uint32_t getCount() const {
ob_error *error = nullptr;
auto count = ob_sensor_list_get_count(impl_, &error);
Error::handle(&error);
return count;
}
OBSensorType getSensorType(uint32_t index) const {
ob_error *error = nullptr;
auto type = ob_sensor_list_get_sensor_type(impl_, index, &error);
Error::handle(&error);
return type;
}
std::shared_ptr<Sensor> getSensor(uint32_t index) const {
ob_error *error = nullptr;
auto sensor = ob_sensor_list_get_sensor(impl_, index, &error);
Error::handle(&error);
return std::make_shared<Sensor>(sensor);
}
std::shared_ptr<Sensor> getSensor(OBSensorType sensorType) const {
ob_error *error = nullptr;
auto sensor = ob_sensor_list_get_sensor_by_type(impl_, sensorType, &error);
Error::handle(&error);
return std::make_shared<Sensor>(sensor);
}
public:
uint32_t count() const {
return getCount();
}
OBSensorType type(uint32_t index) const {
return getSensorType(index);
}
};
}