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