]> git.sesse.net Git - casparcg/blob - core/producer/frame_producer_device.cpp
2.0.0.2: Display flash version in main window.
[casparcg] / core / producer / frame_producer_device.cpp
1 #include "../StdAfx.h"\r
2 \r
3 #include "frame_producer_device.h"\r
4 \r
5 #include "../mixer/frame/draw_frame.h"\r
6 #include "../mixer/frame_factory.h"\r
7 \r
8 #include "layer.h"\r
9 \r
10 #include <common/concurrency/executor.h>\r
11 #include <common/utility/printer.h>\r
12 \r
13 #include <boost/range/algorithm_ext/erase.hpp>\r
14 #include <boost/lexical_cast.hpp>\r
15 \r
16 #include <tbb/parallel_for.h>\r
17 #include <tbb/mutex.h>\r
18 \r
19 #include <array>\r
20 #include <memory>\r
21 #include <unordered_map>\r
22 \r
23 namespace caspar { namespace core {\r
24 \r
25 struct frame_producer_device::implementation : boost::noncopyable\r
26 {               \r
27         const printer parent_printer_;\r
28 \r
29         std::unordered_map<int, layer> layers_;         \r
30 \r
31         output_func output_;\r
32 \r
33         const safe_ptr<frame_factory> factory_;\r
34         \r
35         mutable executor executor_;\r
36 public:\r
37         implementation(const printer& parent_printer, const safe_ptr<frame_factory>& factory, const output_func& output)  \r
38                 : parent_printer_(parent_printer)\r
39                 , factory_(factory)\r
40                 , output_(output)\r
41         {\r
42                 executor_.start();\r
43                 executor_.begin_invoke([=]{tick();});\r
44         }\r
45 \r
46         ~implementation()\r
47         {\r
48                 CASPAR_LOG(info) << "Shutting down producer-device.";\r
49         }\r
50                                         \r
51         void tick()\r
52         {               \r
53                 output_(draw());\r
54                 executor_.begin_invoke([=]{tick();});\r
55         }\r
56         \r
57         std::vector<safe_ptr<draw_frame>> draw()\r
58         {       \r
59                 std::vector<safe_ptr<draw_frame>> frames(layers_.size(), draw_frame::empty());\r
60                 tbb::parallel_for(tbb::blocked_range<size_t>(0, frames.size(), 1), [&](const tbb::blocked_range<size_t>& r)\r
61                 {\r
62                         auto it = layers_.begin();\r
63                         std::advance(it, r.begin());\r
64                         for(size_t i = r.begin(); i != r.end(); ++i, ++it)\r
65                         {\r
66                                 frames[i] = it->second.receive();\r
67                                 frames[i]->set_layer_index(i);\r
68                         }\r
69                 });             \r
70                 boost::range::remove_erase(frames, draw_frame::empty());\r
71                 return frames;\r
72         }\r
73 \r
74         void load(int index, const safe_ptr<frame_producer>& producer, bool play_on_load, bool preview)\r
75         {\r
76                 producer->set_parent_printer(std::bind(&layer::print, &layers_[index]));\r
77                 producer->initialize(factory_);\r
78                 executor_.invoke([&]{layers_[index].load(producer, play_on_load, preview);});\r
79         }\r
80 \r
81         void pause(int index)\r
82         {               \r
83                 executor_.invoke([&]{layers_[index].pause();});\r
84         }\r
85 \r
86         void play(int index)\r
87         {               \r
88                 executor_.invoke([&]{layers_[index].play();});\r
89         }\r
90 \r
91         void stop(int index)\r
92         {               \r
93                 executor_.invoke([&]{layers_[index].stop();});\r
94         }\r
95 \r
96         void clear(int index)\r
97         {\r
98                 executor_.invoke([&]{layers_[index].clear();});\r
99         }\r
100                 \r
101         void clear()\r
102         {\r
103                 executor_.invoke([&]\r
104                 {\r
105                         BOOST_FOREACH(auto& pair, layers_)\r
106                                 pair.second.clear();\r
107                 });\r
108         }       \r
109         \r
110         void swap_layer(int index, size_t other_index)\r
111         {\r
112                 executor_.invoke([&]\r
113                 {\r
114                         layers_[index].swap(layers_[other_index]);\r
115                 });\r
116         }\r
117 \r
118         void swap_layer(int index, size_t other_index, frame_producer_device& other)\r
119         {\r
120                 if(other.impl_.get() == this)\r
121                         swap_layer(index, other_index);\r
122                 else\r
123                 {\r
124                         auto func = [&]\r
125                         {\r
126                                 layers_[index].swap(other.impl_->layers_.at(other_index));              \r
127 \r
128                                 CASPAR_LOG(info) << print() << L" Swapped layer " << index << L" with " << other.impl_->print() << L" layer " << other_index << L".";   \r
129                         };\r
130                 \r
131                         executor_.invoke([&]{other.impl_->executor_.invoke(func);});\r
132                 }\r
133         }\r
134 \r
135         void swap(frame_producer_device& other)\r
136         {\r
137                 if(other.impl_.get() == this)\r
138                         return;\r
139 \r
140                 auto func = [&]\r
141                 {\r
142                         std::set<int> my_indices;\r
143                         BOOST_FOREACH(auto& pair, layers_)\r
144                                 my_indices.insert(pair.first);\r
145 \r
146                         std::set<int> other_indicies;\r
147                         BOOST_FOREACH(auto& pair, other.impl_->layers_)\r
148                                 other_indicies.insert(pair.first);\r
149                         \r
150                         std::vector<int> indices;\r
151                         std::set_union(my_indices.begin(), my_indices.end(), other_indicies.begin(), other_indicies.end(), std::back_inserter(indices));\r
152                         \r
153                         BOOST_FOREACH(auto index, indices)\r
154                                 layers_[index].swap(other.impl_->layers_[index]);\r
155 \r
156                         CASPAR_LOG(info) << print() << L" Swapped layers with " << other.impl_->print() << L".";\r
157                 };\r
158                 \r
159                 executor_.invoke([&]{other.impl_->executor_.invoke(func);});\r
160         }\r
161         \r
162         boost::unique_future<safe_ptr<frame_producer>> foreground(int index) const\r
163         {\r
164                 return executor_.begin_invoke([=]() mutable -> safe_ptr<frame_producer>\r
165                 {                       \r
166                         auto it = layers_.find(index);\r
167                         return it != layers_.end() ? it->second.foreground() : frame_producer::empty();\r
168                 });\r
169         }\r
170 \r
171         std::wstring print() const\r
172         {\r
173                 return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"producer";\r
174         }\r
175 };\r
176 \r
177 frame_producer_device::frame_producer_device(const printer& parent_printer, const safe_ptr<frame_factory>& factory, const output_func& output) : impl_(new implementation(parent_printer, factory, output)){}\r
178 frame_producer_device::frame_producer_device(frame_producer_device&& other) : impl_(std::move(other.impl_)){}\r
179 void frame_producer_device::swap(frame_producer_device& other){impl_->swap(other);}\r
180 void frame_producer_device::load(int index, const safe_ptr<frame_producer>& producer, bool play_on_load, bool preview){impl_->load(index, producer, play_on_load, preview);}\r
181 void frame_producer_device::pause(int index){impl_->pause(index);}\r
182 void frame_producer_device::play(int index){impl_->play(index);}\r
183 void frame_producer_device::stop(int index){impl_->stop(index);}\r
184 void frame_producer_device::clear(int index){impl_->clear(index);}\r
185 void frame_producer_device::clear(){impl_->clear();}\r
186 void frame_producer_device::swap_layer(int index, size_t other_index){impl_->swap_layer(index, other_index);}\r
187 void frame_producer_device::swap_layer(int index, size_t other_index, frame_producer_device& other){impl_->swap_layer(index, other_index, other);}\r
188 boost::unique_future<safe_ptr<frame_producer>> frame_producer_device::foreground(size_t index) const{   return impl_->foreground(index);}\r
189 }}