sync.h
Go to the documentation of this file.
00001 // License: Apache 2.0. See LICENSE file in root directory.
00002 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
00003 
00004 #pragma once
00005 #ifndef LIBREALSENSE_SYNC_H
00006 #define LIBREALSENSE_SYNC_H
00007 
00008 #include "archive.h"
00009 #include <atomic>
00010 #include "timestamps.h"
00011 #include <chrono>
00012 
00013 namespace rsimpl
00014 {
00015     class fps_calc
00016     {
00017     public:
00018         fps_calc(unsigned long long in_number_of_frames_to_sampling, int expected_fps)
00019             : _number_of_frames_to_sample(in_number_of_frames_to_sampling),
00020             _frame_counter(0),
00021             _actual_fps(expected_fps)
00022         {
00023             _time_samples.reserve(2);
00024         }
00025         double calc_fps(std::chrono::time_point<std::chrono::system_clock>& now_time)
00026         {
00027             ++_frame_counter;
00028             if (_frame_counter == _number_of_frames_to_sample || _frame_counter == 1)
00029             {
00030                 _time_samples.push_back(now_time);
00031                 if (_time_samples.size() == 2)
00032                 {
00033                     auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
00034                         _time_samples[1] - _time_samples[0]).count();
00035                     _actual_fps = (((double)_frame_counter - 1.) / duration) * 1000.;
00036                     _frame_counter = 0;
00037                     _time_samples.clear();
00038                 }
00039             }
00040 
00041             return _actual_fps;
00042         }
00043 
00044     private:
00045         double _actual_fps;
00046         unsigned long long _number_of_frames_to_sample;
00047         unsigned long long _frame_counter;
00048         std::vector<std::chrono::time_point<std::chrono::system_clock>> _time_samples;
00049     };
00050 
00051     class syncronizing_archive : public frame_archive
00052     {
00053     private:
00054         // This data will be left constant after creation, and accessed from all threads
00055         subdevice_mode_selection modes[RS_STREAM_NATIVE_COUNT];
00056         rs_stream key_stream;
00057         std::vector<rs_stream> other_streams;
00058 
00059         // This data will be read and written exclusively from the application thread
00060         frameset frontbuffer;
00061 
00062         // This data will be read and written by all threads, and synchronized with a mutex
00063         std::vector<frame> frames[RS_STREAM_NATIVE_COUNT];
00064         std::condition_variable_any cv;
00065         
00066         void get_next_frames();
00067         void dequeue_frame(rs_stream stream);
00068         void discard_frame(rs_stream stream);
00069         void cull_frames();
00070 
00071         timestamp_corrector            ts_corrector;
00072     public:
00073         syncronizing_archive(const std::vector<subdevice_mode_selection> & selection, 
00074             rs_stream key_stream, 
00075             std::atomic<uint32_t>* max_size,
00076             std::atomic<uint32_t>* event_queue_size,
00077             std::atomic<uint32_t>* events_timeout,
00078             std::chrono::high_resolution_clock::time_point capture_started = std::chrono::high_resolution_clock::now());
00079         
00080         // Application thread API
00081         void wait_for_frames();
00082         bool poll_for_frames();
00083 
00084         frameset * wait_for_frames_safe();
00085         bool poll_for_frames_safe(frameset ** frames);
00086 
00087         double get_frame_metadata(rs_stream stream, rs_frame_metadata frame_metadata) const;
00088         bool supports_frame_metadata(rs_stream stream, rs_frame_metadata frame_metadata) const;
00089         const byte * get_frame_data(rs_stream stream) const;
00090         double get_frame_timestamp(rs_stream stream) const;
00091         unsigned long long get_frame_number(rs_stream stream) const;
00092         long long get_frame_system_time(rs_stream stream) const;
00093         int get_frame_stride(rs_stream stream) const;
00094         int get_frame_bpp(rs_stream stream) const;
00095 
00096         frameset * clone_frontbuffer();
00097 
00098         // Frame callback thread API
00099         void commit_frame(rs_stream stream);
00100 
00101         void flush() override;
00102 
00103         void correct_timestamp(rs_stream stream);
00104         void on_timestamp(rs_timestamp_data data);
00105 
00106     };
00107 }
00108 
00109 #endif


librealsense
Author(s): Sergey Dorodnicov , Mark Horn , Reagan Lopez
autogenerated on Tue Jun 25 2019 19:54:39