mf
Media Framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
graph.h
Go to the documentation of this file.
1 #ifndef MF_FLOW_GRAPH_H_
2 #define MF_FLOW_GRAPH_H_
3 
4 #include "../common.h"
5 #include "../os/event.h"
6 #include "filter_node.h"
7 #include "sink_node.h"
8 #include <utility>
9 #include <vector>
10 #include <memory>
11 #include <stdexcept>
12 #include <type_traits>
13 #include <functional>
14 
15 namespace mf { namespace flow {
16 
17 class node;
18 class sync_node;
19 class filter;
20 class sink_filter;
21 
23 class graph {
24 public:
26 
27 private:
28  std::vector<std::unique_ptr<node>> nodes_;
29  sink_node* sink_ = nullptr;
30  bool was_setup_ = false;
31  bool running_ = false;
32  sticky_event stop_event_;
33 
34  template<typename Node, typename... Args>
35  Node& add_node_(Args&&... args) {
36  static_assert(std::is_base_of<node, Node>::value, "");
37  if(was_setup_) throw std::logic_error("cannot add node after graph already set up");
38  Node* nd = new Node(*this, std::forward<Args>(args)...);
39  nodes_.emplace_back(nd);
40  return *nd;
41  }
42 
43  template<typename Node, typename... Args>
44  Node& add_sink_(Args&&... args) {
45  static_assert(std::is_base_of<sink_node, Node>::value, "");
46  Node& sink = add_node_<Node>(std::forward<Args>(args)...);
47  sink_ = &sink;
48  return sink;
49  }
50 
51  void pull_next_frame_();
52 
53 
54 public:
55  std::function<frame_callback_function_type> callback_function;
56 
57  ~graph();
58 
59  template<typename Filter, typename Node = sync_node, typename... Args>
60  Filter& add_filter(Args&&... args) {
61  static_assert(std::is_base_of<filter, Filter>::value, "");
62  static_assert(std::is_base_of<filter_node, Node>::value, "");
63  filter_node& nd = add_node_<Node>();
64  return nd.set_filter<Filter>(std::forward<Args>(args)...);
65  }
66 
67  template<typename Filter, typename... Args>
68  Filter& add_sink_filter(Args&&... args) {
69  static_assert(std::is_base_of<sink_filter, Filter>::value, "");
70  filter_node& nd = add_sink_<sink_node>();
71  return nd.set_filter<Filter>(std::forward<Args>(args)...);
72  }
73 
74  bool was_setup() const { return was_setup_; }
75  bool is_running() const { return running_; }
76 
77  event& stop_event() { return stop_event_; }
78 
79  void setup();
80 
81  void launch();
82  void stop();
83 
84  time_unit current_time() const;
85 
86  void run_until(time_unit last_frame);
87  void run_for(time_unit duration);
88  bool run();
89 
90  void seek(time_unit target_time);
91 };
92 
93 }}
94 
95 #endif
Filter & add_sink_filter(Args &&...args)
Definition: graph.h:68
bool was_setup() const
Definition: graph.h:74
Synchronous node base class.
Definition: sync_node.h:44
time_unit current_time() const
Definition: graph.cc:47
std::ptrdiff_t time_unit
Discrete time unit type.
Definition: common.h:52
Filter & add_filter(Args &&...args)
Definition: graph.h:60
void setup()
Definition: graph.cc:23
std::function< frame_callback_function_type > callback_function
Definition: graph.h:55
void run_until(time_unit last_frame)
Definition: graph.cc:52
void(time_unit t) frame_callback_function_type
Definition: graph.h:25
Node which delegates concrete frame processing to associated filter object.
Definition: filter_node.h:15
void launch()
Definition: graph.cc:31
Sink node base class.
Definition: sink_node.h:12
Event which is repeatedly received after having been notified once.
Definition: event.h:65
void run_for(time_unit duration)
Definition: graph.cc:62
bool run()
Definition: graph.cc:67
void stop()
Definition: graph.cc:38
bool is_running() const
Definition: graph.h:75
event & stop_event()
Definition: graph.h:77
~graph()
Definition: graph.cc:18
Filter & set_filter(Args &&...args)
Definition: filter_node.h:30
Graph containing interconnected nodes through which media frames flow.
Definition: graph.h:23
void seek(time_unit target_time)
Definition: graph.cc:76