]> git.sesse.net Git - casparcg/blob - core/producer/frame_producer_device.cpp
5ad3d892bb1befc7ca6353b62a27fbbddf0f03e1
[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 <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::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         layer& get_layer(int index)\r
58         {\r
59                 auto it = layers_.find(index);\r
60                 if(it == layers_.end())\r
61                         it = layers_.insert(std::make_pair(index, layer(index, std::bind(&implementation::print, this)))).first;\r
62                 return it->second;\r
63         }\r
64         \r
65         std::vector<safe_ptr<draw_frame>> draw()\r
66         {       \r
67                 std::vector<safe_ptr<draw_frame>> frames(layers_.size(), draw_frame::empty());\r
68                 tbb::parallel_for(tbb::blocked_range<size_t>(0, frames.size(), 1), [&](const tbb::blocked_range<size_t>& r)\r
69                 {\r
70                         auto it = layers_.begin();\r
71                         std::advance(it, r.begin());\r
72                         for(size_t i = r.begin(); i != r.end(); ++i, ++it)\r
73                         {\r
74                                 frames[i] = it->second.receive();\r
75                                 frames[i]->set_layer_index(it->first);\r
76                         }\r
77                 });             \r
78                 boost::range::remove_erase(frames, draw_frame::empty());\r
79                 return frames;\r
80         }\r
81 \r
82         void load(int index, const safe_ptr<frame_producer>& producer, bool play_on_load, bool preview)\r
83         {\r
84                 producer->set_parent_printer(std::bind(&layer::print, &get_layer(index)));\r
85                 producer->initialize(factory_);\r
86                 executor_.invoke([&]{get_layer(index).load(producer, play_on_load, preview);});\r
87         }\r
88 \r
89         void pause(int index)\r
90         {               \r
91                 executor_.invoke([&]{get_layer(index).pause();});\r
92         }\r
93 \r
94         void play(int index)\r
95         {               \r
96                 executor_.invoke([&]{get_layer(index).play();});\r
97         }\r
98 \r
99         void stop(int index)\r
100         {               \r
101                 executor_.invoke([&]{get_layer(index).stop();});\r
102         }\r
103 \r
104         void clear(int index)\r
105         {\r
106                 executor_.invoke([&]{layers_.erase(index);});\r
107         }\r
108                 \r
109         void clear()\r
110         {\r
111                 executor_.invoke([&]{layers_.clear();});\r
112         }       \r
113         \r
114         void swap_layer(int index, size_t other_index)\r
115         {\r
116                 executor_.invoke([&]\r
117                 {\r
118                         get_layer(index).swap(layers_[other_index]);\r
119                 });\r
120         }\r
121 \r
122         void swap_layer(int index, size_t other_index, frame_producer_device& other)\r
123         {\r
124                 if(other.impl_.get() == this)\r
125                         swap_layer(index, other_index);\r
126                 else\r
127                 {\r
128                         if(factory_->get_video_format_desc() != other.impl_->factory_->get_video_format_desc())\r
129                                 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Cannot swap between channels with different formats."));\r
130 \r
131                         auto func = [&]\r
132                         {\r
133                                 get_layer(index).swap(other.impl_->layers_.at(other_index));            \r
134 \r
135                                 CASPAR_LOG(info) << print() << L" Swapped layer " << index << L" with " << other.impl_->print() << L" layer " << other_index << L".";   \r
136                         };\r
137                 \r
138                         executor_.invoke([&]{other.impl_->executor_.invoke(func);});\r
139                 }\r
140         }\r
141 \r
142         void swap(frame_producer_device& other)\r
143         {\r
144                 if(other.impl_.get() == this)\r
145                         return;\r
146 \r
147                 if(factory_->get_video_format_desc() != other.impl_->factory_->get_video_format_desc())\r
148                         BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Cannot swap between channels with different formats."));\r
149 \r
150                 auto func = [&]\r
151                 {\r
152                         std::set<int> my_indices;\r
153                         BOOST_FOREACH(auto& pair, layers_)\r
154                                 my_indices.insert(pair.first);\r
155 \r
156                         std::set<int> other_indicies;\r
157                         BOOST_FOREACH(auto& pair, other.impl_->layers_)\r
158                                 other_indicies.insert(pair.first);\r
159                         \r
160                         std::vector<int> indices;\r
161                         std::set_union(my_indices.begin(), my_indices.end(), other_indicies.begin(), other_indicies.end(), std::back_inserter(indices));\r
162                         \r
163                         BOOST_FOREACH(auto index, indices)\r
164                                 get_layer(index).swap(other.impl_->get_layer(index));\r
165 \r
166                         CASPAR_LOG(info) << print() << L" Swapped layers with " << other.impl_->print() << L".";\r
167                 };\r
168                 \r
169                 executor_.invoke([&]{other.impl_->executor_.invoke(func);});\r
170         }\r
171         \r
172         boost::unique_future<safe_ptr<frame_producer>> foreground(int index) const\r
173         {\r
174                 return executor_.begin_invoke([=]() mutable -> safe_ptr<frame_producer>\r
175                 {                       \r
176                         auto it = layers_.find(index);\r
177                         return it != layers_.end() ? it->second.foreground() : frame_producer::empty();\r
178                 });\r
179         }\r
180 \r
181         std::wstring print() const\r
182         {\r
183                 return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"producer";\r
184         }\r
185 };\r
186 \r
187 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
188 frame_producer_device::frame_producer_device(frame_producer_device&& other) : impl_(std::move(other.impl_)){}\r
189 void frame_producer_device::swap(frame_producer_device& other){impl_->swap(other);}\r
190 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
191 void frame_producer_device::pause(int index){impl_->pause(index);}\r
192 void frame_producer_device::play(int index){impl_->play(index);}\r
193 void frame_producer_device::stop(int index){impl_->stop(index);}\r
194 void frame_producer_device::clear(int index){impl_->clear(index);}\r
195 void frame_producer_device::clear(){impl_->clear();}\r
196 void frame_producer_device::swap_layer(int index, size_t other_index){impl_->swap_layer(index, other_index);}\r
197 void frame_producer_device::swap_layer(int index, size_t other_index, frame_producer_device& other){impl_->swap_layer(index, other_index, other);}\r
198 boost::unique_future<safe_ptr<frame_producer>> frame_producer_device::foreground(size_t index) const{   return impl_->foreground(index);}\r
199 }}