1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
3 
4 #pragma once
5 #ifndef LIBREALSENSE_DEVICE_H
6 #define LIBREALSENSE_DEVICE_H
7 
8 #include "uvc.h"
9 #include "stream.h"
10 #include <chrono>
11 
12 namespace rsimpl
13 {
14     struct frame_timestamp_reader
15     {
16         virtual bool validate_frame(const subdevice_mode & mode, const void * frame) const = 0;
17         virtual int get_frame_timestamp(const subdevice_mode & mode, const void * frame) = 0;
18     };
19 }
20 
21 struct rs_device
22 {
23 private:
24     const std::shared_ptr<rsimpl::uvc::device>  device;
25 protected:
26     rsimpl::device_config                       config;
27 private:
28     rsimpl::native_stream                       depth, color, infrared, infrared2;
29     rsimpl::point_stream                        points;
30     rsimpl::rectified_stream                    rect_color;
31     rsimpl::aligned_stream                      color_to_depth, depth_to_color, depth_to_rect_color, infrared2_to_depth, depth_to_infrared2;
32     rsimpl::native_stream *                     native_streams[RS_STREAM_NATIVE_COUNT];
33     rsimpl::stream_interface *                  streams[RS_STREAM_COUNT];
34 
35     bool                                        capturing;
36     std::chrono::high_resolution_clock::time_point capture_started;
37 
38     std::shared_ptr<rsimpl::frame_archive>      archive;
39 protected:
get_devicers_device40     const rsimpl::uvc::device &                 get_device() const { return *device; }
get_devicers_device41     rsimpl::uvc::device &                       get_device() { return *device; }
42 public:
43                                                 rs_device(std::shared_ptr<rsimpl::uvc::device> device, const rsimpl::static_device_info & info);
44                                                 ~rs_device();
45 
get_stream_interfacers_device46     const rsimpl::stream_interface &            get_stream_interface(rs_stream stream) const { return *streams[stream]; }
47 
get_namers_device48     const char *                                get_name() const { return config.info.name.c_str(); }
get_serialrs_device49     const char *                                get_serial() const { return config.info.serial.c_str(); }
get_firmware_versionrs_device50     const char *                                get_firmware_version() const { return config.info.firmware_version.c_str(); }
get_depth_scalers_device51     float                                       get_depth_scale() const { return config.depth_scale; }
52 
53     void                                        enable_stream(rs_stream stream, int width, int height, rs_format format, int fps);
54     void                                        enable_stream_preset(rs_stream stream, rs_preset preset);
55     void                                        disable_stream(rs_stream stream);
56 
57     void                                        start();
58     void                                        stop();
is_capturingrs_device59     bool                                        is_capturing() const { return capturing; }
60 
61     void                                        wait_all_streams();
62     bool                                        poll_all_streams();
63 
64     virtual bool                                supports_option(rs_option option) const;
65     virtual void                                get_option_range(rs_option option, double & min, double & max, double & step);
66     virtual void                                set_options(const rs_option options[], int count, const double values[]) = 0;
67     virtual void                                get_options(const rs_option options[], int count, double values[]) = 0;
68 
69     virtual void                                on_before_start(const std::vector<rsimpl::subdevice_mode_selection> & selected_modes) = 0;
70     virtual rs_stream                           select_key_stream(const std::vector<rsimpl::subdevice_mode_selection> & selected_modes) = 0;
71     virtual std::shared_ptr<rsimpl::frame_timestamp_reader>
72                                                 create_frame_timestamp_reader() const = 0;
73 };
74 
75 namespace rsimpl
76 {
77     // This class is used to buffer up several writes to a structure-valued XU control, and send the entire structure all at once
78     // Additionally, it will ensure that any fields not set in a given struct will retain their original values
79     template<class T, class R, class W> struct struct_interface
80     {
81         T struct_;
82         R reader;
83         W writer;
84         bool active;
85 
struct_interfacestruct_interface86         struct_interface(R r, W w) : reader(r), writer(w), active(false) {}
87 
activatestruct_interface88         void activate() { if(!active) { struct_ = reader(); active = true; } }
getstruct_interface89         template<class U> double get(U T::* field) { activate(); return struct_.*field; }
setstruct_interface90         template<class U, class V> void set(U T::* field, V value) { activate(); struct_.*field = static_cast<U>(value); }
commitstruct_interface91         void commit() { if(active) writer(struct_); }
92     };
93 
make_struct_interface(R r,W w)94     template<class T, class R, class W> struct_interface<T,R,W> make_struct_interface(R r, W w) { return {r,w}; }
95 }
96 
97 #endif
98