mf
Media Framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
shared_ring.h
Go to the documentation of this file.
1 #ifndef MF_SHARED_RING_H_
2 #define MF_SHARED_RING_H_
3 
4 #include "timed_ring.h"
5 #include "../os/event.h"
6 #include <mutex>
7 #include <condition_variable>
8 #include <stdexcept>
9 #include <atomic>
10 #include <ostream>
11 
12 namespace mf {
13 
15 
32 class shared_ring {
33 public:
35 
36 private:
38 
39  using thread_state = time_unit;
40  enum : thread_state { idle = 0, accessing = -1 };
41 
42  timed_ring ring_;
43 
44  const bool seekable_;
45 
46  std::atomic<time_unit> end_time_{-1};
47  // TODO make non-atomic? verify
48 
49  mutable std::mutex mutex_;
50  event reader_idle_event_;
51  event reader_seek_event_;
52  event writer_idle_event_;
53 
54  std::atomic<thread_state> reader_state_{idle};
55  std::atomic<thread_state> writer_state_{idle};
56 
57  std::atomic<time_unit> read_start_time_{0};
58 
59  void skip_available_(time_unit duration);
60  void read_and_discard_(time_unit duration);
61 
62 public:
63  shared_ring(const frame_array_properties&, bool seekable, time_unit end_time = -1);
64 
65  void initialize();
66 
67  const frame_format& format() const noexcept { return ring_.format(); }
68 
70 
71  time_unit capacity() const { return ring_.shape().front(); }
72 
74 
83  section_view_type begin_write(time_unit write_duration);
84 
86 
88 
90 
94  bool wait_writable(event& break_event);
95 
97 
101  void end_write(time_unit written_duration, bool mark_end = false);
102 
104 
107 
109 
117  section_view_type begin_read(time_unit read_duration);
118 
120 
122 
124 
128  bool wait_readable(event& break_event);
129 
130 
132 
134  void end_read(time_unit read_duration);
135 
137 
142  void skip(time_unit skip_duration);
143 
145 
146  bool is_seekable() const { return seekable_; }
147 
149 
152  void seek(time_unit target_time);
153 
154 
156 
157  bool can_seek(time_unit target_time) const;
158 
160 
161  time_unit current_time() const;
162 
164 
167  time_unit write_start_time() const;
168 
170 
172  time_unit read_start_time() const;
173 
175 
178 
180 
183 
185 
187 
189 
191 
193 
195  time_unit end_time() const { return end_time_; }
196 
198  bool end_time_is_defined() const { return (end_time_ != -1); }
199 
201 
203  bool writer_reached_end() const;
204 
206 
209  bool reader_reached_end() const;
210 };
211 
212 
213 }
214 
215 #endif
Synchronization primitive representing event that a thread can wait for.
Definition: event.h:21
std::ptrdiff_t time_unit
Discrete time unit type.
Definition: common.h:52
const frame_format & format() const noexcept
Definition: ndarray_generic.h:45
time_unit readable_duration() const
Currently readable duration.
Definition: shared_ring.h:190
shared_ring(const frame_array_properties &, bool seekable, time_unit end_time=-1)
Definition: shared_ring.cc:7
timed_ring::section_view_type section_view_type
Definition: shared_ring.h:34
section_view_type begin_read_span(time_span)
Begin reading frames at time span span.
Definition: shared_ring.cc:142
void skip(time_unit skip_duration)
Skips duration frames.
Definition: shared_ring.cc:263
bool writer_reached_end() const
True if writer has written last frame.
Definition: shared_ring.cc:436
time_unit capacity() const
Capacity of buffer.
Definition: shared_ring.h:71
void end_write(time_unit written_duration, bool mark_end=false)
End writing written_duration frames.
Definition: shared_ring.cc:122
time_unit writable_duration() const
Currently writable duration.
Definition: shared_ring.h:186
bool wait_readable(event &break_event)
Wait until a frame become readable, or break_event occurs.
Definition: shared_ring.cc:229
time_unit end_time() const
End of file time. */.
Definition: shared_ring.h:195
time_unit read_start_time() const
Read start time.
Definition: shared_ring.cc:412
bool end_time_is_defined() const
True if end of file time is known. */.
Definition: shared_ring.h:198
time_span writable_time_span() const
Currently writable time span.
Definition: shared_ring.cc:421
Timed ring buffer with changed semantics, for dual-thread use.
Definition: shared_ring.h:32
bool can_seek(time_unit target_time) const
Verifies if is is possible to seek to read time t.
Definition: shared_ring.cc:336
void seek(time_unit target_time)
Seeks to read time t.
Definition: shared_ring.cc:342
time_unit current_time() const
Time of last written frame in buffer.
Definition: shared_ring.cc:401
bool is_seekable() const
Check if the ring is seekable.
Definition: shared_ring.h:146
time_unit duration() const
Definition: common.h:72
Ring buffer which adds absolute time index to frames.
Definition: timed_ring.h:13
const shape_type & shape() const noexcept
Definition: ndarray.h:101
section_view_type try_begin_read(time_unit read_duration)
Begin reading read_duration frames at current read start time, if they are available.
Definition: shared_ring.cc:206
bool reader_reached_end() const
True if reader has read last frame.
Definition: shared_ring.cc:442
const frame_format & format() const noexcept
Definition: shared_ring.h:67
One-dimensional time span.
Definition: common.h:61
time_unit write_start_time() const
Presumptive start time of next write.
Definition: shared_ring.cc:406
section_view_type begin_write(time_unit write_duration)
Begin writing write_duration frames at current write start time.
Definition: shared_ring.cc:23
section_view_type try_begin_write(time_unit write_duration)
Begin writing write_duration frames at current write start time, if they are available.
Definition: shared_ring.cc:84
bool wait_writable(event &break_event)
Wait until a frame become writable, or break_event occurs.
Definition: shared_ring.cc:104
void initialize()
Definition: shared_ring.cc:14
ndarray_generic_properties frame_array_properties
Definition: frame.h:13
time_span readable_time_span() const
Currently readable time span.
Definition: shared_ring.cc:430
Generic ndarray_timed_view where lower dimension(s) are type-erased.
Definition: ndarray_timed_view_generic.h:12
section_view_type begin_read(time_unit read_duration)
Begin reading read_duration frames at current read start time.
Definition: shared_ring.cc:161
timed_frame_array_view section_view_type
Definition: timed_ring.h:23
void end_read(time_unit read_duration)
End reading read_duration frames.
Definition: shared_ring.cc:248
Format information of type-erased frame of ndarray_view_generic.
Definition: frame_format.h:14