mf
Media Framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
node.h
Go to the documentation of this file.
1 #ifndef MF_FLOW_NODE_H_
2 #define MF_FLOW_NODE_H_
3 
4 #include "../common.h"
5 #include "../queue/frame.h"
6 #include <vector>
7 #include <atomic>
8 #include <string>
9 #include <memory>
10 
11 namespace mf { namespace flow {
12 
13 class graph;
14 class node_output;
15 class node_input;
16 class node_job;
17 
19 class node {
20 private:
21  graph& graph_;
22  std::vector<std::unique_ptr<node_output>> outputs_;
23  std::vector<std::unique_ptr<node_input>> inputs_;
24 
25  bool was_setup_ = false;
26  time_unit prefetch_duration_ = 0;
27  time_unit offset_ = -1;
28  time_unit stream_duration_ = -1;
29  bool seekable_ = false;
30 
31  bool active_ = true;
32  std::atomic<time_unit> current_time_ {-1};
33  std::atomic<time_unit> end_time_ {-1};
34 
35  void propagate_offset_(time_unit offset);
36  void propagate_setup_();
37  void deduce_stream_properties_();
38 
39 protected:
40  explicit node(graph& gr) : graph_(gr) { }
41  node(const node&) = delete;
42  node& operator=(const node&) = delete;
43 
44  void setup_sink();
45 
46  void set_current_time(time_unit t) noexcept { current_time_ = t; }
47  void mark_end() { end_time_ = current_time_ + 1; }
48 
50  void cancel_job(node_job&);
51 
52  template<typename Input>
53  Input& add_input_(time_unit past_window, time_unit future_window) {
54  Input* input = new Input(*this, inputs_.size(), past_window, future_window);
55  inputs_.emplace_back(input);
56  return *input;
57  }
58 
59  template<typename Output>
60  Output& add_output_(const frame_format& format) {
61  Output* output = new Output(*this, outputs_.size(), format);
62  outputs_.emplace_back(output);
63  return *output;
64  }
65 
66 public:
67  std::string name;
68 
69  virtual ~node() { }
70 
71  graph& this_graph() noexcept { return graph_; }
72 
75 
76  const auto& inputs() noexcept { return inputs_; }
77  const auto& outputs() noexcept { return outputs_; }
78 
79  bool is_source() const noexcept { return inputs_.empty(); }
80  bool is_sink() const noexcept { return outputs_.empty(); }
81 
82  virtual void internal_setup() = 0;
83  virtual void launch() = 0;
84  virtual void stop() = 0;
85 
86  virtual bool process_next_frame() = 0;
87 
88  time_unit end_time() const noexcept { return end_time_; }
89  bool reached_end() const noexcept;
90 
91  bool was_setup() const noexcept { return was_setup_; }
92 
93  time_unit prefetch_duration() const noexcept { return prefetch_duration_; }
94  time_unit offset() const noexcept { return offset_; }
95  bool stream_duration_is_defined() const noexcept { return (stream_duration_ != -1); }
96  time_unit stream_duration() const noexcept { return stream_duration_; }
97  bool is_seekable() const noexcept { return seekable_; }
98  bool is_bounded() const;
99 
100  bool is_active() const noexcept { MF_EXPECTS(was_setup_); return active_; }
101  void update_activation();
102 
103  time_unit current_time() const noexcept { return current_time_; }
104 };
105 
106 
108 class node_output {
109 private:
110  node& node_;
111  std::ptrdiff_t index_ = -1;
112 
113  node_input* connected_input_ = nullptr;
114  frame_format format_;
115  std::size_t frame_length_;
116 
117  bool active_ = true;
118 
119 protected:
120  node_output(const node_output&) = delete;
121 
122 public:
123  node_output(node& nd, std::ptrdiff_t index, const frame_format&);
124 
125  virtual ~node_output() { }
126 
127  std::ptrdiff_t index() const noexcept { return index_; }
128  node& this_node() const noexcept { return node_; }
129 
130  void define_frame_length(std::size_t len) { frame_length_ = len; }
131  std::size_t frame_length() const noexcept { return frame_length_; }
132 
133  void define_format(const frame_format& format) { format_ = format; }
134  const frame_format& format() const noexcept { return format_; }
135 
136  bool is_connected() const noexcept { return (connected_input_ != nullptr); }
137  node_input& connected_input() const noexcept { MF_EXPECTS(is_connected()); return *connected_input_; }
139 
140  // TODO adjust format for thin node series
141  virtual void setup() = 0;
142 
143  bool is_active() const noexcept { return active_; }
144  void propagate_activation(bool active);
145 
149  virtual void pull(time_span span) = 0;
150  virtual timed_frame_array_view begin_read(time_unit duration) = 0;
151  virtual void end_read(time_unit duration) = 0;
152  virtual time_unit end_time() const = 0;
154 
158  virtual frame_view begin_write_frame(time_unit& t) = 0;
159  virtual void end_write_frame(bool was_last_frame) = 0;
160  virtual void cancel_write_frame() = 0;
162 };
163 
164 
166 class node_input {
167 private:
168  node& node_;
169  std::ptrdiff_t index_ = -1;
170 
171  time_unit past_window_ = 0;
172  time_unit future_window_ = 0;
173  node_output* connected_output_ = nullptr;
174 
175  bool activated_ = true;
176 
177 protected:
178  node_input(const node_input&) = delete;
179 
180 public:
181  node_input(node& nd, std::ptrdiff_t index, time_unit past_window, time_unit future_window);
182 
183  std::ptrdiff_t index() const noexcept { return index_; }
184  node& this_node() const noexcept { return node_; }
185 
186  std::size_t frame_length() const noexcept { return connected_output().frame_length(); }
187  const frame_format& format() const noexcept { return connected_output().format(); }
188 
189  time_unit past_window_duration() const noexcept { return past_window_; }
190  time_unit future_window_duration() const noexcept { return future_window_; }
191 
192  void connect(node_output&);
193  bool is_connected() const noexcept { return (connected_output_ != nullptr); }
194  node_output& connected_output() const noexcept { MF_EXPECTS(is_connected()); return *connected_output_; }
195  node& connected_node() const noexcept { MF_EXPECTS(is_connected()); return connected_output().this_node(); }
196 
197  bool is_activated() const noexcept { return activated_; }
198  void set_activated(bool);
199 
202  void pull(time_unit t);
204  void end_read_frame(time_unit t);
205  void cancel_read_frame();
206  time_unit end_time() const { return connected_node().end_time(); }
208 };
209 
210 
211 }}
212 
213 #endif
node & connected_node() const noexcept
Definition: node.h:195
Input port of node in flow graph.
Definition: node.h:166
Generic ndarray_view where lower dimension(s) are type-erased.
Definition: ndarray_view_generic.h:25
Output port of node in flow graph.
Definition: node.h:108
void connect(node_output &)
Definition: node.cc:174
void end_read_frame(time_unit t)
Definition: node.cc:197
time_unit stream_duration() const noexcept
Definition: node.h:96
std::size_t frame_length() const noexcept
Definition: node.h:131
bool is_activated() const noexcept
Definition: node.h:197
std::ptrdiff_t time_unit
Discrete time unit type.
Definition: common.h:52
const frame_format & format() const noexcept
Definition: node.h:187
node_output(const node_output &)=delete
virtual void internal_setup()=0
Called by propagate_setup_.
node_input & connected_input() const noexcept
Definition: node.h:137
virtual ~node()
Definition: node.h:69
Output & add_output_(const frame_format &format)
Definition: node.h:60
virtual timed_frame_array_view begin_read(time_unit duration)=0
virtual void end_write_frame(bool was_last_frame)=0
bool reached_end() const noexcept
Definition: node.cc:141
void input_has_connected(node_input &)
Definition: node.cc:154
Node in flow graph, base class.
Definition: node.h:19
std::ptrdiff_t index() const noexcept
Definition: node.h:127
node & operator=(const node &)=delete
bool is_active() const noexcept
Definition: node.h:143
virtual time_unit end_time() const =0
time_unit past_window_duration() const noexcept
Definition: node.h:189
virtual bool process_next_frame()=0
std::string name
Definition: node.h:67
virtual void end_read(time_unit duration)=0
bool is_source() const noexcept
Definition: node.h:79
void mark_end()
Definition: node.h:47
virtual frame_view begin_write_frame(time_unit &t)=0
virtual void setup()=0
std::size_t frame_length() const noexcept
Definition: node.h:186
node_job make_job()
Definition: node.cc:130
timed_frame_array_view begin_read_frame(time_unit t)
Definition: node.cc:190
const auto & inputs() noexcept
Definition: node.h:76
bool stream_duration_is_defined() const noexcept
Definition: node.h:95
const auto & outputs() noexcept
Definition: node.h:77
bool was_setup() const noexcept
Definition: node.h:91
void cancel_job(node_job &)
Definition: node.cc:135
bool is_bounded() const
Definition: node.cc:102
Input & add_input_(time_unit past_window, time_unit future_window)
Definition: node.h:53
time_unit prefetch_duration() const noexcept
Definition: node.h:93
bool is_active() const noexcept
Definition: node.h:100
void set_current_time(time_unit t) noexcept
Definition: node.h:46
time_unit end_time() const noexcept
Definition: node.h:88
virtual void launch()=0
Called by graph for all nodes, before any frame is pulled from sink.
void set_prefetch_duration(time_unit)
Definition: node.cc:83
void define_source_stream_properties(bool seekable, time_unit stream_duration=-1)
Definition: node.cc:70
bool is_sink() const noexcept
Definition: node.h:80
node_output & connected_output() const noexcept
Definition: node.h:194
void propagate_activation(bool active)
Definition: node.cc:159
void define_format(const frame_format &format)
Definition: node.h:133
virtual void stop()=0
Called by graph for all node, before destruction of any node.
time_unit end_time() const
Definition: node.h:206
bool is_seekable() const noexcept
Definition: node.h:97
Work unit of flow graph node.
Definition: node_job.h:13
#define MF_EXPECTS(condition)
Definition: common.h:27
bool is_connected() const noexcept
Definition: node.h:136
const frame_format & format() const noexcept
Definition: node.h:134
void pull(time_unit t)
Definition: node.cc:180
std::ptrdiff_t index() const noexcept
Definition: node.h:183
virtual void cancel_write_frame()=0
void cancel_read_frame()
Definition: node.cc:203
graph & this_graph() noexcept
Definition: node.h:71
node & this_node() const noexcept
Definition: node.h:128
One-dimensional time span.
Definition: common.h:61
time_unit future_window_duration() const noexcept
Definition: node.h:190
time_unit offset() const noexcept
Definition: node.h:94
time_unit current_time() const noexcept
Definition: node.h:103
virtual void pull(time_span span)=0
node & this_node() const noexcept
Definition: node.h:184
bool is_connected() const noexcept
Definition: node.h:193
void define_frame_length(std::size_t len)
Definition: node.h:130
Generic ndarray_timed_view where lower dimension(s) are type-erased.
Definition: ndarray_timed_view_generic.h:12
node_input(const node_input &)=delete
Graph containing interconnected nodes through which media frames flow.
Definition: graph.h:23
void setup_sink()
Called by sink node, runs set up procedure for all node in graph.
Definition: node.cc:91
Format information of type-erased frame of ndarray_view_generic.
Definition: frame_format.h:14
node(graph &gr)
Definition: node.h:40
void update_activation()
Called by output, propagates to preceding nodes.
Definition: node.cc:110
virtual ~node_output()
Definition: node.h:125
void set_activated(bool)
Definition: node.cc:208