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_SYNC_H
6 #define LIBREALSENSE_SYNC_H
7 
8 #include "types.h"
9 
10 namespace rsimpl
11 {
12     class frame_archive
13     {
14         // Define a movable but explicitly noncopyable buffer type to hold our frame data
15         struct frame
16         {
17             std::vector<byte> data;
18             int timestamp;
19 
frameframe20             frame() : timestamp() {}
21             frame(const frame & r) = delete;
frameframe22             frame(frame && r) : frame() { *this = std::move(r); }
23 
24             frame & operator = (const frame & r) = delete;
25             frame & operator = (frame && r) { data = move(r.data); timestamp = r.timestamp; return *this; }
26         };
27 
28         // This data will be left constant after creation, and accessed from all threads
29         subdevice_mode_selection modes[RS_STREAM_NATIVE_COUNT];
30         rs_stream key_stream;
31         std::vector<rs_stream> other_streams;
32 
33         // This data will be read and written exclusively from the application thread
34         frame frontbuffer[RS_STREAM_NATIVE_COUNT];
35 
36         // This data will be read and written by all threads, and synchronized with a mutex
37         std::vector<frame> frames[RS_STREAM_NATIVE_COUNT];
38         std::vector<frame> freelist;
39         std::mutex mutex;
40         std::condition_variable cv;
41 
42         // This data will be read and written exclusively from the frame callback thread
43         frame backbuffer[RS_STREAM_NATIVE_COUNT];
44 
45         void get_next_frames();
46         void dequeue_frame(rs_stream stream);
47         void discard_frame(rs_stream stream);
48         void cull_frames();
49     public:
50         frame_archive(const std::vector<subdevice_mode_selection> & selection, rs_stream key_stream);
51 
52         // Safe to call from any thread
is_stream_enabled(rs_stream stream)53         bool is_stream_enabled(rs_stream stream) const { return modes[stream].mode.pf.fourcc != 0; }
get_mode(rs_stream stream)54         const subdevice_mode_selection & get_mode(rs_stream stream) const { return modes[stream]; }
55 
56         // Application thread API
57         void wait_for_frames();
58         bool poll_for_frames();
59         const byte * get_frame_data(rs_stream stream) const;
60         int get_frame_timestamp(rs_stream stream) const;
61 
62         // Frame callback thread API
63         byte * alloc_frame(rs_stream stream, int timestamp);
64         void commit_frame(rs_stream stream);
65     };
66 }
67 
68 #endif